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

Prepare parameter hash before passing to Tower API credential CU #14483

Merged
merged 5 commits into from
Mar 27, 2017

Conversation

jameswnl
Copy link
Contributor

  • convert :userid to :username
  • add :kind
  • add :organization => 1 if it's the EmbeddedAnsible. I.e. assign to Default organization in Tower

@jameswnl
Copy link
Contributor Author

@miq-bot add_labels wip, enhancement, providers/ansible_provider

@miq-bot
Copy link
Member

miq-bot commented Mar 23, 2017

@jameswnl Cannot apply the following label because they are not recognized: providers/ansible_provider

@miq-bot miq-bot changed the title Prepare parameter hash before passing to Tower API credential CU [WIP] Prepare parameter hash before passing to Tower API credential CU Mar 23, 2017
@jameswnl
Copy link
Contributor Author

@miq-bot add_labels providers/ansible_tower

@jameswnl jameswnl changed the title [WIP] Prepare parameter hash before passing to Tower API credential CU Prepare parameter hash before passing to Tower API credential CU Mar 23, 2017
@jameswnl
Copy link
Contributor Author

@miq-bot remove_labels wip

@miq-bot miq-bot removed the wip label Mar 23, 2017
bdunne
bdunne previously requested changes Mar 24, 2017
let(:finished_task) { FactoryGirl.create(:miq_task, :state => "Finished") }
let(:manager) { FactoryGirl.create(:provider_ansible_tower, :with_authentication).managers.first }
let(:manager) { FactoryGirl.create(ansible_provider, :with_authentication).managers.first }
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 it would be much simpler to define a let for the provider outside of the shared examples?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I tried it (defining outside), but it is failing somehow. To succeed, I'll have to pass it implicitly.

I feel having the shared_examples parameters being explicitly declared in the block opening (as in shared_examples_for do | ansible_provider | ) provides more clarity.

Copy link
Member

Choose a reason for hiding this comment

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

It works here... #14419

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, but as I said, that is passing the parameter implicitly and I prefer more explicit passing which brings more clarity to the code.

@@ -75,4 +83,5 @@ def delete_in_provider_queue
COMMON_ATTRIBUTES = {}.freeze
EXTRA_ATTRIBUTES = {}.freeze
API_ATTRIBUTES = COMMON_ATTRIBUTES.merge(EXTRA_ATTRIBUTES).freeze
# TOWER_KIND = 'ssh'.freeze # default to `ssh` just like Tower does
Copy link
Member

Choose a reason for hiding this comment

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

Why is this commented?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i am not sure if we want/need this default at all. what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

This type should never be created, right? So I wouldn't include the constant here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right. I had this line started out for helping the test (as it was originally at the Ansible::Credential level. yeah, now removed.

include ManageIQ::Providers::AnsibleTower::Shared::AutomationManager::Credential

def self.provider_params(params)
super.merge(:organization => 1)
Copy link
Member

Choose a reason for hiding this comment

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

Is it safe to hard code this id?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See my new patch. Thanks Brandon!

@bdunne bdunne dismissed their stale review March 24, 2017 22:31

Not blocking the PR for the spec

@mzazrivec
Copy link
Contributor

With the changes from this PR in place, I still cannot update userid for a machine credential via api:

$ curl -X PUT -d '{"userid":"user01"}' http://admin:smartvm@localhost:3000/api/authentications/10000000000184
...

Not when I look at the task id returned:

$ curl http://admin:smartvm@localhost:3000/api/tasks/10000000111645|json_reformat
{
    "href": "http://localhost:3000/api/tasks/10000000111645",
    "id": 10000000111645,
    "name": "Updating ManageIQ::Providers::EmbeddedAnsible::AutomationManager::MachineCredential with manager_ref=45",
    "state": "Finished",
    "status": "Error",
    "message": "undefined method `userid=' for #<AnsibleTowerClient::Credential:0x0055efef731808>\nDid you mean?  username=",
    "userid": "system",
    "created_on": "2017-03-27T13:27:56Z",
    "updated_on": "2017-03-27T13:28:00Z"
}

@jameswnl
Copy link
Contributor Author

@mzazrivec I just did one with my setup and can't reproduce yours.

@jameswnl
Copy link
Contributor Author

I just pushed an update to have EmbeddedAnsible to always augment the parameters being passed to Tower to have :organization_id => provider.default_organization.

@jameswnl
Copy link
Contributor Author

Found the cause to @mzazrivec 's issue. the userid being passed in is string while backend code is expecting :userid - a symbol. Now backend will work on both.

@@ -1,5 +1,9 @@
require 'support/ansible_shared/automation_manager/credential'

describe ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ScmCredential do
it_behaves_like 'ansible credential', :provider_embedded_ansible
let(:ansible_manager) do
FactoryGirl.create(:provider_embedded_ansible, :with_authentication, :with_default_organization).managers.first
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it's reusable enough to justify adding a trait to the factory, this should work:

FactoryGirl.create(:provider_embedded_ansible, :with_authentication, :default_organization => 1).managers.first

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great! Good to know. Done now

let(:finished_task) { FactoryGirl.create(:miq_task, :state => "Finished") }
let(:manager) { FactoryGirl.create(ansible_provider, :with_authentication).managers.first }
let(:manager) { ansible_manager }
Copy link
Member

Choose a reason for hiding this comment

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

You don't need to do this. You can drop this line if you rename here and here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

@@ -23,6 +23,11 @@
x.authentications << FactoryGirl.create(:authentication, :status => "Valid")
end
end
trait(:with_default_organization) do
Copy link
Member

Choose a reason for hiding this comment

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

Please remove. See comment below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

@miq-bot
Copy link
Member

miq-bot commented Mar 27, 2017

Checked commits jameswnl/manageiq@5346f5c~...de286f8 with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0
9 files checked, 7 offenses detected

spec/support/ansible_shared/automation_manager/credential.rb

  • ❗ - Line 22, Col 9 - Style/AlignHash - Align the elements of a hash literal if they span more than one line.
  • ❗ - Line 23, Col 9 - Style/AlignHash - Align the elements of a hash literal if they span more than one line.
  • ❗ - Line 29, Col 9 - Style/AlignHash - Align the elements of a hash literal if they span more than one line.
  • ❗ - Line 30, Col 9 - Style/AlignHash - Align the elements of a hash literal if they span more than one line.
  • ❗ - Line 31, Col 9 - Style/AlignHash - Align the elements of a hash literal if they span more than one line.
  • ❗ - Line 32, Col 9 - Style/AlignHash - Align the elements of a hash literal if they span more than one line.
  • ❗ - Line 33, Col 9 - Style/AlignHash - Align the elements of a hash literal if they span more than one line.

@@ -1,5 +1,9 @@
require 'support/ansible_shared/automation_manager/credential'

describe ManageIQ::Providers::AnsibleTower::AutomationManager::Credential do
describe ManageIQ::Providers::AnsibleTower::AutomationManager::ScmCredential do
Copy link
Member

Choose a reason for hiding this comment

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

Why is credential_spec.rb testing ScmCredential?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because ManageIQ::Providers::AnsibleTower::AutomationManager::Credential is not a concrete class and doesn't have TOWER_KIND.

Maybe I'll rename the file to scm_credential_spec.rb?

@@ -2,7 +2,15 @@ module ManageIQ::Providers::AnsibleTower::Shared::AutomationManager::Credential
extend ActiveSupport::Concern

module ClassMethods
def provider_params(params)
params[:username] = params.delete(:userid) if params.include?(:userid)
params[:username] = params.delete('userid') if params.include?('userid')
Copy link
Member

Choose a reason for hiding this comment

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

Why both a String and Symbol key for userid?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

string type is coming from API calls via HTTP.

You want me to take away the line handling :userid? I keep it there so it will be consistent with all other attributes as in here

Other usage through ruby method invocation can use whole set of attributes using symbol key.

or else will have to change all the credentials' ATTRIBUTES hash keys.

@bdunne bdunne merged commit 4a645a4 into ManageIQ:master Mar 27, 2017
bdunne added a commit to bdunne/manageiq that referenced this pull request Mar 27, 2017
@bdunne bdunne added this to the Sprint 57 Ending Mar 27, 2017 milestone Mar 27, 2017
@chessbyte chessbyte assigned bdunne and unassigned blomquisg Mar 27, 2017
mzazrivec added a commit to mzazrivec/manageiq-ui-classic that referenced this pull request Mar 28, 2017
odravison pushed a commit to odravison/manageiq that referenced this pull request Mar 28, 2017
mzazrivec added a commit to mzazrivec/manageiq-ui-classic that referenced this pull request Mar 29, 2017
@jameswnl jameswnl deleted the massage-cred branch March 29, 2017 14:34
@martinpovolny
Copy link
Member

martinpovolny commented Apr 4, 2017

UI ManageIQ/manageiq-ui-classic#818 is fine/yes so this has to be too.

@simaishi
Copy link
Contributor

simaishi commented Apr 4, 2017

@jameswnl Can you take a look at the fine branch and see if there is anything that need to be backported from this PR? It seems backporting other PR(s) already put all needed changes to fine branch. After resolving conflicts, I ended up with just 1 file changed, which is just to add extra spaces.

+++ b/spec/support/ansible_shared/automation_manager/credential.rb
@@ -17,10 +17,10 @@ shared_examples_for "ansible credential" do
 
     let(:params) do
       {
-        :description => "Description",
-        :name        => "My Credential",
-        :related     => {},
-        :userid      => 'john'
+        :description  => "Description",
+        :name         => "My Credential",
+        :related      => {},
+        :userid       => 'john'
       }
     end

@jameswnl
Copy link
Contributor Author

jameswnl commented Apr 4, 2017

@simaishi fine branch looks fine to me :)

@simaishi
Copy link
Contributor

simaishi commented Apr 4, 2017

Thanks @jameswnl - marking it as fine/backported

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.

8 participants