From 527c5898823398d3f8bd1306e4902214bfd3dbe7 Mon Sep 17 00:00:00 2001 From: Alex Shatov Date: Tue, 26 Sep 2017 16:58:37 -0400 Subject: UT 78% dcaepolicy plugin * unit test withcoverage of 78 % * minor refactoring for lazy init for discovery - useful for UT Change-Id: I7bee1d5dc5c66477c6e8f19484b0179d5f726fe0 Issue-Id: DCAEGEN2-62 Signed-off-by: Alex Shatov --- dcae-policy/dcaepolicyplugin/tasks.py | 45 +++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 15 deletions(-) (limited to 'dcae-policy/dcaepolicyplugin/tasks.py') diff --git a/dcae-policy/dcaepolicyplugin/tasks.py b/dcae-policy/dcaepolicyplugin/tasks.py index 7b5ea1f..2676864 100644 --- a/dcae-policy/dcaepolicyplugin/tasks.py +++ b/dcae-policy/dcaepolicyplugin/tasks.py @@ -32,28 +32,43 @@ from cloudify.exceptions import NonRecoverableError from .discovery import discover_service_url -SERVICE_NAME_POLICY_HANDLER = "policy_handler" -X_ECOMP_REQUESTID = 'X-ECOMP-RequestID' POLICY_ID = 'policy_id' POLICY_REQUIRED = 'policy_required' POLICY_BODY = 'policy_body' DCAE_POLICY_TYPE = 'dcae.nodes.policy' -POLICY_HANDLER_URL = discover_service_url(SERVICE_NAME_POLICY_HANDLER) +class PolicyHandler(object): + """talk to policy-handler""" + SERVICE_NAME_POLICY_HANDLER = "policy_handler" + X_ECOMP_REQUESTID = 'X-ECOMP-RequestID' + _url = None -def _get_latest_policy(policy_id): - """retrieve the latest policy for policy_id from policy-handler""" - ph_path = "{0}/policy_latest/{1}".format(POLICY_HANDLER_URL, policy_id) - headers = {X_ECOMP_REQUESTID: str(uuid.uuid4())} + @staticmethod + def _lazy_init(): + """discover policy-handler""" + if PolicyHandler._url: + return - ctx.logger.info("getting latest policy from {0} headers={1}".format( \ - ph_path, json.dumps(headers))) - res = requests.get(ph_path, headers=headers) - res.raise_for_status() + PolicyHandler._url = "{0}/policy_latest".format( + discover_service_url(PolicyHandler.SERVICE_NAME_POLICY_HANDLER) + ) - if res.status_code == requests.codes.ok: - return res.json() - return {} + @staticmethod + def get_latest_policy(policy_id): + """retrieve the latest policy for policy_id from policy-handler""" + PolicyHandler._lazy_init() + + ph_path = "{0}/{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( \ + ph_path, json.dumps(headers))) + res = requests.get(ph_path, headers=headers) + res.raise_for_status() + + if res.status_code == requests.codes.ok: + return res.json() + return {} ######################################################### @operation @@ -71,7 +86,7 @@ def policy_get(**kwargs): try: policy_id = ctx.node.properties[POLICY_ID] - policy = _get_latest_policy(policy_id) + policy = PolicyHandler.get_latest_policy(policy_id) if not policy: raise NonRecoverableError("policy not found for policy_id {0}".format(policy_id)) -- cgit 1.2.3-korg