summaryrefslogtreecommitdiffstats
path: root/onap-dcae-dcaepolicy-lib/onap_dcae_dcaepolicy_lib/policies_output.py
diff options
context:
space:
mode:
authorAlex Shatov <alexs@att.com>2018-04-12 17:15:52 -0400
committerAlex Shatov <alexs@att.com>2018-04-12 17:15:52 -0400
commit80f199c60874325c2330e8c1c8804ce9b70e43c4 (patch)
tree9b1a10cdd329fe6754905a687e8c5ffc9ecb8636 /onap-dcae-dcaepolicy-lib/onap_dcae_dcaepolicy_lib/policies_output.py
parent5da6535505f510533fddd483dc3dff89fbfaab45 (diff)
2.4.0 onap-dcae-dcaepolicy-lib
- discovering consul at consul:8500 instead of localhost:8500 - added and refactored unit tests for discovery -- coverage 94% Change-Id: Id9f52cf95ab3dca0b2759021b408925a1dc87264 Signed-off-by: Alex Shatov <alexs@att.com> Issue-ID: DCAEGEN2-438
Diffstat (limited to 'onap-dcae-dcaepolicy-lib/onap_dcae_dcaepolicy_lib/policies_output.py')
-rw-r--r--onap-dcae-dcaepolicy-lib/onap_dcae_dcaepolicy_lib/policies_output.py62
1 files changed, 40 insertions, 22 deletions
diff --git a/onap-dcae-dcaepolicy-lib/onap_dcae_dcaepolicy_lib/policies_output.py b/onap-dcae-dcaepolicy-lib/onap_dcae_dcaepolicy_lib/policies_output.py
index dd82f5b..d8125c0 100644
--- a/onap-dcae-dcaepolicy-lib/onap_dcae_dcaepolicy_lib/policies_output.py
+++ b/onap-dcae-dcaepolicy-lib/onap_dcae_dcaepolicy_lib/policies_output.py
@@ -15,7 +15,8 @@
# ============LICENSE_END=========================================================
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.
-"""client to talk to consul at the standard port 8500 on localhost"""
+
+"""client to talk to consul on standard port 8500"""
import base64
import json
@@ -29,59 +30,69 @@ from cloudify import ctx
class PoliciesOutput(object):
"""static class for store-delete policies in consul kv"""
- # it is safe to assume that consul agent is at localhost:8500 along with cloudify manager
- CONSUL_TRANSACTION_URL = "http://localhost:8500/v1/txn"
+ # it is safe to assume that consul agent is at consul:8500
+ # define consul alis in /etc/hosts on cloudify manager vm
+ # $ cat /etc/hosts
+ # 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 consul
+ CONSUL_TRANSACTION_URL = "http://consul:8500/v1/txn"
POLICIES_EVENT = 'policies_event'
POLICIES_FOLDER_MASK = "{0}:policies/{1}"
MAX_OPS_PER_TXN = 64
- # MAX_VALUE_LEN = 512 * 1000
OPERATION_SET = "set"
OPERATION_DELETE = "delete"
OPERATION_DELETE_FOLDER = "delete-tree"
SERVICE_COMPONENT_NAME = "service_component_name"
+
@staticmethod
def _gen_txn_operation(verb, service_component_name, key=None, value=None):
"""returns the properly formatted operation to be used inside transaction"""
- key = PoliciesOutput.POLICIES_FOLDER_MASK.format(service_component_name, urllib.quote(key or ""))
+ key = PoliciesOutput.POLICIES_FOLDER_MASK.format(
+ service_component_name, urllib.quote(key or "")
+ )
if value:
return {"KV": {"Verb": verb, "Key": key, "Value": base64.b64encode(value)}}
return {"KV": {"Verb": verb, "Key": key}}
+
@staticmethod
def _run_transaction(operation_name, txn):
"""run a single transaction of several operations at consul /txn"""
if not txn:
- return
+ return None
response = None
try:
response = requests.put(PoliciesOutput.CONSUL_TRANSACTION_URL, json=txn)
except requests.exceptions.RequestException as ex:
- ctx.logger.error("failed to {0} at {1}: {2} on txn={3}"
+ ctx.logger.error(
+ "RequestException - failed to {0} at {1}: {2} on txn={3}"
.format(operation_name, PoliciesOutput.CONSUL_TRANSACTION_URL,
str(ex), json.dumps(txn)))
- return
+ return None
if response.status_code != requests.codes.ok:
- ctx.logger.error("failed {0} {1}: {2} text={3} txn={4} headers={5}"
- .format(operation_name, PoliciesOutput.CONSUL_TRANSACTION_URL, response.status_code,
- response.text, json.dumps(txn),
- json.dumps(dict(response.request.headers.items()))))
- return
- ctx.logger.info("response for {0} {1}: {2} text={3} txn={4} headers={5}"
- .format(operation_name, PoliciesOutput.CONSUL_TRANSACTION_URL, response.status_code,
- response.text, json.dumps(txn),
- json.dumps(dict(response.request.headers.items()))))
+ ctx.logger.error(
+ "failed {0} for {1} {2}: text={3} txn={4}"
+ .format(response.status_code, operation_name,
+ PoliciesOutput.CONSUL_TRANSACTION_URL, response.text, json.dumps(txn)))
+ return None
+ ctx.logger.info(
+ "response {0} for {1} {2}: text={3} txn={4}"
+ .format(response.status_code, operation_name,
+ PoliciesOutput.CONSUL_TRANSACTION_URL, response.text, json.dumps(txn)))
return True
+
@staticmethod
def store_policies(action, policy_bodies):
"""put the policy_bodies for service_component_name into consul-kv"""
- service_component_name = ctx.instance.runtime_properties.get(PoliciesOutput.SERVICE_COMPONENT_NAME)
+ service_component_name = ctx.instance.runtime_properties.get(
+ PoliciesOutput.SERVICE_COMPONENT_NAME
+ )
if not service_component_name:
ctx.logger.warn("failed to find service_component_name to store_policies in consul-kv")
return False
@@ -100,8 +111,10 @@ class PoliciesOutput(object):
for policy_id, policy_body in policy_bodies.iteritems()
]
txn = [
- PoliciesOutput._gen_txn_operation(PoliciesOutput.OPERATION_DELETE_FOLDER, service_component_name),
- PoliciesOutput._gen_txn_operation(PoliciesOutput.OPERATION_SET, service_component_name, "event", json.dumps(event))
+ PoliciesOutput._gen_txn_operation(
+ PoliciesOutput.OPERATION_DELETE_FOLDER, service_component_name),
+ PoliciesOutput._gen_txn_operation(
+ PoliciesOutput.OPERATION_SET, service_component_name, "event", json.dumps(event))
]
idx_step = PoliciesOutput.MAX_OPS_PER_TXN - len(txn)
for idx in xrange(0, len(store_policies), idx_step):
@@ -113,18 +126,23 @@ class PoliciesOutput(object):
PoliciesOutput._run_transaction("store_policies", txn)
return True
+
@staticmethod
def delete_policies():
"""delete policies for service_component_name in consul-kv"""
if PoliciesOutput.POLICIES_EVENT not in ctx.instance.runtime_properties:
return
- service_component_name = ctx.instance.runtime_properties.get(PoliciesOutput.SERVICE_COMPONENT_NAME)
+ service_component_name = ctx.instance.runtime_properties.get(
+ PoliciesOutput.SERVICE_COMPONENT_NAME
+ )
if not service_component_name:
ctx.logger.warn("failed to find service_component_name to delete_policies in consul-kv")
return
delete_policies = [
- PoliciesOutput._gen_txn_operation(PoliciesOutput.OPERATION_DELETE_FOLDER, service_component_name)
+ PoliciesOutput._gen_txn_operation(
+ PoliciesOutput.OPERATION_DELETE_FOLDER, service_component_name
+ )
]
PoliciesOutput._run_transaction("delete_policies", delete_policies)