Skip to content
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

vdk-control-cli: Adopt new auth exceptions #846

Merged
merged 1 commit into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Copyright 2021 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0
import sys

import click
from vdk.internal.control.auth.login_types import LoginTypes
from vdk.internal.control.exception.vdk_exception import VDKException
from vdk.internal.control.utils import cli_utils
from vdk.internal.control.utils.cli_utils import extended_option
from vdk.plugin.control_api_auth.auth_exception import VDKAuthException
from vdk.plugin.control_api_auth.auth_exception import VDKInvalidAuthParamError
from vdk.plugin.control_api_auth.authentication import Authentication


Expand Down Expand Up @@ -121,17 +124,16 @@ def login(
)
auth.authenticate()
click.echo("Login Successful")
except VDKInvalidAuthParamError:
click.echo(
f"Login type: {auth_type} requires arguments:, --client-secret, --client-id, "
"--oauth2-exchange-url, --oauth2-discovery-url"
)
sys.exit(1)
except VDKAuthException as e:
if (
"requires client_id and auth_discovery_url to be specified" in e.message
or "auth_url was not specified." in e.message
):
click.echo(
f"Login type: {auth_type} requires arguments:, --client-secret, --client-id, "
"--oauth2-exchange-url, --oauth2-discovery-url"
)
else:
click.echo(f"Login failed. Error was: {e.message}")
click.echo(f"Login failed. Error was: {e.message}")
sys.exit(1)

elif auth_type == LoginTypes.API_TOKEN.value:
api_token = cli_utils.get_or_prompt("Oauth2 API token", api_token)
if not api_token:
Expand All @@ -153,6 +155,7 @@ def login(
apikey_auth.authenticate()
except VDKAuthException as e:
click.echo(f"Login failed. Error was: {e.message}")
sys.exit(1)
click.echo("Login Successful")
else:
click.echo(f"Login type: {auth_type} not supported")
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def test_login_credentials_exceptions(httpserver: PluginHTTPServer):
"client_secret",
],
)
test_utils.assert_click_status(result, 1)
assert (
"requires arguments:, --client-secret, --client-id, --oauth2-exchange-url, --oauth2-discovery-url"
in result.output
Expand All @@ -185,6 +186,7 @@ def test_login_credentials_exceptions(httpserver: PluginHTTPServer):
"client_secret",
],
)
test_utils.assert_click_status(result, 1)
assert (
"requires arguments:, --client-secret, --client-id, --oauth2-exchange-url, --oauth2-discovery-url"
in result.output
Expand All @@ -204,6 +206,7 @@ def test_login_credentials_exceptions(httpserver: PluginHTTPServer):
"client_secret",
],
)
test_utils.assert_click_status(result, 1)
assert (
"requires arguments:, --client-secret, --client-id, --oauth2-exchange-url, --oauth2-discovery-url"
in result.output
Expand Down