from .xlate import XLATOR
-__version__ = '0.3.0'
+__version__ = '0.4.0'
LOG = logging.getLogger(__name__)
LOGLEVEL_REQUESTS_SET = False
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):
"""
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
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)
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."""