blob: ce24c3da72353a4b055da61963293511b320209d [file] [log] [blame]
alex_sh9d980ce2017-08-23 17:30:56 -04001# ================================================================================
Lusheng Ji967cc9a2018-02-12 10:45:42 -05002# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
alex_sh9d980ce2017-08-23 17:30:56 -04003# ================================================================================
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15# ============LICENSE_END=========================================================
16#
17# ECOMP is a trademark and service mark of AT&T Intellectual Property.
18
Alex Shatovf53e5e72018-01-11 11:15:56 -050019"""client to talk to consul at the standard port 8500"""
20
alex_sh9d980ce2017-08-23 17:30:56 -040021import base64
Alex Shatovb9b955c2018-03-08 13:12:23 -050022import json
23import logging
24
alex_sh9d980ce2017-08-23 17:30:56 -040025import requests
26
Alex Shatovb9b955c2018-03-08 13:12:23 -050027from .customize import CustomizerUser
28
alex_sh9d980ce2017-08-23 17:30:56 -040029class DiscoveryClient(object):
30 """talking to consul at http://consul:8500
31
32 relies on proper --add-host "consul:<consul-agent ip>" in
33 docker run command that runs along the consul-agent:
34
35 docker run --name ${APPNAME} -d
36 -e HOSTNAME
37 --add-host "consul:<consul-agent ip>"
38 -v ${BASEDIR}/logs:${TARGETDIR}/logs
39 -v ${BASEDIR}/etc:${TARGETDIR}/etc
40 -p <outport>:<innerport>
41 ${APPNAME}:latest
42 """
43 CONSUL_SERVICE_MASK = "http://consul:8500/v1/catalog/service/{0}"
44 CONSUL_KV_MASK = "http://consul:8500/v1/kv/{0}"
alex_sh9d980ce2017-08-23 17:30:56 -040045 _logger = logging.getLogger("policy_handler.discovery")
46
alex_sh9d980ce2017-08-23 17:30:56 -040047 @staticmethod
Alex Shatovb9b955c2018-03-08 13:12:23 -050048 def get_service_url(audit, service_name):
alex_sh9d980ce2017-08-23 17:30:56 -040049 """find the service record in consul"""
50 service_path = DiscoveryClient.CONSUL_SERVICE_MASK.format(service_name)
Alex Shatovb9b955c2018-03-08 13:12:23 -050051 log_line = "discover {0}".format(service_path)
52 DiscoveryClient._logger.info(log_line)
53 audit.info(log_line)
alex_sh9d980ce2017-08-23 17:30:56 -040054 response = requests.get(service_path)
Alex Shatovb9b955c2018-03-08 13:12:23 -050055
56 log_line = "response {0} for {1}: {2}".format(
57 response.status_code, service_path, response.text)
58 DiscoveryClient._logger.info(log_line)
59 audit.info(log_line)
60
alex_sh9d980ce2017-08-23 17:30:56 -040061 response.raise_for_status()
Alex Shatovb9b955c2018-03-08 13:12:23 -050062
Alex Shatov1369bea2018-01-10 11:00:50 -050063 service = response.json()
64 if not service:
Alex Shatovb9b955c2018-03-08 13:12:23 -050065 log_line = "failed discover {0}".format(service_path)
66 DiscoveryClient._logger.error(log_line)
67 audit.error(log_line)
Alex Shatov1369bea2018-01-10 11:00:50 -050068 return
69 service = service[0]
Alex Shatovb9b955c2018-03-08 13:12:23 -050070
71 service_url = CustomizerUser.get_customizer().get_service_url(audit, service_name, service)
72 if not service_url:
73 log_line = "failed to get service_url for {0}".format(service_name)
74 DiscoveryClient._logger.error(log_line)
75 audit.error(log_line)
76 return
77
78 log_line = "got service_url: {0} for {1}".format(service_url, service_name)
79 DiscoveryClient._logger.info(log_line)
80 audit.info(log_line)
81 return service_url
alex_sh9d980ce2017-08-23 17:30:56 -040082
83 @staticmethod
84 def get_value(key):
85 """get the value for the key from consul-kv"""
86 response = requests.get(DiscoveryClient.CONSUL_KV_MASK.format(key))
87 response.raise_for_status()
Alex Shatov1369bea2018-01-10 11:00:50 -050088 data = response.json()
89 if not data:
90 DiscoveryClient._logger.error("failed get_value %s", key)
91 return
92 value = base64.b64decode(data[0]["Value"]).decode("utf-8")
93 DiscoveryClient._logger.info("consul-kv key=%s value(%s) data=%s",
94 key, value, json.dumps(data))
alex_sh9d980ce2017-08-23 17:30:56 -040095 return json.loads(value)