From 8896d2cb80d19a2c2fc9cd1e3a193fc08194a43b Mon Sep 17 00:00:00 2001 From: Henderson Hummel Date: Fri, 17 Dec 2021 10:08:18 -0800 Subject: [PATCH 01/13] First crack at removing all the unneeded functionality --- galaxykit/command.py | 214 +++++++++---------------------------------- 1 file changed, 41 insertions(+), 173 deletions(-) diff --git a/galaxykit/command.py b/galaxykit/command.py index 89bfa7a..ea71296 100644 --- a/galaxykit/command.py +++ b/galaxykit/command.py @@ -16,7 +16,7 @@ def print_unknown_error(args): - print(f"Unknown {args.kind} operation '{args.operation}'") + print(f"Unknown {args.object} operation '{args.operation}'") sys.exit(EXIT_UNKNOWN_ERROR) @@ -40,16 +40,42 @@ def report_error(resp): ) +def user(parent_parser, client): + parser = argparse.ArgumentParser(parent=parent_parser) + args = parser.parse_args(parent_parser.rest) + return None + + +def namespace(parent_parser, client): + return None + + +def collection(parent_parser, client): + return None + + +def group(parent_parser, client): + return None + + +def registry(parent_parser, client): + return None + + +def container(parent_parser, client): + return None + + def main(): parser = argparse.ArgumentParser() parser.add_argument( - "kind", + "object", type=str, action="store", - help="Kind of API content to operate against (user, group, namespace)", + help="Type of API content to operate against (user, group, namespace)", ) parser.add_argument("operation", type=str, action="store") - parser.add_argument("rest", type=str, action="store", nargs="*") + parser.add_argument("remaining", type=str, action="store", nargs="*") parser.add_argument("-i", "--ignore", default=False, action="store_true") parser.add_argument("-u", "--username", type=str, action="store") parser.add_argument("-p", "--password", type=str, action="store") @@ -74,178 +100,20 @@ def main(): client = GalaxyClient( args.server, (args.username, args.password), https_verify=https_verify ) - resp = None - try: - if args.kind == "user": - if args.operation == "list": - resp = users.get_user_list(client) - print(format_list(resp["data"], "username")) - elif args.operation == "create": - username, password = args.rest - created, resp = users.get_or_create_user( - client, username, password, None - ) - if created: - print("Created user", username) - else: - print(f"User {username} already existed") - elif args.operation == "delete": - (username,) = args.rest - try: - resp = users.delete_user(client, username) - except ValueError as e: - if not args.ignore: - print(e) - sys.exit(EXIT_NOT_FOUND) - elif args.operation == "group": - subop, *subopargs = args.rest - if subop == "add": - username, groupname = subopargs - user_data = users.get_user(client, username) - group_id = groups.get_group_id(client, groupname) - user_data["groups"].append( - { - "id": group_id, - "name": groupname, - "pulp_href": f"/pulp/api/v3/groups/{group_id}", - } - ) - resp = users.update_user(client, user_data) - else: - print_unknown_error(args) - - elif args.kind == "group": - if args.operation == "list": - resp = groups.get_group_list(client) - print(format_list(resp["data"], "name")) - elif args.operation == "create": - (name,) = args.rest - resp = groups.create_group(client, name) - elif args.operation == "delete": - (name,) = args.rest - try: - resp = groups.delete_group(client, name) - except ValueError as e: - if not args.ignore: - print(e) - sys.exit(EXIT_NOT_FOUND) - elif args.operation == "perm": - subop, *subopargs = args.rest - if subop == "list": - (groupname,) = subopargs - resp = groups.get_permissions(client, groupname) - print(format_list(resp["data"], "permission")) - elif subop == "add": - groupname, perm = subopargs - perms = [ - p["permission"] - for p in groups.get_permissions(client, groupname)["data"] - ] - perms = list(set(perms) | set([perm])) - resp = groups.set_permissions(client, groupname, perms) - elif subop == "remove": - groupname, perm = subopargs - resp = groups.delete_permission(client, groupname, perm) - else: - print(f"Unknown group perm operation '{subop}'") - sys.exit(EXIT_UNKNOWN_ERROR) - else: - print_unknown_error(args) - - elif args.kind == "namespace": - if args.operation == "get": - (name,) = args.rest - print(json.dumps(namespaces.get_namespace(client, name))) - elif args.operation == "list-collections": - (name,) = args.rest - print(json.dumps(namespaces.get_namespace_collections(client, name))) - elif args.operation == "create": - if len(args.rest) == 2: - name, group = args.rest - else: - (name,) = args.rest - group = None - resp = namespaces.create_namespace(client, name, group) - elif args.operation == "delete": - raise NotImplementedError - elif args.operation == "groups": - raise NotImplementedError - elif args.operation == "addgroup": - name, group = args.rest - resp = namespaces.add_group(client, name, group) - elif args.operation == "removegroup": - name, group = args.rest - resp = namespaces.remove_group(client, name, group) - elif args.operation == "addgroupperm": - raise NotImplementedError - elif args.operation == "removegroupperm": - raise NotImplementedError - else: - print_unknown_error(args) - - elif args.kind == "container": - if args.operation == "readme": - if len(args.rest) == 1: - (container,) = args.rest - resp = containers.get_readme(client, container) - print(resp["text"]) - elif len(args.rest) == 2: - container, readme = args.rest - resp = containers.set_readme(client, container, readme) - else: - print("container readme takes either 1 or 2 parameters.") - sys.exit(EXIT_UNKNOWN_ERROR) - else: - print_unknown_error(args) - - elif args.kind == "collection": - if args.operation == "upload": - if len(args.rest) == 0: - (namespace, collection_name) = (client.username, None) - else: - (namespace, collection_name) = args.rest - - resp = namespaces.create_namespace(client, namespace, None) - artifact = collections.upload_test_collection( - client, namespace=namespace, collection_name=collection_name - ) - print(json.dumps(artifact)) - elif args.operation == "move": - if len(args.rest) == 2: - (namespace, collection_name) = args.rest - # defaults to version = 1.0.0, source = staging, destination = published - collections.move_collection(client, namespace, collection_name) - else: - ( - namespace, - collection_name, - version, - source, - destination, - ) = args.rest - collections.move_collection( - client, namespace, collection_name, version, source, destination - ) - else: - print_unknown_error(args) - - elif args.kind == "url": - if args.operation == "get": - (url,) = args.rest - print(json.dumps(client.get(url))) - elif args.operation == "post": - raise NotImplementedError - else: - print_unknown_error(args) - + funs = { + "user": user, + "group": group, + "namespace": namespace, + "container": container, + "collection": collection, + "url": url, + } + if args.object in funs: + funs[args.object](args.rest, client) else: - print(f"Unknown resource type '{args.kind}'") + print(f"Unknown resource type '{args.object}'") sys.exit(EXIT_UNKNOWN_ERROR) - - if resp and not ignore: - report_error(resp) - except GalaxyClientError: if not ignore: raise From 75cccba3066683b329a66fe0dd645efb49566de5 Mon Sep 17 00:00:00 2001 From: Henderson Hummel Date: Mon, 20 Dec 2021 09:48:33 -0800 Subject: [PATCH 02/13] `user` command now parses as it should. --- galaxykit/command.py | 144 ++++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 85 deletions(-) diff --git a/galaxykit/command.py b/galaxykit/command.py index ea71296..e3c4b0f 100644 --- a/galaxykit/command.py +++ b/galaxykit/command.py @@ -2,118 +2,92 @@ import sys import json -from .client import GalaxyClient, GalaxyClientError -from . import containers -from . import collections -from . import groups -from . import namespaces -from . import users - -EXIT_OK = 0 -EXIT_UNKNOWN_ERROR = 1 -EXIT_NOT_FOUND = 2 -EXIT_DUPLICATE = 4 - - -def print_unknown_error(args): - print(f"Unknown {args.object} operation '{args.operation}'") - sys.exit(EXIT_UNKNOWN_ERROR) - - -def format_list(data, identifier): - buffer = [] - for datum in data: - line = [datum[identifier]] - for key, value in datum.items(): - if key != identifier and value: - s = f"{key}={value}" - line.append(s) - buffer.append(" ".join(line)) - return "\n".join(buffer) - - -def report_error(resp): - if "errors" in resp: - for error in resp["errors"]: - print( - f"API Failure: HTTP {error['status']} {error['code']}; {error['title']} ({error['detail']})" - ) - - -def user(parent_parser, client): - parser = argparse.ArgumentParser(parent=parent_parser) - args = parser.parse_args(parent_parser.rest) - return None - -def namespace(parent_parser, client): +def parse_collections(subparsers): return None -def collection(parent_parser, client): +def parse_containers(subparsers): return None -def group(parent_parser, client): +def parse_groups(subparsers): return None -def registry(parent_parser, client): +def parse_namespaces(subparsers): return None -def container(parent_parser, client): - return None +def parse_user(subparsers): + user_parser = subparsers.add_parser("user", help="user help") + user_subparser = user_parser.add_subparsers() + # 'user list' subcommand + user_create_parser = user_subparser.add_parser("list") + user_create_parser.set_defaults(function="list") -def main(): - parser = argparse.ArgumentParser() - parser.add_argument( - "object", - type=str, - action="store", - help="Type of API content to operate against (user, group, namespace)", + # 'user create' subcommand + user_create_parser = user_subparser.add_parser("create") + user_create_parser.add_argument( + "new_user", type=str, help="username of the user to be created." + ) + user_create_parser.add_argument( + "new_password", type=str, help="password of the user to be created." + ) + user_create_parser.add_argument( + "--email", type=str, help="email of the user to be created." + ) + user_create_parser.add_argument( + "--first-name", type=str, help="first name of the user to be created." + ) + user_create_parser.add_argument( + "--last-name", type=str, help="last name of the user to be created." + ) + user_create_parser.set_defaults(function="create") + + # 'user delete' subcommand + user_delete_parser = user_subparser.add_parser("delete") + user_delete_parser.add_argument( + "selected_user", type=str, help="username of the user to be deleted." ) - parser.add_argument("operation", type=str, action="store") - parser.add_argument("remaining", type=str, action="store", nargs="*") + user_delete_parser.set_defaults(function="delete") + + +def test(commant_to_test): + parser = argparse.ArgumentParser(prog="galaxykit") + subparsers = parser.add_subparsers() + parser.add_argument("-i", "--ignore", default=False, action="store_true") - parser.add_argument("-u", "--username", type=str, action="store") - parser.add_argument("-p", "--password", type=str, action="store") + parser.add_argument("-u", "--username", default="admin", type=str, action="store") + parser.add_argument("-p", "--password", default="admin", type=str, action="store") parser.add_argument( "-c", "--ignore-certs", - default=False, action="store_true", + default=False, help="Ignore invalid SSL certificates", ) parser.add_argument( "-s", "--server", - type=str, action="store", default="http://localhost:8002/api/automation-hub/", + type=str, ) - args = parser.parse_args() - ignore = args.ignore - https_verify = not args.ignore_certs - client = GalaxyClient( - args.server, (args.username, args.password), https_verify=https_verify - ) - try: - funs = { - "user": user, - "group": group, - "namespace": namespace, - "container": container, - "collection": collection, - "url": url, - } - if args.object in funs: - funs[args.object](args.rest, client) - else: - print(f"Unknown resource type '{args.object}'") - sys.exit(EXIT_UNKNOWN_ERROR) - except GalaxyClientError: - if not ignore: - raise + # parsing for subcommands + parse_collections(subparsers) + parse_containers(subparsers) + parse_groups(subparsers) + parse_namespaces(subparsers) + parse_user(subparsers) + + args = parser.parse_args(command_to_test) + print(args) + + +command_to_test = "user create jdoe passwd --email 'jdoe@redhat.com' --first-name John --last-name Doe".split( + " " +) +test(command_to_test) From 535789a533653342a25ad05ff96328456ce71acb Mon Sep 17 00:00:00 2001 From: Henderson Hummel Date: Mon, 20 Dec 2021 11:16:31 -0800 Subject: [PATCH 03/13] Typo fix --- galaxykit/command.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/galaxykit/command.py b/galaxykit/command.py index e3c4b0f..339dc2b 100644 --- a/galaxykit/command.py +++ b/galaxykit/command.py @@ -54,7 +54,7 @@ def parse_user(subparsers): user_delete_parser.set_defaults(function="delete") -def test(commant_to_test): +def test(command_to_test): parser = argparse.ArgumentParser(prog="galaxykit") subparsers = parser.add_subparsers() From a1c6c5a69b3f52c382130380e79c7aa4da27dfad Mon Sep 17 00:00:00 2001 From: Henderson Hummel Date: Mon, 20 Dec 2021 13:33:41 -0800 Subject: [PATCH 04/13] Skeleton of parse_namespaces --- galaxykit/command.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/galaxykit/command.py b/galaxykit/command.py index 339dc2b..9932266 100644 --- a/galaxykit/command.py +++ b/galaxykit/command.py @@ -16,7 +16,32 @@ def parse_groups(subparsers): def parse_namespaces(subparsers): - return None + """ + # these three only take 1 positional argument, which is the name of the namespace + "get": + "list-collections": + "create": + + # these take 2 positional arguments, name and group + "addgroup": + "removegroup": + + # all the below just raise NotImplementedError + # TODO: implement these down the line + "delete": + "groups": + "addgroupperm": + "removegroupperm": + """ + + namespace_parser = subparsers.add_parser("namespace", help="namespace help") + namespace_subparser = namespace_parser.add_subparsers() + + # 'namespace list' subcommand + namespace_create_parser = namespace_subparser.add_parser("get") + namespace_create_parser.add_argument( + "namespace", type=str, help="Namespace to be listed." + ) def parse_user(subparsers): @@ -49,7 +74,7 @@ def parse_user(subparsers): # 'user delete' subcommand user_delete_parser = user_subparser.add_parser("delete") user_delete_parser.add_argument( - "selected_user", type=str, help="username of the user to be deleted." + "user_to_delete", type=str, help="username of the user to be deleted." ) user_delete_parser.set_defaults(function="delete") @@ -87,7 +112,7 @@ def test(command_to_test): print(args) -command_to_test = "user create jdoe passwd --email 'jdoe@redhat.com' --first-name John --last-name Doe".split( +command_to_test = "user create newusername newpasswd --email '$jdoe@redhat.com' --last-name Doe".split( " " ) test(command_to_test) From f7675782da0cd375592abf457e83e1ad5f37f321 Mon Sep 17 00:00:00 2001 From: Shaiah Emigh-Doyle Date: Mon, 20 Dec 2021 16:36:39 -0500 Subject: [PATCH 05/13] add is-superuser and groups arguments to user_create_parser --- galaxykit/command.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/galaxykit/command.py b/galaxykit/command.py index 339dc2b..bd83576 100644 --- a/galaxykit/command.py +++ b/galaxykit/command.py @@ -44,6 +44,12 @@ def parse_user(subparsers): user_create_parser.add_argument( "--last-name", type=str, help="last name of the user to be created." ) + user_create_parser.add_argument( + "--is-superuser", help="make a superuser.", action='store_true', default=False, + ) + user_create_parser.add_argument( + "--groups", type=str, help="add user to a group." + ) user_create_parser.set_defaults(function="create") # 'user delete' subcommand From 725276a8de57443f9bb424ce5d09c9ba540ecc4e Mon Sep 17 00:00:00 2001 From: Shaiah Emigh-Doyle Date: Mon, 20 Dec 2021 17:09:22 -0500 Subject: [PATCH 06/13] add parser and subparsers for namespace command --- galaxykit/command.py | 53 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/galaxykit/command.py b/galaxykit/command.py index 7de4c31..39db74b 100644 --- a/galaxykit/command.py +++ b/galaxykit/command.py @@ -1,6 +1,7 @@ import argparse import sys import json +from typing import Collection def parse_collections(subparsers): @@ -37,20 +38,58 @@ def parse_namespaces(subparsers): namespace_parser = subparsers.add_parser("namespace", help="namespace help") namespace_subparser = namespace_parser.add_subparsers() - # 'namespace list' subcommand - namespace_create_parser = namespace_subparser.add_parser("get") + # 'namespace get' subcommand + namespace_get_parser = namespace_subparser.add_parser("get", help="Get namespace metadata.") + namespace_get_parser.set_defaults(function="get-namespace") + namespace_get_parser.add_argument( + "namespace", type=str, + ) + + #list Collections subcommand + namespace_list_parser = namespace_subparser.add_parser("list-collections", help="Get namespace collections.") + namespace_list_parser.set_defaults(function-"get-namespace-collections") + namespace_list_parser.add_argument( + "namespace", type=str, + ) + + #create a namespace subcommand + namespace_create_parser = namespace_subparser.add_parser("create", help="Create a namespace") + namespace_create_parser.set_defaults(function="create-namespace") namespace_create_parser.add_argument( - "namespace", type=str, help="Namespace to be listed." + "namespace", type=str, + ) + + #add group to a namespace subcommand + namespace_add_group_parser = namespace_subparser.add_parser("add-group") + namespace_add_group_parser.set_defaults(function="namespace-add-group") + namespace_add_group_parser.add_argument( + "namespace", type=str, help="Namespace name." + ) + namespace_add_group_parser.add_argument( + "group", type=str, help="Namespace group." ) + #remove group from a namespace subcommand + namespace_remove_group_parser = namespace_subparser.add_parser("remove-group") + namespace_remove_group_parser.set_defaults(function="namespace-remove-group") + namespace_remove_group_parser.add_argument( + "namespace", type=str, help="Namespace name." + ) + + namespace_add_group_parser.add_argument( + "group", type=str, help="Namespace group." + ) + + + def parse_user(subparsers): user_parser = subparsers.add_parser("user", help="user help") user_subparser = user_parser.add_subparsers() # 'user list' subcommand - user_create_parser = user_subparser.add_parser("list") - user_create_parser.set_defaults(function="list") + user_list_parser = user_subparser.add_parser("list") + user_list_parser.set_defaults(function="list") # 'user create' subcommand user_create_parser = user_subparser.add_parser("create") @@ -75,14 +114,14 @@ def parse_user(subparsers): user_create_parser.add_argument( "--groups", type=str, help="add user to a group." ) - user_create_parser.set_defaults(function="create") + user_create_parser.set_defaults(function="user create") # 'user delete' subcommand user_delete_parser = user_subparser.add_parser("delete") user_delete_parser.add_argument( "user_to_delete", type=str, help="username of the user to be deleted." ) - user_delete_parser.set_defaults(function="delete") + user_delete_parser.set_defaults(function="user delete") def test(command_to_test): From b7ccfd77dce2162173a78a97f293197e500cec5c Mon Sep 17 00:00:00 2001 From: Henderson Hummel Date: Tue, 21 Dec 2021 14:24:43 -0800 Subject: [PATCH 07/13] Finish groups, begin on cli_functions that actually do stuff --- galaxykit/cli_functions.py | 32 +++++++ galaxykit/command.py | 167 ++++++++++++++++++++++++++++--------- 2 files changed, 158 insertions(+), 41 deletions(-) create mode 100644 galaxykit/cli_functions.py diff --git a/galaxykit/cli_functions.py b/galaxykit/cli_functions.py new file mode 100644 index 0000000..8f586a1 --- /dev/null +++ b/galaxykit/cli_functions.py @@ -0,0 +1,32 @@ +from .client import GalaxyClient, GalaxyClientError +from . import containers +from . import collections +from . import groups +from . import namespaces +from . import users + + +def format_list(data, identifier): + buffer = [] + for datum in data: + line = [datum[identifier]] + for key, value in datum.items(): + if key != identifier and value: + s = f"{key}={value}" + line.append(s) + buffer.append(" ".join(line)) + return "\n".join(buffer) + + +def get_client(args): + ignore = args.ignore + https_verify = not args.ignore_certs + return GalaxyClient( + args.server, (args.username, args.password), https_verify=https_verify + ) + + +def list_users(args): + client = get_client(args) + resp = users.get_user_list(client) + print(format_list(resp["data"], "username")) diff --git a/galaxykit/command.py b/galaxykit/command.py index 39db74b..da11315 100644 --- a/galaxykit/command.py +++ b/galaxykit/command.py @@ -2,6 +2,7 @@ import sys import json from typing import Collection +from . import cli_functions def parse_collections(subparsers): @@ -9,25 +10,106 @@ def parse_collections(subparsers): def parse_containers(subparsers): - return None + container_parser = subparsers.add_parser( + "container", help="subcommand for manipulating container metadata" + ) + container_subparser = container_parser.add_subparsers() + + # parsing the get-readme subcommand + container_get_readme_parser = container_subparser.add_parser( + "get-readme", help="Returns the readme for the given container." + ) + container_get_readme_parser.add_argument( + "collection_name", help="name of container." + ) + container_get_readme_parser.get_defaults(function="container-get-readme") + + # parsing the set-readme subcommand + container_set_readme_parser = container_subparser.add_parser( + "set-readme", + help="Sets the readme for the given container to the passed string.", + ) + container_set_readme_parser.add_argument( + "collection_name", help="Name of container." + ) + container_set_readme_parser.add_argument( + "new_readme", help="String that will be set as new readme." + ) + container_set_readme_parser.set_defaults(function="container-set-readme") def parse_groups(subparsers): - return None + """ + if args.operation == "list": + elif args.operation == "create": + elif args.operation == "delete": + elif args.operation == "perm": + if subop == "list": + elif subop == "add": + elif subop == "remove": + """ + group_parser = subparsers.add_parser( + "group", help="List, create, delete, edit permissions of groups." + ) + group_subparser = group_parser.add_subparsers() + # group list subcommand + group_list_parser = group_subparser.add_parser("list", help="List all groups") + group_list_parser.set_defaults(function="group-list") + + # group create + group_create_parser = group_subparser.add_parser( + "create", help="create a group with given name" + ) + group_create_parser.add_argument( + "group_name", type=str, help="name of group to be created." + ) + group_create_parser.set_defaults(function="group-create") + + # group delete + group_delete_parser = group_subparser.add_parser( + "delete", help="delete a group with given name" + ) + group_delete_parser.add_argument( + "group_name", type=str, help="name of group to be deleted." + ) + group_delete_parser.set_defaults(function="group-delete") + + # permissions subcommands + group_perm_parser = subparsers.add_parser("perm", help="Permission subcommand") + group_perm_subparser = group_parser.add_subparsers() -def parse_namespaces(subparsers): """ - # these three only take 1 positional argument, which is the name of the namespace - "get": - "list-collections": - "create": + if subop == "list": + elif subop == "add": + elif subop == "remove": + """ + # group perm list + group_perm_list_parser = group_perm_subparser.add_parser( + "list", help="list the permissions of a given group." + ) + group_perm_list_parser.add_argument("group_name") + group_perm_list_parser.set_defaults(function="group-perm-list") - # these take 2 positional arguments, name and group - "addgroup": - "removegroup": + # group perm add + group_perm_add_parser = group_perm_subparser.add_parser( + "add", help="add the passed permission to the given group." + ) + group_perm_add_parser.add_argument("group_name") + group_perm_add_parser.add_argument("permission_name") + group_perm_add_parser.set_defaults(function="group-perm-add") + + # group perm remove + group_perm_remove_parser = group_perm_subparser.remove_parser( + "remove", help="remove the passed permission from the given group." + ) + group_perm_remove_parser.remove_argument("group_name") + group_perm_remove_parser.remove_argument("permission_name") + group_perm_remove_parser.set_defaults(function="group-perm-remove") - # all the below just raise NotImplementedError + +def parse_namespaces(subparsers): + """ # TODO: implement these down the line "delete": "groups": @@ -39,48 +121,51 @@ def parse_namespaces(subparsers): namespace_subparser = namespace_parser.add_subparsers() # 'namespace get' subcommand - namespace_get_parser = namespace_subparser.add_parser("get", help="Get namespace metadata.") + namespace_get_parser = namespace_subparser.add_parser( + "get", help="Get namespace metadata." + ) namespace_get_parser.set_defaults(function="get-namespace") namespace_get_parser.add_argument( - "namespace", type=str, + "namespace", + type=str, ) - #list Collections subcommand - namespace_list_parser = namespace_subparser.add_parser("list-collections", help="Get namespace collections.") - namespace_list_parser.set_defaults(function-"get-namespace-collections") + # list Collections subcommand + namespace_list_parser = namespace_subparser.add_parser( + "list-collections", help="Get namespace collections." + ) + namespace_list_parser.set_defaults(function="get-namespace-collections") namespace_list_parser.add_argument( - "namespace", type=str, + "namespace", + type=str, ) - #create a namespace subcommand - namespace_create_parser = namespace_subparser.add_parser("create", help="Create a namespace") + # create a namespace subcommand + namespace_create_parser = namespace_subparser.add_parser( + "create", help="Create a namespace" + ) namespace_create_parser.set_defaults(function="create-namespace") namespace_create_parser.add_argument( - "namespace", type=str, + "namespace", + type=str, ) - #add group to a namespace subcommand + # add group to a namespace subcommand namespace_add_group_parser = namespace_subparser.add_parser("add-group") namespace_add_group_parser.set_defaults(function="namespace-add-group") namespace_add_group_parser.add_argument( - "namespace", type=str, help="Namespace name." - ) - namespace_add_group_parser.add_argument( - "group", type=str, help="Namespace group." + "namespace", type=str, help="Namespace name." ) + namespace_add_group_parser.add_argument("group", type=str, help="Namespace group.") - #remove group from a namespace subcommand + # remove group from a namespace subcommand namespace_remove_group_parser = namespace_subparser.add_parser("remove-group") namespace_remove_group_parser.set_defaults(function="namespace-remove-group") namespace_remove_group_parser.add_argument( - "namespace", type=str, help="Namespace name." - ) - - namespace_add_group_parser.add_argument( - "group", type=str, help="Namespace group." + "namespace", type=str, help="Namespace name." ) - + namespace_add_group_parser.add_argument("group", type=str, help="Namespace group.") def parse_user(subparsers): @@ -89,7 +174,7 @@ def parse_user(subparsers): # 'user list' subcommand user_list_parser = user_subparser.add_parser("list") - user_list_parser.set_defaults(function="list") + user_list_parser.set_defaults(function=cli_functions.list_users) # 'user create' subcommand user_create_parser = user_subparser.add_parser("create") @@ -109,11 +194,12 @@ def parse_user(subparsers): "--last-name", type=str, help="last name of the user to be created." ) user_create_parser.add_argument( - "--is-superuser", help="make a superuser.", action='store_true', default=False, - ) - user_create_parser.add_argument( - "--groups", type=str, help="add user to a group." + "--is-superuser", + help="make a superuser.", + action="store_true", + default=False, ) + user_create_parser.add_argument("--groups", type=str, help="add user to a group.") user_create_parser.set_defaults(function="user create") # 'user delete' subcommand @@ -154,10 +240,9 @@ def test(command_to_test): parse_user(subparsers) args = parser.parse_args(command_to_test) - print(args) + breakpoint() + args.function(args) -command_to_test = "user create newusername newpasswd --email '$jdoe@redhat.com' --last-name Doe".split( - " " -) +command_to_test = "user list".split(" ") test(command_to_test) From 1ced17dd70e8129322b10bb3e4c35743a0723ef7 Mon Sep 17 00:00:00 2001 From: Shaiah Emigh-Doyle Date: Tue, 21 Dec 2021 17:26:49 -0500 Subject: [PATCH 08/13] add subparsers to collection parsers --- galaxykit/command.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/galaxykit/command.py b/galaxykit/command.py index 39db74b..5b0e88d 100644 --- a/galaxykit/command.py +++ b/galaxykit/command.py @@ -5,15 +5,38 @@ def parse_collections(subparsers): - return None - + collection_parser = subparsers.add_parser("collection", help="collection help") + collection_subparser = collection_parser.add_subparsers() + + #collection upload subcommand + #takes NO positional arguments + #optional arguments namespace and collection_name + collection_upload_parser = collection_subparser.add_parser("upload", help="upload a collection") + collection_upload_parser.set_defaults(function="upload-collection") + + collection_upload_parser.add_argument("--namespace", type=str) + collection_upload_parser.add_argument("--collection-name", type-str) + collection_upload_parser.add_argument("--version", type=str, default='1.0.0') + + #collection move subcommand + #takes 2 positional arguments, namespace and collection_name + optional args + collection_move_parser = collection_subparser.add_parser("move", help="move a collection.") + collection_move_parser.set_defaults(function="move-collection") + collection_move_parser.add_argument("namespace", type=str) + collection_move_parser.add_argument("collection-name", type=str) + collection_move_parser.add_argument("--version", type=str, default='1.0.0') + collection_move_parser.add_argument("--source", type=str, default='staging') + collection_move_parser.add_argument("--destination", type=str, default='publishing') def parse_containers(subparsers): return None def parse_groups(subparsers): - return None + groups_parser = subparsers.add_parser("group", help="group help") + groups_subparser = groups_parser.add_subparsers() + + def parse_namespaces(subparsers): From f2f54f821578c4703392ca41b080276d3bdf3142 Mon Sep 17 00:00:00 2001 From: Shaiah Emigh-Doyle Date: Wed, 22 Dec 2021 12:43:36 -0500 Subject: [PATCH 09/13] fix imports errors and debug command.py --- galaxykit/cli_functions.py | 23 ++++++++++++++++------- galaxykit/client.py | 8 ++++---- galaxykit/command.py | 34 ++++++++++++---------------------- galaxykit/namespaces.py | 2 +- 4 files changed, 33 insertions(+), 34 deletions(-) diff --git a/galaxykit/cli_functions.py b/galaxykit/cli_functions.py index 8f586a1..c2e3c91 100644 --- a/galaxykit/cli_functions.py +++ b/galaxykit/cli_functions.py @@ -1,9 +1,9 @@ -from .client import GalaxyClient, GalaxyClientError -from . import containers -from . import collections -from . import groups -from . import namespaces -from . import users +from client import GalaxyClient, GalaxyClientError +import containers +import collections +import groups +import namespaces +import users def format_list(data, identifier): @@ -25,8 +25,17 @@ def get_client(args): args.server, (args.username, args.password), https_verify=https_verify ) - +## users def list_users(args): client = get_client(args) resp = users.get_user_list(client) print(format_list(resp["data"], "username")) + + +#containers + +##groups + +##collections + +##namespaces diff --git a/galaxykit/client.py b/galaxykit/client.py index 4772921..e5b73de 100644 --- a/galaxykit/client.py +++ b/galaxykit/client.py @@ -8,10 +8,10 @@ import requests -from . import containers -from . import containerutils -from . import groups -from . import users +import containers +import containerutils +import groups +import users class GalaxyClientError(Exception): diff --git a/galaxykit/command.py b/galaxykit/command.py index f3263b6..90311da 100644 --- a/galaxykit/command.py +++ b/galaxykit/command.py @@ -2,7 +2,7 @@ import sys import json from typing import Collection -from . import cli_functions +import cli_functions def parse_collections(subparsers): @@ -16,7 +16,7 @@ def parse_collections(subparsers): collection_upload_parser.set_defaults(function="upload-collection") collection_upload_parser.add_argument("--namespace", type=str) - collection_upload_parser.add_argument("--collection-name", type-str) + collection_upload_parser.add_argument("--collection-name", type=str) collection_upload_parser.add_argument("--version", type=str, default='1.0.0') #collection move subcommand @@ -42,7 +42,7 @@ def parse_containers(subparsers): container_get_readme_parser.add_argument( "collection_name", help="name of container." ) - container_get_readme_parser.get_defaults(function="container-get-readme") + container_get_readme_parser.set_defaults(function="container-get-readme") # parsing the set-readme subcommand container_set_readme_parser = container_subparser.add_parser( @@ -59,7 +59,7 @@ def parse_containers(subparsers): def parse_groups(subparsers): - + group_parser = subparsers.add_parser( "group", help="List, create, delete, edit permissions of groups." ) @@ -88,35 +88,25 @@ def parse_groups(subparsers): group_delete_parser.set_defaults(function="group-delete") # permissions subcommands - group_perm_parser = subparsers.add_parser("perm", help="Permission subcommand") - group_perm_subparser = group_parser.add_subparsers() - """ - if subop == "list": - elif subop == "add": - elif subop == "remove": - """ + # group perm list - group_perm_list_parser = group_perm_subparser.add_parser( - "list", help="list the permissions of a given group." - ) + + group_perm_list_parser = subparsers.add_parser('perm-list') + group_perm_list_parser.add_argument("group_name") group_perm_list_parser.set_defaults(function="group-perm-list") # group perm add - group_perm_add_parser = group_perm_subparser.add_parser( - "add", help="add the passed permission to the given group." - ) + group_perm_add_parser = subparsers.add_parser('perm-add') group_perm_add_parser.add_argument("group_name") group_perm_add_parser.add_argument("permission_name") group_perm_add_parser.set_defaults(function="group-perm-add") # group perm remove - group_perm_remove_parser = group_perm_subparser.remove_parser( - "remove", help="remove the passed permission from the given group." - ) - group_perm_remove_parser.remove_argument("group_name") - group_perm_remove_parser.remove_argument("permission_name") + group_perm_remove_parser = subparsers.add_parser('perm-remove') + group_perm_remove_parser.add_argument("group_name") + group_perm_remove_parser.add_argument("permission_name") group_perm_remove_parser.set_defaults(function="group-perm-remove") diff --git a/galaxykit/namespaces.py b/galaxykit/namespaces.py index dc16b9a..3942078 100644 --- a/galaxykit/namespaces.py +++ b/galaxykit/namespaces.py @@ -1,4 +1,4 @@ -from . import groups +import groups def create_namespace(client, name, group): From 92e3160aad604f3c473b7c2e06c3460087b1ebe4 Mon Sep 17 00:00:00 2001 From: Henderson Hummel Date: Wed, 22 Dec 2021 10:02:47 -0800 Subject: [PATCH 10/13] adding the old command file so it's easily referenced --- galaxykit/command-old.py | 251 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 251 insertions(+) create mode 100644 galaxykit/command-old.py diff --git a/galaxykit/command-old.py b/galaxykit/command-old.py new file mode 100644 index 0000000..89bfa7a --- /dev/null +++ b/galaxykit/command-old.py @@ -0,0 +1,251 @@ +import argparse +import sys +import json + +from .client import GalaxyClient, GalaxyClientError +from . import containers +from . import collections +from . import groups +from . import namespaces +from . import users + +EXIT_OK = 0 +EXIT_UNKNOWN_ERROR = 1 +EXIT_NOT_FOUND = 2 +EXIT_DUPLICATE = 4 + + +def print_unknown_error(args): + print(f"Unknown {args.kind} operation '{args.operation}'") + sys.exit(EXIT_UNKNOWN_ERROR) + + +def format_list(data, identifier): + buffer = [] + for datum in data: + line = [datum[identifier]] + for key, value in datum.items(): + if key != identifier and value: + s = f"{key}={value}" + line.append(s) + buffer.append(" ".join(line)) + return "\n".join(buffer) + + +def report_error(resp): + if "errors" in resp: + for error in resp["errors"]: + print( + f"API Failure: HTTP {error['status']} {error['code']}; {error['title']} ({error['detail']})" + ) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument( + "kind", + type=str, + action="store", + help="Kind of API content to operate against (user, group, namespace)", + ) + parser.add_argument("operation", type=str, action="store") + parser.add_argument("rest", type=str, action="store", nargs="*") + parser.add_argument("-i", "--ignore", default=False, action="store_true") + parser.add_argument("-u", "--username", type=str, action="store") + parser.add_argument("-p", "--password", type=str, action="store") + parser.add_argument( + "-c", + "--ignore-certs", + default=False, + action="store_true", + help="Ignore invalid SSL certificates", + ) + parser.add_argument( + "-s", + "--server", + type=str, + action="store", + default="http://localhost:8002/api/automation-hub/", + ) + + args = parser.parse_args() + ignore = args.ignore + https_verify = not args.ignore_certs + client = GalaxyClient( + args.server, (args.username, args.password), https_verify=https_verify + ) + resp = None + + try: + if args.kind == "user": + if args.operation == "list": + resp = users.get_user_list(client) + print(format_list(resp["data"], "username")) + elif args.operation == "create": + username, password = args.rest + created, resp = users.get_or_create_user( + client, username, password, None + ) + if created: + print("Created user", username) + else: + print(f"User {username} already existed") + elif args.operation == "delete": + (username,) = args.rest + try: + resp = users.delete_user(client, username) + except ValueError as e: + if not args.ignore: + print(e) + sys.exit(EXIT_NOT_FOUND) + elif args.operation == "group": + subop, *subopargs = args.rest + if subop == "add": + username, groupname = subopargs + user_data = users.get_user(client, username) + group_id = groups.get_group_id(client, groupname) + user_data["groups"].append( + { + "id": group_id, + "name": groupname, + "pulp_href": f"/pulp/api/v3/groups/{group_id}", + } + ) + resp = users.update_user(client, user_data) + else: + print_unknown_error(args) + + elif args.kind == "group": + if args.operation == "list": + resp = groups.get_group_list(client) + print(format_list(resp["data"], "name")) + elif args.operation == "create": + (name,) = args.rest + resp = groups.create_group(client, name) + elif args.operation == "delete": + (name,) = args.rest + try: + resp = groups.delete_group(client, name) + except ValueError as e: + if not args.ignore: + print(e) + sys.exit(EXIT_NOT_FOUND) + elif args.operation == "perm": + subop, *subopargs = args.rest + if subop == "list": + (groupname,) = subopargs + resp = groups.get_permissions(client, groupname) + print(format_list(resp["data"], "permission")) + elif subop == "add": + groupname, perm = subopargs + perms = [ + p["permission"] + for p in groups.get_permissions(client, groupname)["data"] + ] + perms = list(set(perms) | set([perm])) + resp = groups.set_permissions(client, groupname, perms) + elif subop == "remove": + groupname, perm = subopargs + resp = groups.delete_permission(client, groupname, perm) + else: + print(f"Unknown group perm operation '{subop}'") + sys.exit(EXIT_UNKNOWN_ERROR) + else: + print_unknown_error(args) + + elif args.kind == "namespace": + if args.operation == "get": + (name,) = args.rest + print(json.dumps(namespaces.get_namespace(client, name))) + elif args.operation == "list-collections": + (name,) = args.rest + print(json.dumps(namespaces.get_namespace_collections(client, name))) + elif args.operation == "create": + if len(args.rest) == 2: + name, group = args.rest + else: + (name,) = args.rest + group = None + resp = namespaces.create_namespace(client, name, group) + elif args.operation == "delete": + raise NotImplementedError + elif args.operation == "groups": + raise NotImplementedError + elif args.operation == "addgroup": + name, group = args.rest + resp = namespaces.add_group(client, name, group) + elif args.operation == "removegroup": + name, group = args.rest + resp = namespaces.remove_group(client, name, group) + elif args.operation == "addgroupperm": + raise NotImplementedError + elif args.operation == "removegroupperm": + raise NotImplementedError + else: + print_unknown_error(args) + + elif args.kind == "container": + if args.operation == "readme": + if len(args.rest) == 1: + (container,) = args.rest + resp = containers.get_readme(client, container) + print(resp["text"]) + elif len(args.rest) == 2: + container, readme = args.rest + resp = containers.set_readme(client, container, readme) + else: + print("container readme takes either 1 or 2 parameters.") + sys.exit(EXIT_UNKNOWN_ERROR) + else: + print_unknown_error(args) + + elif args.kind == "collection": + if args.operation == "upload": + if len(args.rest) == 0: + (namespace, collection_name) = (client.username, None) + else: + (namespace, collection_name) = args.rest + + resp = namespaces.create_namespace(client, namespace, None) + artifact = collections.upload_test_collection( + client, namespace=namespace, collection_name=collection_name + ) + print(json.dumps(artifact)) + elif args.operation == "move": + if len(args.rest) == 2: + (namespace, collection_name) = args.rest + # defaults to version = 1.0.0, source = staging, destination = published + collections.move_collection(client, namespace, collection_name) + else: + ( + namespace, + collection_name, + version, + source, + destination, + ) = args.rest + collections.move_collection( + client, namespace, collection_name, version, source, destination + ) + else: + print_unknown_error(args) + + elif args.kind == "url": + if args.operation == "get": + (url,) = args.rest + print(json.dumps(client.get(url))) + elif args.operation == "post": + raise NotImplementedError + else: + print_unknown_error(args) + + else: + print(f"Unknown resource type '{args.kind}'") + sys.exit(EXIT_UNKNOWN_ERROR) + + if resp and not ignore: + report_error(resp) + + except GalaxyClientError: + if not ignore: + raise From 7ca66e4a81a63b0bc37d9d2599f77305c9535404 Mon Sep 17 00:00:00 2001 From: Henderson Hummel Date: Wed, 22 Dec 2021 11:11:32 -0800 Subject: [PATCH 11/13] Filled out user cli_functions. --- galaxykit/cli_functions.py | 41 ++++++++++++++++++++++++++-- galaxykit/command.py | 56 +++++++++++++++++++++----------------- galaxykit/users.py | 11 ++++++-- 3 files changed, 78 insertions(+), 30 deletions(-) diff --git a/galaxykit/cli_functions.py b/galaxykit/cli_functions.py index c2e3c91..76eccab 100644 --- a/galaxykit/cli_functions.py +++ b/galaxykit/cli_functions.py @@ -25,14 +25,51 @@ def get_client(args): args.server, (args.username, args.password), https_verify=https_verify ) + ## users -def list_users(args): +def user_list(args): client = get_client(args) resp = users.get_user_list(client) print(format_list(resp["data"], "username")) + return resp + + +def user_create(args): + client = get_client(args) + if args.group: + args.group = users.get_group(client, args.group) + created, resp = users.get_or_create_user( + client, + args.new_user, + args.new_password, + args.group, + fname=args.first_name, + lname=args.last_name, + email=args.email, + superuser=args.is_superuser, + ) + if created: + print("Created user", args.username) + else: + print(f"User {args.username} already existed") + return resp + + +def user_delete(args): + client = get_client(args) + users.delete_user(client, args.user_to_delete) + # there's no response from the above call, so we need to explicitly + # add a return that indicates success or failure here. + try: + users.get_user(client, args.user_to_delete) + print(f"Unable to delete user {args.user_to_delete}") + return False + except: + print(f"Successfully deleted user {args.user_to_delete}") + return True -#containers +# containers ##groups diff --git a/galaxykit/command.py b/galaxykit/command.py index 90311da..e104c08 100644 --- a/galaxykit/command.py +++ b/galaxykit/command.py @@ -9,25 +9,30 @@ def parse_collections(subparsers): collection_parser = subparsers.add_parser("collection", help="collection help") collection_subparser = collection_parser.add_subparsers() - #collection upload subcommand - #takes NO positional arguments - #optional arguments namespace and collection_name - collection_upload_parser = collection_subparser.add_parser("upload", help="upload a collection") + # collection upload subcommand + # takes NO positional arguments + # optional arguments namespace and collection_name + collection_upload_parser = collection_subparser.add_parser( + "upload", help="upload a collection" + ) collection_upload_parser.set_defaults(function="upload-collection") collection_upload_parser.add_argument("--namespace", type=str) collection_upload_parser.add_argument("--collection-name", type=str) - collection_upload_parser.add_argument("--version", type=str, default='1.0.0') + collection_upload_parser.add_argument("--version", type=str, default="1.0.0") - #collection move subcommand - #takes 2 positional arguments, namespace and collection_name + optional args - collection_move_parser = collection_subparser.add_parser("move", help="move a collection.") + # collection move subcommand + # takes 2 positional arguments, namespace and collection_name + optional args + collection_move_parser = collection_subparser.add_parser( + "move", help="move a collection." + ) collection_move_parser.set_defaults(function="move-collection") collection_move_parser.add_argument("namespace", type=str) collection_move_parser.add_argument("collection-name", type=str) - collection_move_parser.add_argument("--version", type=str, default='1.0.0') - collection_move_parser.add_argument("--source", type=str, default='staging') - collection_move_parser.add_argument("--destination", type=str, default='publishing') + collection_move_parser.add_argument("--version", type=str, default="1.0.0") + collection_move_parser.add_argument("--source", type=str, default="staging") + collection_move_parser.add_argument("--destination", type=str, default="publishing") + def parse_containers(subparsers): container_parser = subparsers.add_parser( @@ -89,22 +94,21 @@ def parse_groups(subparsers): # permissions subcommands - # group perm list - group_perm_list_parser = subparsers.add_parser('perm-list') + group_perm_list_parser = subparsers.add_parser("perm-list") group_perm_list_parser.add_argument("group_name") group_perm_list_parser.set_defaults(function="group-perm-list") # group perm add - group_perm_add_parser = subparsers.add_parser('perm-add') + group_perm_add_parser = subparsers.add_parser("perm-add") group_perm_add_parser.add_argument("group_name") group_perm_add_parser.add_argument("permission_name") group_perm_add_parser.set_defaults(function="group-perm-add") # group perm remove - group_perm_remove_parser = subparsers.add_parser('perm-remove') + group_perm_remove_parser = subparsers.add_parser("perm-remove") group_perm_remove_parser.add_argument("group_name") group_perm_remove_parser.add_argument("permission_name") group_perm_remove_parser.set_defaults(function="group-perm-remove") @@ -176,7 +180,7 @@ def parse_user(subparsers): # 'user list' subcommand user_list_parser = user_subparser.add_parser("list") - user_list_parser.set_defaults(function=cli_functions.list_users) + user_list_parser.set_defaults(function=cli_functions.user_list) # 'user create' subcommand user_create_parser = user_subparser.add_parser("create") @@ -187,13 +191,16 @@ def parse_user(subparsers): "new_password", type=str, help="password of the user to be created." ) user_create_parser.add_argument( - "--email", type=str, help="email of the user to be created." + "--email", type=str, help="email of the user to be created.", default="" ) user_create_parser.add_argument( - "--first-name", type=str, help="first name of the user to be created." + "--first-name", + type=str, + help="first name of the user to be created.", + default="", ) user_create_parser.add_argument( - "--last-name", type=str, help="last name of the user to be created." + "--last-name", type=str, help="last name of the user to be created.", default="" ) user_create_parser.add_argument( "--is-superuser", @@ -201,15 +208,15 @@ def parse_user(subparsers): action="store_true", default=False, ) - user_create_parser.add_argument("--groups", type=str, help="add user to a group.") - user_create_parser.set_defaults(function="user create") + user_create_parser.add_argument("--group", type=str, help="add user to a group.") + user_create_parser.set_defaults(function=cli_functions.user_create) # 'user delete' subcommand user_delete_parser = user_subparser.add_parser("delete") user_delete_parser.add_argument( "user_to_delete", type=str, help="username of the user to be deleted." ) - user_delete_parser.set_defaults(function="user delete") + user_delete_parser.set_defaults(function=cli_functions.user_delete) def test(command_to_test): @@ -241,10 +248,9 @@ def test(command_to_test): parse_namespaces(subparsers) parse_user(subparsers) + command_to_test = command_to_test.split(" ") args = parser.parse_args(command_to_test) - breakpoint() args.function(args) -command_to_test = "user list".split(" ") -test(command_to_test) +breakpoint() diff --git a/galaxykit/users.py b/galaxykit/users.py index 129ca19..eca7886 100644 --- a/galaxykit/users.py +++ b/galaxykit/users.py @@ -23,7 +23,9 @@ def get_or_create_user( user_url = f"_ui/v1/users?username={username}" user_resp = client.get(user_url) if user_resp["meta"]["count"] == 0: - return True, create_user(client, username, password, group, fname, lname, email) + return True, create_user( + client, username, password, group, fname, lname, email, superuser + ) return False, user_resp["data"][0] @@ -90,11 +92,14 @@ def get_user_id(client, username): def get_user(client, username): """ - Returns the id for a given username + Returns user data for a given username """ user_url = f"_ui/v1/users/?username={username}" user_resp = client.get(user_url) - return user_resp["data"][0] + if user_resp["data"] is not []: + return user_resp["data"][0] + else: + return None def get_user_list(client): From 7d9ef8f433da7b96ecf2547c44dcb9bf715e35fe Mon Sep 17 00:00:00 2001 From: Henderson Hummel Date: Tue, 4 Jan 2022 11:04:08 -0800 Subject: [PATCH 12/13] Fix incorrect module call --- galaxykit/cli_functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/galaxykit/cli_functions.py b/galaxykit/cli_functions.py index 76eccab..904a3c4 100644 --- a/galaxykit/cli_functions.py +++ b/galaxykit/cli_functions.py @@ -37,7 +37,7 @@ def user_list(args): def user_create(args): client = get_client(args) if args.group: - args.group = users.get_group(client, args.group) + args.group = groups.get_group(client, args.group) created, resp = users.get_or_create_user( client, args.new_user, From d4e0a2b48b8acc3b17a41c27b4bbf270a4cbd572 Mon Sep 17 00:00:00 2001 From: Henderson Hummel Date: Mon, 7 Mar 2022 16:06:51 -0800 Subject: [PATCH 13/13] Begin the url command in command.py --- galaxykit/command.py | 72 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 11 deletions(-) diff --git a/galaxykit/command.py b/galaxykit/command.py index e104c08..6c60325 100644 --- a/galaxykit/command.py +++ b/galaxykit/command.py @@ -5,6 +5,16 @@ import cli_functions +def parse_url(subparsers): + url_parser = subparsers.add_parser( + "url", help="subcommand for directly requesting a given URL." + ) + url_subparser = url_parser.add_subparsers() + + # url get parser + # + + def parse_collections(subparsers): collection_parser = subparsers.add_parser("collection", help="collection help") collection_subparser = collection_parser.add_subparsers() @@ -15,7 +25,7 @@ def parse_collections(subparsers): collection_upload_parser = collection_subparser.add_parser( "upload", help="upload a collection" ) - collection_upload_parser.set_defaults(function="upload-collection") + collection_upload_parser.set_defaults(function="collection-upload") collection_upload_parser.add_argument("--namespace", type=str) collection_upload_parser.add_argument("--collection-name", type=str) @@ -26,12 +36,23 @@ def parse_collections(subparsers): collection_move_parser = collection_subparser.add_parser( "move", help="move a collection." ) - collection_move_parser.set_defaults(function="move-collection") + collection_move_parser.set_defaults(function="collection-move") collection_move_parser.add_argument("namespace", type=str) collection_move_parser.add_argument("collection-name", type=str) collection_move_parser.add_argument("--version", type=str, default="1.0.0") collection_move_parser.add_argument("--source", type=str, default="staging") - collection_move_parser.add_argument("--destination", type=str, default="publishing") + collection_move_parser.add_argument("--destination", type=str, default="published") + + # collection delete subcommand + # takes 2 positional arguments, namespace and collection_name + optional args + collection_delete_parser = collection_subparser.add_parser( + "delete", help="delete a collection." + ) + collection_delete_parser.set_defaults(function="collection-delete") + collection_delete_parser.add_argument("namespace", type=str) + collection_delete_parser.add_argument("collection-name", type=str) + collection_delete_parser.add_argument("--version", type=str, default="1.0.0") + collection_delete_parser.add_argument("--repository", type=str, default="published") def parse_containers(subparsers): @@ -40,28 +61,57 @@ def parse_containers(subparsers): ) container_subparser = container_parser.add_subparsers() - # parsing the get-readme subcommand + # get-readme subcommand + # takes 1 positional argument, which is the name of the container container_get_readme_parser = container_subparser.add_parser( "get-readme", help="Returns the readme for the given container." ) container_get_readme_parser.add_argument( - "collection_name", help="name of container." + "container_name", help="name of container." ) container_get_readme_parser.set_defaults(function="container-get-readme") - # parsing the set-readme subcommand + # set-readme subcommand + # takes 2 positional arguments, which are the name of the container and the new readme (a string) container_set_readme_parser = container_subparser.add_parser( "set-readme", help="Sets the readme for the given container to the passed string.", ) container_set_readme_parser.add_argument( - "collection_name", help="Name of container." + "container_name", type=str, help="Name of container." ) container_set_readme_parser.add_argument( - "new_readme", help="String that will be set as new readme." + "new_readme", type=str, help="String that will be set as new readme." ) container_set_readme_parser.set_defaults(function="container-set-readme") + # delete subcommand + # takes just the name of the container as an argument + container_delete_parser = container_subparser.add_parser( + "delete", + help="Deletes a container", + ) + container_delete_parser.add_argument( + "container_name", type=str, help="Name of the container to delete" + ) + container_delete_parser.add_argument("image", type=str, help="Image tag to delete") + container_set_readme_parser.set_defaults(function="container-delete") + + +def parse_container_registries(subparsers): + container_registry_parser = subparsers.add_parser( + "container_registry", help="subcommand for manipulating container registries" + ) + container_registry_subparser = container_parser.add_subparsers() + container_registry_delete_parser = container_registry_subparser.add_parser( + "delete", help="Delete a given container registry." + ) + + container_registry_delete_parser.add_argument( + "registry_name", help="Name of the registry to delete." + ) + container_registry_delete_parser.set_defaults(function="container-registry-delete") + def parse_groups(subparsers): @@ -130,7 +180,7 @@ def parse_namespaces(subparsers): namespace_get_parser = namespace_subparser.add_parser( "get", help="Get namespace metadata." ) - namespace_get_parser.set_defaults(function="get-namespace") + namespace_get_parser.set_defaults(function="namespace-get") namespace_get_parser.add_argument( "namespace", type=str, @@ -140,7 +190,7 @@ def parse_namespaces(subparsers): namespace_list_parser = namespace_subparser.add_parser( "list-collections", help="Get namespace collections." ) - namespace_list_parser.set_defaults(function="get-namespace-collections") + namespace_list_parser.set_defaults(function="namespace-get-collections") namespace_list_parser.add_argument( "namespace", type=str, @@ -150,7 +200,7 @@ def parse_namespaces(subparsers): namespace_create_parser = namespace_subparser.add_parser( "create", help="Create a namespace" ) - namespace_create_parser.set_defaults(function="create-namespace") + namespace_create_parser.set_defaults(function="namespace-create") namespace_create_parser.add_argument( "namespace", type=str,