Skip to content

Commit 81f353f

Browse files
committed
Merge pull request 'Set Puppet ENC environment from linuxaid_tag' (#1478) from puppet_enc_environment_support into master
Reviewed-on: https://gitea.obmondo.com/EnableIT/LinuxAid/pulls/1478
2 parents 8b04d37 + beadce6 commit 81f353f

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

puppet_enc.rb

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,31 @@
3737
customer_id = certname_parts['customer_id']
3838
node_name = certname_parts['node_name']
3939

40-
# When running tests we let everyone have a subscription
41-
subscription = if TESTING
42-
{'product_id' => 'silver'}
43-
else
44-
begin
45-
obmondo_api("/subscription/#{CERTNAME}")
46-
rescue
47-
nil
48-
end
49-
end
50-
51-
tag_keys = if TESTING
52-
['tag1']
53-
else
54-
begin
55-
obmondo_api("/server/#{CERTNAME}/tags")
56-
rescue ArgumentError => e
57-
warn 'API is down; no tags available!'
58-
warn "#{e.class}: #{e}"
59-
[]
60-
end
61-
end
40+
# Single API call to get server data (subscription, tags, environment, etc.)
41+
server_data = if TESTING
42+
{
43+
'tags' => ['tag1'],
44+
'product_id' => 'silver',
45+
'linuxaid_tag' => nil,
46+
}
47+
else
48+
begin
49+
response = obmondo_api("/servers/customer/#{customer_id}?certname=#{CERTNAME}")
50+
if response['data'] && !response['data'].empty?
51+
response['data'].first
52+
else
53+
nil
54+
end
55+
rescue => e
56+
warn 'API is down; no server data available!'
57+
warn "#{e.class}: #{e}"
58+
nil
59+
end
60+
end
61+
62+
subscription_product_id = server_data&.dig('product_id')
63+
tag_keys = server_data&.dig('tags') || []
64+
linuxaid_tag = server_data&.dig('linuxaid_tag')
6265

6366
# ensure that we don't have too many tags -- if we allow for more tags, we also
6467
# need to update hiera.yaml!
@@ -82,11 +85,11 @@
8285
'customer_id' => customer_id,
8386
'node_name' => node_name,
8487
'tags' => tag_keys,
85-
'monitor' => !subscription.nil?,
86-
'subscription' => (subscription['product_id'] unless subscription.nil?),
88+
'monitor' => !subscription_product_id.nil?,
89+
'subscription' => subscription_product_id,
8790
},
88-
'subscription' => (subscription['product_id'] unless subscription.nil?),
89-
'obmondo_monitor' => !subscription.nil?,
91+
'subscription' => subscription_product_id,
92+
'obmondo_monitor' => !subscription_product_id.nil?,
9093
'hiera_datapath' => customer_id + '/' + DEFAULT_HIERA_BRANCH,
9194
'obmondo_tags' => tag_keys,
9295
}.merge(tags)
@@ -95,4 +98,6 @@
9598
'parameters' => parameters
9699
}
97100

101+
output['environment'] = linuxaid_tag if linuxaid_tag
102+
98103
puts YAML.dump(output)

0 commit comments

Comments
 (0)