alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 1 | # ================================================================================ |
Alex Shatov | 78ff88f | 2020-02-27 12:45:54 -0500 | [diff] [blame^] | 2 | # Copyright (c) 2017-2020 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 | # |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 17 | |
Alex Shatov | 209f823 | 2018-09-20 15:15:45 -0400 | [diff] [blame] | 18 | """client to talk to consul services and kv""" |
Alex Shatov | f53e5e7 | 2018-01-11 11:15:56 -0500 | [diff] [blame] | 19 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 20 | import base64 |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 21 | import json |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 22 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 23 | import requests |
| 24 | |
Alex Shatov | 209f823 | 2018-09-20 15:15:45 -0400 | [diff] [blame] | 25 | from .config import Config |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 26 | from .customize import CustomizerUser |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 27 | from .onap.audit import AuditHttpCode, Metrics |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 28 | from .utils import Utils |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 29 | |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 30 | _LOGGER = Utils.get_logger(__file__) |
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 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 55 | @staticmethod |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 56 | def _discover_service(audit, service_name, service_path): |
| 57 | """find the service record in consul""" |
Alex Shatov | a39f4e8 | 2018-12-05 15:23:50 -0500 | [diff] [blame] | 58 | response = requests.get(service_path, timeout=Config.consul_timeout_in_secs) |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 59 | _LOGGER.info(audit.info("response {} from {}: {}".format( |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 60 | response.status_code, service_path, response.text))) |
| 61 | |
| 62 | response.raise_for_status() |
| 63 | status_code = response.status_code |
| 64 | service = response.json()[0] |
| 65 | return (status_code, |
| 66 | CustomizerUser.get_customizer().get_service_url(audit, service_name, service)) |
| 67 | |
| 68 | @staticmethod |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 69 | def get_service_url(audit, service_name): |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 70 | """find the service record in consul""" |
Alex Shatov | 209f823 | 2018-09-20 15:15:45 -0400 | [diff] [blame] | 71 | service_path = DiscoveryClient.CONSUL_SERVICE_MASK.format(Config.consul_url, service_name) |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 72 | metrics = Metrics(aud_parent=audit, targetEntity=DiscoveryClient.CONSUL_ENTITY, |
| 73 | targetServiceName=service_path) |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 74 | |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 75 | log_line = "get from {} at {}".format(DiscoveryClient.CONSUL_ENTITY, service_path) |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 76 | |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 77 | _LOGGER.info(metrics.metrics_start(log_line)) |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 78 | status_code = None |
| 79 | try: |
| 80 | (status_code, |
| 81 | service_url) = DiscoveryClient._discover_service(audit, service_name, service_path) |
| 82 | except Exception as ex: |
| 83 | error_code = (AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value |
| 84 | if isinstance(ex, requests.exceptions.RequestException) |
| 85 | else AuditHttpCode.SERVER_INTERNAL_ERROR.value) |
| 86 | error_msg = ("failed {}/{} to {} {}: {}".format(status_code, error_code, log_line, |
| 87 | type(ex).__name__, str(ex))) |
Alex Shatov | 78ff88f | 2020-02-27 12:45:54 -0500 | [diff] [blame^] | 88 | _LOGGER.exception(metrics.fatal(error_msg)) |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 89 | metrics.set_http_status_code(error_code) |
| 90 | audit.set_http_status_code(error_code) |
| 91 | metrics.metrics(error_msg) |
| 92 | return None |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 93 | |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 94 | if not service_url: |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 95 | error_code = AuditHttpCode.DATA_ERROR.value |
| 96 | error_msg = "failed {}/{} to {}".format(status_code, error_code, log_line) |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 97 | _LOGGER.error(audit.error(error_msg)) |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 98 | metrics.set_http_status_code(error_code) |
| 99 | audit.set_http_status_code(error_code) |
| 100 | metrics.metrics(error_msg) |
| 101 | return None |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 102 | |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 103 | log_line = "response {} {}".format(status_code, log_line) |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 104 | _LOGGER.info(audit.info("got service_url: {} after {}".format(service_url, log_line))) |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 105 | |
| 106 | metrics.set_http_status_code(status_code) |
| 107 | audit.set_http_status_code(status_code) |
| 108 | metrics.metrics(log_line) |
Alex Shatov | b9b955c | 2018-03-08 13:12:23 -0500 | [diff] [blame] | 109 | return service_url |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 110 | |
| 111 | @staticmethod |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 112 | def _get_value_from_kv(url): |
| 113 | """get the value from consul-kv at discovery url""" |
Alex Shatov | a39f4e8 | 2018-12-05 15:23:50 -0500 | [diff] [blame] | 114 | response = requests.get(url, timeout=Config.consul_timeout_in_secs) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 115 | response.raise_for_status() |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 116 | data = response.json() |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 117 | value = base64.b64decode(data[0]["Value"]).decode("utf-8") |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 118 | return response.status_code, json.loads(value) |
| 119 | |
| 120 | @staticmethod |
| 121 | def get_value(audit, key): |
| 122 | """get the value for the key from consul-kv""" |
Alex Shatov | 209f823 | 2018-09-20 15:15:45 -0400 | [diff] [blame] | 123 | discovery_url = DiscoveryClient.CONSUL_KV_MASK.format(Config.consul_url, key) |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 124 | metrics = Metrics(aud_parent=audit, targetEntity=DiscoveryClient.CONSUL_ENTITY, |
| 125 | targetServiceName=discovery_url) |
| 126 | |
| 127 | log_line = "get from {} at {}".format(DiscoveryClient.CONSUL_ENTITY, discovery_url) |
| 128 | |
Alex Shatov | 9a4d3c5 | 2019-04-01 11:32:06 -0400 | [diff] [blame] | 129 | _LOGGER.info(metrics.metrics_start(log_line)) |
Alex Shatov | a39f4e8 | 2018-12-05 15:23:50 -0500 | [diff] [blame] | 130 | status_code = None |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 131 | try: |
| 132 | status_code, value = DiscoveryClient._get_value_from_kv(discovery_url) |
| 133 | except Exception as ex: |
| 134 | error_code = (AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value |
| 135 | if isinstance(ex, requests.exceptions.RequestException) |
| 136 | else AuditHttpCode.SERVER_INTERNAL_ERROR.value) |
| 137 | error_msg = ("failed {}/{} to {} {}: {}".format(status_code, error_code, log_line, |
| 138 | type(ex).__name__, str(ex))) |
Alex Shatov | 78ff88f | 2020-02-27 12:45:54 -0500 | [diff] [blame^] | 139 | _LOGGER.exception(metrics.fatal(error_msg)) |
Alex Shatov | 1d69337 | 2018-08-24 13:15:04 -0400 | [diff] [blame] | 140 | metrics.set_http_status_code(error_code) |
| 141 | audit.set_http_status_code(error_code) |
| 142 | metrics.metrics(error_msg) |
| 143 | return None |
| 144 | |
| 145 | log_line = "response {} {}".format(status_code, log_line) |
| 146 | metrics.set_http_status_code(status_code) |
| 147 | audit.set_http_status_code(status_code) |
| 148 | metrics.metrics(log_line) |
| 149 | return value |