Skip to content

Commit

Permalink
feat(secret): updates spinner and refactors existing commands
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabpain committed Aug 3, 2023
1 parent dd33d75 commit 0949154
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 35 deletions.
7 changes: 4 additions & 3 deletions riocli/secret/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -14,6 +14,7 @@
import click
from click_help_colors import HelpColorsGroup

from riocli.constants import Colors
from riocli.secret.create import create_secret
from riocli.secret.delete import delete_secret
from riocli.secret.import_secret import import_secret
Expand All @@ -24,8 +25,8 @@
@click.group(
invoke_without_command=False,
cls=HelpColorsGroup,
help_headers_color='yellow',
help_options_color='green',
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
def secret() -> None:
"""
Expand Down
37 changes: 24 additions & 13 deletions riocli/secret/delete.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,29 +12,40 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from click_spinner import spinner

from riocli.config import new_client
from riocli.constants import Colors, Symbols
from riocli.secret.util import name_to_guid
from riocli.utils.spinner import with_spinner
from click_help_colors import HelpColorsCommand


@click.command('delete')
@click.command(
'delete',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.option('--force', '-f', '--silent', 'force', is_flag=True,
default=False, help='Skip confirmation')
@click.argument('secret-name', type=str)
@name_to_guid
def delete_secret(force: str, secret_name: str, secret_guid: str) -> None:
@with_spinner(text='Deleting secret...')
def delete_secret(force: str, secret_name: str, secret_guid: str, spinner=None) -> None:
"""
Deletes the secret resource from the Platform
Deletes a secret
"""
if not force:
click.confirm('Deleting secret {} ({})'.format(secret_name, secret_guid), abort=True)
with spinner.hidden():
if not force:
click.confirm(
'Deleting secret {} ({})'.format(secret_name, secret_guid),
abort=True)

try:
client = new_client()
with spinner():
client.delete_secret(secret_guid)
click.secho('Secret deleted successfully!', fg='green')
client.delete_secret(secret_guid)
spinner.text = click.style('Secret deleted successfully.', fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
click.secho(str(e), fg='red')
raise SystemExit(1)
spinner.text = click.style('Failed to delete secret: {}'.format(e), fg=Colors.RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1) from e
15 changes: 11 additions & 4 deletions riocli/secret/inspect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,29 +12,36 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from click_help_colors import HelpColorsCommand
from rapyuta_io import Secret

from riocli.config import new_client
from riocli.constants import Colors
from riocli.secret.util import name_to_guid
from riocli.utils import inspect_with_format


@click.command('inspect')
@click.command(
'inspect',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.option('--format', '-f', 'format_type', default='yaml',
type=click.Choice(['json', 'yaml'], case_sensitive=False))
@click.argument('secret-name', type=str)
@name_to_guid
def inspect_secret(format_type: str, secret_name: str, secret_guid: str) -> None:
"""
Inspect the secret resource
Inspect a secret
"""
try:
client = new_client()
secret = client.get_secret(secret_guid)
data = make_secret_inspectable(secret)
inspect_with_format(data, format_type)
except Exception as e:
click.secho(str(e), fg='red')
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1)


Expand Down
16 changes: 11 additions & 5 deletions riocli/secret/list.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -18,9 +18,15 @@

from riocli.config import new_client
from riocli.utils import tabulate_data


@click.command('list')
from riocli.constants import Colors
from click_help_colors import HelpColorsCommand

@click.command(
'list',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.option('--secret-type', '-t', default=['docker', 'source'], multiple=True,
help='Types to filter the list of Secret [default: docker,source]')
def list_secrets(secret_type: typing.Union[str, typing.Tuple[str]]) -> None:
Expand All @@ -33,7 +39,7 @@ def list_secrets(secret_type: typing.Union[str, typing.Tuple[str]]) -> None:
secrets = sorted(secrets, key=lambda s: s.name.lower())
_display_secret_list(secrets, secret_type, show_header=True)
except Exception as e:
click.secho(str(e), fg='red')
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1)


Expand Down
34 changes: 24 additions & 10 deletions riocli/secret/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,22 +13,28 @@
# limitations under the License.
import typing

from rapyuta_io import Secret as v1Secret, SecretConfigDocker, \
SecretConfigSourceBasicAuth, \
SecretConfigSourceSSHAuth, Client
from rapyuta_io import (
Client,
Secret as v1Secret,
SecretConfigDocker,
SecretConfigSourceBasicAuth,
SecretConfigSourceSSHAuth,
)

from riocli.jsonschema.validate import load_schema
from riocli.model import Model


class Secret(Model):

def __init__(self, *args, **kwargs):
self.update(*args, **kwargs)

def find_object(self, client: Client) -> bool:
_, secret = self.rc.find_depends(
{'kind': 'secret', 'nameOrGUID': self.metadata.name})
_, secret = self.rc.find_depends({
'kind': 'secret',
'nameOrGUID': self.metadata.name
})

if not secret:
return False

Expand All @@ -51,16 +57,24 @@ def to_v1(self) -> v1Secret:
return self._git_secret_to_v1()

def _docker_secret_to_v1(self) -> v1Secret:
config = SecretConfigDocker(self.spec.docker.username, self.spec.docker.password, self.spec.docker.email,
self.spec.docker.registry)
config = SecretConfigDocker(
self.spec.docker.username,
self.spec.docker.password,
self.spec.docker.email,
self.spec.docker.registry,
)
return v1Secret(self.metadata.name, config)

def _git_secret_to_v1(self) -> v1Secret:
if self.spec.git.authMethod == 'SSH Auth':
config = SecretConfigSourceSSHAuth(self.spec.git.privateKey)
elif self.spec.git.authMethod == 'HTTP/S Basic Auth':
ca_cert = self.spec.git.get('ca_cert', None)
config = SecretConfigSourceBasicAuth(self.spec.git.username, self.spec.git.password, ca_cert=ca_cert)
config = SecretConfigSourceBasicAuth(
self.spec.git.username,
self.spec.git.password,
ca_cert=ca_cert
)
elif self.spec.git.authMethod == 'HTTP/S Token Auth':
# TODO(ankit): Implement it once SDK has support for it.
raise Exception('token-based secret is not supported yet!')
Expand Down

0 comments on commit 0949154

Please sign in to comment.