blob: bfb4e9aeb1c9ba96ddf7eb3ceb77857beaeb1e8e [file] [log] [blame]
alex_sh9d980ce2017-08-23 17:30:56 -04001# org.onap.dcae
2# ================================================================================
3# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
4# ================================================================================
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16# ============LICENSE_END=========================================================
17#
18# ECOMP is a trademark and service mark of AT&T Intellectual Property.
19
Alex Shatovf53e5e72018-01-11 11:15:56 -050020"""client to talk to consul at the standard port 8500"""
21
alex_sh9d980ce2017-08-23 17:30:56 -040022import logging
23import json
24import base64
25import requests
26
27class DiscoveryClient(object):
28 """talking to consul at http://consul:8500
29
30 relies on proper --add-host "consul:<consul-agent ip>" in
31 docker run command that runs along the consul-agent:
32
33 docker run --name ${APPNAME} -d
34 -e HOSTNAME
35 --add-host "consul:<consul-agent ip>"
36 -v ${BASEDIR}/logs:${TARGETDIR}/logs
37 -v ${BASEDIR}/etc:${TARGETDIR}/etc
38 -p <outport>:<innerport>
39 ${APPNAME}:latest
40 """
41 CONSUL_SERVICE_MASK = "http://consul:8500/v1/catalog/service/{0}"
42 CONSUL_KV_MASK = "http://consul:8500/v1/kv/{0}"
43 SERVICE_MASK = "http://{0}:{1}"
Alex Shatov1369bea2018-01-10 11:00:50 -050044 SERVICE_ADDRESS = "ServiceAddress"
45 SERVICE_PORT = "ServicePort"
alex_sh9d980ce2017-08-23 17:30:56 -040046 _logger = logging.getLogger("policy_handler.discovery")
47
48
49 @staticmethod
50 def get_service_url(service_name):
51 """find the service record in consul"""
52 service_path = DiscoveryClient.CONSUL_SERVICE_MASK.format(service_name)
53 DiscoveryClient._logger.info("discover %s", service_path)
54 response = requests.get(service_path)
55 response.raise_for_status()
Alex Shatov1369bea2018-01-10 11:00:50 -050056 service = response.json()
57 if not service:
58 DiscoveryClient._logger.error("failed discover %s", service_path)
59 return
60 service = service[0]
61 return DiscoveryClient.SERVICE_MASK.format(
62 service[DiscoveryClient.SERVICE_ADDRESS], service[DiscoveryClient.SERVICE_PORT]
63 )
alex_sh9d980ce2017-08-23 17:30:56 -040064
65 @staticmethod
66 def get_value(key):
67 """get the value for the key from consul-kv"""
68 response = requests.get(DiscoveryClient.CONSUL_KV_MASK.format(key))
69 response.raise_for_status()
Alex Shatov1369bea2018-01-10 11:00:50 -050070 data = response.json()
71 if not data:
72 DiscoveryClient._logger.error("failed get_value %s", key)
73 return
74 value = base64.b64decode(data[0]["Value"]).decode("utf-8")
75 DiscoveryClient._logger.info("consul-kv key=%s value(%s) data=%s",
76 key, value, json.dumps(data))
alex_sh9d980ce2017-08-23 17:30:56 -040077 return json.loads(value)