Consul Parser

class django_docker_helpers.config.backends.consul_parser.ConsulParser(endpoint='service', host='127.0.0.1', port=8500, scheme='http', verify=True, cert=None, kv_get_opts=None, path_separator='.', inner_parser_class=<class 'django_docker_helpers.config.backends.yaml_parser.YamlParser'>)[source]

Reads a whole config bundle from a consul kv key and provides the unified interface to access config options.

It assumes that config in your storage can be parsed with any simple parser, like YamlParser.

Compared to, e.g. EnvironmentParser it does not have scope support by design, since endpoint is a good enough scope by itself.

Example:

parser = ConsulParser('my/server/config.yml', host=CONSUL_HOST, port=CONSUL_PORT)
parser.get('nested.a.b', coerce_type=int)
Parameters
  • endpoint (str) – specifies a key in consul kv storage, e.g. 'services/mailer/config.yml'

  • host (str) – consul host, default is '127.0.0.1'

  • port (int) – consul port, default is 8500

  • scheme (str) – consul scheme, default is 'http'

  • verify (bool) – verify certs, default is True

  • cert (Optional[str]) – path to certificate bundle

  • kv_get_opts (Optional[Dict]) – read config bundle with optional arguments to client.kv.get()

  • path_separator (str) – specifies which character separates nested variables, default is '.'

  • inner_parser_class (Optional[Type[BaseParser]]) – use the specified parser to read config from endpoint key

get(variable_path, default=None, coerce_type=None, coercer=None, **kwargs)[source]

Reads a value of variable_path from consul kv storage.

Parameters
  • variable_path (str) – a delimiter-separated path to a nested value

  • default (Optional[Any]) – default value if there’s no object by specified path

  • coerce_type (Optional[Type]) – cast a type of a value to a specified one

  • coercer (Optional[Callable]) – perform a type casting with specified callback

  • kwargs – additional arguments inherited parser may need

Returns

value or default

Raises
  • config.exceptions.KVStorageKeyDoestNotExist – if specified endpoint does not exists

  • config.exceptions.KVStorageValueIsEmpty – if specified endpoint does not contain a config

get_client()[source]

If your backend needs a client, inherit this method and use client() shortcut.

Returns

an instance of backend-specific client

property inner_parser: django_docker_helpers.config.backends.base.BaseParser

Prepares inner config parser for config stored at endpoint.

Return type

BaseParser

Returns

an instance of BaseParser

Raises
  • config.exceptions.KVStorageKeyDoestNotExist – if specified endpoint does not exists

  • config.exceptions.KVStorageValueIsEmpty – if specified endpoint does not contain a config