Skip to content

Commit

Permalink
Ensure list/dump commands do not fail, ie when sensuctl is not yet co…
Browse files Browse the repository at this point in the history
…nfigured
  • Loading branch information
treydock committed Mar 24, 2023
1 parent de62f55 commit cf7c408
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 21 deletions.
3 changes: 2 additions & 1 deletion lib/puppet/provider/sensu_postgres_config/sensuctl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def self.instances
data.each do |d|
config = {}
config[:ensure] = :present
config[:name] = d['metadata']['name']
config[:name] = d.fetch('metadata', {}).fetch('name', nil)
next if config[:name].nil?
d['spec'].each_pair do |key,value|
if !!value == value
value = value.to_s.to_sym
Expand Down
20 changes: 14 additions & 6 deletions lib/puppet/provider/sensuctl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def self.sensuctl_list(command, namespaces = true)
end
data = []
begin
output = sensuctl(args, {:failonfail => false})
output = sensuctl(args, failonfail: false)
Puppet.debug("sensuctl #{args.join(' ')}: #{output}")
rescue Exception => e
Puppet.notice("Failed to list resources with sensuctl: #{e}")
Expand Down Expand Up @@ -152,14 +152,17 @@ def sensuctl_delete(*args)
end

def self.sensuctl_auth_types()
output = sensuctl(['auth','list','--format','yaml'])
output = sensuctl(['auth','list','--format','yaml'], failonfail: false)
Puppet.debug("YAML auth list: #{output}")
auth_types = {}
auths = output.split('---')
Puppet.debug("auths: #{auths}")
auths.each do |auth|
a = YAML.load(auth)
auth_types[a['metadata']['name']] = a['type']
next if a.nil?
name = a.fetch('metadata', {}).fetch('name', nil)
next if name.nil?
auth_types[name] = a['type']
end
Puppet.debug("auth_types: #{auth_types}")
auth_types
Expand All @@ -168,8 +171,13 @@ def self.sensuctl_auth_types()
def self.dump(resource_type)
# Dump YAML because 'sensuctl dump' does not yet support '--format json'
# https://github.com/sensu/sensu-go/issues/3424
output = sensuctl(['dump',resource_type,'--format','yaml','--all-namespaces'])
Puppet.debug("YAML dump of #{resource_type}:\n#{output}")
begin
output = sensuctl(['dump',resource_type,'--format','yaml','--all-namespaces'], {:failonfail => false})
Puppet.debug("YAML dump of #{resource_type}:\n#{output}")
rescue Exception => e
Puppet.notice("Failed to dump resources with sensuctl: #{e}")
return []
end
resources = []
dumps = output.split('---')
dumps.each do |d|
Expand Down Expand Up @@ -226,7 +234,7 @@ def valid_json?(json)
end

def self.version
output = sensuctl(['version'], {:failonfail => false})
output = sensuctl(['version'], failonfail: false)
version = output[%r{version ([0-9.]+)}, 1]
return version
rescue Exception => e
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/provider/sensu_cluster_federation/sensuctl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

describe 'self.instances' do
it 'should create instances' do
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.out'))
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.out'))
expect(provider.instances.length).to eq(1)
end

it 'should return the resource for a check' do
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.out'))
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.out'))
property_hash = provider.instances[0].instance_variable_get("@property_hash")
expect(property_hash[:name]).to eq('test')
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

describe 'self.instances' do
it 'should create instances' do
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.out'))
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.out'))
expect(provider.instances.length).to eq(2)
end

it 'should return the resource for a check' do
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.out'))
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.out'))
property_hash = provider.instances[0].instance_variable_get("@property_hash")
expect(property_hash[:name]).to eq('https://10.0.0.1:8080 in test')
end
Expand All @@ -30,7 +30,7 @@
expected_spec = {
:api_urls => ['https://10.0.0.1:8080','https://10.0.0.2:8080','https://10.0.0.3:8080'],
}
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.out'))
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.out'))
expect(resource.provider).to receive(:sensuctl_create).with('Cluster', expected_metadata, expected_spec, 'federation/v1')
resource.provider.create
property_hash = resource.provider.instance_variable_get("@property_hash")
Expand All @@ -44,7 +44,7 @@
expected_spec = {
:api_urls => ['https://10.0.0.1:8080','https://10.0.0.2:8080'],
}
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.out'))
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.Cluster','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.out'))
expect(resource.provider).to receive(:sensuctl_create).with('Cluster', expected_metadata, expected_spec, 'federation/v1')
resource.provider.destroy
property_hash = resource.provider.instance_variable_get("@property_hash")
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/provider/sensu_etcd_replicator/sensuctl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

describe 'self.instances' do
it 'should create instances' do
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.EtcdReplicator','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.txt'))
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.EtcdReplicator','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.txt'))
expect(provider.instances.length).to eq(2)
end

it 'should return the resource for a check' do
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.EtcdReplicator','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.txt'))
allow(provider).to receive(:sensuctl).with(['dump','federation/v1.EtcdReplicator','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.txt'))
property_hash = provider.instances[0].instance_variable_get("@property_hash")
expect(property_hash[:name]).to eq('role_replicator')
end
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/provider/sensu_postgres_config/sensuctl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

describe 'self.instances' do
it 'should create instances' do
allow(provider).to receive(:sensuctl).with(['dump','store/v1.PostgresConfig','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.txt'))
allow(provider).to receive(:sensuctl).with(['dump','store/v1.PostgresConfig','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.txt'))
expect(provider.instances.length).to eq(1)
end

it 'should return the resource for a postres config' do
allow(provider).to receive(:sensuctl).with(['dump','store/v1.PostgresConfig','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.txt'))
allow(provider).to receive(:sensuctl).with(['dump','store/v1.PostgresConfig','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.txt'))
property_hash = provider.instances[0].instance_variable_get("@property_hash")
expect(property_hash[:name]).to eq('my-postgres')
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

describe 'self.instances' do
it 'should create instances' do
allow(provider).to receive(:sensuctl).with(['dump','secrets/v1.Provider','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.txt'))
allow(provider).to receive(:sensuctl).with(['dump','secrets/v1.Provider','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.txt'))
expect(provider.instances.length).to eq(3)
end

it 'should return the resource for a check' do
allow(provider).to receive(:sensuctl).with(['dump','secrets/v1.Provider','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.txt'))
allow(provider).to receive(:sensuctl).with(['dump','secrets/v1.Provider','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.txt'))
property_hash = provider.instances[0].instance_variable_get("@property_hash")
expect(property_hash[:name]).to eq('my_vault')
end
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/provider/sensuctl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@

context 'sensuctl_auth_types' do
it 'should return auth and their types' do
allow(subject).to receive(:sensuctl).with(['auth','list','--format','yaml']).and_return(my_fixture_read('auths.txt'))
allow(subject).to receive(:sensuctl).with(['auth','list','--format','yaml'], failonfail: false).and_return(my_fixture_read('auths.txt'))
expect(subject.sensuctl_auth_types).to eq({"activedirectory"=>"AD", "activedirectory2"=>"AD", "openldap"=>"LDAP"})
end
end

context 'dump' do
it 'dumps multiple resources' do
allow(subject).to receive(:sensuctl).with(['dump','federation/v1.EtcdReplicator','--format','yaml','--all-namespaces']).and_return(my_fixture_read('dump.txt'))
allow(subject).to receive(:sensuctl).with(['dump','federation/v1.EtcdReplicator','--format','yaml','--all-namespaces'], failonfail: false).and_return(my_fixture_read('dump.txt'))
ret = subject.dump('federation/v1.EtcdReplicator')
expect(ret.size).to eq(2)
expect(ret[0]['metadata']['name']).to eq('role_replicator')
Expand Down

0 comments on commit cf7c408

Please sign in to comment.