Skip to main content

Framework for using Consul as your project options storage

Project description

https://travis-ci.org/Fatal1ty/consul-options.svg?branch=master https://img.shields.io/pypi/v/consul-options.svg https://img.shields.io/pypi/pyversions/consul-options.svg https://img.shields.io/badge/License-Apache%202.0-blue.svg

How often do you wonder where to store the project settings? When your project is small it is not a big problem. But if your big project consists of dozens of microservices then there is the problem of centralized configuration management. The Сonsul have a key/value storage that is ideal for this.

With consul-options you can define and use options in your code by a simple and elegant way. Just take a look at the example:

class DB(ConsulKV):
    pass


class Users(DB):
    host = '127.0.0.1'
    port = 5432
    user = 'postgres'
    password = 'postgres'

    dbname = 'users'


class Orders(DB):
    host = '127.0.0.1'
    port = 5432
    user = 'postgres'
    password = 'postgres'

    dbname = 'orders'

Now you can access to the option values in a clear way:

from consul_options import options

print options.db.users.host
print options.db.orders.dbname

consul-options automagically creates folders and keys with default values defined above in Consul and later read them from there. So if anyone will change the value of db/orders/host key to something different in Consul then you will get that value.

How project options stored in Consul

When you declare a new class based on consul_options.ConsulKV default bahavior is creation a folder in Consul key/value storage with name of your class in lowercase. Each class attribute you define will have mapping to key folder.key. If you want to change this name you can use reserved class attribute __key__ as shown below:

class WorkerOptions(ConsulKV):
    __key__ = 'worker'

    host = '127.0.0.1'
    port = 80

After that you can access to the option with “worker” in path:

from consul_options import options

print options.worker.host
print options.worker.port

To create hierarchial key structure you can take advantage of usual class hierarchy:

from consul_options import ConsulKV, options

class WorkerOptions(ConsulKV):
    __key__ = 'worker'

    host = '127.0.0.1'
    port = 80

class DB(WorkerOptions):
    host = '127.0.0.1'
    port = 5432
    user = 'postgres'
    password = 'postgres'

print options.worker.db.host  # 'host'
print options.worker.db.port  # 5432

It is also possible to create keys at root level with class attribute __root__:

class RootOptions(ConsulKV):
    __root__ = True

    host = '127.0.0.1'
    port = 80

print options.host
print options.port

Compatibility

consul-options is compatible with both Python 2 and Python 3.

Installation

Use pip to install:

$ pip install consul-options

License

consul-options is developed and distributed under the Apache 2.0 license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

consul-options-0.7.tar.gz (4.1 kB view details)

Uploaded Source

File details

Details for the file consul-options-0.7.tar.gz.

File metadata

File hashes

Hashes for consul-options-0.7.tar.gz
Algorithm Hash digest
SHA256 9a4d92b7e1db5b046a0cd11ec49d7079e7a14e2d931b75fad8148d27ca8cf74e
MD5 778db8c37fdad0c87714c07037b5a1b1
BLAKE2b-256 e5b6ca5dc3dd384c506fff564245d76c19d44d66a9c8a54b2d75fa9b24166bb1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page