-
Notifications
You must be signed in to change notification settings - Fork 15
[WIP] Refactor command.py #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hendersonreed
wants to merge
15
commits into
main
Choose a base branch
from
refactor-command.py
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8896d2c
First crack at removing all the unneeded functionality
75cccba
`user` command now parses as it should.
535789a
Typo fix
a1c6c5a
Skeleton of parse_namespaces
f767578
add is-superuser and groups arguments to user_create_parser
ShaiahWren 5826c86
Merge remote-tracking branch 'upstream/refactor-command.py' into refa…
ShaiahWren 725276a
add parser and subparsers for namespace command
ShaiahWren b7ccfd7
Finish groups, begin on cli_functions that actually do stuff
1ced17d
add subparsers to collection parsers
ShaiahWren 4160672
add subparsers for containers and groups from upstream/refactor-comma…
ShaiahWren f2f54f8
fix imports errors and debug command.py
ShaiahWren 92e3160
adding the old command file so it's easily referenced
7ca66e4
Filled out user cli_functions.
7d9ef8f
Fix incorrect module call
d4e0a2b
Begin the url command in command.py
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| from client import GalaxyClient, GalaxyClientError | ||
| import containers | ||
| import collections | ||
| import groups | ||
| import namespaces | ||
| 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 | ||
| ) | ||
|
|
||
|
|
||
| ## users | ||
| 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 = groups.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 | ||
|
|
||
| ##groups | ||
|
|
||
| ##collections | ||
|
|
||
| ##namespaces |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the removal of relative imports here? I was going to suggest adding them in
cli_functions.pyand then see the opposite here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC we were having a weird import error with them - removing it resolved them, and I did a bit of research as to why I was seeing the error but couldn't suss it out.
Down the line once this is closer to merging we can add them back in and work out why they were causing an issue (or maybe the issue won't be reproducible.)