alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 1 | # 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 Shatov | f53e5e7 | 2018-01-11 11:15:56 -0500 | [diff] [blame^] | 20 | """client to talk to consul at the standard port 8500""" |
| 21 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 22 | import logging |
| 23 | import json |
| 24 | import base64 |
| 25 | import requests |
| 26 | |
| 27 | class 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 Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 44 | SERVICE_ADDRESS = "ServiceAddress" |
| 45 | SERVICE_PORT = "ServicePort" |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 46 | _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 Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 56 | 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_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 64 | |
| 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 Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 70 | 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_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 77 | return json.loads(value) |