-
-
Notifications
You must be signed in to change notification settings - Fork 777
Adding get_by_ref for ParametersViewController #5509
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ | |
| from st2common.content import utils | ||
| from st2common.models.api.action import ActionAPI | ||
| from st2common.models.utils import action_param_utils | ||
| from st2common.models.system.common import ResourceReference | ||
| from st2common.persistence.action import Action | ||
| from st2common.persistence.runner import RunnerType | ||
| from st2common.rbac.types import PermissionType | ||
|
|
@@ -50,6 +51,18 @@ def _get_action_by_id(id): | |
| LOG.exception(msg) | ||
| abort(http_client.NOT_FOUND, msg) | ||
|
|
||
| @staticmethod | ||
| def _get_action_by_ref(ref): | ||
| try: | ||
| action_db = Action.get_by_ref(ref) | ||
| if not action_db: | ||
| raise ValueError('Referenced action "%s" doesnt exist' % (ref)) | ||
| return action_db | ||
| except Exception as e: | ||
| msg = 'Database lookup for ref="%s" resulted in exception. %s' % (ref, e) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems a little odd to raise an exception in this method only to re-catch it 2 lines down. But equally you need to handle generic and the None being returned, so not sure I could think of a cleaner way. |
||
| LOG.exception(msg) | ||
| abort(http_client.NOT_FOUND, msg) | ||
|
|
||
| @staticmethod | ||
| def _get_runner_by_id(id): | ||
| try: | ||
|
|
@@ -70,18 +83,21 @@ def _get_runner_by_name(name): | |
|
|
||
|
|
||
| class ParametersViewController(object): | ||
| def get_one(self, action_id, requester_user): | ||
| return self._get_one(action_id, requester_user=requester_user) | ||
| def get_one(self, ref_or_id, requester_user): | ||
| return self._get_one(ref_or_id, requester_user=requester_user) | ||
|
|
||
| @staticmethod | ||
| def _get_one(action_id, requester_user): | ||
| def _get_one(ref_or_id, requester_user): | ||
| """ | ||
| List merged action & runner parameters by action id. | ||
| List merged action & runner parameters by action id or ref. | ||
|
|
||
| Handle: | ||
| GET /actions/views/parameters/1 | ||
| """ | ||
| action_db = LookupUtils._get_action_by_id(action_id) | ||
| if ResourceReference.is_resource_reference(ref_or_id): | ||
| action_db = LookupUtils._get_action_by_ref(ref_or_id) | ||
| else: | ||
| action_db = LookupUtils._get_action_by_id(ref_or_id) | ||
|
|
||
| permission_type = PermissionType.ACTION_VIEW | ||
| rbac_utils = get_rbac_backend().get_utils_class() | ||
|
|
@@ -193,7 +209,7 @@ def get_all( | |
| def _transform_action_api(action_api, requester_user): | ||
| action_id = action_api.id | ||
| result = ParametersViewController._get_one( | ||
| action_id=action_id, requester_user=requester_user | ||
| ref_or_id=action_id, requester_user=requester_user | ||
| ) | ||
| action_api.parameters = result.get("parameters", {}) | ||
| return action_api | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Looking at the other methods, then we don't usually check the value of the return value from get_by_id etc, just return it. So for consistency I would do the same and return None rather than throw an exception to be consistent with others.
Uh oh!
There was an error while loading. Please reload this page.
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.
Thing is I wanted to throw a meaningful exception in case it was not found in DB same there is for
get_by_id.When I perform a lookup for non existing id I get:
@amanda11 Seems you are right, I assume it should behave the same for
Action.get_by_ref:st2/st2common/st2common/models/db/__init__.py
Line 521 in 76feb92
Uh oh!
There was an error while loading. Please reload this page.
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.
@amanda11 did a re-check and seems I need to handle that exception as when I perform a lookup for a non exesting action ref I get:
Full trace from st2api:
Seems it is not raising the exception when the action reference is not found