From d1a814370eb3e45368af9d6a76f8ded77cca5c82 Mon Sep 17 00:00:00 2001 From: Tommy Carpenter Date: Thu, 22 Feb 2018 14:32:46 -0500 Subject: Implement the CBS 2.0.0 API Change-Id: Iadf9e3d071ea24e240599fca2c13d1251e31f729 Issue-ID: DCAEGEN2-351 Signed-off-by: Tommy Carpenter --- onap-dcae-cbs-docker-client/.gitignore | 1 + onap-dcae-cbs-docker-client/Changelog.md | 4 ++ onap-dcae-cbs-docker-client/README.md | 7 ++-- .../onap_dcae_cbs_docker_client/client.py | 14 +++++-- onap-dcae-cbs-docker-client/setup.py | 2 +- onap-dcae-cbs-docker-client/tests/test_client.py | 48 +++++++++++----------- 6 files changed, 45 insertions(+), 31 deletions(-) diff --git a/onap-dcae-cbs-docker-client/.gitignore b/onap-dcae-cbs-docker-client/.gitignore index c86ccf6..4f07413 100644 --- a/onap-dcae-cbs-docker-client/.gitignore +++ b/onap-dcae-cbs-docker-client/.gitignore @@ -1,3 +1,4 @@ +.pytest_cache/ xunit-results.xml .DS_Store # Byte-compiled / optimized / DLL files diff --git a/onap-dcae-cbs-docker-client/Changelog.md b/onap-dcae-cbs-docker-client/Changelog.md index e9c3347..0211329 100644 --- a/onap-dcae-cbs-docker-client/Changelog.md +++ b/onap-dcae-cbs-docker-client/Changelog.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [1.0.0] +* Depend on the CBS 2.0.0 API +* Add new endpoint for getting all + ## [0.0.5] * Dont pin requests version diff --git a/onap-dcae-cbs-docker-client/README.md b/onap-dcae-cbs-docker-client/README.md index 1e622a7..81a8ef0 100644 --- a/onap-dcae-cbs-docker-client/README.md +++ b/onap-dcae-cbs-docker-client/README.md @@ -1,11 +1,11 @@ # Python CBS Docker Client -Used for DCAE Dockerized microservices written in Python. Pulls your configuration from the config_binding_service. Expects that CONSUL_HOST and HOSTNAME are set as env variables, which is true in DCAE. +Used for DCAE Dockerized microservices written in Python. Pulls your configuration from the config_binding_service. Expects that CONSUL_HOST and HOSTNAME are set as env variables, which is true in DCAE. # Client Usage ## Development outside of Docker -To test your raw code without Docker, you will need to set the env variables CONSUL_HOST and HOSTNAME (name of your key to pull from) that are set in DCAEs Docker enviornment. +To test your raw code without Docker, you will need to set the env variables CONSUL_HOST and HOSTNAME (name of your key to pull from) that are set in DCAEs Docker enviornment. 1. `CONSUL_HOST` is the hostname only of the Consul instance you are talking to 2. HOSTNAME is the name of your component in Consul @@ -13,6 +13,7 @@ To test your raw code without Docker, you will need to set the env variables CON ``` >>> from onap_dcae_cbs_docker_client import client >>> client.get_config() +>>> client.get_all() ``` # Installation @@ -24,6 +25,6 @@ pip install onap-dcae-cbs-docker-client # Testing ``` -tox -c tox-local.ini +tox ``` diff --git a/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/client.py b/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/client.py index 3abf447..f83111c 100644 --- a/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/client.py +++ b/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/client.py @@ -24,6 +24,9 @@ import logging root = logging.getLogger() logger = root.getChild(__name__) +######### +# HELPERS + def _get_uri_from_consul(consul_url, name): """ Call consul's catalog @@ -48,8 +51,10 @@ def _get_envs(): CONSUL_HOST = os.environ["CONSUL_HOST"] return HOSTNAME, CONSUL_HOST -#Public -def get_config(): + +######### +# Public +def get_all(): """ This call does not raise an exception if Consul or the CBS cannot complete the request. It logs an error and returns {} if the config is not bindable. @@ -72,7 +77,7 @@ def get_config(): logger.error("Cannot bind config at this time, cbs is unreachable") else: #get my config - my_config_endpoint = "{0}/service_component/{1}".format(cbs_url, HOSTNAME) + my_config_endpoint = "{0}/service_component_all/{1}".format(cbs_url, HOSTNAME) res = requests.get(my_config_endpoint) try: res.raise_for_status() @@ -82,3 +87,6 @@ def get_config(): logger.error("in get_config, the config binding service endpoint {0} blew up on me. Error code: {1}, Error text: {2}".format(my_config_endpoint, res.status_code, res.text)) return config +def get_config(): + allk = get_all() + return allk["config"] diff --git a/onap-dcae-cbs-docker-client/setup.py b/onap-dcae-cbs-docker-client/setup.py index fcefa91..d3236e8 100644 --- a/onap-dcae-cbs-docker-client/setup.py +++ b/onap-dcae-cbs-docker-client/setup.py @@ -22,7 +22,7 @@ from setuptools import setup, find_packages setup( name = "onap_dcae_cbs_docker_client", description = "very lightweight client for a DCAE dockerized component to get it's config from the CBS", - version = "0.0.5", + version = "1.0.0", packages=find_packages(), author = "Tommy Carpenter", author_email = "tommy@research.att.com", diff --git a/onap-dcae-cbs-docker-client/tests/test_client.py b/onap-dcae-cbs-docker-client/tests/test_client.py index db8e1b5..1e4ec13 100644 --- a/onap-dcae-cbs-docker-client/tests/test_client.py +++ b/onap-dcae-cbs-docker-client/tests/test_client.py @@ -15,8 +15,7 @@ # ============LICENSE_END========================================================= # # ECOMP is a trademark and service mark of AT&T Intellectual Property. -from onap_dcae_cbs_docker_client.client import get_config -import requests +from onap_dcae_cbs_docker_client.client import get_config, get_all class FakeResponse: def __init__(self, status_code, thejson): @@ -27,27 +26,28 @@ class FakeResponse: def json(self): return self.thejson -def test_client(monkeypatch): - - def monkeyed_requests_get(url): - #mock all the get calls for existent and non-existent - if url == "http://consuldotcom:8500/v1/catalog/service/config_binding_service": - return FakeResponse( - status_code = 200, - thejson = [ - {"ServiceAddress" : "666.666.666.666", - "ServicePort" : 8888 - }] - ) - elif url == "http://666.666.666.666:8888/service_component/mybestfrienddotcom": - return FakeResponse( - status_code = 200, - thejson = {"key_to_your_heart" : 666}) - - +def monkeyed_requests_get(url): + #mock all the get calls for existent and non-existent + if url == "http://consuldotcom:8500/v1/catalog/service/config_binding_service": + return FakeResponse(status_code=200, + thejson=[{"ServiceAddress": "666.666.666.666", + "ServicePort": 8888}]) + elif url == "http://666.666.666.666:8888/service_component_all/mybestfrienddotcom": + return FakeResponse(status_code=200, + thejson={"config": {"key_to_your_heart": 666}, + "dti": {"some amazing": "dti stuff"}, + "policies": {"event": {"foo": "bar"}, + "items": [{"foo2": "bar2"}]}, + "otherkey": {"foo3": "bar3"}}) + +def test_config(monkeypatch): monkeypatch.setattr('requests.get', monkeyed_requests_get) - assert(get_config() == {"key_to_your_heart" : 666}) - - - + assert(get_config() == {"key_to_your_heart": 666}) +def test_all(monkeypatch): + monkeypatch.setattr('requests.get', monkeyed_requests_get) + assert(get_all() == {"config": {"key_to_your_heart": 666}, + "dti": {"some amazing": "dti stuff"}, + "policies": {"event": {"foo": "bar"}, + "items": [{"foo2": "bar2"}]}, + "otherkey": {"foo3": "bar3"}}) -- cgit 1.2.3-korg