alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 1 | # ================================================================================ |
Lusheng Ji | 967cc9a | 2018-02-12 10:45:42 -0500 | [diff] [blame] | 2 | # Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved. |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 3 | # ================================================================================ |
| 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 Shatov | 209f823 | 2018-09-20 15:15:45 -0400 | [diff] [blame^] | 19 | """client to talk to consul services and kv""" |
Alex Shatov | f53e5e7 | 2018-01-11 11:15:56 -0500 | [diff] [blame] | 20 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 21 | import base64 |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 22 | import json |
| 23 | import logging |
| 24 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 25 | import requests |
| 26 | |
Alex Shatov | 209f823 | 2018-09-20 15:15:45 -0400 | [diff] [blame^] | 27 | from .config import Config |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 28 | from .customize import CustomizerUser |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 29 | from .onap.audit import AuditHttpCode, Metrics |
| 30 | |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 31 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 32 | class DiscoveryClient(object): |
Alex Shatov | 209f823 | 2018-09-20 15:15:45 -0400 | [diff] [blame^] | 33 | """talking to consul at Config.consul_url |
| 34 | |
| 35 | Config.consul_url is populated |
| 36 | from env var $CONSUL_URL |
| 37 | if not provided, then from consul_url in etc/config.json |
| 38 | if not provided, then from hardcoded value of http://consul:8500 |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 39 | |
| 40 | relies on proper --add-host "consul:<consul-agent ip>" in |
| 41 | docker run command that runs along the consul-agent: |
| 42 | |
| 43 | docker run --name ${APPNAME} -d |
Alex Shatov | 209f823 | 2018-09-20 15:15:45 -0400 | [diff] [blame^] | 44 | -e HOSTNAME -e CONSUL_URL |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 45 | --add-host "consul:<consul-agent ip>" |
| 46 | -v ${BASEDIR}/logs:${TARGETDIR}/logs |
| 47 | -v ${BASEDIR}/etc:${TARGETDIR}/etc |
| 48 | -p <outport>:<innerport> |
| 49 | ${APPNAME}:latest |
| 50 | """ |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 51 | CONSUL_ENTITY = "consul" |
Alex Shatov | 209f823 | 2018-09-20 15:15:45 -0400 | [diff] [blame^] | 52 | CONSUL_SERVICE_MASK = "{}/v1/catalog/service/{}" |
| 53 | CONSUL_KV_MASK = "{}/v1/kv/{}" |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 54 | _logger = logging.getLogger("policy_handler.discovery") |
| 55 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 56 | @staticmethod |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 57 | def _discover_service(audit, service_name, service_path): |
| 58 | """find the service record in consul""" |
| 59 | response = requests.get(service_path) |
| 60 | DiscoveryClient._logger.info(audit.info("response {} from {}: {}".format( |
| 61 | response.status_code, service_path, response.text))) |
| 62 | |
| 63 | response.raise_for_status() |
| 64 | status_code = response.status_code |
| 65 | service = response.json()[0] |
| 66 | return (status_code, |
| 67 | CustomizerUser.get_customizer().get_service_url(audit, service_name, service)) |
| 68 | |
| 69 | @staticmethod |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 70 | def get_service_url(audit, service_name): |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 71 | """find the service record in consul""" |
Alex Shatov | 209f823 | 2018-09-20 15:15:45 -0400 | [diff] [blame^] | 72 | service_path = DiscoveryClient.CONSUL_SERVICE_MASK.format(Config.consul_url, service_name) |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 73 | metrics = Metrics(aud_parent=audit, targetEntity=DiscoveryClient.CONSUL_ENTITY, |
| 74 | targetServiceName=service_path) |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 75 | |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 76 | log_line = "get from {} at {}".format(DiscoveryClient.CONSUL_ENTITY, service_path) |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 77 | |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 78 | DiscoveryClient._logger.info(metrics.metrics_start(log_line)) |
| 79 | status_code = None |
| 80 | try: |
| 81 | (status_code, |
| 82 | service_url) = DiscoveryClient._discover_service(audit, service_name, service_path) |
| 83 | except Exception as ex: |
| 84 | error_code = (AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value |
| 85 | if isinstance(ex, requests.exceptions.RequestException) |
| 86 | else AuditHttpCode.SERVER_INTERNAL_ERROR.value) |
| 87 | error_msg = ("failed {}/{} to {} {}: {}".format(status_code, error_code, log_line, |
| 88 | type(ex).__name__, str(ex))) |
| 89 | DiscoveryClient._logger.exception(error_msg) |
| 90 | metrics.set_http_status_code(error_code) |
| 91 | audit.set_http_status_code(error_code) |
| 92 | metrics.metrics(error_msg) |
| 93 | return None |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 94 | |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 95 | if not service_url: |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 96 | error_code = AuditHttpCode.DATA_ERROR.value |
| 97 | error_msg = "failed {}/{} to {}".format(status_code, error_code, log_line) |
| 98 | DiscoveryClient._logger.error(audit.error(error_msg)) |
| 99 | metrics.set_http_status_code(error_code) |
| 100 | audit.set_http_status_code(error_code) |
| 101 | metrics.metrics(error_msg) |
| 102 | return None |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 103 | |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 104 | log_line = "response {} {}".format(status_code, log_line) |
| 105 | DiscoveryClient._logger.info(audit.info("got service_url: {} after {}" |
| 106 | .format(service_url, log_line))) |
| 107 | |
| 108 | metrics.set_http_status_code(status_code) |
| 109 | audit.set_http_status_code(status_code) |
| 110 | metrics.metrics(log_line) |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 111 | return service_url |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 112 | |
| 113 | @staticmethod |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 114 | def _get_value_from_kv(url): |
| 115 | """get the value from consul-kv at discovery url""" |
| 116 | response = requests.get(url) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 117 | response.raise_for_status() |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 118 | data = response.json() |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 119 | value = base64.b64decode(data[0]["Value"]).decode("utf-8") |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 120 | return response.status_code, json.loads(value) |
| 121 | |
| 122 | @staticmethod |
| 123 | def get_value(audit, key): |
| 124 | """get the value for the key from consul-kv""" |
Alex Shatov | 209f823 | 2018-09-20 15:15:45 -0400 | [diff] [blame^] | 125 | discovery_url = DiscoveryClient.CONSUL_KV_MASK.format(Config.consul_url, key) |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 126 | metrics = Metrics(aud_parent=audit, targetEntity=DiscoveryClient.CONSUL_ENTITY, |
| 127 | targetServiceName=discovery_url) |
| 128 | |
| 129 | log_line = "get from {} at {}".format(DiscoveryClient.CONSUL_ENTITY, discovery_url) |
| 130 | |
| 131 | DiscoveryClient._logger.info(metrics.metrics_start(log_line)) |
| 132 | try: |
| 133 | status_code, value = DiscoveryClient._get_value_from_kv(discovery_url) |
| 134 | except Exception as ex: |
| 135 | error_code = (AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value |
| 136 | if isinstance(ex, requests.exceptions.RequestException) |
| 137 | else AuditHttpCode.SERVER_INTERNAL_ERROR.value) |
| 138 | error_msg = ("failed {}/{} to {} {}: {}".format(status_code, error_code, log_line, |
| 139 | type(ex).__name__, str(ex))) |
| 140 | DiscoveryClient._logger.exception(error_msg) |
| 141 | metrics.set_http_status_code(error_code) |
| 142 | audit.set_http_status_code(error_code) |
| 143 | metrics.metrics(error_msg) |
| 144 | return None |
| 145 | |
| 146 | log_line = "response {} {}".format(status_code, log_line) |
| 147 | metrics.set_http_status_code(status_code) |
| 148 | audit.set_http_status_code(status_code) |
| 149 | metrics.metrics(log_line) |
| 150 | return value |