From 77e27adeab5ff155b690f2e058c06f0a7812e225 Mon Sep 17 00:00:00 2001 From: "Schmalzried, Terry (ts862m)" Date: Thu, 14 Nov 2019 13:07:56 -0500 Subject: DCAEGEN2-1920 enhance policy plugin for error conditions Issue-ID: DCAEGEN2-1920 Change-Id: I7e1b08cb008f60d7f08ed2db41236a03b8490287 Signed-off-by: Schmalzried, Terry (ts862m) --- dcae-policy/README.md | 2 +- dcae-policy/dcaepolicy-node-type.yaml | 13 ++++++------- dcae-policy/dcaepolicyplugin/discovery.py | 6 +++--- dcae-policy/dcaepolicyplugin/tasks.py | 17 +++++++++++------ dcae-policy/pom.xml | 4 ++-- dcae-policy/setup.py | 4 ++-- dcae-policy/tests/test_discovery.py | 8 ++++---- dcae-policy/tests/test_tasks.py | 6 +++--- 8 files changed, 32 insertions(+), 28 deletions(-) diff --git a/dcae-policy/README.md b/dcae-policy/README.md index a3a070b..db729e9 100644 --- a/dcae-policy/README.md +++ b/dcae-policy/README.md @@ -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 diff --git a/dcae-policy/dcaepolicy-node-type.yaml b/dcae-policy/dcaepolicy-node-type.yaml index 1c07b7a..ec7f373 100644 --- a/dcae-policy/dcaepolicy-node-type.yaml +++ b/dcae-policy/dcaepolicy-node-type.yaml @@ -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. @@ -16,16 +16,11 @@ # # 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: diff --git a/dcae-policy/dcaepolicyplugin/discovery.py b/dcae-policy/dcaepolicyplugin/discovery.py index 0517377..8612160 100644 --- a/dcae-policy/dcaepolicyplugin/discovery.py +++ b/dcae-policy/dcaepolicyplugin/discovery.py @@ -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))) diff --git a/dcae-policy/dcaepolicyplugin/tasks.py b/dcae-policy/dcaepolicyplugin/tasks.py index bbf3ec1..4e3b37a 100644 --- a/dcae-policy/dcaepolicyplugin/tasks.py +++ b/dcae-policy/dcaepolicyplugin/tasks.py @@ -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 diff --git a/dcae-policy/pom.xml b/dcae-policy/pom.xml index 949ecf8..0b8da47 100644 --- a/dcae-policy/pom.xml +++ b/dcae-policy/pom.xml @@ -1,7 +1,7 @@