From fe50fa1a87a3d3483d723a80c02357d808e90806 Mon Sep 17 00:00:00 2001 From: Shaiah Emigh-Doyle Date: Thu, 16 Dec 2021 16:24:02 -0500 Subject: [PATCH 1/3] add assorted kwargs --- galaxykit/users.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/galaxykit/users.py b/galaxykit/users.py index 129ca19..25bee1c 100644 --- a/galaxykit/users.py +++ b/galaxykit/users.py @@ -22,6 +22,7 @@ def get_or_create_user( # check if the user already exists 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) @@ -29,7 +30,7 @@ def get_or_create_user( def create_user( - client, username, password, group, fname="", lname="", email="", superuser=False + client, username, password, group, *args, **kwargs, ): """ Create a new user. All the arguments aside from @@ -42,6 +43,10 @@ def create_user( "pulp_href": f"/pulp/api/v3/groups/{group_id}", } """ + + #define optional arguments + + if group is None: group = [] else: @@ -51,6 +56,11 @@ def create_user( assert "pulp_href" in group group = [group] + fname = kwargs.get('first_name', None) + lname = kwargs.get('last_name', None) + email = kwargs.get('email', None) + superuser = kwargs.get('superuser', False) + create_body = { "username": username, "first_name": fname, From 9592155a6493b05b19d9a7a8d10a38916ceeb4c1 Mon Sep 17 00:00:00 2001 From: Shaiah Emigh-Doyle Date: Fri, 17 Dec 2021 11:43:40 -0500 Subject: [PATCH 2/3] rewriting main.py() argparsers --- galaxykit/command.py | 19 ++++ galaxykit/command2.py | 251 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 270 insertions(+) create mode 100644 galaxykit/command2.py diff --git a/galaxykit/command.py b/galaxykit/command.py index 89bfa7a..f7aab8d 100644 --- a/galaxykit/command.py +++ b/galaxykit/command.py @@ -39,6 +39,25 @@ def report_error(resp): f"API Failure: HTTP {error['status']} {error['code']}; {error['title']} ({error['detail']})" ) +def user(): + + return None + +def namespace(): + return None + +def collection(): + return None + +def group(): + return None + +def remote_registry(): + return None + +def execution_environment(): + return None + def main(): parser = argparse.ArgumentParser() diff --git a/galaxykit/command2.py b/galaxykit/command2.py new file mode 100644 index 0000000..89bfa7a --- /dev/null +++ b/galaxykit/command2.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 8896d2cb80d19a2c2fc9cd1e3a193fc08194a43b Mon Sep 17 00:00:00 2001 From: Henderson Hummel Date: Fri, 17 Dec 2021 10:08:18 -0800 Subject: [PATCH 3/3] 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