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 cc0d4b8 commit 47a7889
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 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 ResponseSystemError
from .utility import (
error,
form_profile_value,
get_gas,
is_accesskey,
read_config,
update_config,
Expand Down Expand Up @@ -57,8 +58,12 @@ def _implement_auth( # pylint: disable=too-many-arguments
else:
error(f'Invalid argument "{arg1}"')

if is_accesskey(arg1):
_check_auth(arg1, arg2, obj["profile_name"])
else:
_check_auth(arg2, arg1, obj["profile_name"])
_update_profile(config_parser, obj["profile_name"], arg1, arg2)
write_config(config_parser)
write_config(config_parser, show_message=False)


def _get_auth(obj: Dict[str, str], config_parser: ConfigParser, is_all: bool) -> None:
Expand Down Expand Up @@ -132,3 +137,18 @@ def _check_args_and_options(arg1: str, arg2: str, get: bool, unset: bool, is_all
def _echo_formatted_profile(name: str, value: str) -> None:
formatted_value = indent(value, INDENT)
click.echo(f"{name} = {formatted_value}\n")


def _check_auth(access_key: str, url: str, profile_name: str) -> None:
gas_client = get_gas(access_key, url, profile_name)
try:
userinfo = gas_client.get_user()
except ResponseSystemError:
error(f"{access_key} is not a valid AccessKey")
else:
message = f"\nSuccessfully set authentication info of {userinfo.name}"
if userinfo.team:
message += f" in {click.style(userinfo.team.name, bold=True, fg='blue')} team"
if profile_name != "default":
message += f" into profile {click.style(profile_name, bold=True, fg='blue')}"
click.echo(f"{message}\n")

0 comments on commit 47a7889

Please sign in to comment.