From 8b7a667bfb3042233c2e3b72041a479a17fe35e2 Mon Sep 17 00:00:00 2001 From: Daniel Bernstein Date: Fri, 20 Oct 2023 12:08:04 -0700 Subject: [PATCH] Add output-json parameter to publish command for use by downstream processes. --- core/cli.py | 7 +++++++ .../publish_dashboard_from_template.py | 17 +++++++++++++++-- tests/core/operation/test_publish_operation.py | 12 ++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/core/cli.py b/core/cli.py index 114c081..5e261dd 100644 --- a/core/cli.py +++ b/core/cli.py @@ -121,11 +121,17 @@ def import_template( help="The namespace you wish to target (e.g. tpp-prod, tpp-dev, tpp-staging).", ) @click.option("--group-name", required=True, help="Name of the Quicksight User Group") +@click.option( + "--output-json", + required=True, + help="The file path to which operation output should be written as json", +) def publish_dashboard( aws_account_id: str, template_id: str, target_namespace: str, group_name: str, + output_json: str, ): """ Create/Update a dashboard from a template @@ -141,6 +147,7 @@ def publish_dashboard( template_id=template_id, target_namespace=target_namespace, group_name=group_name, + output_json=output_json, ).execute() log.info(result) diff --git a/core/operation/publish_dashboard_from_template.py b/core/operation/publish_dashboard_from_template.py index 4b62f51..4e8f7da 100644 --- a/core/operation/publish_dashboard_from_template.py +++ b/core/operation/publish_dashboard_from_template.py @@ -10,11 +10,18 @@ class PublishDashboardFromTemplateOperation(BaseOperation): """ def __init__( - self, template_id: str, target_namespace: str, group_name: str, *args, **kwargs + self, + template_id: str, + target_namespace: str, + group_name: str, + output_json: str, + *args, + **kwargs, ): self._template_id = template_id self._target_namespace = target_namespace self._group_name = group_name + self._output_json = output_json super().__init__(*args, **kwargs) def execute(self) -> dict: @@ -110,12 +117,18 @@ def execute(self) -> dict: f"Unexpected response from trying to update_dashboard_permissions : {json.dumps(response, indent=4)} " ) - return { + result = { "status": "success", "dashboard_arn": dashboard_arn, "dashboard_id": dashboard_id, } + with open(self._output_json, "w") as output: + output.write(json.dumps(result)) + self._log.info(f"Output written to {self._output_json}") + + return result + def _create_or_update_dashboard(self, dashboard_params: dict) -> tuple[str, str]: """ Creates new or updates existing template. diff --git a/tests/core/operation/test_publish_operation.py b/tests/core/operation/test_publish_operation.py index 316ce8f..5252262 100644 --- a/tests/core/operation/test_publish_operation.py +++ b/tests/core/operation/test_publish_operation.py @@ -1,3 +1,7 @@ +import json +import os.path +import tempfile + import botocore from botocore.config import Config from botocore.stub import Stubber @@ -14,6 +18,8 @@ def test(self): account = "012345678910" group_name = "my_group" + output_json = tempfile.NamedTemporaryFile() + boto_config = Config( region_name="us-east-1", ) @@ -188,9 +194,15 @@ def test(self): target_namespace=target_namespace, aws_account_id=account, group_name=group_name, + output_json=output_json.name, ) result = op.execute() assert result["status"] == "success" assert result["dashboard_id"] == template_id + assert os.path.exists(output_json.name) + + with open(output_json.name) as file: + result_from_file = json.loads(file.read()) + assert result_from_file["dashboard_id"] == result["dashboard_id"]