From 24a4af884d540205ebebfe039df3f46ddbcc63c6 Mon Sep 17 00:00:00 2001 From: Jeffrey Clark Date: Wed, 8 Feb 2023 02:16:13 -0600 Subject: [PATCH] extend influxdb_auth to force update --- .../provider/influxdb_auth/influxdb_auth.rb | 7 +- lib/puppet/type/influxdb_auth.rb | 6 ++ .../influxdb_auth/influxdb_auth_spec.rb | 90 +++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/lib/puppet/provider/influxdb_auth/influxdb_auth.rb b/lib/puppet/provider/influxdb_auth/influxdb_auth.rb index 2045ca1..61c456d 100644 --- a/lib/puppet/provider/influxdb_auth/influxdb_auth.rb +++ b/lib/puppet/provider/influxdb_auth/influxdb_auth.rb @@ -106,7 +106,12 @@ def update(context, name, should) # If the status property is unchanged, then a different, immutable property has been changed. if self_token['status'] == should[:status] - context.warning("Unable to update properties other than 'status'. Please delete and recreate resource with the desired properties") + if should[:force] + create(context, name, should) + delete(context, name) + else + context.warning("Unable to update properties other than 'status'. Please delete and recreate resource with the desired properties") + end else auth_id = self_token['id'] body = { diff --git a/lib/puppet/type/influxdb_auth.rb b/lib/puppet/type/influxdb_auth.rb index 0a1e3f2..0448fba 100644 --- a/lib/puppet/type/influxdb_auth.rb +++ b/lib/puppet/type/influxdb_auth.rb @@ -75,6 +75,12 @@ desc: 'Whether to enable SSL for the InfluxDB service', default: true, behavior: :parameter, + }, + force: { + type: 'Boolean', + desc: 'Recreate resource if immutable property changes', + default: false, + behavior: :parameter, } }, ) diff --git a/spec/unit/puppet/provider/influxdb_auth/influxdb_auth_spec.rb b/spec/unit/puppet/provider/influxdb_auth/influxdb_auth_spec.rb index 17d636c..ef245dd 100644 --- a/spec/unit/puppet/provider/influxdb_auth/influxdb_auth_spec.rb +++ b/spec/unit/puppet/provider/influxdb_auth/influxdb_auth_spec.rb @@ -275,6 +275,96 @@ provider.update(context, should_hash[:name], should_hash) end end + + context 'when force updating immutable properties' do + it 'updates resources' do + should_hash = { + ensure: 'present', + user: 'admin', + name: 'token_1', + status: 'active', + org: 'puppetlabs', + force: true, + permissions: [ + { + 'action' => 'read', + 'resource' => { + 'type' => 'telegrafs' + } + }, + { + 'action' => 'write', + 'resource' => { + 'type' => 'telegrafs' + } + }, + ] + } + + provider.instance_variable_set( + '@self_hash', + [ + { + 'id' => '123', + 'token' => 'foo', + 'status' => 'active', + 'description' => 'token_1', + 'orgID' => '123', + 'org' => 'puppetlabs', + 'userID' => '123', + 'user' => 'admin', + 'permissions' => [ + { + 'action' => 'read', + 'resource' => { + 'type' => 'telegrafs' + } + }, + ], + 'links' => { + 'self' => '/api/v2/authorizations/123', + }, + }, + ], + ) + + post_data = JSON.dump( + { + 'orgID': 123, + 'permissions': [ + { + 'action': 'read', + 'resource': { + 'type': 'telegrafs' + } + }, + { + 'action': 'write', + 'resource': { + 'type': 'telegrafs' + } + }, + ], + 'description': 'token_1', + 'status': 'active', + 'userID': 123 + }, + ) + + provider.instance_variable_set('@org_hash', [{ 'name' => 'puppetlabs', 'id' => 123 }]) + provider.instance_variable_set('@user_map', [{ 'name' => 'admin', 'id' => 123 }]) + + post_args = ['/api/v2/authorizations', post_data] + + expect(context).to receive(:debug).with("Creating '#{should_hash[:name]}' with #{should_hash.inspect}") + expect(context).to receive(:debug).with("Updating '#{should_hash[:name]}' with #{should_hash.inspect}") + expect(context).to receive(:debug).with("Deleting '#{should_hash[:name]}'") + expect(provider).to receive(:influx_delete).with('/api/v2/authorizations/123') + expect(provider).to receive(:influx_post).with(*post_args) + + provider.update(context, should_hash[:name], should_hash) + end + end end describe '#delete' do