8000 Adding get_by_ref for ParametersViewController by DavidMeu · Pull Request #5509 · StackStorm/st2 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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 1 commit into from
Jan 18, 2022

Conversation

DavidMeu
Copy link

Adding ability to get action parameters by reference.

@pull-request-size pull-request-size bot added the size/S PR that changes 10-29 lines. Very easy to review. label Dec 19, 2021
@CLAassistant
Copy link
CLAassistant commented Dec 19, 2021

CLA assistant check
All committers have signed the CLA.

@pull-request-size pull-request-size bot added size/M PR that changes 30-99 lines. Good size to review. and removed size/S PR that changes 10-29 lines. Very easy to review. labels Dec 19, 2021
@amanda11 amanda11 added this to the 3.7.0 milestone Dec 19, 2021
@DavidMeu DavidMeu marked this pull request as draft December 19, 2021 15:36
@DavidMeu DavidMeu marked this pull request as ready for review December 19, 2021 15:42
@DavidMeu DavidMeu marked this pull request as draft December 19, 2021 15:43
@DavidMeu DavidMeu marked this pull request as ready for review December 20, 2021 10:29
@DavidMeu DavidMeu force-pushed the adding_get_by_ref branch 4 times, most recently from 6a1f956 to 9a2efa9 Compare December 21, 2021 20:08
Copy link
Contributor
@amanda11 amanda11 left a comment

Choose a reason for hiding this comment

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

Thanks for the contirbution.

It looks good to me, just a few minor comments but please also add an entry to CHANGELOG.rst.

I wouldn't mind another TSC member to check as well, as I'm not sure if there are other consequences for changing the outer method to lookup on id or ref.

Also might be worth a mention of the use-case in the Conversation for the need for the get_one to also get by reference.

def _get_action_by_ref(ref):
try:
action_db = Action.get_by_ref(ref)
if not action_db:
Copy link
Contributor

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.

Copy link
Author
@DavidMeu DavidMeu Dec 23, 2021

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:

{
    "faultstring": "Database lookup for id=\"61b9fa409a66b66d529d8345\" resulted in exception. Unable to find the ActionDB instance. {'id': '61b9fa409a66b66d529d8345'}"
}

@amanda11 Seems you are right, I assume it should behave the same for Action.get_by_ref:

msg = "Unable to find the %s instance. %s" % (self.model.__name__, kwargs)

Copy link
Author
@DavidMeu DavidMeu Dec 28, 2021

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:

{
    "faultstring": "Internal Server Error"
}

Full trace from st2api:

2021-12-28 09:36:27,898 ERROR [-] Failed to call controller function "get_one" for operation "st2api.controllers.v1.action_view
s:parameters_view_controller.get_one": 'NoneType' object has no attribute 'get_uid'
Traceback (most recent call last):
  File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2common/router.py", line 516, in __call__
    resp = func(**kw)
  File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2api/controllers/v1/action_views.py", line 84, in get_one
    return self._get_one(action_id, requester_user=requester_user)
  File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2api/controllers/v1/action_views.py", line 104, in _get_one
    permission_type=permission_type,
  File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2rbac_backend/utils.py", line 123, in assert_user_has_resource_db_per
mission
    permission_type=permission_type)
  File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2rbac_backend/utils.py", line 282, in user_has_resource_db_permission

    permission_type=permission_type)
  File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2rbac_backend/resolvers.py", line 415, in user_has_resource_db_permis
sion
    action_uid = resource_db.get_uid()
AttributeError: 'NoneType' object has no attribute 'get_uid'
2021-12-28 09:36:27,898 ERROR [-] API call failed: 'NoneType' object has no attribute 'get_uid'
Traceback (most recent call last):
  File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2common/middleware/error_handling.py", line 49, in __call__
    return self.app(environ, start_response)
  File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2common/middleware/streaming.py", line 48, in __call__
    return self.app(environ, start_response)
  File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2common/router.py", line 599, in as_wsgi
    resp = self(req)
  File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2common/router.py", line 524, in __call__
    raise e
  File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2common/router.py", line 516, in __call__
    resp = func(**kw)
  File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2api/controllers/v1/action_views.py", line 84, in get_one
    return self._get_one(action_id, requester_user=requester_user)
  File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2api/controllers/v1/action_views.py", line 104, in _get_one
    permission_type=permission_type,
  File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2rbac_backend/utils.py", line 123, in assert_user_has_resource_db_per
mission
    permission_type=permission_type)
  File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2rbac_backend/utils.py", line 282, in user_has_resource_db_permission

    permission_type=permission_type)
  File "/opt/stackstorm/st2/lib/python3.6/site-packages/st2rbac_backend/resolvers.py", line 415, in user_has_resource_db_permis
sion
    action_uid = resource_db.get_uid()
AttributeError: 'NoneType' object has no attribute 'get_uid' (_exception_class='AttributeError',_exception_message="'NoneType'
object has no attribute 'get_uid'",_exception_data={})

Seems it is not raising the exception when the action reference is not found

@@ -81,7 +94,10 @@ def _get_one(action_id, requester_user):
Handle:
GET /actions/views/parameters/1
"""
action_db = LookupUtils._get_action_by_id(action_id)
if ResourceReference.is_resource_reference(action_id):
Copy link
Contributor

Choose a reason for hiding this comment

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

This method has changed so that it isn't looking up by action_id, but rather now action_id or action_ref. So we should change the pydoc to make that clear.

Copy link
Author

Choose a reason for hiding this comment

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

Added

or ref

@DavidMeu DavidMeu force-pushed the adding_get_by_ref branch 2 times, most recently from 94560f4 to 02fc6cb Compare December 26, 2021 07:42
@DavidMeu DavidMeu requested a review from amanda11 December 26, 2021 07:43
@DavidMeu
Copy link
Author

@amanda11

please also add an entry to CHANGELOG.rst.

Done:
56b41f9#diff-2c623f3c6a917be56c59d43279244996836262cb1e12d9d0786c9c49eef6b43cR10

Copy link
Contributor
@amanda11 amanda11 left a comment

Choose a reason for hiding this comment

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

A few minor comments on the review, and I wouldn't mind @armab to take a quick look in case I'm missing any knock-on affects I might not have thought of.

CHANGELOG.rst Outdated
Added
~~~~~

* Added st2 API get action parameters by ref.
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add reference to the pull request, e.g. #5509 (see the other formats in the file). That way its easy to track PRs from changelog.
Also each new entry doesn't need a header "added", just add to the existing "Added" section already in place for the current "in development" section.

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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member
@arm4b arm4b left a comment

Choose a reason for hiding this comment

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

LGTM overall 👍

To make sure the https://api.stackstorm.com/api/v1/actions/#/parameters_view_controller.get_one will be updated, we'll also need to include in this PR the new Openapi spec with adjusted endpoint

# NOTE: This file is auto-generated - DO NOT EDIT MANUALLY
# Edit st2common/st2common/openapi.yaml.j2 and then run
# make .generate-api-spec
# to generate the final spec file
swagger: '2.0'

@DavidMeu
Copy link
Author
DavidMeu commented Jan 8, 2022

Also might be worth a mention of the use-case in the Conversation for the need for the get_one to also get by reference.

I have a UI where I'm using the GET action parameters by action ref and then creating an execution (with the POST execution) so I need to use the action ref from the previous call (GET action parameters).

@DavidMeu
Copy link
Author
DavidMeu commented Jan 10, 2022

LGTM overall 👍

To make sure the https://api.stackstorm.com/api/v1/actions/#/parameters_view_controller.get_one will be updated, we'll also need to include in this PR the new Openapi spec with adjusted endpoint

# NOTE: This file is auto-generated - DO NOT EDIT MANUALLY
# Edit st2common/st2common/openapi.yaml.j2 and then run
# make .generate-api-spec
# to generate the final spec file
swagger: '2.0'

I have adjusted:
st2common/st2common/openapi.yaml.j2

But can't seem to succeed in regenarating it:

$ make .generate-api-spec
/usr/bin/sh: -c: line 0: syntax error near unexpected token `('
/usr/bin/sh: -c: line 0: `dirname C:/Users/DavidMeu(IL)/OneDrive/Desktop/st2_official/Makefile'

================== Lint API spec ====================

. virtualenv/bin/activate; python st2common/bin/st2-validate-api-spec --config-file conf/st2.dev.conf
/usr/bin/sh: virtualenv/bin/activate: No such file or directory
make: *** [Makefile:448: .lint-api-spec] Error 1

And ofcourse the CI is failing for that:
https://github.com/StackStorm/st2/runs/4758265346?check_suite_focus=true#step:11:62

@amanda11
Copy link
Contributor

https://github.com/StackStorm/st2/runs/4758265346?check_suite_focus=true#step:11:62

@DavidMeu If you un-do your changes on your local system, and you run the regenerate command does it work? Just wondering if the older version can't be re-generated either....

@amanda11
Copy link
Contributor

https://github.com/StackStorm/st2/runs/4758265346?check_suite_focus=true#step:11:62

@DavidMeu If you un-do your changes on your local system, and you run the regenerate command does it work? Just wondering if the older version can't be re-generated either....

Actually the problem is that the Makefile isn't coping because you have a ( in your path name.

The error is about the "(" in the pathname.
So I'd either run the re-generate on your system in a path that doesn't have ( in, and might even need to be a Linux system.

Copy link
Contributor
@amanda11 amanda11 left a comment

Choose a reason for hiding this comment

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

LGTM - thanks for the contribution, but looks like UTs are now failing.

@DavidMeu DavidMeu force-pushed the adding_get_by_ref branch 3 times, most recently from 8b846ac to 9f89e78 Compare January 16, 2022 08:28
@DavidMeu
Copy link
Author

@amanda11 Thank you for assisting.
@armab
From my POV this PR is ready.

Copy link
Member
@arm4b arm4b left a comment

Choose a reason for hiding this comment

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

Thanks! 👍

@arm4b arm4b merged commit ba00eb2 into StackStorm:master Jan 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API enhancement size/M PR that changes 30-99 lines. Good size to review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0