Skip to content

Commit

Permalink
feat(cli): validate AccessKey and display the user info in "gas auth"
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallmallows committed Jul 22, 2021
1 parent 0a7e2bf commit 062a348
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions tensorbay/cli/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@

"""Implementation of gas auth."""


from configparser import ConfigParser
from textwrap import indent
from typing import Dict, Optional
from urllib.parse import urljoin

import click

from ..exception import UnauthorizedError
from .utility import (
error,
form_profile_value,
get_gas,
is_accesskey,
read_config,
update_config,
Expand Down Expand Up @@ -58,7 +59,6 @@ def _implement_auth( # pylint: disable=too-many-arguments
error(f'Invalid argument "{arg1}"')

_update_profile(config_parser, obj["profile_name"], arg1, arg2)
write_config(config_parser)


def _get_auth(obj: Dict[str, str], config_parser: ConfigParser, is_all: bool) -> None:
Expand Down Expand Up @@ -110,13 +110,29 @@ def _is_gas_url(arg: str) -> bool:


def _update_profile(config_parser: ConfigParser, profile_name: str, arg1: str, arg2: str) -> None:
gas_client = get_gas(arg2, arg1, profile_name) if arg2 else get_gas(arg1, arg2, profile_name)
try:
user_info = gas_client.get_user()
except UnauthorizedError:
error(f"{arg1} is not a valid AccessKey")

if not config_parser.has_section("profiles"):
config_parser.add_section("profiles")

config_parser["profiles"][profile_name] = (
form_profile_value(arg1) if not arg2 else form_profile_value(arg2, arg1)
)

write_config(config_parser, show_message=False)

messages = [f"\nSuccessfully set authentication info of {user_info.name}"]
if user_info.team:
messages.append(f" in {click.style(user_info.team.name, bold=True, fg='blue')} team")
if profile_name != "default":
messages.append(f" into profile {click.style(profile_name, bold=True, fg='blue')}")
messages.append("\n")
click.echo("".join(messages))


def _check_args_and_options(arg1: str, arg2: str, get: bool, unset: bool, is_all: bool) -> None:
if is_all and not (get or unset):
Expand Down

0 comments on commit 062a348

Please sign in to comment.