DCAEGEN2-1920 enhance policy plugin for error conditions 21/98421/3
authorSchmalzried, Terry (ts862m) <ts862m@att.com>
Thu, 14 Nov 2019 18:07:56 +0000 (13:07 -0500)
committerSchmalzried, Terry (ts862m) <ts862m@att.com>
Thu, 14 Nov 2019 20:26:45 +0000 (15:26 -0500)
Issue-ID: DCAEGEN2-1920
Change-Id: I7e1b08cb008f60d7f08ed2db41236a03b8490287
Signed-off-by: Schmalzried, Terry (ts862m) <ts862m@att.com>
dcae-policy/README.md
dcae-policy/dcaepolicy-node-type.yaml
dcae-policy/dcaepolicyplugin/discovery.py
dcae-policy/dcaepolicyplugin/tasks.py
dcae-policy/pom.xml
dcae-policy/setup.py
dcae-policy/tests/test_discovery.py
dcae-policy/tests/test_tasks.py

index a3a070b..db729e9 100644 (file)
@@ -35,7 +35,7 @@ import the dcaepolicy-node-type.yaml into your blueprint to use the dcae.nodes.t
 
 ```yaml
 imports:
-    - https://YOUR_NEXUS_RAW_SERVER/type_files/dcaepolicy/2.3.0/node-type.yaml
+    - https://YOUR_NEXUS_RAW_SERVER/type_files/dcaepolicy/2.3.2/node-type.yaml
 ```
 
 provide the value for policy_id property
index 1c07b7a..ec7f373 100644 (file)
@@ -1,5 +1,5 @@
 # ================================================================================
-# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
+# Copyright (c) 2017-2019 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.
 #
 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
-tosca_definitions_version: cloudify_dsl_1_3
-
-imports:
-    - http://www.getcloudify.org/spec/cloudify/3.4/types.yaml
-
 plugins:
   dcaepolicy:
     executor: 'central_deployment_agent'
     package_name: dcaepolicyplugin
-    package_version: 2.3.0
+    package_version: 2.3.2
 
 data_types:
     # the properties inside dcae.data.policy_filter are identical to /getConfig API of policy-engine except the requestID field.
@@ -77,6 +72,10 @@ node_types:
             policy_filter:
                 type: dcae.data.policy_filter
                 default: {}
+            policy_required:
+                description: whether to throw an exception when failed to get a policy
+                type: boolean
+                default: false
         interfaces:
             cloudify.interfaces.lifecycle:
                 create:
index 0517377..8612160 100644 (file)
@@ -1,5 +1,5 @@
 # ================================================================================
-# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
+# Copyright (c) 2017-2019 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.
@@ -40,7 +40,7 @@ def discover_service_url(service_name):
     ctx.logger.info("getting service_url at {0}".format(service_url))
 
     try:
-        response = requests.get(service_url)
+        response = requests.get(service_url, timeout=60)
     except requests.ConnectionError as ex:
         raise NonRecoverableError(
             "ConnectionError - failed to get {0}: {1}".format(service_url, str(ex)))
@@ -63,7 +63,7 @@ def discover_value(key):
     ctx.logger.info("getting kv at {0}".format(kv_url))
 
     try:
-        response = requests.get(kv_url)
+        response = requests.get(kv_url, timeout=60)
     except requests.ConnectionError as ex:
         raise NonRecoverableError(
             "ConnectionError - failed to get {0}: {1}".format(kv_url, str(ex)))
index bbf3ec1..4e3b37a 100644 (file)
@@ -1,5 +1,5 @@
 # ================================================================================
-# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
+# Copyright (c) 2017-2019 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.
@@ -94,7 +94,7 @@ class PolicyHandler(object):
 
         ctx.logger.info("getting latest policy from {0} headers={1}".format(
             ph_path, json.dumps(headers)))
-        res = requests.get(ph_path, headers=headers)
+        res = requests.get(ph_path, headers=headers, timeout=60)
         ctx.logger.info("latest policy for policy_id({0}) status({1}) response: {2}"
                         .format(policy_id, res.status_code, res.text))
 
@@ -117,7 +117,7 @@ class PolicyHandler(object):
         ctx.logger.info("finding the latest polices from {0} by {1} headers={2}".format(
             ph_path, json.dumps(policy_filter), json.dumps(headers)))
 
-        res = requests.post(ph_path, json=policy_filter, headers=headers)
+        res = requests.post(ph_path, json=policy_filter, headers=headers, timeout=60)
         ctx.logger.info("latest policies status({0}) response: {1}"
                         .format(res.status_code, res.text))
 
@@ -150,8 +150,7 @@ def _policy_get():
     except Exception as ex:
         error = "failed to get policy({0}): {1}".format(policy_id, str(ex))
         ctx.logger.error("{0}: {1}".format(error, traceback.format_exc()))
-        if policy_required:
-            raise NonRecoverableError(error)
+        raise NonRecoverableError(error)
 
     if not policy:
         error = "policy not found for policy_id {0}".format(policy_id)
@@ -192,6 +191,8 @@ def _policies_find():
     if DCAE_POLICIES_TYPE not in ctx.node.type_hierarchy:
         return
 
+    policy_required = ctx.node.properties.get(POLICY_REQUIRED)
+
     try:
         policy_filter = copy.deepcopy(dict(
             (k, v) for (k, v) in dict(ctx.node.properties.get(POLICY_FILTER, {})).iteritems()
@@ -205,7 +206,10 @@ def _policies_find():
         policies_filtered = PolicyHandler.find_latest_policies(policy_filter)
 
         if not policies_filtered:
-            ctx.logger.info("policies not found by {0}".format(json.dumps(policy_filter)))
+            error = "policies not found by {0}".format(json.dumps(policy_filter))
+            ctx.logger.info(error)
+            if policy_required:
+                raise NonRecoverableError(error)
             return True
 
         ctx.logger.info("found policies by {0}: {1}".format(
@@ -216,6 +220,7 @@ def _policies_find():
     except Exception as ex:
         error = "failed to find policies: {0}".format(str(ex))
         ctx.logger.error("{0}: {1}".format(error, traceback.format_exc()))
+        raise NonRecoverableError(error)
 
     return True
 
index 949ecf8..0b8da47 100644 (file)
@@ -1,7 +1,7 @@
 <?xml version="1.0"?>
 <!--
 ================================================================================
-Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
+Copyright (c) 2017-2019 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.
@@ -28,7 +28,7 @@ ECOMP is a trademark and service mark of AT&T Intellectual Property.
   <groupId>org.onap.dcaegen2.platform.plugins</groupId>
   <artifactId>dcae-policy</artifactId>
   <name>dcae-policy-plugin</name>
-  <version>2.3.0-SNAPSHOT</version>
+  <version>2.3.2-SNAPSHOT</version>
   <url>http://maven.apache.org</url>
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
index d1a014e..d7a6756 100644 (file)
@@ -1,5 +1,5 @@
 # ================================================================================
-# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
+# Copyright (c) 2017-2019 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.
@@ -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.3.0",
+    version="2.3.2",
     author='Alex Shatov',
     packages=['dcaepolicyplugin'],
     install_requires=[
index 129ad99..0c41fe3 100644 (file)
@@ -1,5 +1,5 @@
 # ================================================================================
-# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
+# Copyright (c) 2019 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.
@@ -35,7 +35,7 @@ from tests.mock_setup import (MONKEYED_POLICY_ID, POLICY_ID, MonkeyedNode,
 POLICY_HANDLER_FROM_KV = "http://policy_handler_from_kv:25577"
 
 
-def monkeyed_discovery_get_failure(full_path):
+def monkeyed_discovery_get_failure(full_path, **kwargs):
     """monkeypatch for the GET to consul"""
     raise requests.ConnectionError("monkey-boom")
 
@@ -65,7 +65,7 @@ def test_discovery_failure(monkeypatch):
         current_ctx.clear()
 
 
-def monkeyed_discovery_get_kv(full_path):
+def monkeyed_discovery_get_kv(full_path, **kwargs):
     """monkeypatch for the GET to consul"""
     if full_path.startswith(discovery.CONSUL_SERVICE_URL.format("")):
         return MonkeyedResponse(full_path)
@@ -102,7 +102,7 @@ def test_discovery_kv(monkeypatch):
         current_ctx.clear()
 
 
-def monkeyed_discovery_get(full_path):
+def monkeyed_discovery_get(full_path, **kwargs):
     """monkeypatch for the GET to consul"""
     return MonkeyedResponse(full_path, {},
         [{"ServiceAddress": "monkey-policy-handler-address", "ServicePort": "9999"}])
index f2ca205..dc89d65 100644 (file)
@@ -1,5 +1,5 @@
 # ================================================================================
-# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
+# Copyright (c) 2017-2019 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.
@@ -36,7 +36,7 @@ from tests.mock_setup import (CONFIG_NAME, MONKEYED_POLICY_ID, POLICY_BODY,
 LATEST_POLICIES = "latest_policies"
 
 
-def monkeyed_policy_handler_get(full_path, headers=None):
+def monkeyed_policy_handler_get(full_path, headers=None, **kwargs):
     """monkeypatch for the GET to policy-engine"""
     return MonkeyedResponse(full_path, headers,
         MonkeyedPolicyBody.create_policy(MONKEYED_POLICY_ID))
@@ -105,7 +105,7 @@ def test_policy_get_fail(monkeypatch):
         current_ctx.clear()
 
 
-def monkeyed_policy_handler_find(full_path, json, headers):
+def monkeyed_policy_handler_find(full_path, json, headers, **kwargs):
     """monkeypatch for the GET to policy-engine"""
     return MonkeyedResponse(
         full_path, headers,