|
37 | 37 | customer_id = certname_parts['customer_id'] |
38 | 38 | node_name = certname_parts['node_name'] |
39 | 39 |
|
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') |
62 | 65 |
|
63 | 66 | # ensure that we don't have too many tags -- if we allow for more tags, we also |
64 | 67 | # need to update hiera.yaml! |
|
82 | 85 | 'customer_id' => customer_id, |
83 | 86 | 'node_name' => node_name, |
84 | 87 | '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, |
87 | 90 | }, |
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?, |
90 | 93 | 'hiera_datapath' => customer_id + '/' + DEFAULT_HIERA_BRANCH, |
91 | 94 | 'obmondo_tags' => tag_keys, |
92 | 95 | }.merge(tags) |
|
95 | 98 | 'parameters' => parameters |
96 | 99 | } |
97 | 100 |
|
| 101 | +output['environment'] = linuxaid_tag if linuxaid_tag |
| 102 | + |
98 | 103 | puts YAML.dump(output) |
0 commit comments