Skip to content

Commit

Permalink
refactor(apply): prints specific status messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabpain committed Sep 4, 2024
1 parent dbe83e4 commit 487705d
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 31 deletions.
29 changes: 20 additions & 9 deletions riocli/apply/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from riocli.apply.util import (get_model, init_jinja_environment, message_with_prompt, print_resolved_objects)
from riocli.config import Configuration
from riocli.constants import Colors, Symbols
from riocli.constants import Colors, Symbols, ApplyResult
from riocli.exceptions import ResourceNotFound
from riocli.utils import dump_all_yaml, print_centered_text, run_bash
from riocli.utils.graph import Graphviz
Expand Down Expand Up @@ -81,10 +81,10 @@ def apply(self, *args, **kwargs):

try:
apply_func(*args, **kwargs)
spinner.text = 'Apply successful.'
spinner.text = click.style('Apply successful.', fg=Colors.BRIGHT_GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
spinner.text = 'Apply failed. Error: {}'.format(e)
spinner.text = click.style('Apply failed. Error: {}'.format(e), fg=Colors.BRIGHT_RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1) from e

Expand Down Expand Up @@ -135,10 +135,10 @@ def delete(self, *args, **kwargs):

try:
delete_func(*args, **kwargs)
spinner.text = 'Delete successful.'
spinner.text = click.style('Delete successful.', fg=Colors.BRIGHT_GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
spinner.text = 'Delete failed. Error: {}'.format(e)
spinner.text = click.style('Delete failed. Error: {}'.format(e), fg=Colors.BRIGHT_RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1) from e

Expand Down Expand Up @@ -253,15 +253,24 @@ def _apply_manifest(self, obj_key: str, *args, **kwargs) -> None:
kls.validate(obj)
ist = kls(munchify(obj))

obj_key = click.style(obj_key, bold=True)

message_with_prompt("{} Applying {}...".format(
Symbols.WAITING, obj_key), fg=Colors.CYAN, spinner=spinner)

try:
result = ApplyResult.CREATED
if not dryrun:
ist.apply(*args, **kwargs)
result = ist.apply(*args, **kwargs)

message_with_prompt("{} Applied {}".format(
Symbols.SUCCESS, obj_key), fg=Colors.GREEN, spinner=spinner)
if result == ApplyResult.EXISTS:
message_with_prompt("{} {} already exists".format(
Symbols.INFO, obj_key), fg=Colors.WHITE, spinner=spinner)
return

message_with_prompt("{} {} {}".format(
Symbols.SUCCESS, result, obj_key),
fg=Colors.GREEN, spinner=spinner)
except Exception as ex:
message_with_prompt("{} Failed to apply {}. Error: {}".format(
Symbols.ERROR, obj_key, str(ex)), fg=Colors.RED, spinner=spinner)
Expand All @@ -277,6 +286,8 @@ def _delete_manifest(self, obj_key: str, *args, **kwargs) -> None:
kls.validate(obj)
ist = kls(munchify(obj))

obj_key = click.style(obj_key, bold=True)

message_with_prompt("{} Deleting {}...".format(
Symbols.WAITING, obj_key), fg=Colors.CYAN, spinner=spinner)

Expand All @@ -289,7 +300,7 @@ def _delete_manifest(self, obj_key: str, *args, **kwargs) -> None:
if not dryrun and can_delete:
ist.delete(*args, **kwargs)

message_with_prompt("{} Deleted {}.".format(
message_with_prompt("{} Deleted {}".format(
Symbols.SUCCESS, obj_key), fg=Colors.GREEN, spinner=spinner)
except ResourceNotFound:
message_with_prompt("{} {} not found".format(
Expand Down
6 changes: 3 additions & 3 deletions riocli/constants/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# limitations under the License.

from riocli.constants.colors import Colors
from riocli.constants.symbols import Symbols
from riocli.constants.regions import Regions
from riocli.constants.status import Status
from riocli.constants.status import Status, ApplyResult
from riocli.constants.symbols import Symbols

__all__ = [Colors, Symbols, Regions, Status]
__all__ = [Colors, Symbols, Regions, Status, ApplyResult]
24 changes: 24 additions & 0 deletions riocli/constants/status.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# 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.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.

from enum import Enum


Expand All @@ -8,3 +22,13 @@ def __str__(self):

RUNNING = 'Running'
AVAILABLE = 'Available'


class ApplyResult(str, Enum):

def __str__(self):
return str(self.value)

CREATED = 'Created'
UPDATED = 'Updated'
EXISTS = 'Exists'
7 changes: 4 additions & 3 deletions riocli/deployment/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from munch import unmunchify

from riocli.config import new_v2_client
from riocli.constants import Status
from riocli.constants import Status, ApplyResult
from riocli.exceptions import ResourceNotFound
from riocli.model import Model
from riocli.v2client import Client
Expand All @@ -29,7 +29,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.update(*args, **kwargs)

def apply(self, *args, **kwargs) -> typing.Any:
def apply(self, *args, **kwargs) -> ApplyResult:
hard_dependencies = [d.nameOrGUID for d in self.spec.get('depends', []) if d.get('wait', False)]

client = new_v2_client()
Expand All @@ -43,8 +43,9 @@ def apply(self, *args, **kwargs) -> typing.Any:

try:
client.create_deployment(unmunchify(self))
return ApplyResult.CREATED
except HttpAlreadyExistsError:
pass
return ApplyResult.EXISTS

def delete(self, *args, **kwargs):
client = new_v2_client()
Expand Down
13 changes: 10 additions & 3 deletions riocli/device/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from rapyuta_io.clients.device import Device as v1Device, DevicePythonVersion

from riocli.config import new_client
from riocli.constants import ApplyResult
from riocli.device.util import (DeviceNotFound, create_hwil_device, delete_hwil_device, execute_onboard_command,
find_device_by_name, make_device_labels_from_hwil_device)
from riocli.exceptions import ResourceNotFound
Expand All @@ -25,7 +26,7 @@ class Device(Model):
def __init__(self, *args, **kwargs):
self.update(*args, **kwargs)

def apply(self, *args, **kwargs) -> None:
def apply(self, *args, **kwargs) -> ApplyResult:
client = new_client()

device = None
Expand All @@ -39,11 +40,15 @@ def apply(self, *args, **kwargs) -> None:
if not self.spec.get('virtual', {}).get('enabled', False):
if device is None:
client.create_device(self.to_v1())
return
return ApplyResult.CREATED

return ApplyResult.EXISTS

# Return if the device is already online or initializing.
if device and device['status'] in ('ONLINE', 'INITIALIZING'):
return
return ApplyResult.EXISTS

result = ApplyResult.CREATED if device is None else ApplyResult.UPDATED

# Create the HWIL (virtual) device and then generate the labels
# to store HWIL metadata in rapyuta.io device.
Expand Down Expand Up @@ -74,6 +79,8 @@ def apply(self, *args, **kwargs) -> None:
onboard_command = onboard_script.full_command()
execute_onboard_command(hwil_response.id, onboard_command)

return result

def delete(self, *args, **kwargs) -> None:
client = new_client()

Expand Down
6 changes: 4 additions & 2 deletions riocli/disk/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from munch import unmunchify

from riocli.config import new_v2_client
from riocli.constants import ApplyResult
from riocli.exceptions import ResourceNotFound
from riocli.model import Model
from riocli.v2client.error import HttpAlreadyExistsError, HttpNotFoundError
Expand All @@ -25,7 +26,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.update(*args, **kwargs)

def apply(self, *args, **kwargs) -> None:
def apply(self, *args, **kwargs) -> ApplyResult:
client = new_v2_client()

self.metadata.createdAt = None
Expand All @@ -41,8 +42,9 @@ def apply(self, *args, **kwargs) -> None:
retry_count=retry_count,
sleep_interval=retry_interval,
)
return ApplyResult.CREATED
except HttpAlreadyExistsError:
pass
return ApplyResult.EXISTS
except Exception as e:
raise e

Expand Down
6 changes: 4 additions & 2 deletions riocli/managedservice/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from munch import unmunchify

from riocli.config import new_v2_client
from riocli.constants import ApplyResult
from riocli.exceptions import ResourceNotFound
from riocli.model import Model
from riocli.v2client.error import HttpAlreadyExistsError, HttpNotFoundError
Expand All @@ -25,13 +26,14 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.update(*args, **kwargs)

def apply(self, *args, **kwargs) -> None:
def apply(self, *args, **kwargs) -> ApplyResult:
client = new_v2_client()

try:
client.create_instance(unmunchify(self))
return ApplyResult.CREATED
except HttpAlreadyExistsError:
pass
return ApplyResult.EXISTS

def delete(self, *args, **kwargs) -> None:
client = new_v2_client()
Expand Down
3 changes: 2 additions & 1 deletion riocli/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from munch import Munch

from riocli.constants import ApplyResult
from riocli.jsonschema.validate import load_schema

DELETE_POLICY_LABEL = 'rapyuta.io/deletionPolicy'
Expand All @@ -41,7 +42,7 @@ def delete(self, *args, **kwargs):
schema that are defined in the schema files.
"""
@abstractmethod
def apply(self, *args, **kwargs) -> None:
def apply(self, *args, **kwargs) -> ApplyResult:
"""Create or update the object.
This method should be implemented by the subclasses. It should
Expand Down
4 changes: 3 additions & 1 deletion riocli/network/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from munch import unmunchify

from riocli.config import new_v2_client
from riocli.constants import ApplyResult
from riocli.exceptions import ResourceNotFound
from riocli.model import Model
from riocli.v2client.error import HttpAlreadyExistsError, HttpNotFoundError
Expand All @@ -37,8 +38,9 @@ def apply(self, *args, **kwargs) -> None:
try:
r = client.create_network(unmunchify(self))
client.poll_network(r.metadata.name, retry_count=retry_count, sleep_interval=retry_interval)
return ApplyResult.CREATED
except HttpAlreadyExistsError:
pass
return ApplyResult.EXISTS
except Exception as e:
raise e

Expand Down
6 changes: 4 additions & 2 deletions riocli/package/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from munch import unmunchify

from riocli.config import new_v2_client
from riocli.constants import ApplyResult
from riocli.exceptions import ResourceNotFound
from riocli.model import Model
from riocli.package.enum import RestartPolicy
Expand All @@ -33,15 +34,16 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.update(*args, **kwargs)

def apply(self, *args, **kwargs) -> None:
def apply(self, *args, **kwargs) -> ApplyResult:
client = new_v2_client()

package = self._sanitize_package()

try:
client.create_package(package)
return ApplyResult.CREATED
except HttpAlreadyExistsError:
pass
return ApplyResult.EXISTS

def delete(self, *args, **kwargs) -> None:
client = new_v2_client()
Expand Down
6 changes: 4 additions & 2 deletions riocli/project/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from waiting import wait

from riocli.config import Configuration, new_v2_client
from riocli.constants import ApplyResult
from riocli.exceptions import ResourceNotFound
from riocli.model import Model
from riocli.project.util import ProjectNotFound, find_project_guid
Expand All @@ -30,7 +31,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.update(*args, **kwargs)

def apply(self, *args, **kwargs) -> None:
def apply(self, *args, **kwargs) -> ApplyResult:
client = new_v2_client()

project = unmunchify(self)
Expand All @@ -42,10 +43,11 @@ def apply(self, *args, **kwargs) -> None:
r = client.create_project(project)
wait(self.is_ready, timeout_seconds=PROJECT_READY_TIMEOUT,
sleep_seconds=(1, 30, 2))
return ApplyResult.CREATED
except HttpAlreadyExistsError:
guid = find_project_guid(client, self.metadata.name, Configuration().organization_guid)
client.update_project(guid, project)
return
return ApplyResult.UPDATED
except Exception as e:
raise e

Expand Down
3 changes: 3 additions & 0 deletions riocli/secret/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from munch import unmunchify

from riocli.config import new_v2_client
from riocli.constants import ApplyResult
from riocli.exceptions import ResourceNotFound
from riocli.model import Model
from riocli.v2client.error import HttpAlreadyExistsError, HttpNotFoundError
Expand All @@ -32,8 +33,10 @@ def apply(self, *args, **kwargs) -> None:

try:
client.create_secret(unmunchify(self))
return ApplyResult.CREATED
except HttpAlreadyExistsError:
client.update_secret(self.metadata.name, secret)
return ApplyResult.UPDATED

def delete(self, *args, **kwargs) -> None:
client = new_v2_client()
Expand Down
5 changes: 4 additions & 1 deletion riocli/static_route/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from munch import unmunchify

from riocli.config import Configuration, new_v2_client
from riocli.constants import ApplyResult
from riocli.exceptions import ResourceNotFound
from riocli.model import Model
from riocli.v2client.error import HttpAlreadyExistsError, HttpNotFoundError
Expand All @@ -24,15 +25,17 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.update(*args, **kwargs)

def apply(self, *args, **kwargs) -> None:
def apply(self, *args, **kwargs) -> ApplyResult:
client = new_v2_client()

static_route = unmunchify(self)

try:
client.create_static_route(static_route)
return ApplyResult.CREATED
except HttpAlreadyExistsError:
client.update_static_route(self.metadata.name, static_route)
return ApplyResult.UPDATED

def delete(self, *args, **kwargs) -> None:
client = new_v2_client()
Expand Down
Loading

0 comments on commit 487705d

Please sign in to comment.