Skip to content

Commit 8d0cf45

Browse files
Fix ae engine tags
1 parent 7cdc46c commit 8d0cf45

File tree

3 files changed

+101
-27
lines changed

3 files changed

+101
-27
lines changed

lib/miq_automation_engine/engine/miq_ae_engine.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,11 @@ def self.create_automation_attribute_array_key(key)
262262
end
263263

264264
def self.create_automation_attribute_array_value(value)
265-
value.collect do |obj|
266-
obj.kind_of?(ActiveRecord::Base) ? obj.id.to_s : obj.to_s
265+
# When array of length 1 just return the single object or id instead of an array
266+
result = value.collect do |obj|
267+
obj.kind_of?(ActiveRecord::Base) ? "#{obj.class.name}::#{obj.id}" : obj.to_s
267268
end
269+
result.length == 1 ? result.first : result
268270
end
269271

270272
def self.set_automation_attributes_from_objects(objects, attrs_hash)
@@ -320,7 +322,13 @@ def self.create_ae_attrs(attrs, name, vmdb_object, objects = [MiqServer.my_serve
320322
array_objects.each do |array_object|
321323
# Each array attribute is tagged with Array:: before the attribute key unless it already starts with Array::
322324
array_attr_key = array_object
323-
if !array_object.starts_with?("Array::")
325+
if !ae_attrs[array_object].empty? && ae_attrs[array_object].first.to_s.split("::").first == "Classification"
326+
array_attr_key = if array_object.starts_with?("Array::")
327+
"Tag#{array_object}"
328+
else
329+
"TagArray::#{array_object}"
330+
end
331+
elsif !array_object.starts_with?("Array::")
324332
array_attr_key = "Array::#{array_object}"
325333
end
326334
ae_attrs[array_attr_key] = ae_attrs[array_object].collect do |obj|
@@ -329,6 +337,9 @@ def self.create_ae_attrs(attrs, name, vmdb_object, objects = [MiqServer.my_serve
329337
if !array_object.starts_with?("Array::")
330338
ae_attrs.delete(array_object)
331339
end
340+
if !array_object.starts_with?("TagArray::") && array_attr_key.starts_with?("TagArray::")
341+
ae_attrs.delete(array_object)
342+
end
332343
end
333344
ae_attrs
334345
end

lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_object.rb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,19 +258,34 @@ def process_args_as_attributes(args = {})
258258
end
259259

260260
def process_args_array(args, args_key)
261-
# process Array::servers => MiqServer::2,MiqServer::3,MiqServer::4
261+
# Process Array::servers => MiqServer::2,MiqServer::3,MiqServer::4
262+
# If no class seperator exists then return Array::servers => 2,3,4
262263
key = args_key.split(CLASS_SEPARATOR).last
263264
value = args.delete(args_key)
264265
args[key.downcase] = load_array_objects_from_string(value)
265266
end
266267

267268
def attribute_is_array?(attr)
268-
attr.to_s.downcase.starts_with?("array::")
269+
attr.to_s.downcase.starts_with?("array::") && !attr.to_s.downcase.starts_with?("tagarray")
269270
end
270271

271272
def process_args_attribute(args, args_key)
272-
# process MiqServer::svr => 2
273-
if args_key.include?(CLASS_SEPARATOR)
273+
# Process TagArray::dialog_tags => <Classification ...>, <Classification ...>, <Classification ...>
274+
if args_key.to_s.downcase.starts_with?("tagarray")
275+
tags = []
276+
args[args_key].split("\u001F").each do |tag|
277+
# args_key: TagArray::dialog_param_test1
278+
# args[args_key]: <Classification ...>, <Classification ...>, <Classification ...>
279+
# tag: <Classification ...>
280+
key, klass = get_key_name_and_klass_from_key(tag)
281+
tags.push(MiqAeObject.convert_value_based_on_datatype(key, klass))
282+
args["#{key}_id"] = tag if attribute_for_vmdb_object?(klass, args[args_key]) && !@attributes.key?(key)
283+
end
284+
args.delete(args_key)
285+
key = args_key.split("::").last
286+
args[key] = tags
287+
# Process MiqServer::svr => 2
288+
elsif args_key.include?(CLASS_SEPARATOR)
274289
key, klass = get_key_name_and_klass_from_key(args_key)
275290
value = args.delete(args_key)
276291
args["#{key}_id"] = value if attribute_for_vmdb_object?(klass, value) && !@attributes.key?(key)

spec/miq_ae_engine_spec.rb

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,8 @@ def call_automate(obj_type, obj_id, open_url_task_id = nil)
309309

310310
it "will process an array of objects with a server and user" do
311311
extras = "MiqServer%3A%3Amiq_server=12"
312-
FactoryBot.create(:small_environment)
313-
attrs = {"MiqServer::miq_server" => "12", "array::tag" => "Classification::1,Classification::2"}
314-
result_str = "array%3A%3Atag=Classification%3A%3A1%2CClassification%3A%3A2"
312+
attrs = {"MiqServer::miq_server" => "12", "Array::tags" => ["Classification::1,Classification::2"]}
313+
result_str = "TagArray%3A%3Atags=Classification%3A%3A1%2CClassification%3A%3A2"
315314
uri = "/System/Process/AUTOMATION?#{extras}&#{result_str}&object_name=AUTOMATION"
316315
expect(MiqAeEngine.create_automation_object("AUTOMATION", attrs)).to eq(uri)
317316
end
@@ -375,20 +374,22 @@ def call_automate(obj_type, obj_id, open_url_task_id = nil)
375374
end
376375

377376
it "with an array of Vms" do
378-
result_arr = []
379377
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) }
378+
result_str = "vms=#{hash["vms"].collect { |v| "ManageIQ::Providers::Vmware::InfraManager::Vm::#{v.id}" }.join("=")}"
382379
result = MiqAeEngine.create_automation_attributes(hash)
380+
result_arr = if hash["vms"].length == 1
381+
"ManageIQ::Providers::Vmware::InfraManager::Vm::#{hash["vms"][0].id}"
382+
else
383+
hash["vms"].collect { |v| "ManageIQ::Providers::Vmware::InfraManager::Vm::#{v.id}" }
384+
end
383385
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq(result_str)
384386
expect(result["vms"]).to eq(result_arr)
385387
end
386388

387389
it "with an array containing a single Vm" do
388-
result_arr = []
389390
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) }
391+
result_str = "vms=#{hash["vms"].collect { |v| "ManageIQ::Providers::Vmware::InfraManager::Vm::#{v.id}" }.join("=")}"
392+
result_arr = "ManageIQ::Providers::Vmware::InfraManager::Vm::#{hash["vms"][0].id}"
392393
result = MiqAeEngine.create_automation_attributes(hash)
393394
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq(result_str)
394395
expect(result["vms"]).to eq(result_arr)
@@ -408,24 +409,33 @@ def call_automate(obj_type, obj_id, open_url_task_id = nil)
408409
end
409410

410411
it "with an array of Hosts" do
411-
result_arr = []
412412
hash = {"hosts" => Host.all}
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) }
413+
result_str = "hosts=#{hash["hosts"].collect { |h| "Host::#{h.id}" }.join("=")}"
414+
result_arr = if hash["hosts"].length == 1
415+
"Host::#{hash["hosts"][0].id}"
416+
else
417+
hash["hosts"].collect { |h| "Host::#{h.id}" }
418+
end
415419
result = MiqAeEngine.create_automation_attributes(hash)
416420
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq(result_str)
417421
expect(result["hosts"]).to eq(result_arr)
418422
end
419423

420424
it "with multiple arrays" do
421-
vm_result_arr = []
422-
host_result_arr = []
423425
hash = {"vms" => Vm.all}
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) }
426+
vm_result_str = "vms=#{hash["vms"].collect { |v| "ManageIQ::Providers::Vmware::InfraManager::Vm::#{v.id}" }.join("=")}"
427+
vm_result_arr = if hash["vms"].length == 1
428+
"ManageIQ::Providers::Vmware::InfraManager::Vm::#{hash["vms"][0].id}"
429+
else
430+
hash["vms"].collect { |v| "ManageIQ::Providers::Vmware::InfraManager::Vm::#{v.id}" }
431+
end
426432
hash["hosts"] = Host.all
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) }
433+
host_result_str = "hosts=#{hash["hosts"].collect { |h| "Host::#{h.id}" }.join("=")}"
434+
host_result_arr = if hash["hosts"].length == 1
435+
"Host::#{hash["hosts"][0].id}"
436+
else
437+
hash["hosts"].collect { |h| "Host#{h.id}" }
438+
end
429439
result = MiqAeEngine.create_automation_attributes(hash)
430440
expect(result["vms"]).to eq(vm_result_arr)
431441
expect(result["hosts"]).to eq(host_result_arr)
@@ -434,16 +444,32 @@ def call_automate(obj_type, obj_id, open_url_task_id = nil)
434444
expect(result_str).to include(host_result_str)
435445
end
436446

447+
it "with an array of Tags" do
448+
FactoryBot.create(:classification)
449+
FactoryBot.create(:classification)
450+
FactoryBot.create(:classification)
451+
hash = {"tags" => Classification.all}
452+
result_str = "tags=#{hash["tags"].collect { |h| "Classification::#{h.id}" }.join("=")}"
453+
result_arr = if hash["tags"].length == 1
454+
"Classification::#{hash["tags"][0].id}"
455+
else
456+
hash["tags"].collect { |h| "Classification::#{h.id}" }
457+
end
458+
result = MiqAeEngine.create_automation_attributes(hash)
459+
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq(result_str)
460+
expect(result["tags"]).to eq(result_arr)
461+
end
462+
437463
it "with invalid object references" do
438464
hash = {"vms" => ["bogus::12"]}
439465
result = MiqAeEngine.create_automation_attributes(hash)
440-
expect(result["vms"]).to eq(["bogus::12"])
466+
expect(result["vms"]).to eq("bogus::12")
441467
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq("vms=bogus::12")
442468
end
443469

444470
it "with garbage values" do
445471
hash = {"vms" => ["bogus::12,garbage::moreso,notevenclose"]}
446-
bogus_arr = ["bogus::12,garbage::moreso,notevenclose"]
472+
bogus_arr = "bogus::12,garbage::moreso,notevenclose"
447473
result = MiqAeEngine.create_automation_attributes(hash)
448474
expect(result["vms"]).to eq(bogus_arr)
449475
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq("vms=bogus::12,garbage::moreso,notevenclose")
@@ -789,6 +815,28 @@ def call_automate(obj_type, obj_id, open_url_task_id = nil)
789815
my_objects_array.each { |o| o.kind_of?(MiqAeMethodService::MiqAeServiceModelBase) }
790816
end
791817

818+
it "processes tags array arguments properly" do
819+
tag1 = FactoryBot.create(:classification)
820+
tag2 = FactoryBot.create(:classification)
821+
tag3 = FactoryBot.create(:classification)
822+
823+
EvmSpecHelper.import_yaml_model(File.join(model_data_dir, "miq_ae_engine_spec5"), domain)
824+
ws = MiqAeEngine.instantiate("/EVM/AUTOMATE/test1?TagArray::my_objects=Classification::#{tag1.id}\x1FClassification::#{tag2.id}\x1FClassification::#{tag3.id}", user)
825+
my_objects_array = ws.root("my_objects")
826+
expect(my_objects_array.length).to eq(3)
827+
my_objects_array.each { |o| o.kind_of?(MiqAeMethodService::MiqAeServiceModelBase) }
828+
end
829+
830+
it "processes tags array with a single value arguments properly" do
831+
tag1 = FactoryBot.create(:classification)
832+
833+
EvmSpecHelper.import_yaml_model(File.join(model_data_dir, "miq_ae_engine_spec5"), domain)
834+
ws = MiqAeEngine.instantiate("/EVM/AUTOMATE/test1?TagArray::my_objects=Classification::#{tag1.id}", user)
835+
my_objects_array = ws.root("my_objects")
836+
expect(my_objects_array.length).to eq(1)
837+
my_objects_array.each { |o| o.kind_of?(MiqAeMethodService::MiqAeServiceModelBase) }
838+
end
839+
792840
it "processes an empty array properly" do
793841
EvmSpecHelper.import_yaml_model(File.join(model_data_dir, "miq_ae_engine_spec6"), domain)
794842
ws = MiqAeEngine.instantiate("/EVM/AUTOMATE/test1?Array::my_objects=", user)

0 commit comments

Comments
 (0)