From 206a024cbc5bac96b52e8df4d453ca3a9c2ed4ed Mon Sep 17 00:00:00 2001 From: RomilShah Date: Mon, 21 Aug 2023 11:15:15 +0530 Subject: [PATCH] fix(secrets): improves code quality --- riocli/apply/resolver.py | 2 +- riocli/secret/delete.py | 6 ++--- riocli/secret/inspect.py | 4 +--- riocli/secret/util.py | 47 ++++------------------------------------ 4 files changed, 8 insertions(+), 51 deletions(-) diff --git a/riocli/apply/resolver.py b/riocli/apply/resolver.py index 79fb9772..c02f5206 100644 --- a/riocli/apply/resolver.py +++ b/riocli/apply/resolver.py @@ -136,7 +136,7 @@ def _guid_functor(self, kind): def _list_functors(self, kind): mapping = { - 'secret': self.client.list_secrets, + 'secret': self.v2client.list_secrets, "project": self.v2client.list_projects, "package": self.client.get_all_packages, "staticroute": self.client.get_all_static_routes, diff --git a/riocli/secret/delete.py b/riocli/secret/delete.py index a8998631..0fbc1b4b 100644 --- a/riocli/secret/delete.py +++ b/riocli/secret/delete.py @@ -16,7 +16,6 @@ from riocli.config import new_v2_client from riocli.constants import Colors, Symbols -from riocli.secret.util import name_to_guid from riocli.utils.spinner import with_spinner @click.command( @@ -28,16 +27,15 @@ @click.option('--force', '-f', '--silent', 'force', is_flag=True, default=False, help='Skip confirmation') @click.argument('secret-name', type=str) -@name_to_guid @with_spinner(text='Deleting secret...') -def delete_secret(force: str, secret_name: str, secret_guid: str, spinner=None) -> None: +def delete_secret(force: str, secret_name: str, spinner=None) -> None: """ Deletes a secret """ with spinner.hidden(): if not force: click.confirm( - 'Deleting secret {} ({})'.format(secret_name, secret_guid), + 'Deleting secret {} '.format(secret_name), abort=True) try: diff --git a/riocli/secret/inspect.py b/riocli/secret/inspect.py index 278f4ac5..80c76ce6 100644 --- a/riocli/secret/inspect.py +++ b/riocli/secret/inspect.py @@ -17,7 +17,6 @@ from riocli.config import new_v2_client from riocli.constants import Colors -from riocli.secret.util import name_to_guid from riocli.utils import inspect_with_format @@ -30,8 +29,7 @@ @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: +def inspect_secret(format_type: str, secret_name: str) -> None: """ Inspect a secret """ diff --git a/riocli/secret/util.py b/riocli/secret/util.py index 5dfdc21d..80e2b946 100644 --- a/riocli/secret/util.py +++ b/riocli/secret/util.py @@ -11,47 +11,13 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import functools -import typing -import click from rapyuta_io import Client -from riocli.config import new_v2_client -from riocli.constants import Colors - -def name_to_guid(f: typing.Callable) -> typing.Callable: - @functools.wraps(f) - def decorated(**kwargs: typing.Any): - try: - client = new_v2_client() - except Exception as e: - click.secho(str(e), fg=Colors.RED) - raise SystemExit(1) - - name = kwargs.pop('secret_name') - guid = None - - if name.startswith('secret-') and len(name) == 31: - guid = name - name = None - - if name is None: - name = get_secret_name(client, guid) - - if guid is None: - try: - guid = find_secret_guid(client, name) - except Exception as e: - click.secho(str(e), fg='red') - raise SystemExit(1) - - kwargs['secret_name'] = name - kwargs['secret_guid'] = guid - f(**kwargs) - - return decorated - +class SecretNotFound(Exception): + def __init__(self, message='secret not found'): + self.message = message + super().__init__(self.message) def find_secret_guid(client: Client, name: str) -> str: secret = client.get_secret(name) @@ -66,8 +32,3 @@ def get_secret_name(client: Client, name: str) -> str: raise SecretNotFound() - -class SecretNotFound(Exception): - def __init__(self, message='secret not found'): - self.message = message - super().__init__(self.message)