Coverage for relationshipplugin/discovery.py : 58%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# ============LICENSE_START=======================================================
2# org.onap.dcae
3# ================================================================================
4# Copyright (c) 2017-2020 AT&T Intellectual Property. All rights reserved.
5# Copyright (c) 2019 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
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
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=========================================================
19#
21try:
22 from urllib.parse import urlparse
23except ImportError:
24 from urlparse import urlparse
25import json
26import consul
29class DiscoveryError(RuntimeError):
30 pass
32def _create_rel_key(service_component_name):
33 return "{0}:rel".format(service_component_name)
35def _parse_host(host):
36 """Parse host string
38 Returns:
39 Tuple of the hostname and port to use to connect to Consul
40 """
41 def parse_urlparse_result(pr):
42 if not pr.hostname:
43 raise DiscoveryError("Invalid Consul host provided: {0}".format(host))
45 try:
46 # Port 8500 is the Consul default
47 return (pr.hostname, pr.port if pr.port else 8500)
48 except ValueError as e:
49 # Something bad happened with port
50 raise DiscoveryError("Invalid Consul host provided: {0}".format(host))
52 pr = urlparse(host)
54 # urlparse requires scheme to be set in order to be useful
55 if pr.scheme and pr.netloc:
56 return parse_urlparse_result(pr)
57 else:
58 return parse_urlparse_result(urlparse("http://{0}".format(host)))
61def create_kv_conn(host):
62 """Create connection to key-value store
64 Returns a Consul client to the specified Consul host
65 """
66 (hostname, port) = _parse_host(host)
67 return consul.Consul(host=hostname, port=port)
69def store_relationship(kv_conn, source_name, target_name):
70 # TODO: Rel entry may already exist in a one-to-many situation. Need to
71 # support that.
72 rel_key = _create_rel_key(source_name)
73 rel_value = [target_name] if target_name else []
75 kv_conn.kv.put(rel_key, json.dumps(rel_value))
76 print("Added relationship for {0}".format(rel_key))
78def delete_relationship(kv_conn, service_component_name):
79 rel_key = _create_rel_key(service_component_name)
80 index, rels = kv_conn.kv.get(rel_key)
82 if rels:
83 rels = json.loads(rels["Value"].decode("utf-8"))
84 kv_conn.kv.delete(rel_key)
85 return rels
86 else:
87 return []