From c5cb3741ea50eab2c0839bce9eb614fd29db795e Mon Sep 17 00:00:00 2001 From: Dalena Date: Fri, 25 Oct 2024 21:57:29 -0500 Subject: [PATCH] fix: update workspaces (#156) * fix: update workspaces * refactor: refactor remove and update workspaces * fix: mypy * fix: add default * fix: mypy * fix: mypy --- silverback/_cli.py | 29 ++++++++++++++----------- silverback/cluster/client.py | 42 ++++++++++++++++++------------------ 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/silverback/_cli.py b/silverback/_cli.py index 9368d375..15e1b80c 100644 --- a/silverback/_cli.py +++ b/silverback/_cli.py @@ -304,13 +304,15 @@ def new_workspace( @click.option( "-n", "--name", - "update_name", + "name", + default=None, help="Update name for workspace", ) @click.option( "-s", "--slug", - "update_slug", + "slug", + default=None, help="Update slug for workspace", ) @click.argument("workspace") @@ -318,25 +320,26 @@ def new_workspace( def update_workspace( platform: PlatformClient, workspace: str, - update_name: str | None, - update_slug: str | None, + name: str | None, + slug: str | None, ): """Update name and slug for a workspace""" - if not (platform.workspaces.get(workspace)): + if not (workspace_client := platform.workspaces.get(workspace)): raise click.BadOptionUsage("workspace", f"Unknown workspace '{workspace}'") - elif update_name is None and update_slug is None: + elif name is None and slug is None: raise click.UsageError( "No update name or slug found. Please enter a name or slug to update." ) + elif name == "" or slug == "": + raise click.UsageError("Empty string value found for name or slug.") - platform.update_workspace( - workspace=workspace, - update_name=update_name, - update_slug=update_slug, + updated_workspace = workspace_client.update( + name=name, + slug=slug, ) - click.echo(f"{click.style('SUCCESS', fg='green')}: Updated '{update_name}'") + click.echo(f"{click.style('SUCCESS', fg='green')}: Updated '{updated_workspace.name}'") @workspaces.command(name="delete", section="Platform Commands (https://silverback.apeworx.io)") @@ -351,8 +354,8 @@ def delete_workspace(platform: PlatformClient, workspace: str): if len(workspace_client.clusters) > 0: raise click.UsageError("Running Clusters found in Workspace. Shut them down first.") - platform.remove_workspace(workspace) - click.echo(f"{click.style('SUCCESS', fg='green')}: Deleted '{workspace}'") + workspace_client.remove() + click.echo(f"{click.style('SUCCESS', fg='green')}: Deleted '{workspace_client.name}'") @cluster.command(name="list", section="Platform Commands (https://silverback.apeworx.io)") diff --git a/silverback/cluster/client.py b/silverback/cluster/client.py index 2e084c95..6000fe6d 100644 --- a/silverback/cluster/client.py +++ b/silverback/cluster/client.py @@ -402,6 +402,27 @@ def get_payment_stream(self, cluster: ClusterInfo, chain_id: int) -> Stream | No return Stream(manager=StreamManager(stream_info.manager), id=stream_info.stream_id) + def update( + self, + name: str | None = None, + slug: str | None = None, + ) -> "Workspace": + data = dict() + if name: + data["name"] = name + if slug: + data["slug"] = slug + response = self.client.patch( + f"/workspaces/{self.id}", + data=data, + ) + handle_error_with_response(response) + return Workspace.model_validate(response.json()) + + def remove(self): + response = self.client.delete(f"/workspaces/{self.id}") + handle_error_with_response(response) + class PlatformClient(httpx.Client): def __init__(self, *args, **kwargs): @@ -452,27 +473,6 @@ def create_workspace( self.workspaces.update({new_workspace.slug: new_workspace}) # NOTE: Update cache return new_workspace - def remove_workspace(self, workspace_slug): - workspace_id = self.workspaces[workspace_slug].id - response = self.delete(f"/workspaces/{workspace_id}") - handle_error_with_response(response) - - def update_workspace( - self, - workspace: str, - update_slug: str | None, - update_name: str | None, - ): - workspace_id = self.workspaces[workspace].id - response = self.patch( - f"/workspaces/{workspace_id}", - data=dict(slug=update_slug, name=update_name), - ) - handle_error_with_response(response) - update_workspace = Workspace.model_validate_json(response.text) - self.workspaces.update({update_workspace.slug: update_workspace}) # NOTE: Update cache - return update_workspace - def get_stream_manager(self, chain_id: int) -> StreamManager: response = self.get(f"/streams/manager/{chain_id}") handle_error_with_response(response)