1 # ============LICENSE_START====================================================
3 # =============================================================================
4 # Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved.
5 # Copyright (c) 2020 Pantheon.tech. All rights reserved.
6 # =============================================================================
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END======================================================
20 from cloudify import ctx
21 from cloudify.decorators import operation
22 from cloudify.exceptions import NonRecoverableError
23 from dmaapplugin import DMAAP_API_URL, DMAAP_USER, DMAAP_PASS, DMAAP_OWNER
24 from dmaapplugin.dmaaputils import random_string
25 from dmaapcontrollerif.dmaap_requests import DMaaPControllerHandle
27 # Lifecycle operations for DMaaP Data Router feeds
30 def create_feed(**kwargs):
32 Create a new data router feed
33 Expects "feed_name" to be set in node properties
34 If 'feed_name' is not set or is empty, generates a random one.
35 Allows "feed_version", "feed_description", "aspr_classification" and "useExisting" as optional properties
36 (Sets default values if not provided )
37 Sets instance runtime properties:
38 Note that 'useExisting' is a flag indicating whether DBCL will use existing feed if the feed already exists.
45 # Make sure there's a feed_name
46 feed_name = ctx.node.properties.get("feed_name")
47 if not (feed_name and feed_name.strip()):
48 feed_name = random_string(12)
50 # Set defaults/placeholders for the optional properties for the feed
51 if "feed_version" in ctx.node.properties:
52 feed_version = ctx.node.properties["feed_version"]
55 if "feed_description" in ctx.node.properties:
56 feed_description = ctx.node.properties["feed_description"]
58 feed_description = "No description provided"
59 if "aspr_classification" in ctx.node.properties:
60 aspr_classification = ctx.node.properties["aspr_classification"]
62 aspr_classification = "unclassified"
63 if "useExisting" in ctx.node.properties:
64 useExisting = ctx.node.properties["useExisting"]
68 # Make the request to the controller
69 dmc = DMaaPControllerHandle(DMAAP_API_URL, DMAAP_USER, DMAAP_PASS, ctx.logger)
70 ctx.logger.info("Attempting to create feed name {0}".format(feed_name))
71 f = dmc.create_feed(feed_name, feed_version, feed_description, aspr_classification, DMAAP_OWNER, useExisting)
74 # Capture important properties from the result
76 ctx.instance.runtime_properties["feed_id"] = feed["feedId"]
77 ctx.instance.runtime_properties["publish_url"] = feed["publishURL"]
78 ctx.instance.runtime_properties["log_url"] = feed["logURL"]
79 ctx.logger.info("Created feed name {0} with feed id {1}".format(feed_name, feed["feedId"]))
81 except Exception as e:
82 ctx.logger.error("Error creating feed: {er}".format(er=e))
83 raise NonRecoverableError(e)
87 def get_existing_feed(**kwargs):
89 Find information for an existing data router feed
90 Expects "feed_id" to be set in node properties -- uniquely identifies the feed
91 Sets instance runtime properties:
98 # Make the lookup request to the controller
99 dmc = DMaaPControllerHandle(DMAAP_API_URL, DMAAP_USER, DMAAP_PASS, ctx.logger)
100 ctx.logger.info("DMaaPControllerHandle() returned")
101 feed_id_input = False
102 if "feed_id" in ctx.node.properties:
104 f = dmc.get_feed_info(ctx.node.properties["feed_id"])
105 elif "feed_name" in ctx.node.properties:
106 feed_name = ctx.node.properties["feed_name"]
107 f = dmc.get_feed_info_by_name(feed_name)
109 ctx.logger.error("Not find existing feed with feed name {0}".format(feed_name))
110 raise ValueError("Not find existing feed with feed name " + feed_name)
112 raise ValueError("Either feed_id or feed_name must be defined to get existing feed")
116 # Capture important properties from the result
118 feed_id = feed["feedId"]
119 ctx.instance.runtime_properties["feed_id"] = feed_id # Just to be consistent with newly-created node, above
120 ctx.instance.runtime_properties["publish_url"] = feed["publishURL"]
121 ctx.instance.runtime_properties["log_url"] = feed["logURL"]
123 ctx.logger.info("Found existing feed with feed id {0}".format(ctx.node.properties["feed_id"]))
125 ctx.logger.info("Found existing feed with feed name {0}".format(ctx.node.properties["feed_name"]))
127 except ValueError as e:
128 ctx.logger.error("{er}".format(er=e))
129 raise NonRecoverableError(e)
130 except Exception as e:
132 ctx.logger.error("Error getting existing feed id {id}: {er}".format(id=ctx.node.properties["feed_id"],er=e))
134 ctx.logger.error("Error getting existing feed name {name}: {er}".format(name=ctx.node.properties["feed_name"],er=e))
135 raise NonRecoverableError(e)
139 def delete_feed(**kwargs):
142 Expects "feed_id" to be set on the instance's runtime properties
145 # Make the lookup request to the controllerid=ctx.node.properties["feed_id"]
146 dmc = DMaaPControllerHandle(DMAAP_API_URL, DMAAP_USER, DMAAP_PASS, ctx.logger)
147 f = dmc.delete_feed(ctx.instance.runtime_properties["feed_id"])
149 ctx.logger.info("Deleting feed id {0}".format(ctx.instance.runtime_properties["feed_id"]))
151 except Exception as e:
152 ctx.logger.error("Error deleting feed id {id}: {er}".format(id=ctx.instance.runtime_properties["feed_id"],er=e))
153 # don't raise a NonRecoverable error here--let the uninstall workflow continue