From a545c1ef1c8ff9dcab9a58ba7de79b5880af6dbe Mon Sep 17 00:00:00 2001 From: Ankit R Gadiya Date: Thu, 27 Jun 2024 23:13:27 +0530 Subject: [PATCH] fix(deployment): add phase filter for list_deployments (cherry picked from commit 9b8705f1fb56d1dc636f3e0a3fe9368c77176bd8) --- riocli/apply/resolver.py | 6 +++- riocli/deployment/list.py | 26 +++++++++++---- riocli/deployment/util.py | 70 ++++++++++++++++++++++++++++++++------- 3 files changed, 82 insertions(+), 20 deletions(-) diff --git a/riocli/apply/resolver.py b/riocli/apply/resolver.py index 4f54e89a..529e9bbd 100644 --- a/riocli/apply/resolver.py +++ b/riocli/apply/resolver.py @@ -138,7 +138,11 @@ def _list_functors(self, kind): "staticroute": self.v2client.list_static_routes, "disk": self.v2client.list_disks, "network": self.v2client.list_networks, - "deployment": functools.partial(self.v2client.list_deployments), + "deployment": functools.partial(self.v2client.list_deployments, + query={'phases': ['InProgress', + 'Succeeded', + 'FailedToStart', + 'Provisioning']}), "device": self.client.get_all_devices, "managedservice": self._list_managedservices, "usergroup": self.client.list_usergroups diff --git a/riocli/deployment/list.py b/riocli/deployment/list.py index cb79c0b9..7221aa8d 100644 --- a/riocli/deployment/list.py +++ b/riocli/deployment/list.py @@ -1,4 +1,4 @@ -# Copyright 2023 Rapyuta Robotics +# Copyright 2024 Rapyuta Robotics # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -16,14 +16,26 @@ import click from click_help_colors import HelpColorsCommand -from riocli.deployment.model import Deployment -from riocli.deployment.util import ALL_PHASES, DEFAULT_PHASES - from riocli.config import new_v2_client from riocli.constants import Colors -from riocli.deployment.util import process_deployment_errors +from riocli.deployment.model import Deployment from riocli.utils import tabulate_data +ALL_PHASES = [ + 'InProgress', + 'Provisioning', + 'Succeeded', + 'FailedToStart', + 'Stopped', +] + +DEFAULT_PHASES = [ + 'InProgress', + 'Provisioning', + 'Succeeded', + 'FailedToStart', +] + @click.command( 'list', @@ -48,8 +60,8 @@ def list_deployments( List the deployments in the selected project """ try: - client = new_v2_client() - deployments = client.list_deployments(query={"phases": phase, "deviceName": device}) + client = new_v2_client(with_project=True) + deployments = client.list_deployments(query={'phases': phase}) deployments = sorted(deployments, key=lambda d: d.metadata.name.lower()) display_deployment_list(deployments, show_header=True) except Exception as e: diff --git a/riocli/deployment/util.py b/riocli/deployment/util.py index d060c7b5..d2212b91 100644 --- a/riocli/deployment/util.py +++ b/riocli/deployment/util.py @@ -1,4 +1,4 @@ -# Copyright 2023 Rapyuta Robotics +# Copyright 2024 Rapyuta Robotics # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,10 +13,15 @@ # limitations under the License. import functools import re -from typing import List +import typing -from riocli.config import new_client -from riocli.deployment.model import Deployment +import click +from rapyuta_io import DeploymentPhaseConstants +from rapyuta_io.clients.deployment import Deployment + +from riocli.config import new_client, new_v2_client +from riocli.constants import Colors +from riocli.deployment.list import DEFAULT_PHASES from riocli.utils import tabulate_data from riocli.utils.selector import show_selection from riocli.v2client import Client @@ -30,11 +35,52 @@ DeploymentPhaseConstants.DeploymentPhaseStopped, ] -DEFAULT_PHASES = [ - DeploymentPhaseConstants.DeploymentPhaseInProgress, - DeploymentPhaseConstants.DeploymentPhaseProvisioning, - DeploymentPhaseConstants.DeploymentPhaseSucceeded, -] + +def name_to_guid(f: typing.Callable) -> typing.Callable: + @functools.wraps(f) + def decorated(**kwargs: typing.Any) -> None: + try: + client = new_v2_client() + except Exception as e: + click.secho(str(e), fg=Colors.RED) + raise SystemExit(1) from e + + name = kwargs.pop('deployment_name') + guid = None + + if name.startswith('dep-'): + guid = name + name = None + + try: + if name is None: + name = get_deployment_name(client, guid) + + if guid is None: + guid = get_deployment_guid(client, name) + + except Exception as e: + click.secho(str(e), fg=Colors.RED) + raise SystemExit(1) from e + + kwargs['deployment_name'] = name + kwargs['deployment_guid'] = guid + f(**kwargs) + + return decorated + + +def get_deployment_guid(client: Client, name: str) -> str: + deployment = client.get_deployment(name) + return deployment.metadata.guid + + +def get_deployment_name(client: Client, guid: str) -> str: + deployments = client.list_deployments(query={'guids': [guid]}) + if len(deployments) == 0: + raise DeploymentNotFound + + return deployments[0].metadata.name def select_details(deployment_guid, component_name=None, exec_name=None) -> (str, str, str): @@ -76,8 +122,8 @@ def fetch_deployments( client: Client, deployment_name_or_regex: str, include_all: bool, -) -> List[Deployment]: - deployments = client.list_deployments(query={"phases": DEFAULT_PHASES}) +) -> typing.List[Deployment]: + deployments = client.list_deployments(query={'phases': DEFAULT_PHASES}) result = [] for deployment in deployments: if (include_all or deployment_name_or_regex == deployment.metadata.name or @@ -89,7 +135,7 @@ def fetch_deployments( return result -def print_deployments_for_confirmation(deployments: List[Deployment]): +def print_deployments_for_confirmation(deployments: typing.List[Deployment]): headers = ['Name', 'GUID', 'Phase', 'Status'] data = []