Skip to content

Commit

Permalink
Add combined import-and-publish operation to simplify integration wit…
Browse files Browse the repository at this point in the history
…h playbook.
  • Loading branch information
dbernstein committed Oct 30, 2023
1 parent 814714e commit 8990a4f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
88 changes: 86 additions & 2 deletions core/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
10 changes: 5 additions & 5 deletions core/operation/publish_dashboard_from_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/core/operation/test_publish_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

0 comments on commit 8990a4f

Please sign in to comment.