]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Adding properties server_url and kv_url and method remove_key() from ConsulHandler
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 27 May 2024 11:15:06 +0000 (13:15 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 27 May 2024 11:15:06 +0000 (13:15 +0200)
lib/create_terraform/consul.py

index e5fbf45399774465a9684d113bc47c49690833f6..f2c688b61bf6570e730946e188d5fea6065f746f 100644 (file)
@@ -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."""