From 19470fb77656cd6680058f456fb0f09e86f504dc Mon Sep 17 00:00:00 2001 From: Alex Shatov Date: Mon, 26 Mar 2018 17:16:29 -0400 Subject: 2.2.0 dcaepolicyplugin and data types - dcaepolicyplugin to use default policy-handler url by dns to discover policy-handler under k8s when policy_handler record not found under consul-service - pending hardcoding the proper dns based url that is to be provided by k8s + MSB solution Change-Id: I8bf49fe29735ec842235c0b58595c7196f5d24ca Signed-off-by: Alex Shatov Issue-ID: DCAEGEN2-419 --- dcae-policy/dcaepolicy-node-type.yaml | 2 +- dcae-policy/dcaepolicyplugin/discovery.py | 21 ++++++++++---- dcae-policy/dcaepolicyplugin/tasks.py | 5 +++- dcae-policy/pom.xml | 2 +- dcae-policy/setup.py | 2 +- dcae-policy/tests/test_tasks.py | 47 ++++++++++++++++++++++++++----- 6 files changed, 62 insertions(+), 17 deletions(-) diff --git a/dcae-policy/dcaepolicy-node-type.yaml b/dcae-policy/dcaepolicy-node-type.yaml index 04b2fd6..d174294 100644 --- a/dcae-policy/dcaepolicy-node-type.yaml +++ b/dcae-policy/dcaepolicy-node-type.yaml @@ -25,7 +25,7 @@ plugins: dcaepolicy: executor: 'central_deployment_agent' package_name: dcaepolicyplugin - package_version: 2.1.0 + package_version: 2.2.0 data_types: # the properties inside dcae.data.policy_filter are identical to /getConfig API of policy-engine except the requestID field. diff --git a/dcae-policy/dcaepolicyplugin/discovery.py b/dcae-policy/dcaepolicyplugin/discovery.py index f463b04..45a061b 100644 --- a/dcae-policy/dcaepolicyplugin/discovery.py +++ b/dcae-policy/dcaepolicyplugin/discovery.py @@ -20,14 +20,23 @@ import requests +from cloudify import ctx + # it is safe to assume that consul agent is at localhost:8500 along with cloudify manager CONSUL_SERVICE_URL = "http://localhost:8500/v1/catalog/service/{0}" def discover_service_url(service_name): """find the service record in consul""" - response = requests.get(CONSUL_SERVICE_URL.format(service_name)) - response.raise_for_status() - resp_json = response.json() - if resp_json: - service = resp_json[0] - return "http://{0}:{1}".format(service["ServiceAddress"], service["ServicePort"]) + service_url_url = CONSUL_SERVICE_URL.format(service_name) + ctx.logger.info("getting service_url at {0}".format(service_url_url)) + + response = requests.get(service_url_url) + + ctx.logger.info("got service_url at {0} status({1}) response: {2}" + .format(service_url_url, response.status_code, response.text)) + + if response.status_code == requests.codes.ok: + resp_json = response.json() + if resp_json: + service = resp_json[0] + return "http://{0}:{1}".format(service["ServiceAddress"], service["ServicePort"]) diff --git a/dcae-policy/dcaepolicyplugin/tasks.py b/dcae-policy/dcaepolicyplugin/tasks.py index fd3e37c..b3a29aa 100644 --- a/dcae-policy/dcaepolicyplugin/tasks.py +++ b/dcae-policy/dcaepolicyplugin/tasks.py @@ -50,6 +50,7 @@ class PolicyHandler(object): SERVICE_NAME_POLICY_HANDLER = "policy_handler" X_ECOMP_REQUESTID = 'X-ECOMP-RequestID' STATUS_CODE_POLICIES_NOT_FOUND = 404 + DEFAULT_URL = "http://policy-handler" _url = None @staticmethod @@ -59,6 +60,8 @@ class PolicyHandler(object): return PolicyHandler._url = discover_service_url(PolicyHandler.SERVICE_NAME_POLICY_HANDLER) + if not PolicyHandler._url: + PolicyHandler._url = PolicyHandler.DEFAULT_URL @staticmethod def get_latest_policy(policy_id): @@ -68,7 +71,7 @@ class PolicyHandler(object): ph_path = "{0}/policy_latest/{1}".format(PolicyHandler._url, policy_id) headers = {PolicyHandler.X_ECOMP_REQUESTID: str(uuid.uuid4())} - ctx.logger.info("getting latest policy from {0} headers={1}".format( \ + ctx.logger.info("getting latest policy from {0} headers={1}".format( ph_path, json.dumps(headers))) res = requests.get(ph_path, headers=headers) ctx.logger.info("latest policy for policy_id({0}) status({1}) response: {2}" diff --git a/dcae-policy/pom.xml b/dcae-policy/pom.xml index 33fd9f6..90a0e7a 100644 --- a/dcae-policy/pom.xml +++ b/dcae-policy/pom.xml @@ -28,7 +28,7 @@ ECOMP is a trademark and service mark of AT&T Intellectual Property. org.onap.dcaegen2.platform.plugins dcae-policy dcae-policy-plugin - 2.1.0-SNAPSHOT + 2.2.0-SNAPSHOT http://maven.apache.org UTF-8 diff --git a/dcae-policy/setup.py b/dcae-policy/setup.py index c1714f9..8c54e10 100644 --- a/dcae-policy/setup.py +++ b/dcae-policy/setup.py @@ -23,7 +23,7 @@ from setuptools import setup setup( name='dcaepolicyplugin', description='Cloudify plugin for dcae.nodes.policy node to retrieve the policy config', - version="2.1.0", + version="2.2.0", author='Alex Shatov', packages=['dcaepolicyplugin'], install_requires=[ diff --git a/dcae-policy/tests/test_tasks.py b/dcae-policy/tests/test_tasks.py index 9bcf4ff..b9b69b9 100644 --- a/dcae-policy/tests/test_tasks.py +++ b/dcae-policy/tests/test_tasks.py @@ -166,9 +166,22 @@ def monkeyed_discovery_get_failure(full_path): def test_discovery_failure(monkeypatch): """test finding policy-handler in consul""" monkeypatch.setattr('requests.get', monkeyed_discovery_get_failure) - expected = None - tasks.PolicyHandler._lazy_init() - assert expected == tasks.PolicyHandler._url + + node_policy = MonkeyedNode( + 'test_dcae_policy_node_id', + 'test_dcae_policy_node_name', + tasks.DCAE_POLICY_TYPE, + {POLICY_ID: MONKEYED_POLICY_ID} + ) + try: + current_ctx.set(node_policy.ctx) + tasks.PolicyHandler._lazy_init() + assert tasks.PolicyHandler.DEFAULT_URL == tasks.PolicyHandler._url + + finally: + tasks.PolicyHandler._url = None + MockCloudifyContextFull.clear() + current_ctx.clear() def monkeyed_discovery_get(full_path): """monkeypatch for the GET to consul""" @@ -178,17 +191,36 @@ def monkeyed_discovery_get(full_path): def test_discovery(monkeypatch): """test finding policy-handler in consul""" monkeypatch.setattr('requests.get', monkeyed_discovery_get) - expected = "http://monkey-policy-handler-address:9999" - tasks.PolicyHandler._lazy_init() - assert expected == tasks.PolicyHandler._url -def monkeyed_policy_handler_get(full_path, headers): + node_policy = MonkeyedNode( + 'test_dcae_policy_node_id', + 'test_dcae_policy_node_name', + tasks.DCAE_POLICY_TYPE, + {POLICY_ID: MONKEYED_POLICY_ID} + ) + + try: + current_ctx.set(node_policy.ctx) + expected = "http://monkey-policy-handler-address:9999" + CtxLogger.log_ctx_info("before PolicyHandler._lazy_init") + tasks.PolicyHandler._lazy_init() + CtxLogger.log_ctx_info("after PolicyHandler._lazy_init") + assert expected == tasks.PolicyHandler._url + + finally: + tasks.PolicyHandler._url = None + MockCloudifyContextFull.clear() + current_ctx.clear() + + +def monkeyed_policy_handler_get(full_path, headers=None): """monkeypatch for the GET to policy-engine""" return MonkeyedResponse(full_path, headers, \ MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID)) def test_policy_get(monkeypatch): """test policy_get operation on dcae.nodes.policy node""" + tasks.PolicyHandler._url = tasks.PolicyHandler.DEFAULT_URL monkeypatch.setattr('requests.get', monkeyed_policy_handler_get) node_policy = MonkeyedNode( @@ -236,6 +268,7 @@ def monkeyed_policy_handler_find(full_path, json, headers): def test_policies_find(monkeypatch): """test policy_get operation on dcae.nodes.policies node""" + tasks.PolicyHandler._url = tasks.PolicyHandler.DEFAULT_URL monkeypatch.setattr('requests.post', monkeyed_policy_handler_find) node_policies = MonkeyedNode( -- cgit 1.2.3-korg