From 0a1ab72fc06916439c7767878aa0caa11b114437 Mon Sep 17 00:00:00 2001 From: David Meu Date: Tue, 18 Jan 2022 10:15:16 +0200 Subject: [PATCH] Feature adding params controller to get by ref --- CHANGELOG.rst | 8 +++++- st2api/st2api/controllers/v1/action_views.py | 28 +++++++++++++++---- .../unit/controllers/v1/test_action_views.py | 14 +++++++++- st2common/st2common/openapi.yaml | 6 ++-- st2common/st2common/openapi.yaml.j2 | 6 ++-- 5 files changed, 48 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 74f9616831..80bc94671e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,6 +15,12 @@ Fixed Added ~~~~~ +* Added st2 API get action parameters by ref. #5509 + + API endpoint ``/api/v1/actions/views/parameters/{action_id}`` accepts ``ref_or_id``. + + Contributed by @DavidMeu + * Enable setting ttl for MockDatastoreService. #5468 Contributed by @ytjohn @@ -4230,4 +4236,4 @@ v0.5.1 - November 3rd, 2014 Added ~~~~~ -* Initial public release +* Initial public release \ No newline at end of file diff --git a/st2api/st2api/controllers/v1/action_views.py b/st2api/st2api/controllers/v1/action_views.py index 2e528b5b13..ca6afaae4c 100644 --- a/st2api/st2api/controllers/v1/action_views.py +++ b/st2api/st2api/controllers/v1/action_views.py @@ -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) + 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 diff --git a/st2api/tests/unit/controllers/v1/test_action_views.py b/st2api/tests/unit/controllers/v1/test_action_views.py index a28219c04d..bbc76e3760 100644 --- a/st2api/tests/unit/controllers/v1/test_action_views.py +++ b/st2api/tests/unit/controllers/v1/test_action_views.py @@ -199,7 +199,7 @@ class ActionViewsParametersControllerTestCase(FunctionalTest): @mock.patch.object( action_validator, "validate_action", mock.MagicMock(return_value=True) ) - def test_get_one(self): + def test_get_one_by_id(self): post_resp = self.app.post_json("/v1/actions", ACTION_1) action_id = post_resp.json["id"] try: @@ -208,6 +208,18 @@ def test_get_one(self): finally: self.app.delete("/v1/actions/%s" % action_id) + @mock.patch.object( + action_validator, "validate_action", mock.MagicMock(return_value=True) + ) + def test_get_one_by_ref(self): + post_resp = self.app.post_json("/v1/actions", ACTION_1) + action_ref = post_resp.json["ref"] + try: + get_resp = self.app.get("/v1/actions/views/parameters/%s" % action_ref) + self.assertEqual(get_resp.status_int, 200) + finally: + self.app.delete("/v1/actions/%s" % action_ref) + class ActionEntryPointViewControllerTestCase(FunctionalTest): @mock.patch.object( diff --git a/st2common/st2common/openapi.yaml b/st2common/st2common/openapi.yaml index edc81eb1ff..107b97a1c2 100644 --- a/st2common/st2common/openapi.yaml +++ b/st2common/st2common/openapi.yaml @@ -465,15 +465,15 @@ paths: description: Unexpected error schema: $ref: '#/definitions/Error' - /api/v1/actions/views/parameters/{action_id}: + /api/v1/actions/views/parameters/{ref_or_id}: get: operationId: st2api.controllers.v1.action_views:parameters_view_controller.get_one description: | Get parameters for an action. parameters: - - name: action_id + - name: ref_or_id in: path - description: Entity id + description: Entity reference or id type: string required: true x-parameters: diff --git a/st2common/st2common/openapi.yaml.j2 b/st2common/st2common/openapi.yaml.j2 index 1397c0d201..a54bb423f1 100644 --- a/st2common/st2common/openapi.yaml.j2 +++ b/st2common/st2common/openapi.yaml.j2 @@ -461,15 +461,15 @@ paths: description: Unexpected error schema: $ref: '#/definitions/Error' - /api/v1/actions/views/parameters/{action_id}: + /api/v1/actions/views/parameters/{ref_or_id}: get: operationId: st2api.controllers.v1.action_views:parameters_view_controller.get_one description: | Get parameters for an action. parameters: - - name: action_id + - name: ref_or_id in: path - description: Entity id + description: Entity reference or id type: string required: true x-parameters: