PEP8 Compliance
Change-Id: I93935004dabebdb9cb1b917e5f9e462aef46cb40
Issue-ID: DCAEGEN2-348
Signed-off-by: Tommy Carpenter <tommy@research.att.com>
diff --git a/onap-dcae-cbs-docker-client/Changelog.md b/onap-dcae-cbs-docker-client/Changelog.md
index 0211329..1269750 100644
--- a/onap-dcae-cbs-docker-client/Changelog.md
+++ b/onap-dcae-cbs-docker-client/Changelog.md
@@ -4,6 +4,13 @@
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
+## [1.0.2]
+* Refactor some code, PEP8 compliance
+* Add flake8 to tox
+
+## [1.0.1]
+* [Sadly, Missing]
+
## [1.0.0]
* Depend on the CBS 2.0.0 API
* Add new endpoint for getting all
diff --git a/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/__init__.py b/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/__init__.py
index d1f044b..e9d0246 100644
--- a/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/__init__.py
+++ b/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/__init__.py
@@ -15,4 +15,3 @@
# ============LICENSE_END=========================================================
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.
-
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 f21ad0e..adcec85 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
@@ -17,39 +17,47 @@
# ECOMP is a trademark and service mark of AT&T Intellectual Property.
import json
-import requests
import os
import logging
+import requests
-root = logging.getLogger()
-logger = root.getChild(__name__)
+LOGGER = logging.getLogger().getChild(__name__)
+
+
+class ENVsMissing(Exception):
+ """
+ Exception to represent critical ENVs are missing
+ """
+ pass
+
#########
# HELPERS
+
def _get_uri_from_consul(consul_url, name):
"""
Call consul's catalog
- TODO: currently assumes there is only one service with this HOSTNAME
+ TODO: currently assumes there is only one service with this hostname
"""
url = "{0}/v1/catalog/service/{1}".format(consul_url, name)
- logger.debug("Trying to lookup service: {0}".format(url))
+ LOGGER.debug("Trying to lookup service: {0}".format(url))
res = requests.get(url)
- try:
- res.raise_for_status()
- services = res.json()
- return "http://{0}:{1}".format(services[0]["ServiceAddress"], services[0]["ServicePort"])
- except Exception as e:
- logger.error("Exception occured when querying Consul: either could not hit {0} or no service registered. Error code: {1}, Error Text: {2}".format(url, res.status_code, res.text))
- return None
+ res.raise_for_status()
+ services = res.json()
+ return "http://{0}:{1}".format(services[0]["ServiceAddress"], services[0]["ServicePort"])
+
def _get_envs():
"""
- Returns HOSTNAME, CONSUL_HOST, CONFIG_BINDING_SERVICE or crashes for caller to deal with
+ Returns hostname, consul_host.
+ If the necessary ENVs are not found, this is fatal, and raises an exception.
"""
- HOSTNAME = os.environ["HOSTNAME"]
- CONSUL_HOST = os.environ["CONSUL_HOST"]
- return HOSTNAME, CONSUL_HOST
+ if "HOSTNAME" not in os.environ or "CONSUL_HOST" not in os.environ:
+ raise ENVsMissing("HOSTNAME or CONSUL_HOST missing")
+ hostname = os.environ["HOSTNAME"]
+ consul_host = os.environ["CONSUL_HOST"]
+ return hostname, consul_host
def _get_path(path):
@@ -64,32 +72,39 @@
config = {}
- HOSTNAME, CONSUL_HOST = _get_envs()
+ hostname, consul_host = _get_envs()
- #not sure how I as the component developer is supposed to know consul port
- consul_url = "http://{0}:8500".format(CONSUL_HOST)
+ # not sure how I as the component developer is supposed to know consul port
+ consul_url = "http://{0}:8500".format(consul_host)
- #get the CBS URL. Would not need the following hoorahrah if we had DNS.
- cbs_url = _get_uri_from_consul(consul_url, "config_binding_service")
- if cbs_url is None:
- logger.error("Cannot bind config at this time, cbs is unreachable")
- else:
- #get my config
- my_config_endpoint = "{0}/{1}/{2}".format(cbs_url, path, HOSTNAME)
+ try:
+ # get my config
+ cbs_url = _get_uri_from_consul(consul_url, "config_binding_service")
+ my_config_endpoint = "{0}/{1}/{2}".format(cbs_url, path, hostname)
res = requests.get(my_config_endpoint)
- try:
- res.raise_for_status()
- config = res.json()
- logger.info("get_config returned the following configuration: {0}".format(json.dumps(config)))
- except:
- 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))
+
+ res.raise_for_status()
+ config = res.json()
+ LOGGER.info("get_config returned the following configuration: {0}".format(
+ json.dumps(config)))
+ except requests.exceptions.HTTPError as exc:
+ LOGGER.error("in get_config, the config binding service endpoint %s was not reachable. Error code: %d, Error text: %s", my_config_endpoint, res.status_code, res.text)
+ except Exception as exc:
+ LOGGER.exception(exc)
return config
#########
# Public
def get_all():
+ """
+ Hit the CBS service_component_all endpoint
+ """
return _get_path("service_component_all")
+
def get_config():
+ """
+ Hit the CBS service_component endpoint
+ """
return _get_path("service_component")
diff --git a/onap-dcae-cbs-docker-client/setup.py b/onap-dcae-cbs-docker-client/setup.py
index fd7dc4b..b4fdf0e 100644
--- a/onap-dcae-cbs-docker-client/setup.py
+++ b/onap-dcae-cbs-docker-client/setup.py
@@ -16,20 +16,19 @@
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.
-import os
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 = "1.0.1",
+ name="onap_dcae_cbs_docker_client",
+ description="very lightweight client for a DCAE dockerized component to get it's config from the CBS",
+ version="1.0.2",
packages=find_packages(),
- author = "Tommy Carpenter",
- author_email = "tommy@research.att.com",
+ author="Tommy Carpenter",
+ author_email="tommy@research.att.com",
license='Apache 2',
- keywords = "",
- url = "",
- install_requires = [
- "requests"
+ keywords="",
+ url="",
+ install_requires=[
+ "requests>= 2.0.0, < 3.0.0"
]
)
diff --git a/onap-dcae-cbs-docker-client/tests/test_client.py b/onap-dcae-cbs-docker-client/tests/test_client.py
index 8512fad..dd6ab10 100644
--- a/onap-dcae-cbs-docker-client/tests/test_client.py
+++ b/onap-dcae-cbs-docker-client/tests/test_client.py
@@ -17,17 +17,21 @@
# ECOMP is a trademark and service mark of AT&T Intellectual Property.
from onap_dcae_cbs_docker_client.client import get_config, get_all
+
class FakeResponse:
def __init__(self, status_code, thejson):
self.status_code = status_code
self.thejson = thejson
+
def raise_for_status(self):
pass
+
def json(self):
return self.thejson
+
def monkeyed_requests_get(url):
- #mock all the get calls for existent and non-existent
+ # 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",
@@ -49,6 +53,7 @@
monkeypatch.setattr('requests.get', monkeyed_requests_get)
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},
diff --git a/onap-dcae-cbs-docker-client/tox-local.ini b/onap-dcae-cbs-docker-client/tox-local.ini
index 244386f..8e48932 100644
--- a/onap-dcae-cbs-docker-client/tox-local.ini
+++ b/onap-dcae-cbs-docker-client/tox-local.ini
@@ -1,5 +1,5 @@
[tox]
-envlist = py27,py36
+envlist = py27,py36,flake8
[testenv]
deps=
@@ -11,4 +11,11 @@
HOSTNAME = mybestfrienddotcom
commands=pytest --cov {envsitepackagesdir}/onap_dcae_cbs_docker_client --cov-report html
+[testenv:flake8]
+basepython = python3.6
+skip_install = true
+deps = flake8
+commands = flake8 setup.py onap_dcae_cbs_docker_client tests
+[flake8]
+ignore = E501
diff --git a/onap-dcae-cbs-docker-client/tox.ini b/onap-dcae-cbs-docker-client/tox.ini
index d3aa5da..6bbe73f 100644
--- a/onap-dcae-cbs-docker-client/tox.ini
+++ b/onap-dcae-cbs-docker-client/tox.ini
@@ -1,6 +1,6 @@
# content of: tox.ini , put in same dir as setup.py
[tox]
-envlist = py27,py36
+envlist = py27,py36,flake8
[testenv]
deps=
@@ -15,3 +15,12 @@
commands=
pytest --junitxml xunit-results.xml --cov onap_dcae_cbs_docker_client --cov-report xml
coverage xml
+
+[testenv:flake8]
+basepython = python3.6
+skip_install = true
+deps = flake8
+commands = flake8 setup.py onap_dcae_cbs_docker_client tests
+
+[flake8]
+ignore = E501