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

Conversation

gmcculloug
Copy link
Member

Automate supports passing an array of objects on the URL to the engine. This PR modifies that logic to allow strings to be passed as well. This functionality supports the multi-select drop-down being added to the Service Dialogs in PR #10270.

Links

Steps for Testing/QA

Test using the multi-select control from the service dialog wired to a custom button. Configure the button to call the System/Request/InspectMe method. Logging will show the array containing each selected key passed from the dialog field.

@gmcculloug
Copy link
Member Author

@mkanoor @Fryguy Please review

cc @eclarizio @d-m-u

@mkanoor
Copy link
Contributor

mkanoor commented Oct 4, 2016

@gmcculloug @tinaafitz
We can use this format which is support by the engine to have an array of integers
"Array::my_values" => "integer::1,integer::2,integer::3"

this would get stored as my_values = [1,2,3]

This is already supported in https://github.com/ManageIQ/manageiq/blob/master/lib/miq_automation_engine/engine/miq_ae_object.rb#L524

But there is a bug in this line which only seems to honor vmdb objects
https://github.com/ManageIQ/manageiq/blob/master/lib/miq_automation_engine/engine/miq_ae_object.rb#L285
If we remove this line we can pass in integer, float, bool and all the basic types.

Another bug that exists is we cannot have spaces between values after the ,
e.g.
integer::1, integer::2, integer::3 won't work
but
integer::1,integer::2,integer::3 will work
We just need to add a trim after we get the elements one by one
https://github.com/ManageIQ/manageiq/blob/master/lib/miq_automation_engine/engine/miq_ae_object.rb#L283

expect(result["my_values"].first).to eq("abc")
expect(result["my_values"].second).to be_kind_of(MiqAeMethodService::MiqAeServiceVmOrTemplate)
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would just structure this slightly differently:

describe "#process_args_as_attributes" do
  let(:result) { @miq_obj.process_args_as_attributes("Array::my_values" => my_values) }

  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

@syncrou
Copy link
Contributor

syncrou commented Nov 29, 2016

@mkanoor - Regarding the bug referenced in

but there is a bug in this line which only seems to honor vmdb objects
https://github.com/ManageIQ/manageiq/blob/master/lib/miq_automation_engine/engine/miq_ae_object.rb#L285

Is there any known reason why that check took place?

end

it "stores the second value as a Host object" do
expect(result["my_values"].second).to eq("fred::12")
Copy link
Contributor

@syncrou syncrou Nov 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkanoor - @gmcculloug's implementation will allow for "fred::12" to get passed as datatype fred value 12 to process_args_as_attributes.

We had talked earlier of forcing process_args_as_attributes to raise an error if a datatype was invalid.

I prototyped some code to see how this would affect the the current specs.

CLASS_TYPES = %w( boolean TrueClass FalseClass time Time symbol Symbol integer Fixnum float Float array String password )

With the list of allowed datatypes as above

raise MiqAeException::InvalidClass unless CLASS_TYPES.include?(datatype)

This brought out other issues as there are sundry datatypes passed through process_args_as_attributes when the a new instance of the MiqAeEngine is instantiated. Many of these types are neither an exposed service model - or part of the basic types handled here: https://github.com/gmcculloug/manageiq/blob/b07a184a9c7cee349fac2725d93dc7afd6485a72/lib/miq_automation_engine/engine/miq_ae_object.rb#L527-L535

The original code would allow all of these invalid datatypes to pass through.

I'm wondering if @gmcculloug's implementation will allow for fewer legacy Automate code issues?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@syncrou Are we checking if datatype is specified before we check for validity and raise the error

objects_str.split(',').collect do |element|
if element.include?(CLASS_SEPARATOR)
klass, str_value = element.split(CLASS_SEPARATOR)
value = MiqAeObject.convert_value_based_on_datatype(str_value, klass)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gmcculloug - I ran into an issue with klass if strip is not called here.

when a string is passed in "integer::1, integer::3, integer::5" klass is set to " integer" and doesn't resolve correctly in convert_value_based_on_datatype https://github.com/gmcculloug/manageiq/blob/b07a184a9c7cee349fac2725d93dc7afd6485a72/lib/miq_automation_engine/engine/miq_ae_object.rb#L532

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@syncrou we would have to trim the klass value before we use it.

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}" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@syncrou
is the ,, testing nil values? But wouldn't fred::12 raise the exception before it processes the nil value

@syncrou
Copy link
Contributor

syncrou commented Jan 4, 2017

@mkanoor - I modified the test you commented on and added a new one to handle the empty values.

@miq-bot
Copy link
Member

miq-bot commented Jan 9, 2017

This pull request is not mergeable. Please rebase and repush.

@miq-bot
Copy link
Member

miq-bot commented Jan 12, 2017

Checked commits gmcculloug/manageiq@82711a5~...3d1954e with ruby 2.2.5, rubocop 0.37.2, and haml-lint 0.16.1
2 files checked, 1 offense detected

spec/lib/miq_automation_engine/miq_ae_object_spec.rb

@gmcculloug
Copy link
Member Author

@mkanoor Please review

@mkanoor mkanoor merged commit 0629d21 into ManageIQ:master Jan 13, 2017
@mkanoor mkanoor added this to the Sprint 52 Ending Jan 16, 2017 milestone Jan 13, 2017
@simaishi
Copy link
Contributor

simaishi commented Mar 2, 2017

@gmcculloug Is there a BZ for this (or any related PRs)? Can you please create one if it doesn't already exist?

@gmcculloug
Copy link
Member Author

@simaishi BZ https://bugzilla.redhat.com/show_bug.cgi?id=1428552 opened to track this feature.

Also requires #10270 and ManageIQ/manageiq-ui-classic#114

@simaishi
Copy link
Contributor

simaishi commented Mar 9, 2017

@gmcculloug lib/miq_automation_engine/engine/miq_ae_engine/miq_ae_object.rb doesn't exist in Euwe. Please create an Euwe-specific PR (referencing this one) or suggest other PRs to backport.

syncrou added a commit to syncrou/manageiq that referenced this pull request Mar 17, 2017
syncrou added a commit to syncrou/manageiq that referenced this pull request Mar 20, 2017
@simaishi
Copy link
Contributor

Backported to Euwe via #14394

@moolitayer
Copy link

moolitayer commented Apr 2, 2017

@mkanoor @mkanoor @syncrou I suspect I'm seeing an error related to this on euwe:
https://bugzilla.redhat.com/show_bug.cgi?id=1437128

We might be using the automate wrong, but anyway this change exposed the error.
Also might be happening for vm_or_template.check_policy_prevent since those also aren't exposed in SM_LOOKUP

@gmcculloug gmcculloug deleted the automate_array_support branch January 1, 2020 23:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants