diff options
author | Tommy Carpenter <tommy@research.att.com> | 2017-09-20 15:11:00 -0400 |
---|---|---|
committer | Tommy Carpenter <tommy@research.att.com> | 2017-09-20 16:55:34 -0400 |
commit | c3a485a0a2805519a217575fd8c984b0312e9b43 (patch) | |
tree | 71da2f7e0909a41936542e9dd0ff2209fa74ae56 /onap-dcae-cbs-docker-client | |
parent | 385a2870137aa18d558f413136a361d0562a8ac6 (diff) |
Rename the cbs docker client
Issue-ID: DCAEGEN2-60
Change-Id: Ia0d4726215efc1c2fe78bee1bdfdf1403e2c349c
Signed-off-by: Tommy Carpenter <tommy@research.att.com>
Diffstat (limited to 'onap-dcae-cbs-docker-client')
-rw-r--r-- | onap-dcae-cbs-docker-client/.gitignore | 4 | ||||
-rw-r--r-- | onap-dcae-cbs-docker-client/MANIFEST.in | 1 | ||||
-rw-r--r-- | onap-dcae-cbs-docker-client/README.md | 31 | ||||
-rw-r--r-- | onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/__init__.py | 19 | ||||
-rw-r--r-- | onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/client.py | 85 | ||||
-rw-r--r-- | onap-dcae-cbs-docker-client/pom.xml | 248 | ||||
-rw-r--r-- | onap-dcae-cbs-docker-client/requirements.txt | 1 | ||||
-rw-r--r-- | onap-dcae-cbs-docker-client/setup.py | 39 | ||||
-rw-r--r-- | onap-dcae-cbs-docker-client/tox.ini | 11 |
9 files changed, 439 insertions, 0 deletions
diff --git a/onap-dcae-cbs-docker-client/.gitignore b/onap-dcae-cbs-docker-client/.gitignore new file mode 100644 index 0000000..31c3000 --- /dev/null +++ b/onap-dcae-cbs-docker-client/.gitignore @@ -0,0 +1,4 @@ +dist/ +.DS_Store +*.egg-info/ +*.pyc diff --git a/onap-dcae-cbs-docker-client/MANIFEST.in b/onap-dcae-cbs-docker-client/MANIFEST.in new file mode 100644 index 0000000..f9bd145 --- /dev/null +++ b/onap-dcae-cbs-docker-client/MANIFEST.in @@ -0,0 +1 @@ +include requirements.txt diff --git a/onap-dcae-cbs-docker-client/README.md b/onap-dcae-cbs-docker-client/README.md new file mode 100644 index 0000000..b5ddd04 --- /dev/null +++ b/onap-dcae-cbs-docker-client/README.md @@ -0,0 +1,31 @@ +# 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. + +# 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. +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 + +## Usage in your code +``` +>>> from onap_dcae_cbs_docker_client import client +>>> client.get_config() +``` + +# Installation + +## Via pip +``` +pip install --extra-index-url https://YOUR_NEXUS_PYPI_SERVER/simple cbs-docker-client +``` + +## Via requirements.txt +Add the following to your requirements.txt file +``` +--extra-index-url https://YOUR_NEXUS_PYPI_SERVER/simple +onap-dcae-cbs-docker-client==[version] +``` + 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 new file mode 100644 index 0000000..9e81f65 --- /dev/null +++ b/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/__init__.py @@ -0,0 +1,19 @@ +# org.onap.dcae +# ================================================================================ +# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============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 new file mode 100644 index 0000000..4423995 --- /dev/null +++ b/onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/client.py @@ -0,0 +1,85 @@ +# org.onap.dcae +# ================================================================================ +# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +# +# ECOMP is a trademark and service mark of AT&T Intellectual Property. + +import json +import requests +import os +import logging + +root = logging.getLogger() +logger = root.getChild(__name__) + +def _get_uri_from_consul(consul_url, name): + """ + Call consul's catalog + 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)) + 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 + +def _get_envs(): + """ + Returns HOSTNAME, CONSUL_HOST, CONFIG_BINDING_SERVICE or crashes for caller to deal with + """ + HOSTNAME = os.environ["HOSTNAME"] + CONSUL_HOST = os.environ["CONSUL_HOST"] + return HOSTNAME, CONSUL_HOST + +#Public +def get_config(): + """ + 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. + It could be a temporary network outage. Call me again later. + + It will raise an exception if the necessary env parameters were not set because that is irrecoverable. + This function is called in my /heatlhcheck, so this will be caught early. + """ + + config = {} + + 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) + + #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}/service_component/{1}".format(cbs_url, 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)) + return config + diff --git a/onap-dcae-cbs-docker-client/pom.xml b/onap-dcae-cbs-docker-client/pom.xml new file mode 100644 index 0000000..f1b267e --- /dev/null +++ b/onap-dcae-cbs-docker-client/pom.xml @@ -0,0 +1,248 @@ +<?xml version="1.0"?> +<!-- +================================================================================ +Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. +================================================================================ +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +============LICENSE_END========================================================= + +ECOMP is a trademark and service mark of AT&T Intellectual Property. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.onap.dcaegen2.utils</groupId> + <artifactId>utils</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + <groupId>org.onap.dcaegen2.utils</groupId> + <artifactId>onap-dcae-cbs-docker-client</artifactId> + <name>dcaegen2-utils-python-cbs-docker-client</name> + <version>1.0.0-SNAPSHOT</version> + <url>http://maven.apache.org</url> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <!--sonar.skip>false</sonar.skip--> + <sonar.sources>.</sonar.sources> + <!-- customize the SONARQUBE URL --> + <!-- sonar.host.url>http://localhost:9000</sonar.host.url --> + <!-- below are language dependent --> + <!-- for Python --> + <sonar.language>py</sonar.language> + <sonar.pluginName>Python</sonar.pluginName> + <sonar.inclusions>**/*.py</sonar.inclusions> + <!-- for JavaScaript --> + <!-- + <sonar.language>js</sonar.language> + <sonar.pluginName>JS</sonar.pluginName> + <sonar.inclusions>**/*.js</sonar.inclusions> + --> + </properties> + <build> + <finalName>${project.artifactId}-${project.version}</finalName> + <pluginManagement> + <plugins> + <!-- the following plugins are invoked from oparent, we do not need them --> + <plugin> + <groupId>org.sonatype.plugins</groupId> + <artifactId>nexus-staging-maven-plugin</artifactId> + <version>1.6.7</version> + <configuration> + <skipNexusStagingDeployMojo>true</skipNexusStagingDeployMojo> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-deploy-plugin</artifactId> + <!-- This version supports the "deployAtEnd" parameter --> + <version>2.8</version> + <configuration> + <skip>true</skip> + </configuration> + </plugin> + <!-- first disable the default Java plugins at various stages --> + <!-- maven-resources-plugin is called during "*resource" phases by default behavior. it prepares + the resources dir. we do not need it --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-resources-plugin</artifactId> + <version>2.6</version> + <configuration> + <skip>true</skip> + </configuration> + </plugin> + <!-- maven-compiler-plugin is called during "compile" phases by default behavior. we do not need it --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.1</version> + <configuration> + <skip>true</skip> + </configuration> + </plugin> + <!-- maven-jar-plugin is called during "compile" phase by default behavior. we do not need it --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <version>2.4</version> + <executions> + <execution> + <id>default-jar</id> + <phase/> + </execution> + </executions> + </plugin> + <!-- maven-install-plugin is called during "install" phase by default behavior. it tries to copy stuff under + target dir to ~/.m2. we do not need it --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-install-plugin</artifactId> + <version>2.4</version> + <configuration> + <skip>true</skip> + </configuration> + </plugin> + <!-- maven-surefire-plugin is called during "test" phase by default behavior. it triggers junit test. + we do not need it --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <version>2.12.4</version> + <configuration> + <skipTests>true</skipTests> + </configuration> + </plugin> + </plugins> + </pluginManagement> + <plugins> + <!-- plugin> + <artifactId>maven-assembly-plugin</artifactId> + <version>2.4.1</version> + <configuration> + <descriptors> + <descriptor>assembly/dep.xml</descriptor> + </descriptors> + </configuration> + <executions> + <execution> + <id>make-assembly</id> + <phase>package</phase> + <goals> + <goal>single</goal> + </goals> + </execution> + </executions> + </plugin --> + <!-- now we configure custom action (calling a script) at various lifecycle phases --> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>exec-maven-plugin</artifactId> + <version>1.2.1</version> + <executions> + <execution> + <id>clean phase script</id> + <phase>clean</phase> + <goals> + <goal>exec</goal> + </goals> + <configuration> + <arguments> + <argument>${project.artifactId}</argument> + <argument>clean</argument> + </arguments> + </configuration> + </execution> + <execution> + <id>generate-sources script</id> + <phase>generate-sources</phase> + <goals> + <goal>exec</goal> + </goals> + <configuration> + <arguments> + <argument>${project.artifactId}</argument> + <argument>generate-sources</argument> + </arguments> + </configuration> + </execution> + <execution> + <id>compile script</id> + <phase>compile</phase> + <goals> + <goal>exec</goal> + </goals> + <configuration> + <arguments> + <argument>${project.artifactId}</argument> + <argument>compile</argument> + </arguments> + </configuration> + </execution> + <execution> + <id>package script</id> + <phase>package</phase> + <goals> + <goal>exec</goal> + </goals> + <configuration> + <arguments> + <argument>${project.artifactId}</argument> + <argument>package</argument> + </arguments> + </configuration> + </execution> + <execution> + <id>test script</id> + <phase>test</phase> + <goals> + <goal>exec</goal> + </goals> + <configuration> + <arguments> + <argument>${project.artifactId}</argument> + <argument>test</argument> + </arguments> + </configuration> + </execution> + <execution> + <id>install script</id> + <phase>install</phase> + <goals> + <goal>exec</goal> + </goals> + <configuration> + <arguments> + <argument>${project.artifactId}</argument> + <argument>install</argument> + </arguments> + </configuration> + </execution> + <execution> + <id>deploy script</id> + <phase>deploy</phase> + <goals> + <goal>exec</goal> + </goals> + <configuration> + <arguments> + <argument>${project.artifactId}</argument> + <argument>deploy</argument> + </arguments> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> diff --git a/onap-dcae-cbs-docker-client/requirements.txt b/onap-dcae-cbs-docker-client/requirements.txt new file mode 100644 index 0000000..856c82c --- /dev/null +++ b/onap-dcae-cbs-docker-client/requirements.txt @@ -0,0 +1 @@ +requests==2.18.3 diff --git a/onap-dcae-cbs-docker-client/setup.py b/onap-dcae-cbs-docker-client/setup.py new file mode 100644 index 0000000..cef2525 --- /dev/null +++ b/onap-dcae-cbs-docker-client/setup.py @@ -0,0 +1,39 @@ +# org.onap.dcae +# ================================================================================ +# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +# +# ECOMP is a trademark and service mark of AT&T Intellectual Property. + +import os +from setuptools import setup, find_packages +from pip.req import parse_requirements +from pip.download import PipSession + +install_reqs = parse_requirements("requirements.txt", session=PipSession()) +reqs = [str(ir.req) for ir in install_reqs] + +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.2", + packages=find_packages(), + author = "Tommy Carpenter", + author_email = "tommy at eh tee tee.com", + license = "", + keywords = "", + url = "", + install_requires=reqs +) diff --git a/onap-dcae-cbs-docker-client/tox.ini b/onap-dcae-cbs-docker-client/tox.ini new file mode 100644 index 0000000..987b009 --- /dev/null +++ b/onap-dcae-cbs-docker-client/tox.ini @@ -0,0 +1,11 @@ +# content of: tox.ini , put in same dir as setup.py +[tox] +envlist = py27,py35 + +[testenv] +deps= + -rrequirements.txt + pytest + coverage + pytest-cov +commands=pytest --junitxml xunit-results.xml --cov {envsitepackagesdir} --cov-report=xml |