summaryrefslogtreecommitdiffstats
path: root/onap-dcae-cbs-docker-client
diff options
context:
space:
mode:
Diffstat (limited to 'onap-dcae-cbs-docker-client')
-rw-r--r--onap-dcae-cbs-docker-client/.gitignore1
-rw-r--r--onap-dcae-cbs-docker-client/Changelog.md4
-rw-r--r--onap-dcae-cbs-docker-client/README.md7
-rw-r--r--onap-dcae-cbs-docker-client/onap_dcae_cbs_docker_client/client.py14
-rw-r--r--onap-dcae-cbs-docker-client/setup.py2
-rw-r--r--onap-dcae-cbs-docker-client/tests/test_client.py48
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"}})
lass="nn">com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.lang.StringUtils; import org.onap.appc.domainmodel.lcm.VNFOperation; import org.onap.appc.validationpolicy.executors.ActionInProgressRuleExecutor; import org.onap.appc.validationpolicy.executors.RuleExecutor; import org.onap.appc.validationpolicy.objects.Policy; import org.onap.appc.validationpolicy.objects.PolicyNames; import org.onap.appc.validationpolicy.objects.Rule; import org.onap.appc.validationpolicy.objects.ValidationJSON; import org.onap.appc.validationpolicy.rules.RuleFactory; import org.onap.ccsdk.sli.core.dblib.DbLibService; import javax.sql.rowset.CachedRowSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; /** * Reads the request validation policy on start-up and provides * accessors for rule executors */ public class RequestValidationPolicy { private DbLibService dbLibService; private RuleExecutor actionInProgressRuleExecutor; private final EELFLogger logger = EELFManager.getInstance().getLogger(RequestValidationPolicy.class); public void setDbLibService(DbLibService dbLibService) { this.dbLibService = dbLibService; } public void initialize(){ try { String jsonContent = getPolicyJson(); if (jsonContent == null) return; ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); ValidationJSON validationJSON = objectMapper.readValue(jsonContent, ValidationJSON.class); List<Policy> policyList = validationJSON.getPolicies(); policyList.stream() .filter(policy -> PolicyNames.ActionInProgress.name().equals(policy.getPolicyName())) .forEach(policy -> { Rule[] ruleDTOs = policy.getRules(); Map<String, org.onap.appc.validationpolicy.rules.Rule> rules = new HashMap<>(); for(Rule ruleDTO : ruleDTOs) { String action = ruleDTO.getActionReceived(); String validationRule = ruleDTO.getValidationRule(); Set<VNFOperation> inclusionSet = null; Set<VNFOperation> exclusionSet = null; if (ruleDTO.getInclusionList() != null && !ruleDTO.getInclusionList().isEmpty()) { inclusionSet = ruleDTO.getInclusionList().stream() .map(VNFOperation::findByString).filter(operation -> operation != null) .collect(Collectors.toSet()); } if (ruleDTO.getExclusionList() != null && !ruleDTO.getExclusionList().isEmpty()) { exclusionSet = ruleDTO.getExclusionList().stream() .map(VNFOperation::findByString).filter(operation -> operation != null) .collect(Collectors.toSet()); } org.onap.appc.validationpolicy.rules.Rule rule = RuleFactory .createRule(validationRule, inclusionSet, exclusionSet); rules.put(action, rule); } actionInProgressRuleExecutor = new ActionInProgressRuleExecutor(Collections.unmodifiableMap(rules)); }); } catch (Exception e) { logger.error("Error reading request validation policies", e); } } protected String getPolicyJson() { String schema = "sdnctl"; String query = "SELECT MAX(INTERNAL_VERSION),ARTIFACT_CONTENT " + "FROM ASDC_ARTIFACTS " + "WHERE ARTIFACT_NAME = ? " + "GROUP BY ARTIFACT_NAME"; ArrayList<String> arguments = new ArrayList<>(); arguments.add("request_validation_policy"); String jsonContent = null; try{ CachedRowSet rowSet = dbLibService.getData(query, arguments, schema); if(rowSet.next()){ jsonContent = rowSet.getString("ARTIFACT_CONTENT"); } if(logger.isDebugEnabled()){ logger.debug("request validation policy = " + jsonContent); } if(StringUtils.isBlank(jsonContent)){ logger.warn("request validation policy not found in app-c database"); } } catch(SQLException e){ logger.error("Error accessing database", e); throw new RuntimeException(e); } return jsonContent; } public RuleExecutor getInProgressRuleExecutor(){ if(actionInProgressRuleExecutor == null){ throw new RuntimeException("Rule executor not available, initialization of RequestValidationPolicy failed"); } return actionInProgressRuleExecutor; } }