From: Frank Brehm Date: Mon, 27 May 2024 11:15:06 +0000 (+0200) Subject: Adding properties server_url and kv_url and method remove_key() from ConsulHandler X-Git-Tag: 1.9.1^2~3 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=fd7022c8b6fdb1092e09585662851383378a5fa5;p=pixelpark%2Fcreate-terraform.git Adding properties server_url and kv_url and method remove_key() from ConsulHandler --- diff --git a/lib/create_terraform/consul.py b/lib/create_terraform/consul.py index e5fbf45..f2c688b 100644 --- a/lib/create_terraform/consul.py +++ b/lib/create_terraform/consul.py @@ -44,7 +44,7 @@ from .errors import ConsulApiNotFoundError from .xlate import XLATOR -__version__ = '0.3.0' +__version__ = '0.4.0' LOG = logging.getLogger(__name__) LOGLEVEL_REQUESTS_SET = False @@ -341,6 +341,28 @@ class ConsulHandler(HandlingObject): raise ConsulHandlerError(_('Invalid user agent {!r} given.').format(value)) self._user_agent = str(value).strip() + # ----------------------------------------------------------- + @property + def server_url(self): + """The base URL of the Consul server without the key path.""" + url = 'http://{}'.format(self.consul_server) + if self.mocked: + url = 'mock://{}'.format(self.consul_server) + elif self.use_https: + url = 'https://{}'.format(self.consul_server) + if self.port != 443: + url += ':{}'.format(self.port) + else: + if self.port != 80: + url += ':{}'.format(self.port) + return url + + # ----------------------------------------------------------- + @property + def kv_url(self): + """The complete URL of the Consul server ro the key/Value management.""" + return self.server_url + self.path_prefix + # ------------------------------------------------------------------------- def as_dict(self, short=True): """ @@ -360,9 +382,11 @@ class ConsulHandler(HandlingObject): res['default_port'] = self.default_port res['default_timeout'] = self.default_timeout res['default_use_https'] = self.default_use_https + res['kv_url'] = self.kv_url res['path_prefix'] = self.path_prefix res['port'] = self.port res['mocked'] = self.mocked + res['server_url'] = self.server_url res['timeout'] = self.timeout res['use_https'] = self.use_https res['user_agent'] = self.user_agent @@ -376,18 +400,7 @@ class ConsulHandler(HandlingObject): msg = _('The key {!r} must not be an absolute path.').format(key) raise ValueError(msg) - url = 'http://{}'.format(self.consul_server) - if self.mocked: - url = 'mock://{}'.format(self.consul_server) - elif self.use_https: - url = 'https://{}'.format(self.consul_server) - if self.port != 443: - url += ':{}'.format(self.port) - else: - if self.port != 80: - url += ':{}'.format(self.port) - - url += self.path_prefix + '/' + key + url = self.kv_url + '/' + key if params: params_obj = ConsulKeyQueryParams(**params) @@ -537,13 +550,29 @@ class ConsulHandler(HandlingObject): return None encoded_data = first_data['Value'] - json_data = base64.b64decode(encoded_data) + json_data = base64.b64decode(encoded_data).decode('utf-8') + if self.verbose > 3: + LOG.debug(_('Raw json data:') + '\n' + str(json_data)) + value = json.loads(str(json_data)) if self.verbose > 2: LOG.debug(_('Got data from Consul for key {!r}:').format(key) + '\n' + pp(value)) return value + # ------------------------------------------------------------------------- + def remove_key(self, key): + """Remove the given key from the Key/Value store.""" + LOG.warn(_('Deleting key {key!r} from Consul server {srv!r} ...').format( + key=key, srv=self.server_url)) + result = self.perform_request(key, method='DELETE', may_simulate=True) + + LOG.debug(_('Got answer: {!r}').format(result)) + + result_bool = to_bool(result) + + return result_bool + # ------------------------------------------------------------------------- def start_mocking(self, session): """Start mocking mode of this class for unit testing."""