Skip to content

Commit

Permalink
adding graph command
Browse files Browse the repository at this point in the history
  • Loading branch information
eamonnfaherty committed Aug 31, 2022
1 parent 27cbc3a commit 98ef65d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

[tool.poetry]
name = "aws-service-catalog-puppet"
version = "0.186.1"
version = "0.187.0"
description = "Making it easier to deploy ServiceCatalog products"
classifiers = ["Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "Programming Language :: Python :: 3", "License :: OSI Approved :: Apache Software License", "Operating System :: OS Independent", "Natural Language :: English"]
homepage = "https://service-catalog-tools-workshop.com/"
Expand Down
8 changes: 3 additions & 5 deletions servicecatalog_puppet/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from servicecatalog_puppet import constants
from servicecatalog_puppet import environmental_variables
from servicecatalog_puppet.commands import bootstrap as bootstrap_commands
from servicecatalog_puppet.commands import graph as graph_commands
from servicecatalog_puppet.commands import management as management_commands
from servicecatalog_puppet.commands import manifest as manifest_commands
from servicecatalog_puppet.commands import misc as misc_commands
Expand All @@ -34,11 +35,8 @@ def cli(info, info_line_numbers):

@cli.command()
@click.argument("f", type=click.File())
@click.option("--single-account", default=None)
def graph(f, single_account):
raise Exception(
"This command is not supported in this version, please try an older version"
)
def graph(f):
click.echo(graph_commands.graph(f))


@cli.command()
Expand Down
62 changes: 62 additions & 0 deletions servicecatalog_puppet/commands/graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
from servicecatalog_puppet import yaml_utils

colors = dict()

default_color = "#88000022"


def generate_node(task_reference, task):
reference = task_reference.replace("-", "_")
section_name = task.get("section_name")
account_id = task.get("account_id")
region = task.get("region")
return dict(
color=colors.get(section_name, default_color),
label=f"""<table border="0" cellborder="1" cellspacing="0" cellpadding="4">
<tr> <td colspan='2'> <b>{task_reference}</b></td> </tr>
<tr> <td align="left">section_name</td> <td>{section_name}</td> </tr>
<tr> <td align="left">account_id</td> <td>{account_id}</td> </tr>
<tr> <td align="left">region</td> <td>{region}</td> </tr>
</table>""",
)


def escape(input):
return input.replace("-", "_").replace("/", "__")


def generate_edge(task_reference, dependency_by_reference):
return f"{escape(task_reference)} -> {escape(dependency_by_reference)}"


def graph(content_file_path):
content = open(content_file_path.name, "r").read()
reference = yaml_utils.load(content)
all_tasks = reference.get("all_tasks")

nodes = dict()
edges = list()

for task_reference, task in all_tasks.items():
nodes[task_reference] = generate_node(task_reference, task)
for dependency_by_reference in task.get("dependencies_by_reference", []):
edges.append(generate_edge(task_reference, dependency_by_reference))

nodes_as_a_graph = ""
for task_reference, node in nodes.items():
reference = escape(task_reference)
nodes_as_a_graph += f"""{reference} [
color="{node.get('color')}"
label=<{node.get('label')}>
shape=plain
]
"""

for e in edges:
nodes_as_a_graph += e + "\n"

return f"""digraph workflow {{
{nodes_as_a_graph}
}}"""
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

setup_kwargs = {
'name': 'aws-service-catalog-puppet',
'version': '0.186.1',
'version': '0.187.0',
'description': 'Making it easier to deploy ServiceCatalog products',
'long_description': '# aws-service-catalog-puppet\n\n![logo](./docs/logo.png) \n\n## Badges\n\n[![codecov](https://codecov.io/gh/awslabs/aws-service-catalog-puppet/branch/master/graph/badge.svg?token=e8M7mdsmy0)](https://codecov.io/gh/awslabs/aws-service-catalog-puppet)\n\n\n## What is it?\nThis is a python3 framework that makes it easier to share multi region AWS Service Catalog portfolios and makes it \npossible to provision products into accounts declaratively using a metadata based rules engine.\n\nWith this framework you define your accounts in a YAML file. You give each account a set of tags, a default region and \na set of enabled regions.\n\nOnce you have done this you can define portfolios should be shared with each set of accounts using the tags and you \ncan specify which regions the shares occur in.\n\nIn addition to this, you can also define products that should be provisioned into accounts using the same tag based \napproach. The framework will assume role into the target account and provision the product on your behalf.\n\n\n## Getting started\n\nYou can read the [installation how to](https://service-catalog-tools-workshop.com/30-how-tos/10-installation/30-service-catalog-puppet.html)\nor you can read through the [every day use](https://service-catalog-tools-workshop.com/30-how-tos/50-every-day-use.html)\nguides.\n\nYou can read the [documentation](https://aws-service-catalog-puppet.readthedocs.io/en/latest/) to understand the inner \nworkings. \n\n\n## Going further\n\nThe framework is one of a pair. The other is [aws-service-catalog-factory](https://github.com/awslabs/aws-service-catalog-factory).\nWith Service Catalog Factory you can create pipelines that deploy multi region portfolios very easily. \n\n## License\n\nThis library is licensed under the Apache 2.0 License. \n \n',
'author': 'Eamonn Faherty',
Expand Down

0 comments on commit 98ef65d

Please sign in to comment.