Code Monkey home page Code Monkey logo

consul-client's Introduction

Consul Client

A python3 compatible library that abstracts the Consul Rest API.

Install the library

Install it from pypi

pip3 install -i https://test.pypi.org/simple/ ConsulClient==0.1.0

Install it using the released python wheel.

wget https://github.com/lozuwa/consul-client/releases/download/0.1.0/ConsulClient-0.1.0-py3-none-any.whl
pip3 install ConsulClient-0.1.0-py3-none-any.whl

Input parameters user="", password="" are to support Basic Auth connections to Consul.

Examples

KV (Basic CRUD)

Create or update key

from ConsulClient.ConsulClient import ConsulClient

kvs = {"kafka.bootstrap": "kafka-001:2181", "mongo.host": "mongo:27017"}
consul_client = ConsulClient(host="http://localhost", port="8500", token="", prefix="", user="", password="")

for key, value in kvs.items():
  response = self.consul_client.create_update_key(key=key, value=value)
  if response["successful_response"] == True:
    continue
  else:
    raise Exception(f"Could not publish {key} {value}")

Read key

from ConsulClient.ConsulClient import ConsulClient

kvs = {"kafka.bootstrap": "kafka-001:2181", "mongo.host": "mongo:27017"}
consul_client = ConsulClient(host="http://localhost", port="8500", token="", prefix="", user="", password="")

for key, value in kvs.items():
  response = self.consul_client.read_key(key=key)
  if response["successful_response"] == True:
    assert response["body"].get(key) == kvs.get(key), "Value does not exist."
    print(response["body"].get(key))
  else:
    raise Exception(f"Could not read {key}. RC: {response["status_code"]. Error: {response["body"]}}")

Read a tree of keys

from ConsulClient.ConsulClient import ConsulClient

kvs = {"kafka.bootstrap": "kafka-001:2181", "mongo.host": "mongo:27017"}
consul_client = ConsulClient(host="http://localhost", port="8500", token="", prefix="dev/", user="", password="")

# Create keys.
for key, value in kvs.items():
  response = self.consul_client.create_update_key(key=key, value=value)

params = {"recurse": True}
response = consul_client.read_key(key="dev/", params=params)

if response["successful_response"] == True:
  print(response["body"])

Delete key

from ConsulClient.ConsulClient import ConsulClient

kvs = {"kafka.bootstrap": "kafka-001:2181", "mongo.host": "mongo:27017"}
consul_client = ConsulClient(host="http://localhost", port="8500", token="", prefix="", user="", password="")

for key, value in kvs.items():
  response = self.consul_client.delete_key(key=key)
  if response["successful_response"] == True:
    continue
  else:
    raise Exception(f"Could not delete {key}. RC: {response["status_code"]. Error: {response["body"]}}")

Build package

Run the following command:

python setup.py sdist bdist_wheel

consul-client's People

Contributors

lozuwa avatar miguel-callejas-coderoad-com avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.