-
Notifications
You must be signed in to change notification settings - Fork 5
Cli project features #31
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
337bdd1
Added some commands to work with new api
3378b1c
Module import fixed
e9a952d
Modules import for python3 fixes
7a2da9c
Merge branch 'testing' of https://github.com/infrabox/cli into cli_pr…
sept-en ce3a883
Minor improvements and style fixes.
sept-en 9ff9730
Merge branch 'master' of https://github.com/infrabox/cli into cli_pro…
sept-en 1127ca6
New CLI API: new commands and improvements.
sept-en 0a575f0
New CLI API: minor improvements.
sept-en 5f843f2
New CLI API: Refactoring and typo fixes.
sept-en df4b6ed
New CLI API: minor improvements. Refactoring.
sept-en 3f57f97
New CLI API: project-token API improvements.
sept-en 998959b
(ref #30) New CLI API: improvements.
sept-en 08832e5
New CLI API: replaced dummy local api url.
sept-en bf0d25d
New CLI API: project-tokens' deletion refactoring.
sept-en 732e0d1
Updated .gitignore
sept-en 6fffbf2
New CLI API: replaced dummy local API url.
sept-en 73631f8
New CLI API: collaborators' deletion refactoring.
sept-en c009ab8
New CLI API: project-token API changes.
sept-en 5e74195
Description fixes.
sept-en 6664d13
Removed redundant and deprecated method.
sept-en 338a400
New CLI API: login API improved.
sept-en 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
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
Empty file.
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,24 @@ | ||
| import requests | ||
|
|
||
| session = requests.Session() | ||
|
|
||
|
|
||
| def get(url, headers=None, cookies_handler=None): | ||
| response = session.get(url, headers=headers) | ||
| if cookies_handler: | ||
| cookies_handler(session.cookies.get_dict()) | ||
| return response | ||
|
|
||
|
|
||
| def post(url, data, headers=None, cookies_handler=None): | ||
| response = session.post(url, json=data, headers=headers) | ||
| if cookies_handler: | ||
| cookies_handler(session.cookies.get_dict()) | ||
| return response | ||
|
|
||
|
|
||
| def delete(url, headers=None, cookies_handler=None): | ||
| response = session.delete(url, headers=headers) | ||
| if cookies_handler: | ||
| cookies_handler(session.cookies.get_dict()) | ||
| return response |
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,43 @@ | ||
| import pickle | ||
| from os.path import expanduser | ||
|
|
||
| home = expanduser("~") | ||
|
|
||
| def save_user_token(dict): | ||
| try: | ||
| f = open(home + '/.infra.data', 'rb') | ||
| obj = pickle.load(f) | ||
| f.close() | ||
|
|
||
| except: | ||
| obj = {} | ||
|
|
||
| obj['current_user_token'] = dict['token'] | ||
|
|
||
| f = open(home + '/.infra.data', 'wb') | ||
| pickle.dump(obj, f) | ||
| f.close() | ||
|
|
||
|
|
||
| def load_current_user_token(): | ||
| try: | ||
| f = open(home + '/.infra.data', 'rb') | ||
| obj = pickle.load(f) | ||
| f.close() | ||
| curr_token = obj['current_user_token'] | ||
| except: | ||
| raise EnvironmentError('Could not load current user token. Please log in') | ||
|
|
||
| return curr_token | ||
|
|
||
|
|
||
| def load_current_project_token(): | ||
| try: | ||
| f = open(home + '/.infra.data', 'rb') | ||
| obj = pickle.load(f) | ||
| f.close() | ||
| curr_token = obj['current_project_token'] | ||
| except: | ||
| raise EnvironmentError('Could not load current project token. Please set current project') | ||
|
|
||
| return curr_token |
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,166 @@ | ||
| from infraboxcli.dashboard.cli_client import get, post, delete | ||
| from infraboxcli.dashboard.user import get_user_headers | ||
| import infraboxcli.env | ||
|
|
||
| api_projects_endpoint_url = '/api/v1/projects/' | ||
|
|
||
|
|
||
| def delete_project(args): | ||
| infraboxcli.env.check_env_cli_token(args) | ||
| url = args.url + api_projects_endpoint_url + args.project_id | ||
| response = get(url, get_user_headers()) | ||
|
|
||
| return response | ||
|
|
||
|
|
||
| def list_collaborators(args): | ||
| infraboxcli.env.check_env_cli_token(args) | ||
| url = args.url + api_projects_endpoint_url + args.project_id + '/collaborators' | ||
| response = get(url, get_user_headers()) | ||
|
|
||
| if args.verbose: | ||
| print('=== Collaborators ===') | ||
| for collaborator in response.json(): | ||
| print('Username: %s' % collaborator['username']) | ||
| print('E-mail: %s' % collaborator['email']) | ||
| print('---') | ||
|
|
||
| return response | ||
|
|
||
|
|
||
| def add_collaborator(args): | ||
| infraboxcli.env.check_env_cli_token(args) | ||
| url = args.url + api_projects_endpoint_url + args.project_id + '/collaborators' | ||
| data = { 'username': args.username } | ||
|
|
||
| response = post(url, data, get_user_headers()) | ||
| print(response.json()['message']) | ||
|
|
||
| return response | ||
|
|
||
|
|
||
| def remove_collaborator(args): | ||
| infraboxcli.env.check_env_cli_token(args) | ||
|
|
||
| args.verbose = False | ||
| all_project_collaborators = list_collaborators(args).json() | ||
| collaborator_id = None | ||
| for collaborator in all_project_collaborators: | ||
| if collaborator['username'] == args.username: | ||
| collaborator_id = collaborator['id'] | ||
| break | ||
|
|
||
| if collaborator_id is None: | ||
| print('Specified user is not in collaborators list.') | ||
| return | ||
|
|
||
| url = args.url + api_projects_endpoint_url + args.project_id + '/collaborators/' + collaborator_id | ||
| response = delete(url, get_user_headers()) | ||
| print(response.json()['message']) | ||
|
|
||
| return response | ||
|
|
||
|
|
||
| def add_secret(args): | ||
| infraboxcli.env.check_env_cli_token(args) | ||
| url = args.url + api_projects_endpoint_url + args.project_id + '/secrets' | ||
| data = {'name': args.name, 'value': args.value} | ||
|
|
||
| response = post(url, data, get_user_headers()) | ||
|
|
||
| return response | ||
|
|
||
|
|
||
| def delete_secret(args): | ||
| infraboxcli.env.check_env_cli_token(args) | ||
| url = args.url + api_projects_endpoint_url + args.project_id + '/secrets/' + args.name | ||
| response = delete(url, get_user_headers()) | ||
|
|
||
| return response | ||
|
|
||
|
|
||
| def list_project_tokens(args): | ||
| infraboxcli.env.check_env_cli_token(args) | ||
| url = args.url + api_projects_endpoint_url + args.project_id + '/tokens' | ||
|
|
||
| response = get(url, get_user_headers()) | ||
| if args.verbose: | ||
| print('=== Project tokens ===') | ||
| for project_token in response.json(): | ||
| print('Description: %s' % project_token['description']) | ||
| print('Id: %s' % project_token['id']) | ||
| print('Scope push: %s' % project_token['scope_push']) | ||
| print('Scope pull: %s' % project_token['scope_pull']) | ||
| print('---') | ||
|
|
||
| return response | ||
|
|
||
|
|
||
| def get_project_token_id_by_description(args): | ||
| args.verbose = False | ||
| all_project_tokens = list_project_tokens(args).json() | ||
|
|
||
| for project_token in all_project_tokens: | ||
| if args.description == project_token['description']: | ||
| return project_token['id'] | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| def add_project_token(args): | ||
| infraboxcli.env.check_env_cli_token(args) | ||
| url = args.url + api_projects_endpoint_url + args.project_id + '/tokens' | ||
|
|
||
| data = { | ||
| 'description': args.description, | ||
| #TODO<Steffen> when scope push/pull functionality is implemented, | ||
| # delete following 2 lines and uncomment next 2 lines | ||
| 'scope_push': True, | ||
| 'scope_pull': True | ||
| #'scope_push': args.scope_push, | ||
| #'scope_pull': args.scope_pull | ||
| } | ||
|
|
||
| response = post(url, data, get_user_headers()) | ||
|
|
||
| if response.status_code != 200: | ||
| print(response.json()['message']) | ||
| return | ||
|
|
||
| # Print project token to the CLI | ||
| print('=== Authentication Token ===') | ||
| print('Please save your token at a secure place. We will not show it to you again.\n\n') | ||
| print(response.json()['data']['token']) | ||
|
|
||
| return response | ||
|
|
||
|
|
||
| def delete_project_token(args): | ||
| if args.id: | ||
| delete_project_token_by_id(args) | ||
| elif args.description: | ||
| delete_project_token_by_description(args) | ||
| else: | ||
| print('Please, provide either token id or description.') | ||
|
|
||
|
|
||
| def delete_project_token_by_description(args): | ||
| infraboxcli.env.check_env_cli_token(args) | ||
| token_id = get_project_token_id_by_description(args) | ||
|
|
||
| if not token_id: | ||
| print('Token with such a description does not exist.') | ||
| return | ||
|
|
||
| args.id = token_id | ||
| return delete_project_token_by_id(args) | ||
|
|
||
|
|
||
| def delete_project_token_by_id(args): | ||
| infraboxcli.env.check_env_cli_token(args) | ||
| url = args.url + api_projects_endpoint_url + args.project_id + '/tokens/' + args.id | ||
| response = delete(url, get_user_headers()) | ||
|
|
||
| print(response.json()['message']) | ||
|
|
||
| return response |
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,32 @@ | ||
| import getpass | ||
| import json | ||
|
|
||
| from infraboxcli.dashboard.cli_client import post, get | ||
| from infraboxcli.dashboard.external import load_current_user_token, save_user_token | ||
|
|
||
| api_endpoint_url = '/api/v1/' | ||
|
|
||
| def get_user_token(): | ||
| return load_current_user_token() | ||
|
|
||
| def get_user_headers(): | ||
| return {'Authorization': 'token %s' % get_user_token()} | ||
|
|
||
| def login(args): | ||
| email = args.email | ||
| password = args.password | ||
|
|
||
| if email is None: | ||
| email = raw_input("Email: ") | ||
| # Don't allow to pass password without email | ||
| password = None | ||
|
|
||
| if password is None: | ||
| password = getpass.getpass('Password: ') | ||
|
|
||
| data = { "email": email, "password": password} | ||
|
|
||
| url = args.url + api_endpoint_url + 'account/login' | ||
| response = post(url, data, cookies_handler=save_user_token) | ||
|
|
||
| return response |
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.
add optional parameters for email and password so we can use it in scripts