Skip to content

Commit

Permalink
feat(staticroutes): adds v2 static routes
Browse files Browse the repository at this point in the history
This commit introduces support for V2 static routes
  • Loading branch information
RomilShah committed Jun 30, 2023
1 parent 32eab78 commit 74220ac
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 95 deletions.
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
7 changes: 4 additions & 3 deletions 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 All @@ -14,6 +14,7 @@
import click
from click_help_colors import HelpColorsGroup

from riocli.constants import Colors
from riocli.static_route.create import create_static_route
from riocli.static_route.delete import delete_static_route
from riocli.static_route.inspect import inspect_static_route
Expand All @@ -24,8 +25,8 @@
@click.group(
invoke_without_command=False,
cls=HelpColorsGroup,
help_headers_color='yellow',
help_options_color='green',
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
def static_route() -> None:
"""
Expand Down
33 changes: 22 additions & 11 deletions riocli/static_route/create.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 All @@ -12,22 +12,33 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from click_spinner import spinner
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')
@click.argument('prefix', type=str)
def create_static_route(prefix: str) -> None:
@click.command(
'create',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.argument('name', type=str)
@with_spinner(text="Creating static route...")
def create_static_route(name: str, spinner=None) -> None:
"""
Creates a new instance of static route
"""
try:
client = new_client()
with spinner():
route = client.create_static_route(prefix)
click.secho("Static Route created successfully for URL {}".format(route.urlString), fg='green')
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.spec.url), fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
click.secho(str(e), fg='red')
raise SystemExit(1)
43 changes: 28 additions & 15 deletions riocli/static_route/delete.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 All @@ -12,30 +12,43 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from click_spinner import spinner
from click_help_colors import HelpColorsCommand

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


@click.command('delete')
@click.command(
'delete',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.option('--force', '-f', is_flag=True, default=False, help='Skip confirmation')
@click.argument('static-route', type=str)
@name_to_guid
def delete_static_route(static_route: str, static_route_guid: str, force: bool) -> None:
def delete_static_route(
static_route: str,
static_route_guid: str,
force: bool,
spinner=None,
) -> None:
"""
Deletes the static route resource from the Platform
Deletes a static route
"""

if not force:
click.confirm('Deleting static route {} ({})'.format(static_route, static_route_guid),
abort=True)
with spinner.hidden():
if not force:
click.confirm(
'Deleting static route {} ({})'.format(
static_route, static_route_guid), abort=True)

try:
client = new_client()
with spinner():
client.delete_static_route(static_route_guid)
click.secho('Static Route deleted successfully!', fg='green')
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:
click.secho(str(e), fg='red')
raise SystemExit(1)
22 changes: 4 additions & 18 deletions riocli/static_route/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from rapyuta_io.clients.static_route import StaticRoute

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

Expand All @@ -29,24 +28,11 @@ def inspect_static_route(format_type: str, static_route: str, static_route_guid:
Inspect the static route resource
"""
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(route, format_type)
except Exception as e:
click.secho(str(e), fg='red')
raise SystemExit(1)


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__,
}
42 changes: 33 additions & 9 deletions riocli/static_route/list.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
0000#00000000000000000 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 All @@ -11,21 +11,45 @@
# 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 click
from typing import List

from riocli.config import new_client
from riocli.static_route.util import repr_static_routes
import click
from click_help_colors import HelpColorsCommand
from rapyuta_io.clients.static_route import StaticRoute

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

@click.command('list')
@click.command(
'list',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
def list_static_routes() -> None:
"""
List the static routes in the selected project
"""
try:
client = new_client()
routes = client.get_all_static_routes()
repr_static_routes(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='red')
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1)

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

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

tabulate_data(data, headers)
6 changes: 4 additions & 2 deletions riocli/static_route/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ def __init__(self, *args, **kwargs):
self.update(*args, **kwargs)

def find_object(self, client: Client) -> bool:
_, static_route = self.rc.find_depends({'kind': 'staticroute',
'nameOrGUID': self.metadata.name})
_, static_route = self.rc.find_depends({
'kind': 'staticroute',
'nameOrGUID': self.metadata.name
})
if not static_route:
return False

Expand Down
21 changes: 14 additions & 7 deletions riocli/static_route/open.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 All @@ -12,22 +12,29 @@
# See the License for the specific language governing permissions and
# limitations under the License.
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('open')
@click.command(
'open',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.argument('static-route', type=str)
@name_to_guid
def open_static_route(static_route, static_route_guid) -> 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='red')
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1)
Loading

0 comments on commit 74220ac

Please sign in to comment.