diff --git a/stacklet/client/platform/commands/__init__.py b/stacklet/client/platform/commands/__init__.py index 83f24d5..0015a43 100644 --- a/stacklet/client/platform/commands/__init__.py +++ b/stacklet/client/platform/commands/__init__.py @@ -7,6 +7,8 @@ from .policycollection import policy_collection from .repository import repository from .user import user +from .templates import templates +from .report_groups import report_groups commands = [ account, @@ -16,6 +18,8 @@ graphql, policy, policy_collection, + report_groups, repository, + templates, user, ] diff --git a/stacklet/client/platform/commands/report_groups.py b/stacklet/client/platform/commands/report_groups.py new file mode 100644 index 0000000..94ff26d --- /dev/null +++ b/stacklet/client/platform/commands/report_groups.py @@ -0,0 +1,292 @@ +import json +import click + +from stacklet.client.platform.executor import _run_graphql +from stacklet.client.platform.executor import StackletGraphqlExecutor, snippet_options +from stacklet.client.platform.graphql import StackletGraphqlSnippet +from stacklet.client.platform.utils import click_group_entry, default_options + + +@StackletGraphqlExecutor.registry.register("create-report-group") +class CreateReportGroup(StackletGraphqlSnippet): + name = "create-report-group" + pagination = False + parameter_types = {"input": "ReportGroupInput"} + snippet = """ +mutation { + addReportGroup(input: $input) { + reportGroup{ + id + name + schedule + groupBy + useMessageSettings + source + bindings + deliverySettings { + ... on EmailSettings { + type + template + recipients { + value + tag + account_owner + resource_owner + } + subject + fromEmail + cc + priority + format + } + ... on SlackSettings { + type + template + recipients { + value + tag + account_owner + resource_owner + } + } + ... on JiraSettings { + type + template + recipients { + value + tag + account_owner + resource_owner + } + summary + project + description + } + } + } + } +} +""" + + +@StackletGraphqlExecutor.registry.register("get-report-group") +class QueryReportGroup(StackletGraphqlSnippet): + name = "get-report-group" + pagination = False + snippet = """ +query { + reportGroup(name: $name) { + id + name + schedule + groupBy + useMessageSettings + source + bindings + deliverySettings { + ... on EmailSettings { + type + template + recipients { + value + tag + account_owner + resource_owner + } + subject + fromEmail + cc + priority + format + } + ... on SlackSettings { + type + template + recipients { + value + tag + account_owner + resource_owner + } + } + ... on JiraSettings { + type + template + recipients { + value + tag + account_owner + resource_owner + } + summary + project + description + } + } + } +} +""" + required = {"name": ""} + + +@StackletGraphqlExecutor.registry.register("list-report-groups") +class QueryReportGroups(StackletGraphqlSnippet): + name = "list-report-groups" + pagination = True + snippet = """ +query { + reportGroups( + binding: $binding + first: $first + last: $last + after: $after + before: $before + ) { + edges { + node { + id + name + schedule + groupBy + useMessageSettings + source + bindings + deliverySettings { + ... on EmailSettings { + type + template + recipients { + value + tag + account_owner + resource_owner + } + subject + fromEmail + cc + priority + format + } + ... on SlackSettings { + type + template + recipients { + value + tag + account_owner + resource_owner + } + } + ... on JiraSettings { + type + template + recipients { + value + tag + account_owner + resource_owner + } + summary + project + description + } + } + } + } + pageInfo { + hasNextPage + hasPreviousPage + startCursor + endCursor + } + } +} +""" + optional = {"binding": ""} + + +@StackletGraphqlExecutor.registry.register("remove-report-group") +class RemoveReportGroup(StackletGraphqlSnippet): + name = "remove-report-group" + pagination = False + snippet = """ +mutation { + removeReportGroup(name: "$name") +} +""" + required = {"name": ""} + + +@click.group(short_help="Run report groups queries/mutations") +@default_options() +@click.pass_context +def report_groups(*args, **kwargs): + """ + Query against and Run mutations against Report Group objects in Stacklet + + Define a custom config file with the --config option + + Specify a different output format with the --output option + + Example: + + $ stacklet repository --output json list + + """ + click_group_entry(*args, **kwargs) + + +@report_groups.command() +@click.pass_context +@click.option("--file", help="Json file with Report Group.") +def create(ctx, file): + """ + Create a new or update an existing report-group + """ + + with open(file, "rt") as f: + data = json.load(f) + + data["emailSettings"] = [ + d for d in data.get("deliverySettings", []) if d["type"] == "email" + ] + data["slackSettings"] = [ + d for d in data.get("deliverySettings", []) if d["type"] == "slack" + ] + data["jiraSettings"] = [ + d for d in data.get("deliverySettings", []) if d["type"] == "jira" + ] + data.pop("deliverySettings", None) + click.echo( + _run_graphql(ctx=ctx, name="create-report-group", variables={"input": data}) + ) + + +@report_groups.command() +@snippet_options("list-report-groups") +@click.pass_context +def list(ctx, **kwargs): + """ + Query all report-groups + """ + click.echo(_run_graphql(ctx=ctx, name="list-report-groups", variables=kwargs)) + + +@report_groups.command() +@snippet_options("get-report-group") +@click.pass_context +def get(ctx, **kwargs): + """ + Query a specific report-group + """ + click.echo(_run_graphql(ctx=ctx, name="get-report-group", variables=kwargs)) + + +@report_groups.command() +@snippet_options("remove-report-group") +@click.pass_context +def remove(ctx, **kwargs): + """ + Remove report-group + """ + click.echo(_run_graphql(ctx=ctx, name="remove-report-group", variables=kwargs)) diff --git a/stacklet/client/platform/commands/templates.py b/stacklet/client/platform/commands/templates.py new file mode 100644 index 0000000..55292ca --- /dev/null +++ b/stacklet/client/platform/commands/templates.py @@ -0,0 +1,136 @@ +import click + +from stacklet.client.platform.executor import _run_graphql +from stacklet.client.platform.executor import StackletGraphqlExecutor, snippet_options +from stacklet.client.platform.graphql import StackletGraphqlSnippet +from stacklet.client.platform.utils import click_group_entry, default_options + + +@StackletGraphqlExecutor.registry.register("create-template") +class CreateTemplate(StackletGraphqlSnippet): + name = "create-template" + pagination = False + snippet = """ +mutation { + addTemplate(input: { name: $name, content: $content }) { + template { + id + name + content + } + } +} +""" + required = {"name": "", "content": ""} + + +@StackletGraphqlExecutor.registry.register("get-template") +class QueryTemplate(StackletGraphqlSnippet): + name = "get-template" + pagination = False + snippet = """ +query { + template(name: $name) { + id + name + content + } +} +""" + required = {"name": ""} + + +@StackletGraphqlExecutor.registry.register("list-templates") +class QueryTemplates(StackletGraphqlSnippet): + name = "list-templates" + pagination = True + snippet = """ +query { + templates(first: $first, last: $last, before: "$before", after: "$after") { + edges { + node { + id + name + } + } + pageInfo { + hasNextPage + hasPreviousPage + startCursor + endCursor + total + } + } +} +""" + + +@StackletGraphqlExecutor.registry.register("remove-template") +class RemoveTemplate(StackletGraphqlSnippet): + name = "remove-template" + pagination = False + snippet = """ +mutation { + removeTemplate(id: $id) +} +""" + required = {"id": ""} + + +@click.group(short_help="Run templates queries/mutations") +@default_options() +@click.pass_context +def templates(*args, **kwargs): + """ + Query against and Run mutations against Template objects in Stacklet + + Define a custom config file with the --config option + + Specify a different output format with the --output option + + Example: + + $ stacklet repository --output json list + + """ + click_group_entry(*args, **kwargs) + + +@templates.command() +@snippet_options("create-template") +@click.pass_context +def create(ctx, **kwargs): + """ + Create a new or update an existing template + """ + click.echo(_run_graphql(ctx=ctx, name="create-template", variables=kwargs)) + + +@templates.command() +@snippet_options("list-templates") +@click.pass_context +def list(ctx, **kwargs): + """ + Query all templates + """ + click.echo(_run_graphql(ctx=ctx, name="list-templates", variables=kwargs)) + + +@templates.command() +@snippet_options("get-template") +@click.pass_context +def get(ctx, **kwargs): + """ + Query a specific template + """ + click.echo(_run_graphql(ctx=ctx, name="get-template", variables=kwargs)) + + +@templates.command() +@snippet_options("remove-template") +@click.pass_context +def remove(ctx, **kwargs): + """ + Remove template + """ + click.echo(_run_graphql(ctx=ctx, name="remove-template", variables=kwargs))