コード例 #1
0
def _set_up_publisher(dmc, target_feed_id, loc):
    username = random_string(8)
    userpw = random_string(16)
    add_pub = dmc.add_publisher(target_feed_id, loc, username, userpw)
    add_pub.raise_for_status()
    pub_info = add_pub.json()
    return pub_info["pubId"], username, userpw
コード例 #2
0
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)
コード例 #3
0
def create_feed(**kwargs):
    '''
    Create a new data router feed
        Expects "feed_name" to be set in node properties
        If 'feed_name' is not set or is empty, generates a random one.
        Allows "feed_version", "feed_description", "aspr_classification" and "useExisting" as optional properties
        (Sets default values if not provided )
        Sets instance runtime properties:
        Note that 'useExisting' is a flag indicating whether DBCL will use existing feed if the feed already exists.
            - "feed_id"
            - "publish_url"
            - "log_url"

    '''
    try:
        # Make sure there's a feed_name
        feed_name = ctx.node.properties.get("feed_name")
        if not (feed_name and feed_name.strip()):
            feed_name = random_string(12)

        # Set defaults/placeholders for the optional properties for the feed
        if "feed_version" in ctx.node.properties:
            feed_version = ctx.node.properties["feed_version"]
        else:
            feed_version = "0.0"
        if "feed_description" in ctx.node.properties:
            feed_description = ctx.node.properties["feed_description"]
        else:
            feed_description = "No description provided"
        if "aspr_classification" in ctx.node.properties:
            aspr_classification = ctx.node.properties["aspr_classification"]
        else:
            aspr_classification = "unclassified"
        if "useExisting" in ctx.node.properties:
            useExisting = ctx.node.properties["useExisting"]
        else:
            useExisting = False

        # Make the request to the controller
        dmc = DMaaPControllerHandle(DMAAP_API_URL, DMAAP_USER, DMAAP_PASS,
                                    ctx.logger)
        ctx.logger.info("Attempting to create feed name {0}".format(feed_name))
        f = dmc.create_feed(feed_name, feed_version, feed_description,
                            aspr_classification, DMAAP_OWNER, useExisting)
        f.raise_for_status()

        # Capture important properties from the result
        feed = f.json()
        ctx.instance.runtime_properties["feed_id"] = feed["feedId"]
        ctx.instance.runtime_properties["publish_url"] = feed["publishURL"]
        ctx.instance.runtime_properties["log_url"] = feed["logURL"]
        ctx.logger.info("Created feed name {0} with feed id {1}".format(
            feed_name, feed["feedId"]))

    except Exception as e:
        ctx.logger.error("Error creating feed: {er}".format(er=e))
        raise NonRecoverableError(e)
コード例 #4
0
def test_random_string(monkeypatch):
    from dmaapplugin import dmaaputils
    target_length = 10
    assert len(dmaaputils.random_string(target_length)) == target_length
コード例 #5
0
def create_topic(**kwargs):
    '''
    Creates a message router topic.
    Allows 'topic_name', 'topic_description', 'txenable', 'replication_case', 'global_mr_url',
    and 'useExisting' as optional node properties.  If 'topic_name' is not set,
    generates a random one.
    Sets 'fqtn' in the instance runtime_properties.
    Note that 'txenable' is a Message Router flag indicating whether transactions
    are enabled on the topic.
    Note that 'useExisting' is a flag indicating whether DBCL will use existing topic if
    the topic already exists.
    '''
    try:
        # Make sure there's a topic_name
        if "topic_name" in ctx.node.properties:
            topic_name = ctx.node.properties["topic_name"]
            if topic_name == '' or topic_name.isspace():
                topic_name = random_string(12)
        else:
            topic_name = random_string(12)

        # Make sure there's a topic description
        if "topic_description" in ctx.node.properties:
            topic_description = ctx.node.properties["topic_description"]
        else:
            topic_description = "No description provided"

        # ..and the truly optional setting
        if "txenable" in ctx.node.properties:
            txenable = ctx.node.properties["txenable"]
        else:
            txenable = False

        if "replication_case" in ctx.node.properties:
            replication_case = ctx.node.properties["replication_case"]
        else:
            replication_case = None

        if "global_mr_url" in ctx.node.properties:
            global_mr_url = ctx.node.properties["global_mr_url"]
        else:
            global_mr_url = None

        if "useExisting" in ctx.node.properties:
            useExisting = ctx.node.properties["useExisting"]
        else:
            useExisting = False

        # Make the request to the controller
        dmc = DMaaPControllerHandle(DMAAP_API_URL, DMAAP_USER, DMAAP_PASS,
                                    ctx.logger)
        ctx.logger.info(
            "Attempting to create topic name {0}".format(topic_name))
        t = dmc.create_topic(topic_name, topic_description, txenable,
                             DMAAP_OWNER, replication_case, global_mr_url,
                             useExisting)
        t.raise_for_status()

        # Capture important properties from the result
        topic = t.json()
        ctx.instance.runtime_properties["fqtn"] = topic["fqtn"]

    except Exception as e:
        ctx.logger.error("Error creating topic: {er}".format(er=e))
        raise NonRecoverableError(e)
コード例 #6
0
def test_dmaapc(monkeypatch, mockconsul, mockdmaapbc):
    from dmaapplugin.dmaaputils import random_string

    config = mockconsul().get_config('mockkey')['dmaap']
    DMAAP_API_URL = config['url']
    DMAAP_USER = config['username']
    DMAAP_PASS = config['password']
    DMAAP_OWNER = config['owner']

    properties = {'fqdn': 'a.x.example.com', 'openstack': _goodosv2}
    mock_ctx = MockCloudifyContext(node_id='test_node_id',
                                   node_name='test_node_name',
                                   properties=properties,
                                   runtime_properties={
                                       "admin": {
                                           "user": "******"
                                       },
                                       "user": {
                                           "user": "******"
                                       },
                                       "viewer": {
                                           "user": "******"
                                       }
                                   })

    current_ctx.set(mock_ctx)

    kwargs = {
        "topic_name": "ONAP_test",
        "topic_description": "onap dmaap plugin unit test topic"
    }

    # Make sure there's a topic_name
    if "topic_name" in ctx.node.properties:
        topic_name = ctx.node.properties["topic_name"]
        if topic_name == '' or topic_name.isspace():
            topic_name = random_string(12)
    else:
        topic_name = random_string(12)

    # Make sure there's a topic description
    if "topic_description" in ctx.node.properties:
        topic_description = ctx.node.properties["topic_description"]
    else:
        topic_description = "No description provided"

    # ..and the truly optional setting
    if "txenable" in ctx.node.properties:
        txenable = ctx.node.properties["txenable"]
    else:
        txenable = False

    if "replication_case" in ctx.node.properties:
        replication_case = ctx.node.properties["replication_case"]
    else:
        replication_case = None

    if "global_mr_url" in ctx.node.properties:
        global_mr_url = ctx.node.properties["global_mr_url"]
    else:
        global_mr_url = None

    dmc = DMaaPControllerHandle(DMAAP_API_URL, DMAAP_USER, DMAAP_PASS,
                                ctx.logger)
    ctx.logger.info("Attempting to create topic name {0}".format(topic_name))
    t = dmc.create_topic(topic_name, topic_description, txenable, DMAAP_OWNER,
                         replication_case, global_mr_url)

    # Capture important properties from the result
    topic = t.json()
    ctx.instance.runtime_properties["fqtn"] = topic["fqtn"]

    # test DMaaPControllerHandle functions
    path = "myPath"
    url = dmc._make_url(path)
    rc = dmc._get_resource(path)
    rc = dmc._create_resource(path, None)
    rc = dmc._delete_resource(path)