diff options
author | 2017-09-26 16:58:37 -0400 | |
---|---|---|
committer | 2017-09-26 16:58:37 -0400 | |
commit | 527c5898823398d3f8bd1306e4902214bfd3dbe7 (patch) | |
tree | b90cb894abee3b90bef5b6498beb361ba7347d08 /dcae-policy/dcaepolicyplugin/tasks.py | |
parent | d38e228959de388f0ed6d1876e8392e18f17006a (diff) |
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 <alexs@att.com>
Diffstat (limited to 'dcae-policy/dcaepolicyplugin/tasks.py')
-rw-r--r-- | dcae-policy/dcaepolicyplugin/tasks.py | 45 |
1 files changed, 30 insertions, 15 deletions
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)) |