Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for adding data to existing datastores

  • Loading branch information...
commit f8280f438ad3007d1ad4dae1e05fd069e1c671bb 1 parent 48cef18
David Winslow dwins authored
33 src/geoserver/catalog.py
View
@@ -11,6 +11,7 @@
from zipfile import is_zipfile
from xml.etree.ElementTree import XML
from urlparse import urlparse
+from urllib import urlencode
logger = logging.getLogger("gsconfig.catalog")
@@ -117,7 +118,7 @@ def save(self, object):
"Content-type": "application/xml",
"Accept": "application/xml"
}
- response = self.http.request(url, "PUT", message, headers)
+ response = self.http.request(url, object.save_method, message, headers)
self._cache.clear()
return response
@@ -186,6 +187,36 @@ def create_coveragestore2(self, name, workspace = None):
workspace = self.get_default_workspace()
return UnsavedCoverageStore(self, name, workspace)
+ def add_data_to_store(self, store, name, data, overwrite = False, charset = None):
+ if isinstance(data, dict):
+ bundle = prepare_upload_bundle(name, data)
+ else:
+ bundle = data
+
+ params = dict()
+ if overwrite:
+ params["overwrite"] = True
+ if charset is not None:
+ params["charset"] = charset
+
+ if len(params):
+ params = "?" + urlencode(params)
+ else:
+ params = ""
+
+ message = open(bundle)
+ headers = { 'Content-Type': 'application/zip', 'Accept': 'application/xml' }
+ url = "%s/workspaces/%s/datastores/%s/file.shp%s" % (
+ self.service_url, store.workspace.name, store.name, params)
+
+ try:
+ headers, response = self.http.request(url, "PUT", message, headers)
+ self._cache.clear()
+ if headers.status != 201:
+ raise UploadError(response)
+ finally:
+ unlink(bundle)
+
def create_pg_feature(self, storeXML, name, data, workspace=None, overwrite=False, charset=None):
if not overwrite:
8 src/geoserver/store.py
View
@@ -82,10 +82,12 @@ def __repr__(self):
return "DataStore[%s:%s]" % (wsname, self.name)
class UnsavedDataStore(DataStore):
+ save_method = "POST"
+
def __init__(self, catalog, name, workspace):
self.name = name
self.workspace = workspace
- self.href = catalog.service_url + "/workspaces/" + workspace.name + "/datastores/" + name + ".xml"
+ self.href = catalog.service_url + "/workspaces/" + workspace.name + "/datastores/"
self.connection_parameters = dict()
self.enabled = True
@@ -179,10 +181,12 @@ def __repr__(self):
return "CoverageStore[%s:%s]" % (wsname, self.name)
class UnsavedCoverageStore(CoverageStore):
+ save_method = "POST"
+
def __init__(self, catalog, name, workspace):
self.name = name
self.workspace = workspace
- self.href = catalog.service_url + "/workspaces/" + workspace.name + "/coveragestores/" + name + ".xml"
+ self.href = catalog.service_url + "/workspaces/" + workspace.name + "/coveragestores/"
self.type = "GeoTIFF"
self.enabled = True
self.data_url = "file:data/"
3  src/geoserver/support.py
View
@@ -31,6 +31,9 @@
class ResourceInfo(object):
"""A base class for all resource types managed by the catalog """
+ save_method = "PUT"
+ """The HTTP method to use when saving this object"""
+
resource_type = 'abstractResourceType'
"""A string identifier for the *type* of resource, such as layer or style"""
14 test/catalogtests.py
View
@@ -129,6 +129,20 @@ def testDataStoreModify(self):
self.assertEqual("bar", ds.connection_parameters["foo"])
self.assertEqual(orig_ws, ds.workspace.name)
+ def testDataStoreCreateAndThenAlsoImportData(self):
+ ds = self.cat.create_datastore("gsconfig_import_test")
+ ds.connection_parameters.update(
+ host="localhost", port="5432", database="db", user="postgres",
+ passwd="password", dbtype="postgis")
+ self.cat.save(ds)
+ ds = self.cat.get_store("gsconfig_import_test")
+ self.cat.add_data_to_store(ds, "import", {
+ 'shp': 'test/data/states.shp',
+ 'shx': 'test/data/states.shx',
+ 'dbf': 'test/data/states.dbf',
+ 'prj': 'test/data/states.prj'
+ })
+
def testCoverageStoreCreate(self):
ds = self.cat.create_coveragestore2("coverage_gsconfig")
ds.data_url = "file:data/mytiff.tiff"
Please sign in to comment.
Something went wrong with that request. Please try again.