From 78df3d498252b03fde1af5b8d21d225b25f4215b Mon Sep 17 00:00:00 2001 From: Kirill Logachev Date: Fri, 21 Oct 2022 17:09:49 -0700 Subject: [PATCH 1/3] templates api --- .../client/platform/commands/templates.py | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 stacklet/client/platform/commands/templates.py diff --git a/stacklet/client/platform/commands/templates.py b/stacklet/client/platform/commands/templates.py new file mode 100644 index 0000000..a47b9c9 --- /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)) From 68d12ae487e70628e649bda8eb172c1d8910199f Mon Sep 17 00:00:00 2001 From: Kirill Logachev Date: Fri, 21 Oct 2022 19:08:45 -0700 Subject: [PATCH 2/3] report groups RUD --- stacklet/client/platform/commands/__init__.py | 6 +- .../client/platform/commands/report_groups.py | 228 ++++++++++++++++++ 2 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 stacklet/client/platform/commands/report_groups.py diff --git a/stacklet/client/platform/commands/__init__.py b/stacklet/client/platform/commands/__init__.py index 83f24d5..7a2e6ea 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, - user, + 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..1e527c0 --- /dev/null +++ b/stacklet/client/platform/commands/report_groups.py @@ -0,0 +1,228 @@ +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 + snippet = """ +mutation { + addTemplate(input: { name: $name, content: $content }) { + template { + id + name + content + } + } +} +""" + +@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() +@snippet_options("create-report-group") +@click.pass_context +def create(ctx, **kwargs): + """ + Create a new or update an existing report-group + """ + click.echo(_run_graphql(ctx=ctx, name="create-report-group", variables=kwargs)) + +@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)) From 1d4ee7275836903d216e63cf0aed201e5850c7a3 Mon Sep 17 00:00:00 2001 From: Kirill Logachev Date: Sat, 22 Oct 2022 10:20:40 -0700 Subject: [PATCH 3/3] format --- stacklet/client/platform/commands/__init__.py | 2 +- .../client/platform/commands/report_groups.py | 90 ++++++++++++++++--- .../client/platform/commands/templates.py | 20 ++--- 3 files changed, 88 insertions(+), 24 deletions(-) diff --git a/stacklet/client/platform/commands/__init__.py b/stacklet/client/platform/commands/__init__.py index 7a2e6ea..0015a43 100644 --- a/stacklet/client/platform/commands/__init__.py +++ b/stacklet/client/platform/commands/__init__.py @@ -21,5 +21,5 @@ report_groups, repository, templates, - user + user, ] diff --git a/stacklet/client/platform/commands/report_groups.py b/stacklet/client/platform/commands/report_groups.py index 1e527c0..94ff26d 100644 --- a/stacklet/client/platform/commands/report_groups.py +++ b/stacklet/client/platform/commands/report_groups.py @@ -1,3 +1,4 @@ +import json import click from stacklet.client.platform.executor import _run_graphql @@ -10,18 +11,64 @@ class CreateReportGroup(StackletGraphqlSnippet): name = "create-report-group" pagination = False + parameter_types = {"input": "ReportGroupInput"} snippet = """ mutation { - addTemplate(input: { name: $name, content: $content }) { - template { + addReportGroup(input: $input) { + reportGroup{ id name - content + 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" @@ -79,9 +126,8 @@ class QueryReportGroup(StackletGraphqlSnippet): } } """ - required={ - "name": "" - } + required = {"name": ""} + @StackletGraphqlExecutor.registry.register("list-report-groups") class QueryReportGroups(StackletGraphqlSnippet): @@ -156,9 +202,8 @@ class QueryReportGroups(StackletGraphqlSnippet): } } """ - optional={ - "binding": "" - } + optional = {"binding": ""} + @StackletGraphqlExecutor.registry.register("remove-report-group") class RemoveReportGroup(StackletGraphqlSnippet): @@ -169,7 +214,7 @@ class RemoveReportGroup(StackletGraphqlSnippet): removeReportGroup(name: "$name") } """ - required={"name": ""} + required = {"name": ""} @click.group(short_help="Run report groups queries/mutations") @@ -192,13 +237,30 @@ def report_groups(*args, **kwargs): @report_groups.command() -@snippet_options("create-report-group") @click.pass_context -def create(ctx, **kwargs): +@click.option("--file", help="Json file with Report Group.") +def create(ctx, file): """ Create a new or update an existing report-group """ - click.echo(_run_graphql(ctx=ctx, name="create-report-group", variables=kwargs)) + + 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") @@ -209,6 +271,7 @@ def list(ctx, **kwargs): """ click.echo(_run_graphql(ctx=ctx, name="list-report-groups", variables=kwargs)) + @report_groups.command() @snippet_options("get-report-group") @click.pass_context @@ -218,6 +281,7 @@ def get(ctx, **kwargs): """ click.echo(_run_graphql(ctx=ctx, name="get-report-group", variables=kwargs)) + @report_groups.command() @snippet_options("remove-report-group") @click.pass_context diff --git a/stacklet/client/platform/commands/templates.py b/stacklet/client/platform/commands/templates.py index a47b9c9..55292ca 100644 --- a/stacklet/client/platform/commands/templates.py +++ b/stacklet/client/platform/commands/templates.py @@ -21,10 +21,8 @@ class CreateTemplate(StackletGraphqlSnippet): } } """ - required = { - "name": "", - "content": "" - } + required = {"name": "", "content": ""} + @StackletGraphqlExecutor.registry.register("get-template") class QueryTemplate(StackletGraphqlSnippet): @@ -39,9 +37,8 @@ class QueryTemplate(StackletGraphqlSnippet): } } """ - required = { - "name": "" - } + required = {"name": ""} + @StackletGraphqlExecutor.registry.register("list-templates") class QueryTemplates(StackletGraphqlSnippet): @@ -67,6 +64,7 @@ class QueryTemplates(StackletGraphqlSnippet): } """ + @StackletGraphqlExecutor.registry.register("remove-template") class RemoveTemplate(StackletGraphqlSnippet): name = "remove-template" @@ -76,9 +74,8 @@ class RemoveTemplate(StackletGraphqlSnippet): removeTemplate(id: $id) } """ - required = { - "id": "" - } + required = {"id": ""} + @click.group(short_help="Run templates queries/mutations") @default_options() @@ -108,6 +105,7 @@ def create(ctx, **kwargs): """ click.echo(_run_graphql(ctx=ctx, name="create-template", variables=kwargs)) + @templates.command() @snippet_options("list-templates") @click.pass_context @@ -117,6 +115,7 @@ def list(ctx, **kwargs): """ click.echo(_run_graphql(ctx=ctx, name="list-templates", variables=kwargs)) + @templates.command() @snippet_options("get-template") @click.pass_context @@ -126,6 +125,7 @@ def get(ctx, **kwargs): """ click.echo(_run_graphql(ctx=ctx, name="get-template", variables=kwargs)) + @templates.command() @snippet_options("remove-template") @click.pass_context