From e28cde373bfbfa094cbae6a2ec65e7e8ebe7712f Mon Sep 17 00:00:00 2001 From: seesteins Date: Sat, 2 Mar 2024 11:53:12 -0500 Subject: [PATCH] add wait to create_coveragestore to fix issue #20 --- src/geoserver/catalog.py | 66 +++++++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/src/geoserver/catalog.py b/src/geoserver/catalog.py index ceb9a01..9d7bbc3 100644 --- a/src/geoserver/catalog.py +++ b/src/geoserver/catalog.py @@ -9,10 +9,13 @@ # ######################################################################### +import base64 import base64 import logging import os import re +import os +import re from datetime import datetime, timedelta from time import sleep from xml.etree.ElementTree import XML @@ -24,13 +27,30 @@ from six import string_types from urllib3 import Retry +from time import sleep +from xml.etree.ElementTree import XML +from xml.parsers.expat import ExpatError + +import requests +from requests.adapters import HTTPAdapter +from requests.exceptions import HTTPError +from six import string_types +from urllib3 import Retry + from geoserver.layer import Layer from geoserver.layergroup import LayerGroup, UnsavedLayerGroup +from geoserver.layergroup import LayerGroup, UnsavedLayerGroup from geoserver.resource import FeatureType from geoserver.security import user_from_index from geoserver.service import service_from_index from geoserver.settings import GlobalSettings +from geoserver.security import user_from_index +from geoserver.service import service_from_index +from geoserver.settings import GlobalSettings from geoserver.store import ( + UnsavedCoverageStore, + UnsavedDataStore, + UnsavedWmsStore, UnsavedCoverageStore, UnsavedDataStore, UnsavedWmsStore, @@ -41,6 +61,8 @@ from geoserver.style import Style from geoserver.support import build_url, prepare_upload_bundle from geoserver.workspace import Workspace, workspace_from_index +from geoserver.support import build_url, prepare_upload_bundle +from geoserver.workspace import Workspace, workspace_from_index try: from past.builtins import basestring @@ -49,11 +71,14 @@ try: from urllib.parse import parse_qsl, urlencode, urlparse + from urllib.parse import parse_qsl, urlencode, urlparse except ImportError: from urllib import urlencode from urlparse import parse_qsl, urlparse + from urlparse import parse_qsl, urlparse + try: from json.decoder import JSONDecodeError except ImportError: @@ -661,6 +686,9 @@ def create_coveragestore( layer_name, name, resp.status_code, + layer_name, + name, + resp.status_code, ) ) else: @@ -687,18 +715,18 @@ def create_coveragestore( if resp.status_code != 201: raise FailedRequestError( "Failed to create coverage/layer {} for : {}, {}".format( - layer_name, name, resp.status_code, resp.text + layer_name, + name, + resp.status_code, ) ) self._cache.clear() - #servers not on localhost may not return a store throwing an error - time = 0 while name not in [ - store.name for store in self.get_stores(workspaces=[workspace]) and time < 120 + store.name for store in self.get_stores(workspaces=[workspace]) ]: - sleep(.5) - time ++ .5 - return self.get_stores(names=name, workspaces=[workspace])[0] + sleep(1) + + return self.get_resources(names=layer_name, workspaces=[workspace])[0] def add_granule(self, data, store, workspace=None): """Harvest/add a granule into an existing imagemosaic""" @@ -1238,6 +1266,11 @@ def get_style(self, name, workspace=None, recursive=False): Will raise an error if more than one style with the same name is found. """ + url = ( + f"{self.service_url}/workspaces/{workspace}/styles/{name}.json" + if workspace + else f"{self.service_url}/styles/{name}.json" + ) url = ( f"{self.service_url}/workspaces/{workspace}/styles/{name}.json" if workspace @@ -1253,11 +1286,19 @@ def get_style(self, name, workspace=None, recursive=False): if payload.get("workspace") else workspace ) + payload = resp.json()["style"] + extracted_workspace = ( + payload["workspace"].get("name", workspace) + if payload.get("workspace") + else workspace + ) return Style( self, payload["name"], + payload["name"], extracted_workspace, payload["format"] + payload["languageVersion"]["version"], + payload["format"] + payload["languageVersion"]["version"], ) except HTTPError as e: @@ -1269,20 +1310,29 @@ def get_style(self, name, workspace=None, recursive=False): logger.exception(e) raise e + def delete_style(self, name, workspace=None, purge=True): if workspace: + """ """ If workspace is passed, we call directly the wanted style """ url = ( f"{self.service_url}/workspaces/{workspace}/styles/{name}?purge={purge}" ) + """ + url = ( + f"{self.service_url}/workspaces/{workspace}/styles/{name}?purge={purge}" + ) else: + """ """ If is not passed, we try to get the style without passing any workspace """ + """ url = f"{self.service_url}/styles/{name}?purge={purge}" + try: resp = self.http_request(url, method="DELETE") if resp.status_code != 404: @@ -1501,6 +1551,7 @@ def get_services(self, ogc_type="wms"): f"{self.service_url}/services/{ogc_type}/workspaces/{ws.name}/settings" ) services.append(service_from_index(self, data)) + except FailedRequestError: except FailedRequestError: logger.debug(f"Not found {ogc_type} service for workspace {ws.name}") return services @@ -1514,6 +1565,7 @@ def create_user(self, username, password): ) try: tmp_cat.get_version() + except FailedRequestError: except FailedRequestError: logger.error("And we probably have incorrect password") raise FailedRequestError