Skip to content

Commit

Permalink
fix(secrets): improves code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
RomilShah committed Aug 23, 2023
1 parent b8d2e99 commit 206a024
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 51 deletions.
2 changes: 1 addition & 1 deletion riocli/apply/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions riocli/secret/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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:
Expand Down
4 changes: 1 addition & 3 deletions riocli/secret/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
"""
Expand Down
47 changes: 4 additions & 43 deletions riocli/secret/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

0 comments on commit 206a024

Please sign in to comment.