Skip to content

Commit

Permalink
fix(deployment): add phase filter for list_deployments
Browse files Browse the repository at this point in the history
(cherry picked from commit 9b8705f)
  • Loading branch information
ankitrgadiya authored and pallabpain committed Sep 4, 2024
1 parent 54fd266 commit a545c1e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 20 deletions.
6 changes: 5 additions & 1 deletion riocli/apply/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 19 additions & 7 deletions riocli/deployment/list.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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',
Expand All @@ -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:
Expand Down
70 changes: 58 additions & 12 deletions riocli/deployment/util.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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 = []
Expand Down

0 comments on commit a545c1e

Please sign in to comment.