|
3 | 3 | # @param network_name_parameter_name String Name of the parameter that contains the network name to get an IP address for.
|
4 | 4 | #
|
5 | 5 | # @set aquired_ip_address String IP address aquired fomr the DDI provider for the given network.
|
6 |
| -# |
7 |
| -@DEBUG = false |
8 | 6 |
|
9 |
| -DDI_PROVIDERS_URI = 'Infrastructure/Network/DDIProviders'.freeze |
| 7 | +#/ Infrastructure / Network / Operations / Methods / acquire_ip_address |
| 8 | +module RedHatConsulting_Utilities |
| 9 | + module Infrastructure |
| 10 | + module Network |
| 11 | + module Operations |
| 12 | + module Methods |
| 13 | + class AcquireIpAddress |
10 | 14 |
|
11 |
| -def dump_object(object_string, object) |
12 |
| - $evm.log("info", "Listing #{object_string} Attributes:") |
13 |
| - object.attributes.sort.each { |k, v| $evm.log("info", "\t#{k}: #{v}") } |
14 |
| - $evm.log("info", "===========================================") |
15 |
| -end |
| 15 | + include RedHatConsulting_Utilities::StdLib::Core |
| 16 | + DDI_PROVIDERS_URI = 'Infrastructure/Network/DDIProviders'.freeze |
16 | 17 |
|
17 |
| -def dump_current |
18 |
| - $evm.log("info", "Listing Current Object Attributes:") |
19 |
| - $evm.current.attributes.sort.each { |k, v| $evm.log("info", "\t#{k}: #{v}") } |
20 |
| - $evm.log("info", "===========================================") |
21 |
| -end |
| 18 | + def initialize(handle = $evm) |
| 19 | + @handle = handle |
| 20 | + @network_configurations = {} |
| 21 | + @missing_network_configurations = {} |
| 22 | + @DEBUG = true |
| 23 | + end |
22 | 24 |
|
23 |
| -def dump_root |
24 |
| - $evm.log("info", "Listing Root Object Attributes:") |
25 |
| - $evm.root.attributes.sort.each { |k, v| $evm.log("info", "\t#{k}: #{v}") } |
26 |
| - $evm.log("info", "===========================================") |
27 |
| -end |
| 25 | + def main |
| 26 | + begin |
| 27 | + vm,options = get_vm_and_options() |
28 | 28 |
|
29 |
| -# Notify and log a message. |
30 |
| -# |
31 |
| -# @param level Symbol Level of the notification and log message |
32 |
| -# @param message String Message to notify and log |
33 |
| -# @param subject ActiveRecord::Base Subject of the notification |
34 |
| -def notify(level, message, subject) |
35 |
| - $evm.create_notification(:level => level, :message => message, :subject => subject) |
36 |
| - log_level = case level |
37 |
| - when :warning |
38 |
| - :warn |
39 |
| - else |
40 |
| - level |
41 |
| - end |
42 |
| - $evm.log(log_level, message) |
43 |
| -end |
| 29 | + network_name_parameter_name = get_param(:network_name_parameter_name) |
| 30 | + log(:info, "network_name_parameter_name => #{network_name_parameter_name}") |
| 31 | + network_name = get_param(network_name_parameter_name) || get_param("dialog_#{network_name_parameter_name}") || options[network_name_parameter_name.to_sym] || options["dialog_#{network_name_parameter_name}".to_sym] |
44 | 32 |
|
45 |
| -# Log an error and exit. |
46 |
| -# |
47 |
| -# @param msg Message to error with |
48 |
| -def error(msg) |
49 |
| - $evm.log(:error, msg) |
50 |
| - $evm.root['ae_result'] = 'error' |
51 |
| - $evm.root['ae_reason'] = msg.to_s |
52 |
| - exit MIQ_STOP |
53 |
| -end |
| 33 | + if !network_name.blank? |
| 34 | + network_configuration = get_network_configuration(network_name) |
| 35 | + log(:info, "network_name_parameter_name => #{network_name_parameter_name}") if @DEBUG |
| 36 | + log(:info, "network_name => #{network_name}") if @DEBUG |
| 37 | + log(:info, "network_configuration => #{network_configuration}") if @DEBUG |
54 | 38 |
|
55 |
| -# There are many ways to attempt to pass parameters in Automate. |
56 |
| -# This function checks all of them in priorty order as well as checking for symbol or string. |
57 |
| -# |
58 |
| -# Order: |
59 |
| -# 1. Inputs |
60 |
| -# 2. Current |
61 |
| -# 3. Object |
62 |
| -# 4. Root |
63 |
| -# 5. State |
64 |
| -# |
65 |
| -# @return Value for the given parameter or nil if none is found |
66 |
| -def get_param(param) |
67 |
| - # check if inputs has been set for given param |
68 |
| - param_value ||= $evm.inputs[param.to_sym] |
69 |
| - param_value ||= $evm.inputs[param.to_s] |
70 |
| - |
71 |
| - # else check if current has been set for given param |
72 |
| - param_value ||= $evm.current[param.to_sym] |
73 |
| - param_value ||= $evm.current[param.to_s] |
74 |
| - |
75 |
| - # else cehck if current has been set for given param |
76 |
| - param_value ||= $evm.object[param.to_sym] |
77 |
| - param_value ||= $evm.object[param.to_s] |
78 |
| - |
79 |
| - # else check if param on root has been set for given param |
80 |
| - param_value ||= $evm.root[param.to_sym] |
81 |
| - param_value ||= $evm.root[param.to_s] |
82 |
| - |
83 |
| - # check if state has been set for given param |
84 |
| - param_value ||= $evm.get_state_var(param.to_sym) |
85 |
| - param_value ||= $evm.get_state_var(param.to_s) |
86 |
| - |
87 |
| - $evm.log(:info, "{ '#{param}' => '#{param_value}' }") if @DEBUG |
88 |
| - return param_value |
89 |
| -end |
| 39 | + # determine the DDI provider |
| 40 | + ddi_provider = network_configuration['network_ddi_provider'] |
| 41 | + log(:info, "ddi_provider => #{ddi_provider}") if @DEBUG |
90 | 42 |
|
91 |
| -# Function for getting the current VM and associated options based on the vmdb_object_type. |
92 |
| -# |
93 |
| -# Supported vmdb_object_types |
94 |
| -# * miq_provision |
95 |
| -# * vm |
96 |
| -# * automation_task |
97 |
| -# |
98 |
| -# @return vm,options |
99 |
| -def get_vm_and_options() |
100 |
| - $evm.log(:info, "$evm.root['vmdb_object_type'] => '#{$evm.root['vmdb_object_type']}'.") |
101 |
| - case $evm.root['vmdb_object_type'] |
102 |
| - when 'miq_provision' |
103 |
| - # get root object |
104 |
| - $evm.log(:info, "Get VM and dialog attributes from $evm.root['miq_provision']") if @DEBUG |
105 |
| - miq_provision = $evm.root['miq_provision'] |
106 |
| - dump_object('miq_provision', miq_provision) if @DEBUG |
107 |
| - |
108 |
| - # get VM |
109 |
| - vm = miq_provision.vm |
110 |
| - |
111 |
| - # get options |
112 |
| - options = miq_provision.options |
113 |
| - #merge the ws_values, dialog, top level options into one list to make it easier to search |
114 |
| - options = options.merge(options[:ws_values]) if options[:ws_values] |
115 |
| - options = options.merge(options[:dialog]) if options[:dialog] |
116 |
| - when 'vm' |
117 |
| - # get root objet & VM |
118 |
| - $evm.log(:info, "Get VM from paramater and dialog attributes form $evm.root") if @DEBUG |
119 |
| - vm = get_param(:vm) |
120 |
| - dump_object('vm', vm) if @DEBUG |
121 |
| - |
122 |
| - # get options |
123 |
| - options = $evm.root.attributes |
124 |
| - #merge the ws_values, dialog, top level options into one list to make it easier to search |
125 |
| - options = options.merge(options[:ws_values]) if options[:ws_values] |
126 |
| - options = options.merge(options[:dialog]) if options[:dialog] |
127 |
| - when 'automation_task' |
128 |
| - # get root objet |
129 |
| - $evm.log(:info, "Get VM from paramater and dialog attributes form $evm.root") if @DEBUG |
130 |
| - automation_task = $evm.root['automation_task'] |
131 |
| - dump_object('automation_task', automation_task) if @DEBUG |
132 |
| - |
133 |
| - # get VM |
134 |
| - vm = get_param(:vm) |
135 |
| - |
136 |
| - # get options |
137 |
| - options = get_param(:options) |
138 |
| - options = JSON.load(options) if options && options.class == String |
139 |
| - options = options.symbolize_keys if options |
140 |
| - #merge the ws_values, dialog, top level options into one list to make it easier to search |
141 |
| - options = options.merge(options[:ws_values]) if options[:ws_values] |
142 |
| - options = options.merge(options[:dialog]) if options[:dialog] |
143 |
| - else |
144 |
| - error("Can not handle vmdb_object_type: #{$evm.root['vmdb_object_type']}") |
145 |
| - end |
146 |
| - |
147 |
| - # standerdize the option keys |
148 |
| - options = options.symbolize_keys() |
149 |
| - |
150 |
| - $evm.log(:info, "vm => #{vm}") if @DEBUG |
151 |
| - $evm.log(:info, "options => #{options}") if @DEBUG |
152 |
| - return vm,options |
153 |
| -end |
| 43 | + # instantiate instance to acquire IP |
| 44 | + begin |
| 45 | + log(:info, "Acquire IP address using DDI Provider <#{ddi_provider}>") if @DEBUG |
154 | 46 |
|
155 |
| -# Get the network configuration for a given network |
156 |
| -# |
157 |
| -# @param network_name Name of the network to get the configuraiton for |
158 |
| -# @return Hash Configuration information about the given network |
159 |
| -# network_purpose |
160 |
| -# network_address_space |
161 |
| -# network_gateway |
162 |
| -# network_nameservers |
163 |
| -# network_ddi_provider |
164 |
| -@network_configurations = {} |
165 |
| -@missing_network_configurations = {} |
166 |
| -NETWORK_CONFIGURATION_URI = 'Infrastructure/Network/Configuration'.freeze |
167 |
| -def get_network_configuration(network_name) |
168 |
| - if @network_configurations[network_name].blank? && @missing_network_configurations[network_name].blank? |
169 |
| - begin |
170 |
| - escaped_network_name = network_name.gsub(/[^a-zA-Z0-9_\.\-]/, '_') |
171 |
| - @network_configurations[network_name] = $evm.instantiate("#{NETWORK_CONFIGURATION_URI}/#{escaped_network_name}") |
172 |
| - |
173 |
| - if escaped_network_name =~ /^dvs_/ && @network_configurations[network_name]['network_address_space'].blank? |
174 |
| - escaped_network_name = escaped_network_name[/^dvs_(.*)/, 1] |
175 |
| - @network_configurations[network_name] = $evm.instantiate("#{NETWORK_CONFIGURATION_URI}/#{escaped_network_name}") |
| 47 | + @handle.root['network_name'] = network_name |
| 48 | + @handle.instantiate("#{DDI_PROVIDERS_URI}/#{ddi_provider}#acquire_ip_address") |
| 49 | + acquired_ip_address = get_param(:acquired_ip_address) |
| 50 | + log(:info, "Acquired IP address <#{acquired_ip_address}> using DDI Provider <#{ddi_provider}>") |
| 51 | + |
| 52 | + ensure |
| 53 | + |
| 54 | + success = @handle.root['ae_result'] == nil || @handle.root['ae_result'] == 'ok' |
| 55 | + reason = @handle.root['ae_reason'] if !success |
| 56 | + |
| 57 | + # clean up root |
| 58 | + @handle.root['ae_result'] = 'ok' |
| 59 | + @handle.root['ae_reason'] = "Acquired IP address <#{acquired_ip_address}> for VM <#{vm.name}>" |
| 60 | + |
| 61 | + # clean up after call |
| 62 | + @handle.root['network_name'] = nil |
| 63 | + @handle.root['acquired_ip_address'] = nil |
| 64 | + end |
| 65 | + error("Error acquiring IP address using DDI Provider <#{ddi_provider}>: #{reason}") if !success |
| 66 | + else |
| 67 | + log(:warn, "No value for the expected network name parameter <#{network_name_parameter_name}> was given. Skipping aquiring IP address.") |
| 68 | + end |
| 69 | + |
| 70 | + # set the acquired IP |
| 71 | + @handle.object['acquired_ip_address'] = acquired_ip_address |
| 72 | + @handle.set_state_var(:acquired_ip_address, acquired_ip_address) |
| 73 | + |
| 74 | + # set destination IP address to acquired IP address |
| 75 | + @handle.object['destination_ip_address'] = acquired_ip_address |
| 76 | + @handle.set_state_var(:destination_ip_address, acquired_ip_address) |
| 77 | + |
| 78 | + @handle.log(:info, "$evm.object['acquired_ip_address'] => #{@handle.object['acquired_ip_address']}") if @DEBUG |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + # Get the network configuration for a given network |
| 83 | + # |
| 84 | + # @param network_name Name of the network to get the configuraiton for |
| 85 | + # @return Hash Configuration information about the given network |
| 86 | + # network_purpose |
| 87 | + # network_address_space |
| 88 | + # network_gateway |
| 89 | + # network_nameservers |
| 90 | + # network_ddi_provider |
| 91 | + NETWORK_CONFIGURATION_URI = 'Infrastructure/Network/Configuration'.freeze |
| 92 | + def get_network_configuration(network_name) |
| 93 | + if @network_configurations[network_name].blank? && @missing_network_configurations[network_name].blank? |
| 94 | + begin |
| 95 | + escaped_network_name = network_name.gsub(/[^a-zA-Z0-9_\.\-]/, '_') |
| 96 | + @network_configurations[network_name] = @handle.instantiate("#{NETWORK_CONFIGURATION_URI}/#{escaped_network_name}") |
| 97 | + |
| 98 | + if escaped_network_name =~ /^dvs_/ && @network_configurations[network_name]['network_address_space'].blank? |
| 99 | + escaped_network_name = escaped_network_name[/^dvs_(.*)/, 1] |
| 100 | + @network_configurations[network_name] = @handle.instantiate("#{NETWORK_CONFIGURATION_URI}/#{escaped_network_name}") |
| 101 | + end |
| 102 | + rescue |
| 103 | + @missing_network_configurations[network_name] = "WARN: No network configuration exists" |
| 104 | + log(:warn, "No network configuration for Network <#{network_name}> (escaped <#{escaped_network_name}>) exists") |
| 105 | + end |
| 106 | + end |
| 107 | + return @network_configurations[network_name] |
| 108 | + end |
| 109 | + |
| 110 | + end |
| 111 | + end |
176 | 112 | end
|
177 |
| - rescue |
178 |
| - @missing_network_configurations[network_name] = "WARN: No network configuration exists" |
179 |
| - $evm.log(:warn, "No network configuration for Network <#{network_name}> (escaped <#{escaped_network_name}>) exists") |
180 | 113 | end
|
181 | 114 | end
|
182 |
| - return @network_configurations[network_name] |
183 | 115 | end
|
184 | 116 |
|
185 |
| -begin |
186 |
| - vm,options = get_vm_and_options() |
187 |
| - |
188 |
| - network_name_parameter_name = get_param(:network_name_parameter_name) |
189 |
| - network_name = get_param(network_name_parameter_name) || get_param("dialog_#{network_name_parameter_name}") || options[network_name_parameter_name.to_sym] || options["dialog_#{network_name_parameter_name}".to_sym] |
190 |
| - |
191 |
| - if !network_name.blank? |
192 |
| - network_configuration = get_network_configuration(network_name) |
193 |
| - $evm.log(:info, "network_name_parameter_name => #{network_name_parameter_name}") if @DEBUG |
194 |
| - $evm.log(:info, "network_name => #{network_name}") if @DEBUG |
195 |
| - $evm.log(:info, "network_configuration => #{network_configuration}") if @DEBUG |
196 |
| - |
197 |
| - # determine the DDI provider |
198 |
| - ddi_provider = network_configuration['network_ddi_provider'] |
199 |
| - $evm.log(:info, "ddi_provider => #{ddi_provider}") if @DEBUG |
200 |
| - |
201 |
| - # instantiate instance to acquire IP |
202 |
| - begin |
203 |
| - $evm.log(:info, "Acquire IP address using DDI Provider <#{ddi_provider}>") if @DEBUG |
204 |
| - |
205 |
| - $evm.root['network_name'] = network_name |
206 |
| - $evm.instantiate("#{DDI_PROVIDERS_URI}/#{ddi_provider}#acquire_ip_address") |
207 |
| - acquired_ip_address = get_param(:acquired_ip_address) |
208 |
| - |
209 |
| - $evm.log(:info, "Acquired IP address <#{acquired_ip_address}> using DDI Provider <#{ddi_provider}>") if @DEBUG |
210 |
| - ensure |
211 |
| - success = $evm.root['ae_result'] == nil || $evm.root['ae_result'] == 'ok' |
212 |
| - reason = $evm.root['ae_reason'] if !success |
213 |
| - |
214 |
| - # clean up root |
215 |
| - $evm.root['ae_result'] = 'ok' |
216 |
| - $evm.root['ae_reason'] = "Acquired IP address <#{acquired_ip_address}> for VM <#{vm.name}>" |
217 |
| - |
218 |
| - # clean up after call |
219 |
| - $evm.root['network_name'] = nil |
220 |
| - $evm.root['acquired_ip_address'] = nil |
221 |
| - end |
222 |
| - error("Error acquiring IP address using DDI Provider <#{ddi_provider}>: #{reason}") if !success |
223 |
| - else |
224 |
| - $evm.log(:warn, "No value for the expected network name parameter <#{network_name_parameter_name}> was given. Skipping aquiring IP address.") |
225 |
| - end |
226 |
| - |
227 |
| - # set the acquired IP |
228 |
| - $evm.object['acquired_ip_address'] = acquired_ip_address |
229 |
| - $evm.set_state_var(:acquired_ip_address, acquired_ip_address) |
230 |
| - |
231 |
| - # set destination IP address to acquired IP address |
232 |
| - $evm.object['destination_ip_address'] = acquired_ip_address |
233 |
| - $evm.set_state_var(:destination_ip_address, acquired_ip_address) |
234 |
| - |
235 |
| - $evm.log(:info, "$evm.object['acquired_ip_address'] => #{$evm.object['acquired_ip_address']}") if @DEBUG |
| 117 | +if __FILE__ == $PROGRAM_NAME |
| 118 | + RedHatConsulting_Utilities::Infrastructure::Network::Operations::Methods::AcquireIpAddress.new.main |
236 | 119 | end
|
0 commit comments