Skip to content

Commit

Permalink
feat(static_route): uses v2 static route APIs
Browse files Browse the repository at this point in the history
This commit introduces support for V2 static routes
  • Loading branch information
RomilShah authored and pallabpain committed Aug 28, 2023
1 parent 5a0facc commit 17661c3
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 107 deletions.
6 changes: 3 additions & 3 deletions riocli/apply/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def _guid_functor(self, kind):
'secret': lambda x: munchify(x).guid,
"project": lambda x: munchify(x).metadata.guid,
"package": lambda x: munchify(x)['id'],
"staticroute": lambda x: munchify(x)['guid'],
"staticroute": lambda x: munchify(x)['metadata']['guid'],
"build": lambda x: munchify(x)['guid'],
"deployment": lambda x: munchify(x)['deploymentId'],
"network": lambda x: munchify(x).guid,
Expand All @@ -139,7 +139,7 @@ def _list_functors(self, kind):
'secret': self.client.list_secrets,
"project": self.v2client.list_projects,
"package": self.client.get_all_packages,
"staticroute": self.client.get_all_static_routes,
"staticroute": self.v2client.list_static_routes,
"build": self.client.list_builds,
"deployment": functools.partial(self.client.get_all_deployments,
phases=[DeploymentPhaseConstants.SUCCEEDED,
Expand All @@ -159,7 +159,7 @@ def _find_functors(self, kind):
"project": lambda name, projects: filter(lambda i: i.metadata.name == name, projects),
"package": lambda name, obj_list, version: filter(
lambda x: name == x.name and version == x['packageVersion'], obj_list),
"staticroute": lambda name, obj_list: filter(lambda x: name == '-'.join(x.urlPrefix.split('-')[:-1]),
"staticroute": lambda name, obj_list: filter(lambda x: name == '-'.join(x.spec.url.split('-')[:-1]),
obj_list),
"build": self._generate_find_guid_functor(name_field='buildName'),
"deployment": self._generate_find_guid_functor(),
Expand Down
18 changes: 18 additions & 0 deletions riocli/jsonschema/schemas/static_route-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ definitions:
const: StaticRoute
metadata:
"$ref": "#/definitions/metadata"
spec:
"$ref": "#/definitions/staticRouteSpec"
status:
"$ref": "#/definitions/staticRouteStatus"
required:
- apiVersion
- kind
Expand Down Expand Up @@ -47,3 +51,17 @@ definitions:
uuid:
type: string
pattern: "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
staticRouteSpec:
type: object
properties:
url:
type: string
staticRouteStatus:
type: object
properties:
status:
type: string
packageID:
type: string
deploymentID:
type: string
2 changes: 1 addition & 1 deletion riocli/static_route/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 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 Down
18 changes: 10 additions & 8 deletions riocli/static_route/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,32 @@
import click
from click_help_colors import HelpColorsCommand

from riocli.config import new_client
from riocli.config import new_v2_client
from riocli.constants import Colors, Symbols
from riocli.utils.spinner import with_spinner


@click.command(
'create',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.argument('prefix', type=str)
@click.argument('name', type=str)
@with_spinner(text="Creating static route...")
def create_static_route(prefix: str, spinner=None) -> None:
def create_static_route(name: str, spinner=None) -> None:
"""
Creates a new static route
"""
try:
client = new_client()
route = client.create_static_route(prefix)
client = new_v2_client(with_project=True)
payload = {
"metadata": {"name": name}
}
route = client.create_static_route(payload)
spinner.text = click.style(
'Static Route created successfully for URL {}'.format(route.urlString), fg=Colors.GREEN)
'Static Route created successfully for URL {}'.format(route.spec.url), fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
spinner.text = click.style('Failed to create static route: {}'.format(e), fg=Colors.RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1)
raise SystemExit(1) from e
16 changes: 6 additions & 10 deletions riocli/static_route/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
import click
from click_help_colors import HelpColorsCommand

from riocli.config import new_client
from riocli.config import new_v2_client
from riocli.constants import Colors, Symbols
from riocli.static_route.util import name_to_guid
from riocli.utils.spinner import with_spinner


@click.command(
'delete',
cls=HelpColorsCommand,
Expand All @@ -28,11 +26,9 @@
)
@click.option('--force', '-f', is_flag=True, default=False, help='Skip confirmation')
@click.argument('static-route', type=str)
@name_to_guid
@with_spinner(text="Deleting static route...")
def delete_static_route(
static_route: str,
static_route_guid: str,
force: bool,
spinner=None,
) -> None:
Expand All @@ -42,16 +38,16 @@ def delete_static_route(
with spinner.hidden():
if not force:
click.confirm(
'Deleting static route {} ({})'.format(
static_route, static_route_guid), abort=True)
'Deleting static route {}'.format(
static_route), abort=True)

try:
client = new_client()
client.delete_static_route(static_route_guid)
client = new_v2_client()
client.delete_static_route(static_route)
spinner.text = click.style(
'Static Route deleted successfully ', fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
spinner.text = click.style('Failed to delete static route: {}'.format(e), fg=Colors.RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1)
raise SystemExit(1) from e
28 changes: 6 additions & 22 deletions riocli/static_route/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
# limitations under the License.
import click
from click_help_colors import HelpColorsCommand
from rapyuta_io.clients.static_route import StaticRoute
from munch import unmunchify

from riocli.config import new_client
from riocli.config import new_v2_client
from riocli.constants import Colors
from riocli.static_route.util import name_to_guid
from riocli.utils import inspect_with_format


Expand All @@ -31,34 +30,19 @@
type=click.Choice(['json', 'yaml'], case_sensitive=True),
default='yaml')
@click.argument('static-route', type=str)
@name_to_guid
def inspect_static_route(
format_type: str,
static_route: str,
static_route_guid: str
) -> None:
"""
Inspect a static route
"""
try:
client = new_client()
route = client.get_static_route(static_route_guid)
data = make_static_route_inspectable(route)
inspect_with_format(data, format_type)
client = new_v2_client()
route = client.get_static_route(static_route)
inspect_with_format(unmunchify(route), format_type)
except Exception as e:
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1)
raise SystemExit(1) from e


def make_static_route_inspectable(static_route_data: StaticRoute) -> dict:
return {
'created_at': static_route_data.CreatedAt,
'updated_at': static_route_data.UpdatedAt,
'deleted_at': static_route_data.DeletedAt,
'guid': static_route_data.guid,
'url_prefix': static_route_data.urlPrefix,
'url': static_route_data.urlString,
'creator': static_route_data.creator,
'project': static_route_data.projectGUID,
'metadata': static_route_data.metadata.__dict__,
}
20 changes: 9 additions & 11 deletions riocli/static_route/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
from click_help_colors import HelpColorsCommand
from rapyuta_io.clients.static_route import StaticRoute

from riocli.config import new_client
from riocli.config import new_v2_client
from riocli.constants import Colors
from riocli.utils import tabulate_data


@click.command(
'list',
cls=HelpColorsCommand,
Expand All @@ -33,25 +32,24 @@ def list_static_routes() -> None:
List the static routes in the selected project
"""
try:
client = new_client()
routes = client.get_all_static_routes()
client = new_v2_client(with_project=True)
routes = client.list_static_routes()
_display_routes_list(routes)
except Exception as e:
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1)

raise SystemExit(1) from e

def _display_routes_list(routes: List[StaticRoute]) -> None:
headers = ['Route ID', 'Name', 'URL', 'Creator', 'CreatedAt']

data = []
for route in routes:
data.append([
route.guid,
route.urlPrefix,
route.urlString,
route.creator,
route.CreatedAt,
route.metadata.guid,
route.metadata.name,
route.spec.url,
route.metadata.creatorGUID,
route.metadata.createdAt,
])

tabulate_data(data, headers)
19 changes: 13 additions & 6 deletions riocli/static_route/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import typing
from munch import unmunchify

from rapyuta_io import Client
from rapyuta_io.clients.static_route import StaticRoute as v1StaticRoute

from riocli.config import new_v2_client
from riocli.jsonschema.validate import load_schema
from riocli.model import Model

Expand All @@ -29,21 +30,27 @@ def find_object(self, client: Client) -> bool:
'kind': 'staticroute',
'nameOrGUID': self.metadata.name
})

if not static_route:
return False

return static_route

def create_object(self, client: Client, **kwargs) -> v1StaticRoute:
static_route = client.create_static_route(self.metadata.name)
return static_route
def create_object(self, client: Client, **kwargs) -> typing.Any:
client = new_v2_client()

# convert to a dict and remove the ResolverCache
# field since it's not JSON serializable
self.pop("rc", None)
static_route = unmunchify(self)
r = client.create_static_route(static_route)
return unmunchify(r)

def update_object(self, client: Client, obj: typing.Any) -> None:
pass

def delete_object(self, client: Client, obj: typing.Any):
client.delete_static_route(obj.guid)
client = new_v2_client()
client.delete_static_route(obj.name)

@classmethod
def pre_process(cls, client: Client, d: typing.Dict) -> None:
Expand Down
12 changes: 5 additions & 7 deletions riocli/static_route/open.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
import click
from click_help_colors import HelpColorsCommand

from riocli.config import new_client
from riocli.config import new_v2_client
from riocli.constants import Colors
from riocli.static_route.util import name_to_guid


@click.command(
Expand All @@ -26,15 +25,14 @@
help_options_color=Colors.GREEN,
)
@click.argument('static-route', type=str)
@name_to_guid
def open_static_route(static_route, static_route_guid) -> None:
def open_static_route(static_route) -> None:
"""
Opens the static route in the default browser
"""
try:
client = new_client()
route = client.get_static_route(static_route_guid)
click.launch(url='https://{}'.format(route.urlString), wait=False)
client = new_v2_client()
route = client.get_static_route(static_route)
click.launch(url='https://{}'.format(route.spec.url), wait=False)
except Exception as e:
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1)
45 changes: 6 additions & 39 deletions riocli/static_route/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,17 @@
# 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

from rapyuta_io import Client

from riocli.config import new_client


def name_to_guid(f: typing.Callable) -> typing.Callable:
@functools.wraps(f)
def decorated(**kwargs: typing.Any):
client = new_client()
name = kwargs.pop('static_route')
guid = None

if name.startswith('staticroute-'):
guid = name
name = None

if name is None:
name = get_static_route_name(client, guid)

if guid is None:
guid = find_static_route_guid(client, name)

kwargs['static_route'] = name
kwargs['static_route_guid'] = guid
f(**kwargs)

return decorated


def get_static_route_name(client: Client, guid: str) -> str:
static_route = client.get_static_route(guid)
return static_route.urlPrefix.split("-")[0]
from riocli.config import new_v2_client


def find_static_route_guid(client: Client, name: str) -> str:
routes = client.get_all_static_routes()
for route in routes:
if route.urlPrefix == name or route.urlString == name:
return route.guid

raise StaticRouteNotFound()
client = new_v2_client(with_project=True)
static_route = client.get_static_route(name)
if not static_route:
raise StaticRouteNotFound()
return static_route.metadata.guid


class StaticRouteNotFound(Exception):
Expand Down
Loading

0 comments on commit 17661c3

Please sign in to comment.