-
Notifications
You must be signed in to change notification settings - Fork 898
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
Conversation
5d03a30
to
84b0f71
Compare
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}" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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}" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 #
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it. updated
This pull request is not mergeable. Please rebase and repush. |
@miq-bot remove_label wip |
@miq-bot add_label fine/yes |
rescue AnsibleTowerClient::ClientError => error | ||
raise | ||
ensure | ||
self.class.send('notify', 'update_in_provider', manager.id, params, error.nil?) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why send
here?
There was a problem hiding this comment.
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?) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why send
here?
There was a problem hiding this comment.
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?) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why send
here?
There was a problem hiding this comment.
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?) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why send
here?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 #
.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
, not #
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 .
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#
, not .
There was a problem hiding this comment.
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 😱
This pull request is not mergeable. Please rebase and repush. |
baa2aec
to
00ca670
Compare
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] = '******' |
There was a problem hiding this comment.
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?
@bdunne see my new patch. |
queue(manager.my_zone, nil, "create_in_provider", [manager_id, params], action) | ||
end | ||
|
||
private | ||
|
||
def notify(op, manager_id, params, success) | ||
notify_params = params |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
can because of ManageIQ#14685
Checked commits jameswnl/manageiq@0d7c2e7~...7359776 with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0 |
@bdunne Thanks! |
Notification after Tower credential CUD operations (cherry picked from commit 4f7363a)
Fine backport details:
|
For https://bugzilla.redhat.com/show_bug.cgi?id=1437628
@miq-bot add_labels wip, bug, providers/ansible_tower