-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add facade class to copy GeoServer resources
- Loading branch information
1 parent
35a9267
commit cb7670a
Showing
4 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .geoservercloud import GeoServerCloud | ||
from .geoservercloudsync import GeoServerCloudSync | ||
|
||
__all__: list[str] = ["GeoServerCloud"] | ||
__all__: list[str] = ["GeoServerCloud", "GeoServerCloudSync"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from geoservercloud.services import RestService | ||
|
||
|
||
class GeoServerCloudSync: | ||
""" | ||
Facade class allowing synchronization of GeoServer resources between two GeoServer instances | ||
Attributes | ||
---------- | ||
src_url : str | ||
base GeoServer URL for source GeoServer instance | ||
src_user : str | ||
GeoServer username for source GeoServer instance | ||
src_password : str | ||
GeoServer password for source GeoServer instance | ||
dst_url : str | ||
base GeoServer URL for destination GeoServer instance | ||
dst_user : str | ||
GeoServer username for destination GeoServer instance | ||
dst_password : str | ||
GeoServer password for destination GeoServer instance | ||
""" | ||
|
||
def __init__( | ||
self, | ||
src_url: str, | ||
src_user: str, | ||
src_password: str, | ||
dst_url: str, | ||
dst_user: str, | ||
dst_password: str, | ||
) -> None: | ||
self.src_url: str = src_url.strip("/") | ||
self.src_user: str = src_user | ||
self.src_password: str = src_password | ||
self.src_auth: tuple[str, str] = (src_user, src_password) | ||
self.src_instance: RestService = RestService(src_url, self.src_auth) | ||
self.dst_url: str = dst_url.strip("/") | ||
self.dst_user: str = dst_user | ||
self.dst_password: str = dst_password | ||
self.dst_auth: tuple[str, str] = (dst_user, dst_password) | ||
self.dst_instance: RestService = RestService(dst_url, self.dst_auth) | ||
|
||
def copy_workspace(self, workspace_name: str) -> tuple[str, int]: | ||
""" | ||
Shallow copy a workspace from source to destination GeoServer instance | ||
""" | ||
workspace, status_code = self.src_instance.get_workspace(workspace_name) | ||
if isinstance(workspace, str): | ||
return workspace, status_code | ||
return self.dst_instance.create_workspace(workspace) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import pytest | ||
import responses | ||
from responses import matchers | ||
|
||
from geoservercloud import GeoServerCloudSync | ||
|
||
GEOSERVER_SRC_URL = "http://source-geoserver" | ||
GEOSERVER_DST_URL = "http://destination-geoserver" | ||
|
||
|
||
@pytest.fixture | ||
def geoserver_sync(): | ||
return GeoServerCloudSync( | ||
GEOSERVER_SRC_URL, | ||
"admin", | ||
"geoserver", | ||
GEOSERVER_DST_URL, | ||
"admin", | ||
"geoserver", | ||
) | ||
|
||
|
||
def test_copy_workspace(geoserver_sync): | ||
|
||
workspace_name = "test_workspace" | ||
workspace = {"name": workspace_name, "isolated": True} | ||
|
||
with responses.RequestsMock() as rsps: | ||
rsps.get( | ||
url=f"{GEOSERVER_SRC_URL}/rest/workspaces/{workspace_name}.json", | ||
status=200, | ||
json={"workspace": workspace}, | ||
) | ||
rsps.post( | ||
url=f"{GEOSERVER_DST_URL}/rest/workspaces.json", | ||
status=201, | ||
body=b"test_workspace", | ||
match=[matchers.json_params_matcher({"workspace": workspace})], | ||
) | ||
|
||
assert geoserver_sync.copy_workspace(workspace_name) == (workspace_name, 201) |