Skip to content

Commit 1ef49fb

Browse files
committed
Revert "Merge pull request #545 from GilbertCherrie/fix_automation_arrays"
This reverts commit 7707684, reversing changes made to ac85c9f.
1 parent 5aff915 commit 1ef49fb

File tree

3 files changed

+38
-63
lines changed

3 files changed

+38
-63
lines changed

lib/miq_automation_engine/engine/miq_ae_engine.rb

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def self.create_automation_attribute_name(object)
212212

213213
def self.create_automation_attribute_key(object, attr_name = nil)
214214
klass_name = create_automation_attribute_class_name(object)
215-
return klass_name.to_s if automation_attribute_is_array?(klass_name, object)
215+
return klass_name.to_s if automation_attribute_is_array?(klass_name)
216216

217217
attr_name ||= create_automation_attribute_name(object)
218218
"#{klass_name}::#{attr_name}"
@@ -222,12 +222,8 @@ def self.create_automation_attribute_value(object)
222222
object.id
223223
end
224224

225-
def self.automation_attribute_is_array?(attr, object = nil)
226-
if !object.nil? && !object.kind_of?(String) && object.has_attribute?(:options) && !object.options.nil? && !object.options[:dialog].nil?
227-
object[:options][:dialog][attr].kind_of?(Array)
228-
else
229-
attr.to_s.downcase.starts_with?("array::")
230-
end
225+
def self.automation_attribute_is_array?(attr)
226+
attr.to_s.downcase.starts_with?("array::")
231227
end
232228

233229
def self.create_automation_attributes_string(hash)
@@ -258,13 +254,13 @@ def self.create_automation_attribute(key, value)
258254
end
259255

260256
def self.create_automation_attribute_array_key(key)
261-
key
257+
"Array::#{key}"
262258
end
263259

264260
def self.create_automation_attribute_array_value(value)
265261
value.collect do |obj|
266-
obj.kind_of?(ActiveRecord::Base) ? obj.id.to_s : obj.to_s
267-
end
262+
obj.kind_of?(ActiveRecord::Base) ? "#{obj.class.name}::#{obj.id}" : obj.to_s
263+
end.join("\x1F")
268264
end
269265

270266
def self.set_automation_attributes_from_objects(objects, attrs_hash)
@@ -315,20 +311,9 @@ def self.create_ae_attrs(attrs, name, vmdb_object, objects = [MiqServer.my_serve
315311
ae_attrs["MiqRequest::miq_request"] = vmdb_object.id if vmdb_object.kind_of?(MiqRequest)
316312
ae_attrs['vmdb_object_type'] = create_automation_attribute_name(vmdb_object) unless vmdb_object.nil?
317313

318-
# Sends array attributes to the miq_ae_object as strings with \x1F between each array item.
319-
array_objects = ae_attrs.keys.find_all { |key| ae_attrs[key].kind_of?(Array) }
320-
array_objects.each do |array_object|
321-
# Each array attribute is tagged with Array:: before the attribute key unless it already starts with Array::
322-
array_attr_key = array_object
323-
if !array_object.starts_with?("Array::")
324-
array_attr_key = "Array::#{array_object}"
325-
end
326-
ae_attrs[array_attr_key] = ae_attrs[array_object].collect do |obj|
327-
obj.kind_of?(ActiveRecord::Base) ? "#{obj.class.name}::#{obj.id}" : obj.to_s
328-
end.join("\x1F")
329-
if !array_object.starts_with?("Array::")
330-
ae_attrs.delete(array_object)
331-
end
314+
array_objects = ae_attrs.keys.find_all { |key| automation_attribute_is_array?(key) }
315+
array_objects.each do |o|
316+
ae_attrs[o] = ae_attrs[o].first if ae_attrs[o].kind_of?(Array)
332317
end
333318
ae_attrs
334319
end

lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_object.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def user_info_attributes(user)
253253
end
254254

255255
def process_args_as_attributes(args = {})
256-
args.keys.each { |k| attribute_is_array?(k) ? process_args_array(args, k) : process_args_attribute(args, k) }
256+
args.keys.each { |k| MiqAeEngine.automation_attribute_is_array?(k) ? process_args_array(args, k) : process_args_attribute(args, k) }
257257
@attributes.merge!(args)
258258
end
259259

@@ -264,10 +264,6 @@ def process_args_array(args, args_key)
264264
args[key.downcase] = load_array_objects_from_string(value)
265265
end
266266

267-
def attribute_is_array?(attr)
268-
attr.to_s.downcase.starts_with?("array::")
269-
end
270-
271267
def process_args_attribute(args, args_key)
272268
# process MiqServer::svr => 2
273269
if args_key.include?(CLASS_SEPARATOR)

spec/miq_ae_engine_spec.rb

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,10 @@ 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)
293292
FactoryBot.create(:host)
294293
hash = {"hosts" => Host.all}
295294
attrs = {"Array::my_hosts" => hash["hosts"].collect { |h| "Host::#{h.id}" }}
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
295+
result_str = "Array%3A%3Amy_hosts=" + hash["hosts"].collect { |h| "Host%3A%3A#{h.id}" }.join(",")
297296
extras = "MiqServer%3A%3Amiq_server=#{miq_server_id}"
298297
uri = "/System/Process/AUTOMATION?#{result_str}&#{extras}&object_name=AUTOMATION"
299298
expect(MiqAeEngine.create_automation_object("AUTOMATION", attrs)).to eq(uri)
@@ -375,28 +374,26 @@ 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 = []
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)
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)
383381
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq(result_str)
384-
expect(result["vms"]).to eq(result_arr)
382+
expect(result["Array::vms"]).to eq(result_arr)
385383
end
386384

387385
it "with an array containing a single Vm" do
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)
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)
393390
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq(result_str)
394-
expect(result["vms"]).to eq(result_arr)
391+
expect(result["Array::vms"]).to eq(result_arr)
395392
end
396393

397394
it "with an empty array" do
398395
result = MiqAeEngine.create_automation_attributes("vms" => [])
399-
expect(result["vms"]).to eq([])
396+
expect(result["Array::vms"]).to eq("")
400397
end
401398

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

410407
it "with an array of Hosts" do
411-
result_arr = []
412408
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) }
415-
result = MiqAeEngine.create_automation_attributes(hash)
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)
416412
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq(result_str)
417-
expect(result["hosts"]).to eq(result_arr)
413+
expect(result["Array::hosts"]).to eq(result_arr)
418414
end
419415

420416
it "with multiple arrays" do
421-
vm_result_arr = []
422-
host_result_arr = []
423417
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) }
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")
426420
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) }
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)
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)
432426
result_str = MiqAeEngine.create_automation_attributes_string(hash)
433427
expect(result_str).to include(vm_result_str)
434428
expect(result_str).to include(host_result_str)
@@ -437,16 +431,16 @@ def call_automate(obj_type, obj_id, open_url_task_id = nil)
437431
it "with invalid object references" do
438432
hash = {"vms" => ["bogus::12"]}
439433
result = MiqAeEngine.create_automation_attributes(hash)
440-
expect(result["vms"]).to eq(["bogus::12"])
441-
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq("vms=bogus::12")
434+
expect(result["Array::vms"]).to eq("bogus::12")
435+
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq("Array::vms=bogus::12")
442436
end
443437

444438
it "with garbage values" do
445439
hash = {"vms" => ["bogus::12,garbage::moreso,notevenclose"]}
446-
bogus_arr = ["bogus::12,garbage::moreso,notevenclose"]
440+
bogus_arr = "bogus::12,garbage::moreso,notevenclose"
447441
result = MiqAeEngine.create_automation_attributes(hash)
448-
expect(result["vms"]).to eq(bogus_arr)
449-
expect(MiqAeEngine.create_automation_attributes_string(hash)).to eq("vms=bogus::12,garbage::moreso,notevenclose")
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")
450444
end
451445

452446
it "with a string value" do

0 commit comments

Comments
 (0)