Skip to content

Commit

Permalink
added update workspacce function
Browse files Browse the repository at this point in the history
  • Loading branch information
krishnaglodha committed Nov 5, 2024
1 parent a57c52b commit 194e6df
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/geoserverx/_async/gsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from geoserverx.models.workspace import (
NewWorkspace,
NewWorkspaceInfo,
UpdateWorkspace,
UpdateWorkspaceInfo,
WorkspaceModel,
WorkspacesModel,
)
Expand Down Expand Up @@ -142,6 +144,13 @@ async def get_workspace(self, workspace: str) -> Union[WorkspaceModel, GSRespons
results = self.response_recognise(responses.status_code)
return results

# Delete specific workspaces
async def delete_workspace(self, workspace: str) -> GSResponse:
Client = self.http_client
responses = await Client.delete(f"workspaces/{workspace}")
results = self.response_recognise(responses.status_code)
return results

# Create workspace
async def create_workspace(
self, name: str, default: bool = False, Isolated: bool = False
Expand All @@ -158,6 +167,20 @@ async def create_workspace(
results = self.response_recognise(responses.status_code)
return results

# Update workspace on geoserver
async def update_workspace(
self, name: str, update: UpdateWorkspaceInfo
) -> GSResponse:
Client = self.http_client
update_ws = UpdateWorkspace(workspace=update)
responses = await Client.put(
f"workspaces/{name}.json",
data=update_ws.model_dump_json(exclude_unset=True),
headers=self.head,
)
results = self.response_recognise(responses.status_code)
return results

# Get vector stores in specific workspaces
async def get_vector_stores_in_workspaces(self, workspace: str) -> DataStoresModel:
Client = self.http_client
Expand Down
24 changes: 24 additions & 0 deletions src/geoserverx/_sync/gsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from geoserverx.models.workspace import (
NewWorkspace,
NewWorkspaceInfo,
UpdateWorkspace,
UpdateWorkspaceInfo,
WorkspaceModel,
WorkspacesModel,
)
Expand Down Expand Up @@ -159,6 +161,14 @@ def get_workspace(self, workspace: str) -> Union[WorkspaceModel, GSResponse]:
results = self.response_recognise(responses.status_code)
return results

# Delete specific workspaces
@exception_handler
def delete_workspace(self, workspace: str) -> GSResponse:
Client = self.http_client
responses = Client.delete(f"workspaces/{workspace}")
results = self.response_recognise(responses.status_code)
return results

# Create workspace on geoserver
@exception_handler
def create_workspace(
Expand All @@ -176,6 +186,20 @@ def create_workspace(
results = self.response_recognise(responses.status_code)
return results

# Update workspace on geoserver
@exception_handler
def update_workspace(self, name: str, update: UpdateWorkspaceInfo) -> GSResponse:
Client = self.http_client
update_ws = UpdateWorkspace(workspace=update)
print(update_ws.model_dump_json(exclude_unset=True))
responses = Client.put(
f"workspaces/{name}.json",
data=update_ws.model_dump_json(exclude_unset=True),
headers=self.head,
)
results = self.response_recognise(responses.status_code)
return results

# Get vector stores in specific workspaces
@exception_handler
def get_vector_stores_in_workspaces(self, workspace: str) -> DataStoresModel:
Expand Down
63 changes: 63 additions & 0 deletions src/geoserverx/cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import json
from enum import Enum
from pathlib import Path
from typing import Optional

import typer
from rich import print

from geoserverx._sync.gsx import SyncGeoServerX
from geoserverx.models.workspace import UpdateWorkspaceInfo

app = typer.Typer()

Expand Down Expand Up @@ -80,6 +82,32 @@ def workspace(
typer.echo("Async support will be shortly")


# Delete workspace
@SyncGeoServerX.exception_handler
@app.command(help="Delete workspace in the Geoserver")
def delete_workspace(
request: requestEnum = requestEnum._sync,
workspace: str = typer.Option(..., help="Workspace name"),
url: str = typer.Option(
"http://127.0.0.1:8080/geoserver/rest/", help="Geoserver REST URL"
),
password: str = typer.Option("geoserver", help="Geoserver Password"),
username: str = typer.Option("admin", help="Geoserver username"),
):
"""
Delete workspace in the Geoserver
"""
if request.value == "sync":
client = SyncGeoServerX(username, password, url)
result = client.delete_workspace(workspace).model_dump_json()
if "code" in result:
typer.secho(result, fg=typer.colors.RED)
else:
print(result)
else:
typer.echo("Async support will be shortly")


# create workspace
@SyncGeoServerX.exception_handler
@app.command(help="Add workspace in the Geoserver")
Expand Down Expand Up @@ -110,6 +138,41 @@ def create_workspace(
typer.echo("Async support will be shortly")


# Update workspace
@SyncGeoServerX.exception_handler
@app.command(help="Add workspace in the Geoserver")
def update_workspace(
request: requestEnum = requestEnum._sync,
current_name: str = typer.Option(..., help="Current Workspace name"),
new_name: Optional[str] = typer.Option(None, help="New Workspace name"),
isolated: Optional[bool] = typer.Option(False, help="Make workspace isolated?"),
url: str = typer.Option(
"http://127.0.0.1:8080/geoserver/rest/", help="Geoserver REST URL"
),
password: str = typer.Option("geoserver", help="Geoserver Password"),
username: str = typer.Option("admin", help="Geoserver username"),
):
"""
Add workspace in the Geoserver
looks like - gsx create-workspace --workspace <workspacename> --default/--no-default --isolated/--no-isolated --username <username> --password <password>
"""
if request.value == "sync":
client = SyncGeoServerX(username, password, url)
result = client.update_workspace(
current_name,
UpdateWorkspaceInfo(name=new_name, isolated=isolated).model_dump(
exclude_none=True
),
).model_dump_json()
if json.loads(result)["code"] == 200:
typer.secho(result, fg=typer.colors.GREEN)
else:
typer.secho(result, fg=typer.colors.RED)

else:
typer.echo("Async support will be shortly")


# Get vector stores in specific workspaces
@SyncGeoServerX.exception_handler
@app.command(help="Get vector stores in specific workspaces")
Expand Down
9 changes: 9 additions & 0 deletions src/geoserverx/models/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ class NewWorkspaceInfo(BaseModel):

class NewWorkspace(BaseModel):
workspace: NewWorkspaceInfo = ...


class UpdateWorkspaceInfo(BaseModel):
name: Optional[str] = None
isolated: Optional[bool] = None


class UpdateWorkspace(BaseModel):
workspace: UpdateWorkspaceInfo = ...

0 comments on commit 194e6df

Please sign in to comment.