Skip to content

Commit

Permalink
extend influxdb_auth to force update
Browse files Browse the repository at this point in the history
  • Loading branch information
h0tw1r3 committed Feb 8, 2023
1 parent 4315b23 commit 24a4af8
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/puppet/provider/influxdb_auth/influxdb_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
6 changes: 6 additions & 0 deletions lib/puppet/type/influxdb_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
},
)
90 changes: 90 additions & 0 deletions spec/unit/puppet/provider/influxdb_auth/influxdb_auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 24a4af8

Please sign in to comment.