-
Notifications
You must be signed in to change notification settings - Fork 141
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
Return 404 error if perform an action against a non existent Physical Server #202
Conversation
Hi @imtayadeway @agrare, could you review this PR? |
@miq-bot add_label gaprindashvili/yes |
while this might be ok for targeting a single resource, this will be incompatible with and break the behavior on bulk requests. Note that for bulk requests, we don't want to interrupt the request mid-flight and return the 404, callers won't know what was, wasn't done. The action result signature while is a 200 has the success attribute which can be queried. This allows us to complete bulk requests even if a couple of resources in the middle of the request do not exist. |
I agree with @abellotti. You may want to look into what has been done on delete, you may be able to do something similar to handle both scenarios if this is required. |
d04a9aa
to
50be6c4
Compare
@abellotti and @jntullo I believe that with the changes on here this problem is solved, right? |
@douglasgabriel that update has unintended consequences to other controllers as can be seen by the Travis failures. For now, I would keep the update contained to this particular method. |
7c1defb
to
6304023
Compare
Sorry about that @jntullo, I fixed this |
@douglasgabriel can you please add tests? Thanks! |
6304023
to
6566c81
Compare
def single_resource? | ||
!@req.json_body.key?('resources') | ||
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.
I think this method should probably go in lib/api/request_adapter.rb, WDYT @jntullo ?
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.
@abellotti yeah, i agree with that
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.
@abellotti @jntullo Done 👍
6566c81
to
5aaa399
Compare
LGTM. @jntullo ? |
5aaa399
to
a511ced
Compare
This pull request is not mergeable. Please rebase and repush. |
a511ced
to
3720ee5
Compare
3720ee5
to
9c4ff0d
Compare
1bb94f8
to
9d0a2c4
Compare
ping @imtayadeway @jntullo any updates needed 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.
Thanks for your effort here! This looks agreeable in general....but I think the codeclimate complaints are justified and it would be good to address that in this PR. It's also not clear why you needed to rescue error in multiple places from your single test - I think we need at least one test for every path that's described by your changes here. Thanks!
2280b22
to
20d93a8
Compare
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.
@douglasgabriel thanks for your patience! It took me a while to work through. I think I can now pinpoint what I was unsure about before: this change introduces a change of behavior for all the different actions that physical servers support, but there's only one test, for one of the actions. We are going to need tests for each action, and also ensure that we have good coverage for each action in the case that we want to preserve (bulk actions always respond with 200) as a prerequisite to getting this merged.
Indeed, the only change that's required for your existing test to pass is the following:
@@ -54,6 +54,9 @@ module Api
def change_resource_state(state, type, id)
raise BadRequestError, "Must specify an id for changing a #{type} resource" unless id
+ if single_resource? && !collection_class(type).exists?(id)
+ raise NotFoundError
+ end
api_action(type, id) do |klass|
begin
And I think that might be OK, too. You'll need to add something similar to the refresh action. In a pinch, you could extract that into a private method (though try to avoid using predicate methods as guard clauses, as you have with id_present?
. ensure_id_present
or similar would be better). I'd recommend trying to do that instead of trying to refactor refreshes to be a kind of state change. Even though there's incidental duplication there, that's OK - a refresh is not a state change per se and the implementation might change for different reasons to the other actions.
Hm, good point and good suggestion, I agree with you |
20d93a8
to
fd9edb3
Compare
It's done @imtayadeway 👍 |
end | ||
|
||
def ensure_resource_exists(type, id) | ||
raise ActiveRecord::RecordNotFound if single_resource? && !collection_class(type).exists?(id) |
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 is this method needed ? resource_search would throw the NotFoundError plus do the necessary RBAC validation.
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.
@abellotti it does, but we don't want it caught by the api_action
block, which will always respond with a 200. Performing another resource_search
would be unnecessarily expensive if we only want to check for existence.
@douglasgabriel you may want to use our own NotFoundError
here
@@ -41,7 +41,8 @@ def restart_mgmt_controller_resource(type, id, _data) | |||
end | |||
|
|||
def refresh_resource(type, id, _data = nil) | |||
raise BadRequestError, "Must specify an id for refreshing a #{type} resource" unless id |
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 think it would be OK not to extract this into a private method, since that alters the message to talk about "changing" here, instead of "refreshing". Again, I think it's an acceptable structural similarity.
@@ -41,7 +41,8 @@ def restart_mgmt_controller_resource(type, id, _data) | |||
end | |||
|
|||
def refresh_resource(type, id, _data = nil) | |||
raise BadRequestError, "Must specify an id for refreshing a #{type} resource" unless id | |||
ensure_id_present(type, id) | |||
ensure_resource_exists(type, id) |
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 think it may be more appropriate to say:
ensure_resource_exists(type, id) if single_request?
Since we're trying to express that we don't care if a resource exists at this point for a bulk operation, because it's handled elsewhere.
@@ -68,10 +70,6 @@ def change_resource_state(state, type, id) | |||
end | |||
end | |||
|
|||
def server_ident(server) |
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.
Was there a reason this was moved? I think it would be better to leave it here if it is not essential to fixing the bug
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.
Well, ok. it was just for code organization reason
fd9edb3
to
7ed92cf
Compare
…t physical server
7ed92cf
to
124743a
Compare
Checked commit douglasgabriel@124743a with ruby 2.3.3, rubocop 0.47.1, haml-lint 0.20.0, and yamllint 1.10.0 |
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.
Thanks @douglasgabriel for your work on this! LGTM
Thank you for the support 👍 |
Return 404 error if perform an action against a non existent Physical Server (cherry picked from commit d17fd18)
Gaprindashvili backport details:
|
This PR is able to: