def delete_dr_publisher(**kwargs): ''' Deletes publisher (the source of the publishes_files relationship) from the feed (the target of the relationship). Assumes that the 'publisher_id' property was added to the dictionary of feed-related properties, when the publisher was added to the feed. ''' try: # Make sure we have a name under which to store DMaaP configuration # Check early so we don't needlessly create DMaaP entities if 'service_component_name' not in ctx.source.instance.runtime_properties: raise Exception("Source node does not have 'service_component_name' in runtime_properties") # Get the publisher id target_feed = ctx.target.node.id publisher_id = ctx.source.instance.runtime_properties[target_feed]["publisher_id"] ctx.logger.info("Attempting to delete publisher {0}".format(publisher_id)) # Make the request dmc = DMaaPControllerHandle(DMAAP_API_URL, DMAAP_USER, DMAAP_PASS, ctx.logger) del_result = dmc.delete_publisher(publisher_id) del_result.raise_for_status() ctx.logger.info("Deleted publisher {0}".format(publisher_id)) # Attempt to remove the entire ":dmaap" entry from the Consul KV store # Will quietly do nothing if the entry has already been removed ch = ConsulHandle("http://{0}:8500".format(CONSUL_HOST), None, None, ctx.logger) ch.delete_entry("{0}:dmaap".format(ctx.source.instance.runtime_properties['service_component_name'])) except Exception as e: ctx.logger.error("Error deleting publisher: {er}".format(er=e))
def delete_dr_subscriber(**kwargs): ''' Deletes subscriber (the source of the subscribes_to_files relationship) from the feed (the target of the relationship). Assumes that the source node's runtime properties dictionary for the target feed includes 'subscriber_id', set when the publisher was added to the feed. ''' try: # Get the subscriber id target_feed = ctx.target.node.id subscriber_id = ctx.source.instance.runtime_properties[target_feed]["subscriber_id"] ctx.logger.info("Attempting to delete subscriber {0} from feed {1}".format(subscriber_id, target_feed)) # Make the request dmc = DMaaPControllerHandle(DMAAP_API_URL, DMAAP_USER, DMAAP_PASS, ctx.logger) del_result = dmc.delete_subscriber(subscriber_id) del_result.raise_for_status() ctx.logger.info("Deleted subscriber {0}".format(subscriber_id)) # Attempt to remove the entire ":dmaap" entry from the Consul KV store # Will quietly do nothing if the entry has already been removed ch = ConsulHandle("http://{0}:8500".format(CONSUL_HOST), None, None, ctx.logger) ch.delete_entry("{0}:dmaap".format(ctx.source.instance.runtime_properties['service_component_name'])) except Exception as e: ctx.logger.error("Error deleting subscriber: {er}".format(er=e))
def delete_mr_client(**kwargs): ''' Delete the client (publisher or subscriber). Expect property 'client_id' to have been set in the instance's runtime_properties when the client was created. ''' try: target_topic = ctx.target.node.id client_id = ctx.source.instance.runtime_properties[target_topic][ "client_id"] ctx.logger.info("Attempting to delete client {0} ".format(client_id)) dmc = DMaaPControllerHandle(DMAAP_API_URL, DMAAP_USER, DMAAP_PASS, ctx.logger) c = dmc.delete_client(client_id) c.raise_for_status() ctx.logger.info("Deleted client {0}".format(client_id)) # Attempt to remove the entire ":dmaap" entry from the Consul KV store # Will quietly do nothing if the entry has already been removed ch = ConsulHandle("http://{0}:8500".format(CONSUL_HOST), None, None, ctx.logger) ch.delete_entry("{0}:dmaap".format( ctx.source.instance.runtime_properties['service_component_name'])) except Exception as e: ctx.logger.error("Error deleting MR client: {er}".format(er=e))
def test_get_config_service(mockconsul): err_msg = "Error getting ConsulHandle when configuring dmaap plugin: {0}" _ch = ConsulHandle("http://{0}:{1}".format(CONSUL_HOST, CONSUL_PORT), None, None, None) config = _ch.get_config(DBCL_KEY_NAME) DMAAP_USER = config['dmaap']['username'] DMAAP_PASS = config['dmaap']['password'] DMAAP_OWNER = config['dmaap']['owner'] if 'protocol' in config['dmaap']: DMAAP_PROTOCOL = config['dmaap']['protocol'] else: DMAAP_PROTOCOL = 'https' # Default to https (service discovery should give us this but doesn't if 'path' in config['dmaap']: DMAAP_PATH = config['dmaap']['path'] else: DMAAP_PATH = 'webapi' # Should come from service discovery but Consul doesn't support it service_address, service_port = _ch.get_service(DBC_SERVICE_NAME) DMAAP_API_URL = '{0}://{1}:{2}/{3}'.format(DMAAP_PROTOCOL, service_address, service_port, DMAAP_PATH)
def add_dr_publisher(**kwargs): ''' Sets up the source of the publishes_relationship as a publisher to the feed that is the target of the relationship Assumes target (the feed) has the following runtime properties set - feed_id - log_url - publish_url Assumes source (the publisher) has a runtime property whose name matches the node name of the feed. This is a dictionary containing one property: - location (the dcaeLocationName to pass when adding the publisher to the feed) Generates a user name and password that the publisher will need to use when publishing Adds the following properties to the dictionary above: - publish_url - log_url - username - password ''' try: # Make sure we have a name under which to store DMaaP configuration # Check early so we don't needlessly create DMaaP entities if 'service_component_name' not in ctx.source.instance.runtime_properties: raise Exception("Source node does not have 'service_component_name' in runtime_properties") target_feed = ctx.target.node.id ctx.logger.info("Attempting to add publisher {0} to feed {1}".format(ctx.source.node.id, target_feed)) # Set up the parameters for the add_publisher request to the DMaaP bus controller feed_id = ctx.target.instance.runtime_properties["feed_id"] location = ctx.source.instance.runtime_properties[target_feed]["location"] username = random_string(8) password = random_string(16) # Make the request to add the publisher to the feed dmc = DMaaPControllerHandle(DMAAP_API_URL, DMAAP_USER, DMAAP_PASS, ctx.logger) add_pub = dmc.add_publisher(feed_id, location, username, password) add_pub.raise_for_status() publisher_info = add_pub.json() publisher_id = publisher_info["pubId"] ctx.logger.info("Added publisher id {0} to feed {1} at {2}, with user {3}, pass {4}".format(publisher_id, feed_id, location, username, password)) # Set runtime properties on the source ctx.source.instance.runtime_properties[target_feed] = { "publisher_id" : publisher_id, "location" : location, "publish_url" : ctx.target.instance.runtime_properties["publish_url"], "log_url" : ctx.target.instance.runtime_properties["log_url"], "username" : username, "password" : password } # Set key in Consul ch = ConsulHandle("http://{0}:8500".format(CONSUL_HOST), None, None, ctx.logger) cpy = dict(ctx.source.instance.runtime_properties[target_feed]) ch.add_to_entry("{0}:dmaap".format(ctx.source.instance.runtime_properties['service_component_name']), target_feed, cpy) except Exception as e: ctx.logger.error("Error adding publisher to feed: {er}".format(er=e)) raise NonRecoverableError(e)
def add_dr_subscriber(**kwargs): ''' Sets up the source of the subscribes_to_files relationship as a subscriber to the feed that is the target of the relationship. Assumes target (the feed) has the following runtime property set - feed_id Assumes source (the subscriber) has a runtime property whose name matches the node name of the feed. This is a dictionary containing the following properties: - location (the dcaeLocationName to pass when adding the publisher to the feed) - delivery_url (the URL to which data router will deliver files) - username (the username data router will use when delivering files) - password (the password data router will use when delivering files) Adds a property to the dictionary above: - subscriber_id (used to delete the subscriber in the uninstall workflow ''' try: target_feed = ctx.target.node.id ctx.logger.info("Attempting to add subscriber {0} to feed {1}".format(ctx.source.node.id, target_feed)) # Get the parameters for the call feed_id = ctx.target.instance.runtime_properties["feed_id"] feed = ctx.source.instance.runtime_properties[target_feed] location = feed["location"] delivery_url = feed["delivery_url"] username = feed["username"] password = feed["password"] decompress = feed["decompress"] if "decompress" in feed else False privileged = feed["privileged"] if "privileged" in feed else False # Make the request to add the subscriber to the feed dmc = DMaaPControllerHandle(DMAAP_API_URL, DMAAP_USER, DMAAP_PASS, ctx.logger) add_sub = dmc.add_subscriber(feed_id, location, delivery_url,username, password, decompress, privileged) add_sub.raise_for_status() subscriber_info = add_sub.json() subscriber_id = subscriber_info["subId"] ctx.logger.info("Added subscriber id {0} to feed {1} at {2}".format(subscriber_id, feed_id, location)) # Add subscriber_id to the runtime properties # ctx.source.instance.runtime_properties[target_feed]["subscriber_id"] = subscriber_id ctx.source.instance.runtime_properties[target_feed] = { "subscriber_id": subscriber_id, "location" : location, "delivery_url" : delivery_url, "username" : username, "password" : password, "decompress": decompress, "privilegedSubscriber": privileged } ctx.logger.info("on source: {0}".format(ctx.source.instance.runtime_properties[target_feed])) # Set key in Consul ch = ConsulHandle("http://{0}:8500".format(CONSUL_HOST), None, None, ctx.logger) cpy = dict(ctx.source.instance.runtime_properties[target_feed]) ch.add_to_entry("{0}:dmaap".format(ctx.source.instance.runtime_properties['service_component_name']), target_feed, cpy) except Exception as e: ctx.logger.error("Error adding subscriber to feed: {er}".format(er=e)) raise NonRecoverableError(e)
def _add_mr_client(ctype, actions): ''' Adds the node represented by 'source' as a client (publisher or subscriber) to to topic represented by the 'target' node. The list of actions in 'actions' determines whether the client is a subscriber or a publisher. Assumes target (the topic) has the following runtime property set - fqtn Assumes source (the client) has a runtime property whose name matches the node name of the feed. This is a dictionary containing the following properties: - location (the dcaeLocationName to pass when adding the client to the topic) - client_role (the AAF client role under which the client will access the topic) Adds two properties to the dictionary above: - topic_url (the URL that the client can use to access the topic) - client_id (used to delete the client in the uninstall workflow) ''' try: # Make sure we have a name under which to store DMaaP configuration # Check early so we don't needlessly create DMaaP entities if 'service_component_name' not in ctx.source.instance.runtime_properties: raise Exception("Source node does not have 'service_component_name' in runtime_properties") target_topic = ctx.target.node.id # Key for the source's dictionary with topic-related info fqtn = ctx.target.instance.runtime_properties["fqtn"] ctx.logger.info("Attempting to add {0} as {1} to topic {2}".format(ctx.source.node.id, ctype, fqtn)) # Get the parameters needed for adding the client location = ctx.source.instance.runtime_properties[target_topic]["location"] client_role = ctx.source.instance.runtime_properties[target_topic]["client_role"] # Make the request to add the client to the topic dmc = DMaaPControllerHandle(DMAAP_API_URL, DMAAP_USER, DMAAP_PASS, ctx.logger) c = dmc.create_client(fqtn, location, client_role, actions) c.raise_for_status() client_info = c.json() client_id = client_info["mrClientId"] topic_url = client_info["topicURL"] # Update source's runtime properties #ctx.source.instance.runtime_properties[target_topic]["topic_url"] = topic_url #ctx.source.instance.runtime_properties[target_topic]["client_id"] = client_id ctx.source.instance.runtime_properties[target_topic] = { "topic_url" : topic_url, "client_id" : client_id, "location" : location, "client_role" : client_role } ctx.logger.info("Added {0} id {1} to feed {2} at {3}".format(ctype, client_id, fqtn, location)) # Set key in Consul ch = ConsulHandle("http://{0}:8500".format(CONSUL_HOST), None, None, ctx.logger) ch.add_to_entry("{0}:dmaap".format(ctx.source.instance.runtime_properties['service_component_name']), target_topic, ctx.source.instance.runtime_properties[target_topic]) except Exception as e: ctx.logger.error("Error adding client to feed: {er}".format(er=e)) raise NonRecoverableError(e)
def test_add_entry(mockconsul): _ch = ConsulHandle("http://{0}:{1}".format(CONSUL_HOST, CONSUL_PORT), None, None, None) key = 'DMAAP_TEST' name = 'dmaap_test_name' value = 'dmaap_test_value' _ch.add_to_entry(key, name, value) name = "dmaap_test_name_2" value = 'dmaap_test_value_2' _ch.add_to_entry(key, name, value) _ch.delete_entry(key)
def get_handle(): return ConsulHandle('mockconsul', None, None, None)
from consulif.consulif import ConsulHandle from cloudify.exceptions import NonRecoverableError import os os.environ[ "REQUESTS_CA_BUNDLE"] = "/opt/onap/certs/cacert.pem" # This is to handle https request thru plugin CONSUL_HOST = "consul" # Should always be a local consul agent on Cloudify Manager DBCL_KEY_NAME = "dmaap-plugin" # Consul key containing DMaaP data bus credentials # In the ONAP Kubernetes environment, bus controller address is always "dmaap-bc", on port 8080 (http) and 8443 (https) ONAP_SERVICE_ADDRESS = "dmaap-bc" HTTP_PORT = "8080" HTTPS_PORT = "8443" try: _ch = ConsulHandle("http://{0}:8500".format(CONSUL_HOST), None, None, None) except Exception as e: raise NonRecoverableError( "Error getting ConsulHandle when configuring dmaap plugin: {0}".format( e)) try: config = _ch.get_config(DBCL_KEY_NAME) except Exception as e: raise NonRecoverableError( "Error getting config for '{0}' from ConsulHandle when configuring dmaap plugin: {1}" .format(DBCL_KEY_NAME, e)) try: DMAAP_USER = config['dmaap']['username'] except Exception as e: