Skip to content

Commit e83b6fe

Browse files
Update text cases
1 parent b815beb commit e83b6fe

File tree

3 files changed

+58
-35
lines changed

3 files changed

+58
-35
lines changed

lib/miq_automation_engine/engine/miq_ae_engine.rb

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def self.format_benchmark_times(benchmark)
183183
end
184184

185185
def self.create_automation_attribute_class_name(object)
186-
return object if automation_attribute_is_array?(object.class.base_class.name, object)
186+
return object if automation_attribute_is_array?(object)
187187

188188
case object
189189
when MiqRequest
@@ -223,7 +223,7 @@ def self.create_automation_attribute_value(object)
223223
end
224224

225225
def self.automation_attribute_is_array?(attr, object = nil)
226-
if !object.nil? && object.has_attribute?(:options) && !object.options[:dialog].nil?
226+
if !object.nil? && !object.kind_of?(String) && object.has_attribute?(:options) && !object.options.nil? && !object.options[:dialog].nil?
227227
object[:options][:dialog][attr].kind_of?(Array)
228228
else
229229
attr.to_s.downcase.starts_with?("array::")
@@ -234,6 +234,14 @@ def self.create_automation_attributes_string(hash)
234234
args = create_automation_attributes(hash)
235235
return args if args.kind_of?(String)
236236

237+
# result_array = []
238+
# args.keys.each do |k|
239+
# args[k].each do |item|
240+
# result_array.push(item.id)
241+
# end
242+
# end
243+
# result_string = result_array.join("\x1F")
244+
237245
args.collect { |a| a.join("=") }.join("&")
238246
end
239247

@@ -262,7 +270,10 @@ def self.create_automation_attribute_array_key(key)
262270
end
263271

264272
def self.create_automation_attribute_array_value(value)
265-
value
273+
# value
274+
value.collect do |obj|
275+
obj.kind_of?(ActiveRecord::Base) ? obj.id.to_s : obj.to_s
276+
end
266277
end
267278

268279
def self.set_automation_attributes_from_objects(objects, attrs_hash)
@@ -316,11 +327,17 @@ def self.create_ae_attrs(attrs, name, vmdb_object, objects = [MiqServer.my_serve
316327
# Sends array attributes to the miq_ae_object as strings with \x1F between each array item.
317328
array_objects = ae_attrs.keys.find_all { |key| ae_attrs[key].kind_of?(Array) }
318329
array_objects.each do |array_object|
319-
# Each array attribute is tagged with Array:: before the attribute key
320-
ae_attrs["Array::#{array_object}"] = ae_attrs[array_object].collect do |obj|
330+
# Each array attribute is tagged with Array:: before the attribute key unless it already starts with Array::
331+
array_attr_key = array_object
332+
if !array_object.starts_with?("Array::")
333+
array_attr_key = "Array::#{array_object}"
334+
end
335+
ae_attrs[array_attr_key] = ae_attrs[array_object].collect do |obj|
321336
obj.kind_of?(ActiveRecord::Base) ? "#{obj.class.name}::#{obj.id}" : obj.to_s
322337
end.join("\x1F")
323-
ae_attrs.delete(array_object)
338+
if !array_object.starts_with?("Array::")
339+
ae_attrs.delete(array_object)
340+
end
324341
end
325342
ae_attrs
326343
end

lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_object.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def process_args_as_attributes(args = {})
257257
@attributes.merge!(args)
258258
end
259259

260-
def process_args_array(args, args_key)s
260+
def process_args_array(args, args_key)
261261
# process Array::servers => MiqServer::2,MiqServer::3,MiqServer::4
262262
key = args_key.split(CLASS_SEPARATOR).last
263263
value = args.delete(args_key)

spec/miq_ae_engine_spec.rb

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,11 @@ def call_automate(obj_type, obj_id, open_url_task_id = nil)
289289
end
290290

291291
it "will process an array of objects" do
292+
FactoryBot.create(:host)
292293
FactoryBot.create(:host)
293294
hash = {"hosts" => Host.all}
294295
attrs = {"Array::my_hosts" => hash["hosts"].collect { |h| "Host::#{h.id}" }}
295-
result_str = "Array%3A%3Amy_hosts=" + hash["hosts"].collect { |h| "Host%3A%3A#{h.id}" }.join(",")
296+
result_str = "Array%3A%3Amy_hosts=" + hash["hosts"].collect { |h| "Host%3A%3A#{h.id}" }.join("%1F") # After URL encoding the separator "\x1F" is converted to %1F
296297
extras = "MiqServer%3A%3Amiq_server=#{miq_server_id}"
297298
uri = "/System/Process/AUTOMATION?#{result_str}&#{extras}&object_name=AUTOMATION"
298299
expect(MiqAeEngine.create_automation_object("AUTOMATION", attrs)).to eq(uri)
@@ -374,26 +375,28 @@ def call_automate(obj_type, obj_id, open_url_task_id = nil)
374375
end
375376

376377
it "with an array of Vms" do
377-
hash = {"vms" => Vm.all}
378-
result_str = "Array::vms=#{hash["vms"].collect { |v| "ManageIQ::Providers::Vmware::InfraManager::Vm::#{v.id}" }.join("\x1F")}"
379-
result_arr = hash["vms"].collect { |v| "ManageIQ::Providers::Vmware::InfraManager::Vm::#{v.id}" }.join("\x1F")
380-
result = MiqAeEngine.create_automation_attributes(hash)
378+
result_arr = []
379+
hash = {"vms" => Vm.all}
380+
result_str = "vms=#{hash["vms"].collect { |v| v.id.to_s }.join("=")}"
381+
hash["vms"].collect { |v| result_arr.push(v.id.to_s) }
382+
result = MiqAeEngine.create_automation_attributes(hash)
381383
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq(result_str)
382-
expect(result["Array::vms"]).to eq(result_arr)
384+
expect(result["vms"]).to eq(result_arr)
383385
end
384386

385387
it "with an array containing a single Vm" do
386-
hash = {"vms" => [Vm.first]}
387-
result_str = "Array::vms=#{hash["vms"].collect { |v| "ManageIQ::Providers::Vmware::InfraManager::Vm::#{v.id}" }.join("\x1F")}"
388-
result_arr = hash["vms"].collect { |v| "ManageIQ::Providers::Vmware::InfraManager::Vm::#{v.id}" }.join("\x1F")
389-
result = MiqAeEngine.create_automation_attributes(hash)
388+
result_arr = []
389+
hash = {"vms" => [Vm.first]}
390+
result_str = "vms=#{hash["vms"].collect { |v| v.id.to_s }.join("=")}"
391+
hash["vms"].collect { |v| result_arr.push(v.id.to_s) }
392+
result = MiqAeEngine.create_automation_attributes(hash)
390393
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq(result_str)
391-
expect(result["Array::vms"]).to eq(result_arr)
394+
expect(result["vms"]).to eq(result_arr)
392395
end
393396

394397
it "with an empty array" do
395398
result = MiqAeEngine.create_automation_attributes("vms" => [])
396-
expect(result["Array::vms"]).to eq("")
399+
expect(result["vms"]).to eq([])
397400
end
398401

399402
it "with a hash containing a single Vm" do
@@ -405,24 +408,27 @@ def call_automate(obj_type, obj_id, open_url_task_id = nil)
405408
end
406409

407410
it "with an array of Hosts" do
411+
result_arr = []
408412
hash = {"hosts" => Host.all}
409-
result_str = "Array::hosts=#{hash["hosts"].collect { |h| "Host::#{h.id}" }.join("\x1F")}"
410-
result_arr = hash["hosts"].collect { |h| "Host::#{h.id}" }.join("\x1F")
411-
result = MiqAeEngine.create_automation_attributes(hash)
413+
result_str = "hosts=#{hash["hosts"].collect { |h| h.id.to_s }.join("=")}"
414+
hash["hosts"].collect { |h| result_arr.push(h.id.to_s) }
415+
result = MiqAeEngine.create_automation_attributes(hash)
412416
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq(result_str)
413-
expect(result["Array::hosts"]).to eq(result_arr)
417+
expect(result["hosts"]).to eq(result_arr)
414418
end
415419

416420
it "with multiple arrays" do
421+
vm_result_arr = []
422+
host_result_arr = []
417423
hash = {"vms" => Vm.all}
418-
vm_result_str = "Array::vms=#{hash["vms"].collect { |v| "ManageIQ::Providers::Vmware::InfraManager::Vm::#{v.id}" }.join("\x1F")}"
419-
vm_result_arr = hash["vms"].collect { |v| "ManageIQ::Providers::Vmware::InfraManager::Vm::#{v.id}" }.join("\x1F")
424+
vm_result_str = "vms=#{hash["vms"].collect { |v| v.id.to_s }.join("=")}"
425+
hash["vms"].collect { |v| vm_result_arr.push(v.id.to_s) }
420426
hash["hosts"] = Host.all
421-
host_result_str = "Array::hosts=#{hash["hosts"].collect { |h| "Host::#{h.id}" }.join("\x1F")}"
422-
host_result_arr = hash["hosts"].collect { |h| "Host::#{h.id}" }.join("\x1F")
423-
result = MiqAeEngine.create_automation_attributes(hash)
424-
expect(result["Array::vms"]).to eq(vm_result_arr)
425-
expect(result["Array::hosts"]).to eq(host_result_arr)
427+
host_result_str = "hosts=#{hash["hosts"].collect { |h| h.id.to_s }.join("=")}"
428+
hash["hosts"].collect { |h| host_result_arr.push(h.id.to_s) }
429+
result = MiqAeEngine.create_automation_attributes(hash)
430+
expect(result["vms"]).to eq(vm_result_arr)
431+
expect(result["hosts"]).to eq(host_result_arr)
426432
result_str = MiqAeEngine.create_automation_attributes_string(hash)
427433
expect(result_str).to include(vm_result_str)
428434
expect(result_str).to include(host_result_str)
@@ -431,16 +437,16 @@ def call_automate(obj_type, obj_id, open_url_task_id = nil)
431437
it "with invalid object references" do
432438
hash = {"vms" => ["bogus::12"]}
433439
result = MiqAeEngine.create_automation_attributes(hash)
434-
expect(result["Array::vms"]).to eq("bogus::12")
435-
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq("Array::vms=bogus::12")
440+
expect(result["vms"]).to eq(["bogus::12"])
441+
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq("vms=bogus::12")
436442
end
437443

438444
it "with garbage values" do
439445
hash = {"vms" => ["bogus::12,garbage::moreso,notevenclose"]}
440-
bogus_arr = "bogus::12,garbage::moreso,notevenclose"
446+
bogus_arr = ["bogus::12,garbage::moreso,notevenclose"]
441447
result = MiqAeEngine.create_automation_attributes(hash)
442-
expect(result["Array::vms"]).to eq(bogus_arr)
443-
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq("Array::vms=bogus::12,garbage::moreso,notevenclose")
448+
expect(result["vms"]).to eq(bogus_arr)
449+
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq("vms=bogus::12,garbage::moreso,notevenclose")
444450
end
445451

446452
it "with a string value" do

0 commit comments

Comments
 (0)