Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions tests/e2e_tests/geoserver_engine_e2e_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,11 @@ def test_create_shapefile_resource_upload(self):
store_rand = random_string_generator(10)
store_id = '{}:{}'.format(self.workspace_name, store_rand)

with open(shapefile_cst, 'rb') as cst_upload,\
open(shapefile_dbf, 'rb') as dbf_upload,\
open(shapefile_prj, 'rb') as prj_upload,\
open(shapefile_shp, 'rb') as shp_upload,\
open(shapefile_shx, 'rb') as shx_upload:
with open(shapefile_cst, 'rb') as cst_upload, \
open(shapefile_dbf, 'rb') as dbf_upload, \
open(shapefile_prj, 'rb') as prj_upload, \
open(shapefile_shp, 'rb') as shp_upload, \
open(shapefile_shx, 'rb') as shx_upload:
upload_list = [cst_upload, dbf_upload, prj_upload, shp_upload, shx_upload]
response = self.geoserver_engine.create_shapefile_resource(
store_id=store_id,
Expand Down Expand Up @@ -499,7 +499,6 @@ def test_create_coverage_layer_grassgrid(self):
layer_id = '{}:{}'.format(self.workspace_name, layer_name)
expected_coverage_type = 'GrassGrid'
coverage_file_name = 'my_grass.zip'
coverage_name = coverage_file_name.split('.')[0]
coverage_file = os.path.join(self.files_root, "grass_ascii", coverage_file_name)

# Execute
Expand Down Expand Up @@ -995,7 +994,8 @@ def test_link_and_add_table(self):
)

# Check for success response
# TODO: returns an error in PostGIS 3.4: Internal Server Error(500): :java.io.IOException: Error occured calculating bounds for points
# TODO: returns an error in PostGIS 3.4: Internal Server Error(500):
# :java.io.IOException: Error occured calculating bounds for points
self.assertTrue(response['success'])

# TEST list_stores
Expand Down Expand Up @@ -1064,8 +1064,8 @@ def test_create_postgis_store(self):
)

self.assertTrue(response['success'])
# Pause to let GeoServer catch up

# Pause to let GeoServer catch up
sleep(5)

# TEST list_stores
Expand Down Expand Up @@ -1132,7 +1132,7 @@ def test_create_sql_view_layer(self):
password=self.pg_password,
)
self.assertTrue(response['success'])

# Pause to let GeoServer catch up before continuing
sleep(5)

Expand All @@ -1143,7 +1143,8 @@ def test_create_sql_view_layer(self):
debug=True
)

# TODO: returns an error in PostGIS 3.4: Internal Server Error(500): :java.io.IOException: Error occured calculating bounds for points
# TODO: returns an error in PostGIS 3.4: Internal Server Error(500):
# :java.io.IOException: Error occured calculating bounds for points
self.assertTrue(response['success'])

# Pause to let GeoServer catch up before continuing
Expand Down
37 changes: 18 additions & 19 deletions tests/unit_tests/test_geoserver_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def setUp(self):
self.username = 'foo'
self.password = 'bar'
self.auth = (self.username, self.password)

self.engine = GeoServerSpatialDatasetEngine(
endpoint=self.endpoint,
username=self.username,
Expand Down Expand Up @@ -515,7 +515,7 @@ def test_list_styles(self, mock_catalog):
self.assertIn(n, result)

mc.get_styles.assert_called_with(workspaces=[])

@mock.patch('tethys_dataset_services.engines.geoserver_engine.GeoServerCatalog')
def test_list_styles_of_workspace(self, mock_catalog):
mc = mock_catalog()
Expand Down Expand Up @@ -1650,7 +1650,7 @@ def test_update_layer_styles_exception(self, mock_ws, mock_list_styles, mock_put
default_style = self.style_names[0]
other_styles = [self.style_names[1]]

self.assertRaises(requests.RequestException, self.engine.update_layer_styles, layer_id, default_style,
self.assertRaises(requests.RequestException, self.engine.update_layer_styles, layer_id, default_style,
other_styles)

mock_logger.error.assert_called()
Expand Down Expand Up @@ -1959,7 +1959,7 @@ def test_delete_style(self, mock_workspace, mock_delete):
self.assertEqual(expected_headers, delete_call_args[0][1]['headers'])
self.assertEqual(expected_params, delete_call_args[0][1]['params'])

mock_delete.assert_called_with(url=expected_url, auth=self.auth, headers=expected_headers,
mock_delete.assert_called_with(url=expected_url, auth=self.auth, headers=expected_headers,
params=expected_params)

@mock.patch('tethys_dataset_services.engines.geoserver_engine.log')
Expand Down Expand Up @@ -2120,10 +2120,10 @@ def test_create_shapefile_resource_upload(self, mock_catalog, mock_put):
# Workspace is given
store_id = '{}:{}'.format(self.workspace_name, self.store_names[0])

with open(shapefile_cst, 'rb') as cst_upload,\
open(shapefile_dbf, 'rb') as dbf_upload,\
open(shapefile_prj, 'rb') as prj_upload,\
open(shapefile_shp, 'rb') as shp_upload,\
with open(shapefile_cst, 'rb') as cst_upload, \
open(shapefile_dbf, 'rb') as dbf_upload, \
open(shapefile_prj, 'rb') as prj_upload, \
open(shapefile_shp, 'rb') as shp_upload, \
open(shapefile_shx, 'rb') as shx_upload:
upload_list = [cst_upload, dbf_upload, prj_upload, shp_upload, shx_upload]
response = self.engine.create_shapefile_resource(store_id=store_id,
Expand Down Expand Up @@ -2362,7 +2362,7 @@ def test_reload_not_200(self, mock_post, mock_logger):
@mock.patch('tethys_dataset_services.engines.geoserver_engine.requests.post')
def test_reload_connection_error(self, mock_post, mock_logger):
mock_post.side_effect = requests.ConnectionError()
response = self.engine.reload()
self.engine.reload()
mock_logger.warning.assert_called()

@mock.patch('tethys_dataset_services.engines.geoserver_engine.requests.post')
Expand Down Expand Up @@ -2390,7 +2390,7 @@ def test_gwc_reload_not_200(self, mock_post, mock_logger):
@mock.patch('tethys_dataset_services.engines.geoserver_engine.requests.post')
def test_gwc_reload_connection_error(self, mock_post, mock_logger):
mock_post.side_effect = requests.ConnectionError()
response = self.engine.gwc_reload()
self.engine.gwc_reload()
mock_logger.warning.assert_called()

def test_ini_no_slash_endpoint(self):
Expand Down Expand Up @@ -3104,10 +3104,10 @@ def test_create_style(self, mock_workspace, mock_post, mock_get_style, mock_log)
sld_context = {'foo': 'bar'}

mock_get_style.return_value = {
'success': True,
'success': True,
'result': {'name': self.mock_styles[0].name, 'workspace': self.workspace_name}
}

response = self.engine.create_style(style_id, sld_template, sld_context)

# Validate response object
Expand Down Expand Up @@ -3141,7 +3141,7 @@ def test_create_style_cannot_find_style(self, mock_post, mock_get_style, mock_lo
sld_context = {'foo': 'bar'}

mock_get_style.return_value = {
'success': True,
'success': True,
'result': ' warnings '
}

Expand All @@ -3155,7 +3155,6 @@ def test_create_style_cannot_find_style(self, mock_post, mock_get_style, mock_lo
self.assertIn(style_url, post_call_args[0][0][0])
mock_log.warning.assert_called()


@mock.patch('tethys_dataset_services.engines.geoserver_engine.log')
@mock.patch('tethys_dataset_services.engines.geoserver_engine.requests.post')
def test_create_style_exception(self, mock_post, mock_log):
Expand Down Expand Up @@ -3195,7 +3194,7 @@ def test_create_style_overwrite(self, mock_post, mock_logger, mock_get_style):
sld_context = {'foo': 'bar'}
self.engine.delete_style = mock.MagicMock()
mock_get_style.return_value = {
'success': True,
'success': True,
'result': {'name': self.mock_styles[0].name, 'workspace': self.workspace_name}
}

Expand All @@ -3207,7 +3206,7 @@ def test_create_style_overwrite(self, mock_post, mock_logger, mock_get_style):

# Success
self.assertTrue(response['success'])

# Extract Result
result = response['result']

Expand Down Expand Up @@ -3311,7 +3310,7 @@ def test_create_layer_create_feature_type_already_exists(self, mock_workspace, m
mock_post.side_effect = [MockResponse(500, 'already exists'), MockResponse(200)]
mock_workspace().name = self.workspace_name
store_id = 'foo'
layer_name = self.layer_names[0]
layer_name = self.layer_names[0]
geometry_type = 'Point'
srid = 4236
sql = 'SELECT * FROM foo'
Expand Down Expand Up @@ -3772,7 +3771,7 @@ def test_create_postgis_store_validate_connection_false(self, mock_workspace, mo
endpoint=self.endpoint,
workspace=self.workspace_name
)
self.engine.create_postgis_store(store_id, host, port, database, username, password, max_connections,
self.engine.create_postgis_store(store_id, host, port, database, username, password, max_connections,
max_connection_idle_time, evictor_run_periodicity, validate_connections=False)
mock_post.assert_called_with(url=rest_endpoint, data=xml, headers=expected_headers, auth=self.auth)

Expand Down Expand Up @@ -3821,7 +3820,7 @@ def test_create_postgis_store_expose_primary_keys_true(self, mock_workspace, moc
endpoint=self.endpoint,
workspace=self.workspace_name
)
self.engine.create_postgis_store(store_id, host, port, database, username, password, max_connections,
self.engine.create_postgis_store(store_id, host, port, database, username, password, max_connections,
max_connection_idle_time, evictor_run_periodicity, validate_connections=False,
expose_primary_keys=True)
mock_post.assert_called_with(url=rest_endpoint, data=xml, headers=expected_headers, auth=self.auth)
Expand Down
132 changes: 125 additions & 7 deletions tethys_dataset_services/engines/geoserver_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,8 @@ def reload(self, ports=None, public=True):
GeoServer are running in a clustered GeoServer configuration.
public (bool): Use the public geoserver endpoint if True, otherwise use the internal endpoint.
"""
# take this out, it is hardcoded for testing:
# node_endpoints = self._get_node_endpoints(ports=[9090], public=public)
node_endpoints = self._get_node_endpoints(ports=ports, public=public)
log.debug("Catalog Reload URLS: {0}".format(node_endpoints))

Expand Down Expand Up @@ -1332,7 +1334,92 @@ def create_postgis_store(self, store_id, host, port, database, username, passwor

return response_dict

def create_layer_from_postgis_store(self, store_id, table, debug=False):
def create_layer_from_postgis_store(self, store_id, table, layer_name=None, debug=False):
"""
Add an existing PostGIS table as a feature resource to a PostGIS store that already exists.

Args:
store_id (str): Identifier for the store to add the resource to. This can be a store name,
or "workspace:store_name" combo (e.g.: "name" or "workspace:name"). If the workspace
is not specified, the catalog's default workspace is used.
table (str): The underlying table name in the PostGIS database. A layer (feature resource)
will be created referencing this table.
layer_name (str, optional): If provided, this name will be used for the newly created layer
in GeoServer. If not provided, defaults to the same as 'table'.
debug (bool, optional): Pretty print the response dictionary to the console for debugging.
Defaults to False.

Returns:
dict: A response dictionary with 'success', 'result', and/or 'error' keys.

Examples:
# Use the table name for layer:
engine.create_layer_from_postgis_store(
store_id='workspace:store_name',
table='table_name'
)

# Provide a custom layer name:
engine.create_layer_from_postgis_store(
store_id='workspace:store_name',
table='table_name',
layer_name='my_custom_layer'
)
"""
# Extract (workspace, store_name) from the store_id
workspace, store_name = self._process_identifier(store_id)
if not workspace:
workspace = self.catalog.get_default_workspace().name

# Verify the store exists
store_info = self.get_store(store_id, debug=debug)
if not store_info['success']:
message = f"There is no store named '{store_name}'"
if workspace:
message += f" in {workspace}"
return {'success': False, 'error': message}

# If no layer_name was provided, default to the PostGIS table name
if not layer_name:
layer_name = table

# Create an XML body for the new feature type in GeoServer
# The <name> field sets the GeoServer layer (and resource) name.
xml_body = f"""
<featureType>
<name>{layer_name}</name>
<nativeName>{table}</nativeName>
</featureType>
"""

headers = {
"Content-type": "text/xml",
"Accept": "application/xml"
}

# POST /workspaces/<workspace>/datastores/<store_name>/featuretypes
url = self._assemble_url('workspaces', workspace, 'datastores', store_name, 'featuretypes')
response = requests.post(
url=url,
data=xml_body,
headers=headers,
auth=HTTPBasicAuth(username=self.username, password=self.password)
)

if response.status_code != 201:
response_dict = {
'success': False,
'error': f'{response.reason}({response.status_code}): {response.text}'
}
self._handle_debug(response_dict, debug)
return response_dict

# Optionally return the store info, or you could directly query the new layer if desired
response_dict = self.get_store(store_id=store_id, debug=debug)
self._handle_debug(response_dict, debug)
return response_dict

def create_layer_from_postgis_store2(self, store_id, table, debug=False):
"""
Add an existing postgis table as a feature resource to a postgis store that already exists.

Expand Down Expand Up @@ -1501,7 +1588,7 @@ def create_sql_view_layer(self, store_id, layer_name, geometry_type, srid, sql,

retries_remaining = 300
while retries_remaining > 0:
response = requests.post(
response = requests.put(
url,
headers=headers,
auth=(self.username, self.password),
Expand All @@ -1524,7 +1611,7 @@ def create_sql_view_layer(self, store_id, layer_name, geometry_type, srid, sql,
return response_dict

def create_shapefile_resource(self, store_id, shapefile_base=None, shapefile_zip=None, shapefile_upload=None,
overwrite=False, charset=None, debug=False):
overwrite=False, charset=None, default_style=None, debug=False):
"""
Use this method to add shapefile resources to GeoServer.

Expand All @@ -1536,7 +1623,8 @@ def create_shapefile_resource(self, store_id, shapefile_base=None, shapefile_zip
shapefile_zip (string, optional): Path to a zip file containing the shapefile and side cars.
shapefile_upload (FileUpload list, optional): A list of Django FileUpload objects containing a shapefile and side cars that have been uploaded via multipart/form-data form. # noqa: E501
overwrite (bool, optional): Overwrite the file if it already exists.
charset (string, optional): Specify the character encoding of the file being uploaded (e.g.: ISO-8559-1)
charset (string, optional): Specify the character encoding of the file being uploaded (e.g.: ISO-8559-1).
default_style (string, optional): The name of the default style to apply to the layer. Can be a name or a workspace-name combination (e.g.: "name" or "workspace:name"). # noqa: E501
debug (bool, optional): Pretty print the response dictionary to the console for debugging. Defaults to False.

Returns:
Expand Down Expand Up @@ -1682,8 +1770,10 @@ def create_shapefile_resource(self, store_id, shapefile_base=None, shapefile_zip

# Wrap up with failure
if response.status_code != 201:
response_dict = {'success': False,
'error': '{1}({0}): {2}'.format(response.status_code, response.reason, response.text)}
response_dict = {
'success': False,
'error': f'{response.reason}({response.status_code}): {response.text}'
}

self._handle_debug(response_dict, debug)
return response_dict
Expand All @@ -1698,6 +1788,34 @@ def create_shapefile_resource(self, store_id, shapefile_base=None, shapefile_zip
# This case uses the store name as the Resource ID.
resource_id = name

# Set the default style
if default_style is not None:
layer_url = self._assemble_url('layers', f'{workspace}:{resource_id}.xml')
layer_headers = {"Content-Type": "application/xml"}
layer_data = f"""
<layer>
<defaultStyle>
<name>{default_style}</name>
</defaultStyle>
</layer>
"""

layer_response = requests.put(
layer_url,
headers=layer_headers,
data=layer_data,
auth=HTTPBasicAuth(username=self.username, password=self.password)
)

if layer_response.status_code != 200:
layer_response_dict = {
'success': False,
'error': f'{layer_response.reason}({layer_response.status_code}): {layer_response.text}'
}

self._handle_debug(layer_response_dict, debug)
return response_dict

# Wrap up successfully
new_resource = self.catalog.get_resource(name=resource_id, store=name, workspace=workspace)
resource_dict = self._transcribe_geoserver_object(new_resource)
Expand Down Expand Up @@ -2208,7 +2326,7 @@ def update_layer(self, layer_id, debug=False, **kwargs):
"""
# Pop tile caching properties to handle separately
tile_caching = kwargs.pop('tile_caching', None)

# breakpoint()
try:
# Get resource
layer = self.catalog.get_layer(name=layer_id)
Expand Down
Loading