Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automate engine support for array elements containing text values. #11667

Merged
merged 3 commits into from
Jan 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,13 @@ def process_args_attribute(args, args_key)
end

def load_array_objects_from_string(objects_str)
objects_str.split(',').collect do |o|
klass, str_value = o.split(CLASS_SEPARATOR)
value = MiqAeObject.convert_value_based_on_datatype(str_value, klass)
value if value.kind_of?(MiqAeMethodService::MiqAeServiceModelBase)
objects_str.split(',').collect do |element|
if element.include?(CLASS_SEPARATOR)
klass, str_value = element.split(CLASS_SEPARATOR)
MiqAeObject.convert_value_based_on_datatype(str_value.strip, klass.strip)
else
element.presence
end
end.compact
end

Expand Down Expand Up @@ -532,6 +535,8 @@ def self.convert_value_based_on_datatype(value, datatype)
return service_model.find(value)
end

(raise MiqAeException::InvalidClass unless MiqAeField.available_datatypes.include?(datatype)) if datatype

# default datatype => 'string'
value
end
Expand Down
86 changes: 74 additions & 12 deletions spec/lib/miq_automation_engine/miq_ae_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,81 @@ def value_match(value, xml_value)
expect(result["vms"].length).to eq(2)
end

it "#process_args_as_attributes with an array containing invalid entries" do
vm2 = FactoryGirl.create(:vm_vmware)
result = @miq_obj.process_args_as_attributes({"Array::vms" => "VmOrTemplate::#{@vm.id},fred::12,,VmOrTemplate::#{vm2.id}"})
expect(result["vms"]).to be_kind_of(Array)
expect(result["vms"].length).to eq(2)
end
describe "#process_args_as_attributes" do
let(:result) { @miq_obj.process_args_as_attributes("Array::my_values" => my_values) }

context "with an array containing invalid entries" do
let(:my_values) { "VmOrTemplate::#{@vm.id},fred::12,VmOrTemplate::#{FactoryGirl.create(:vm_vmware).id}" }

it "raises an exception" do
expect { @miq_obj.process_args_as_attributes("Array::vms" => my_values) }.to raise_exception MiqAeException::InvalidClass
end
end

context "with an array containing empty entries" do
let(:my_values) { "VmOrTemplate::#{@vm.id},,,VmOrTemplate::#{FactoryGirl.create(:vm_vmware).id}" }

it "ignores empty values and returns everything else" do
expect(result["my_values"].size).to eq 2
expect(result["my_values"][0].id).to eq @vm.id
end
end

context "with an array including spaces after the commas" do
let(:my_values) { "integer::1, integer::3, integer::10" }

it "stores the values as an array of strings" do
expect(result["my_values"]).to eq([1, 3, 10])
end
end

context "with an array including no spaces after the commas" do
let(:my_values) { "integer::1,integer::3,integer::10" }

it "stores the values as an array of strings" do
expect(result["my_values"]).to eq([1, 3, 10])
end
end

context "with an array containing disparate objects" do
let!(:my_values) do
host = FactoryGirl.create(:host)
ems = FactoryGirl.create(:ems_vmware)
"VmOrTemplate::#{@vm.id},Host::#{host.id},ExtManagementSystem::#{ems.id}"
end

it "stores the first value as a VM object" do
expect(result["my_values"].first).to be_kind_of(MiqAeMethodService::MiqAeServiceVm)
end

it "stores the second value as a Host object" do
expect(result["my_values"].second).to be_kind_of(MiqAeMethodService::MiqAeServiceHost)
end

it "stores the third value as a ExtManagementSystem object" do
expect(result["my_values"].third).to be_kind_of(MiqAeMethodService::MiqAeServiceExtManagementSystem)
end
end

it "#process_args_as_attributes with an array containing disparate objects" do
host = FactoryGirl.create(:host)
ems = FactoryGirl.create(:ems_vmware)
result = @miq_obj.process_args_as_attributes({"Array::my_objects" => "VmOrTemplate::#{@vm.id},Host::#{host.id},ExtManagementSystem::#{ems.id}"})
expect(result["my_objects"]).to be_kind_of(Array)
expect(result["my_objects"].length).to eq(3)
context "with an array containing strings" do
let(:my_values) { "abc,xyz,,1" }

it "stores the values as an array of strings" do
expect(result["my_values"]).to eq(%w(abc xyz 1))
end
end

context "with an array containing strings and objects" do
let(:my_values) { "abc,VmOrTemplate::#{@vm.id}" }

it "stores the first value as a string" do
expect(result["my_values"].first).to eq("abc")
end

it "stores the second value as an MiqAeMethodService::MiqAeServiceVmOrTemplate" do
expect(result["my_values"].second).to be_kind_of(MiqAeMethodService::MiqAeServiceVmOrTemplate)
end
end
end

it "disabled inheritance" do
Expand Down