Skip to content

Commit

Permalink
fix: update workspaces (#156)
Browse files Browse the repository at this point in the history
* fix: update workspaces

* refactor: refactor remove and update workspaces

* fix: mypy

* fix: add default

* fix: mypy

* fix: mypy
  • Loading branch information
dtdang authored Oct 26, 2024
1 parent d127f7d commit c5cb374
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
29 changes: 16 additions & 13 deletions silverback/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,39 +304,42 @@ 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")
@platform_client
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)")
Expand All @@ -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)")
Expand Down
42 changes: 21 additions & 21 deletions silverback/cluster/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c5cb374

Please sign in to comment.