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

Notification after Tower credential CUD operations #14625

Merged
merged 7 commits into from
Apr 11, 2017

Conversation

jameswnl
Copy link
Contributor

@jameswnl jameswnl commented Apr 3, 2017

For https://bugzilla.redhat.com/show_bug.cgi?id=1437628

@miq-bot add_labels wip, bug, providers/ansible_tower

refresh(manager)
record = find_by!(:manager_id => manager.id, :manager_ref => project.id)
rescue AnsibleTowerClient::ClientError => tower_error
_log.error "create_in_provider failed: #{tower_error}"
Copy link
Member

Choose a reason for hiding this comment

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

The create_in_provider in all of these message should not be necessary because _log already prefixes with the class, separator, and method.

Copy link
Member

Choose a reason for hiding this comment

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

Also, you don't need to log this since ansible_tower_client will already log its failures to this log file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got it. so we don't need logging for the RecordNotFound as well, right?

project = manager.with_provider_connection do |connection|
connection.api.projects.create!(params)
end
begin
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 you need the begin / end wrapper because you can do:

def create_in_provider(manager_id, params)
  stuff
rescue FirstError
  error stuff
rescue SecondError
  error stuff
end

record = find_by!(:manager_id => manager.id, :manager_ref => project.id)
rescue AnsibleTowerClient::ClientError => tower_error
_log.error "create_in_provider failed: #{tower_error}"
error = tower_error
Copy link
Member

Choose a reason for hiding this comment

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

I'm not a fan of this. I'd rather see:

def create_in_provider(manager_id, params)
  manager = ExtManagementSystem.find(manager_id)
  project = manager.with_provider_connection do |connection|
    connection.api.projects.create!(params)
  end
  refresh(manager)
  find_by!(:manager_id => manager.id, :manager_ref => project.id).tap do
    notify('create_in_provider', manager.id, params, true)
  end
rescue AnsibleTowerClient::ClientError => tower_error
  notify('create_in_provider', manager.id, params, false)
  raise
rescue ActiveRecord::RecordNotFound => not_found_error
  _log.error "Failed to find record: #{not_found_error.message}"
  notify('create_in_provider', manager.id, params, false)
  raise
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, me neither. See my new patch. thanks

_log.error "update_in_provider failed: #{tower_error}"
error = tower_error
rescue ActiveRecord::RecordNotFound => not_found_error
_log.error "update_in_provider couldn't locate record: #{not_found_error}"
Copy link
Member

Choose a reason for hiding this comment

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

But this is an instance method... how would you hit this?

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 was thinking the case that the Tower object got deleted and refresh destroyed this.

But I guess chances are low. I've removed it.

end
self.class.send('refresh', manager)
reload
rescue AnsibleTowerClient::ClientError => tower_error
Copy link
Member

Choose a reason for hiding this comment

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

This pattern could probably be extracted to a method.

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, the python decorator would be nice. Does ruby has something similar?

@@ -28,6 +38,17 @@ def create_in_provider_queue(manager_id, params)

private

def notify(op, manager_id, params, success)
Copy link
Member

Choose a reason for hiding this comment

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

This is identical to the method in the other file...

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 have #14453 in the making.

Do you have other suggestion as to where to move this?

expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc)
store_new_project(project, manager)
expect(EmsRefresh).to receive(:queue_refresh_task).and_return([finished_task])
expect(ExtManagementSystem).to receive(:find).with(manager.id).and_return(manager)

expect(projects).to receive(:create!).with(params)
expect(Notification).to receive(:create).with(expected_notify).and_return(double(Notification))
Copy link
Member

Choose a reason for hiding this comment

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

Do you need to return anything? I don't see it being used anywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, no need. Removed. thanks

expect { described_class.create_in_provider(manager.id, params) }.to raise_error(ActiveRecord::RecordNotFound)
end

it ".create_in_provider_queue" do
it "#create_in_provider_queue" do
Copy link
Member

Choose a reason for hiding this comment

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

This is a class method, please leave the ., not #

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got it. updated

@miq-bot
Copy link
Member

miq-bot commented Apr 5, 2017

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

@jameswnl jameswnl changed the title [WIP] Notification after Tower credential CUD operations Notification after Tower credential CUD operations Apr 5, 2017
@jameswnl
Copy link
Contributor Author

jameswnl commented Apr 5, 2017

@miq-bot remove_label wip

@jameswnl
Copy link
Contributor Author

jameswnl commented Apr 6, 2017

@miq-bot add_label fine/yes

rescue AnsibleTowerClient::ClientError => error
raise
ensure
self.class.send('notify', 'update_in_provider', manager.id, params, error.nil?)
Copy link
Member

Choose a reason for hiding this comment

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

Why send 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.

notify is a private class_method and this is inside an instance method

rescue AnsibleTowerClient::ClientError => error
raise
ensure
self.class.send('notify', 'delete_in_provider', manager.id, "manager_ref=#{manager_ref}", error.nil?)
Copy link
Member

Choose a reason for hiding this comment

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

Why send 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.

same

rescue AnsibleTowerClient::ClientError => error
raise
ensure
self.class.send('notify', 'update_in_provider', resource.id, params, error.nil?)
Copy link
Member

Choose a reason for hiding this comment

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

Why send 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.

same

rescue AnsibleTowerClient::ClientError => error
raise
ensure
self.class.send('notify', 'delete_in_provider', resource.id, "manager_ref=#{manager_ref}", error.nil?)
Copy link
Member

Choose a reason for hiding this comment

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

Why send 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.

same

expect { described_class.create_in_provider(manager.id, params) }.to raise_error(ActiveRecord::RecordNotFound)
end

it ".create_in_provider_queue" do
it "#create_in_provider_queue" do
Copy link
Member

Choose a reason for hiding this comment

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

This is a class method, it should remain as ., not #.

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

expect(described_class.create_in_provider(manager.id, params)).to be_a(described_class)
end

it "not found during refresh" do
it "#create_in_provider to fail (not found during refresh) and send notification" do
Copy link
Member

Choose a reason for hiding this comment

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

., not #

Copy link
Contributor Author

Choose a reason for hiding this comment

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

missed... done now


it "#delete_in_provider" do
it ".delete_in_provider to succeed and send notification" do
Copy link
Member

Choose a reason for hiding this comment

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

This is an instance method, so #, not .

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

ansible_cred.delete_in_provider
end

it "#delete_in_provider_queue" do
it ".delete_in_provider to fail (finding credential) and send notification" do
Copy link
Member

Choose a reason for hiding this comment

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

#, not .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no wonder.... i remember i did them, did them at wrong places 😱

@miq-bot
Copy link
Member

miq-bot commented Apr 6, 2017

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

if defined?(API_ATTRIBUTES) && params.kind_of?(Hash)
params.each do |k, _v|
if self::API_ATTRIBUTES[k] && self::API_ATTRIBUTES[k][:type] == :password
params[k] = '******'
Copy link
Member

Choose a reason for hiding this comment

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

You probably don't want to modify the original, right?

@jameswnl
Copy link
Contributor Author

jameswnl commented Apr 7, 2017

@bdunne see my new patch.

@jameswnl
Copy link
Contributor Author

@simaishi backporting of this to Fine depends on backporting #14453

queue(manager.my_zone, nil, "create_in_provider", [manager_id, params], action)
end

private

def notify(op, manager_id, params, success)
notify_params = params
Copy link
Member

Choose a reason for hiding this comment

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

I was about to comment on unnecessary assignment until I read further. Can you improve the readability here? I think the code in the block is complicated enough to justify its own method.

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've pulled that out to be its own method in its class.

Copy link
Member

@bdunne bdunne left a comment

Choose a reason for hiding this comment

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

Just a comment about readability. Otherwise looks fine

queue(manager.my_zone, nil, "create_in_provider", [manager_id, params], action)
end

private

def notify(op, manager_id, params, success)
if defined?(API_ATTRIBUTES) && params.kind_of?(Hash)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bdunne @blomquisg I've added this since last review to prevent secrets being shown in notification.

queue(manager.my_zone, nil, "create_in_provider", [manager_id, params], action)
end

private
def notify(op, manager_id, params, success)
params = hide_secrets(params) if respond_to? :hide_secrets
Copy link
Member

Choose a reason for hiding this comment

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

Normally we require parenthesis around method arguments. Unfortunately it's hard for Rubocop to catch, so they don't look for it. http://www.rubydoc.info/github/bbatsov/rubocop/Rubocop/Cop/Lint/RequireParentheses

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I've added them.

@miq-bot
Copy link
Member

miq-bot commented Apr 10, 2017

Checked commits jameswnl/manageiq@0d7c2e7~...7359776 with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0
4 files checked, 0 offenses detected
Everything looks good. 🍰

@bdunne bdunne merged commit 4f7363a into ManageIQ:master Apr 11, 2017
@bdunne bdunne added this to the Sprint 58 Ending Apr 10, 2017 milestone Apr 11, 2017
@jameswnl
Copy link
Contributor Author

@bdunne Thanks!

@jameswnl jameswnl deleted the notification branch April 11, 2017 01:30
simaishi pushed a commit that referenced this pull request Apr 11, 2017
Notification after Tower credential CUD operations
(cherry picked from commit 4f7363a)
@simaishi
Copy link
Contributor

Fine backport details:

$ git log -1
commit 3bdf50f0498153f073466d2ce68c478558c88c4f
Author: Brandon Dunne <brandondunne@hotmail.com>
Date:   Mon Apr 10 21:29:29 2017 -0400

    Merge pull request #14625 from jameswnl/notification
    
    Notification after Tower credential CUD operations
    (cherry picked from commit 4f7363a9d5fc590f5c7208e888c0fc1056545bb7)

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.

6 participants