diff --git a/core/cli.py b/core/cli.py index 8b6537b..e2228f7 100644 --- a/core/cli.py +++ b/core/cli.py @@ -144,8 +144,8 @@ def publish_dashboard( target_namespace: str, group_name: str, output_json: str, - result_bucket:str, - result_key:str, + result_bucket: str, + result_key: str, ): """ Create/Update a dashboard from a template @@ -170,3 +170,87 @@ def publish_dashboard( cli.add_command(publish_dashboard) + + +@click.command +@click.option("--aws-account-id", required=True, help="The ID of the AWS account") +@click.option( + "--template-name", required=True, help="The name of the template to be restored" +) +@click.option( + "--data-source-arn", + required=True, + help="The ARN of the data source you want to associate with the data sets", +) +@click.option( + "--target-namespace", + required=True, + help="The namespace you wish to target (e.g. tpp-prod, tpp-dev, tpp-staging).", +) +@click.option( + "--input-dir", + required=True, + help="The path to the input directory from which resources will be imported", +) +@click.option("--group-name", required=True, help="Name of the Quicksight User Group") +@click.option( + "--output-json", + required=False, + help="The file path to which operation output should be written as json",) +@click.option( + "--result-bucket", + required=False, + help="An S3 bucket to save the results to. If specified, you must also specify a result-key" +) +@click.option( + "--result-key", + required=False, + help="An S3 object key to save the results to. If used, result-bucket must be specified." +) +def import_and_publish( + aws_account_id: str, + template_name: str, + data_source_arn: str, + target_namespace: str, + input_dir: str, + group_name: str, + result_bucket: str, + result_key: str, +): + + log.info(f"import_and_publish") + log.info(f"aws_account_id = {aws_account_id}") + log.info(f"template_name = {template_name}") + log.info(f"data_source_arn = {data_source_arn}") + log.info(f"input_dir= {input_dir}") + log.info(f"group_name = {group_name}") + log.info(f"result_bucket = {result_bucket}") + log.info(f"result_key = {result_key}") + + log.info(f"Importing {template_name}") + result = ImportFromJsonOperation( + qs_client=create_quicksight_client(), + aws_account_id=aws_account_id, + template_name=template_name, + target_namespace=target_namespace, + data_source_arn=data_source_arn, + input_dir=input_dir, + ).execute() + log.info(f"Import result: {result}") + template_id: str = result["template"]["id"] + log.info(f"Publishing template {template_id} as dashboard using datasource {data_source_arn}") + + result = PublishDashboardFromTemplateOperation( + qs_client=create_quicksight_client(), + s3_client=create_s3_client(), + aws_account_id=aws_account_id, + template_id=template_id, + target_namespace=target_namespace, + group_name=group_name, + result_bucket=result_bucket, + result_key=result_key, + ).execute() + log.info(f"publish result = {result}") + + +cli.add_command(import_and_publish) diff --git a/core/operation/publish_dashboard_from_template.py b/core/operation/publish_dashboard_from_template.py index 531a106..a9345c6 100644 --- a/core/operation/publish_dashboard_from_template.py +++ b/core/operation/publish_dashboard_from_template.py @@ -123,21 +123,21 @@ def execute(self) -> dict: result = { "status": "success", - "dashboard_arn": dashboard_arn, - "dashboard_id": dashboard_id, + "dashboard_info": { + self._template_id: [dashboard_arn] + } } - json_string = json.dumps(result) if self._output_json: with open(self._output_json, "w") as output: - output.write(json_string) + output.write(json.dumps(result)) self._log.info(f"Output written to {self._output_json}") if self._result_bucket and self._result_key: self._s3_client.put_object( Bucket=self._result_bucket, Key=self._result_key, - Body=json_string) + Body=json.dumps(result["dashboard_info"])) return result diff --git a/tests/core/operation/test_publish_operation.py b/tests/core/operation/test_publish_operation.py index 5252262..ca3d8df 100644 --- a/tests/core/operation/test_publish_operation.py +++ b/tests/core/operation/test_publish_operation.py @@ -205,4 +205,4 @@ def test(self): with open(output_json.name) as file: result_from_file = json.loads(file.read()) - assert result_from_file["dashboard_id"] == result["dashboard_id"] + assert result_from_file["dashboard_info"] == result["dashboard_id"]