|
2 | 2 |
|
3 | 3 | import base64 |
4 | 4 | import json |
| 5 | +import re |
5 | 6 | import time |
| 7 | +from urllib.parse import urlparse |
6 | 8 |
|
7 | 9 | import requests |
8 | 10 |
|
9 | 11 | from .. import mgr |
10 | 12 | from ..exceptions import DashboardException |
11 | 13 | from ..security import Scope |
| 14 | +from ..services.orchestrator import OrchClient |
12 | 15 | from ..settings import Settings |
13 | 16 | from ..tools import configure_cors |
14 | 17 | from . import APIDoc, APIRouter, CreatePermission, DeletePermission, Endpoint, \ |
@@ -89,13 +92,13 @@ def auth(self, url: str, cluster_alias: str, username: str, |
89 | 92 | verify=ssl_verify, cert=ssl_certificate) |
90 | 93 |
|
91 | 94 | # add prometheus targets |
92 | | - prometheus_url = self._proxy('GET', url, 'api/settings/PROMETHEUS_API_HOST', |
| 95 | + prometheus_url = self._proxy('GET', url, 'api/multi-cluster/get_prometheus_api_url', |
93 | 96 | token=cluster_token) |
94 | 97 |
|
95 | | - _set_prometheus_targets(prometheus_url['value']) |
| 98 | + _set_prometheus_targets(prometheus_url) |
96 | 99 |
|
97 | 100 | self.set_multi_cluster_config(fsid, username, url, cluster_alias, |
98 | | - cluster_token, prometheus_url['value'], |
| 101 | + cluster_token, prometheus_url, |
99 | 102 | ssl_verify, ssl_certificate) |
100 | 103 | return True |
101 | 104 |
|
@@ -320,6 +323,25 @@ def check_token_status(self, clustersTokenMap=None): |
320 | 323 | clusters_token_map = json.loads(clustersTokenMap) |
321 | 324 | return self.check_token_status_array(clusters_token_map) |
322 | 325 |
|
| 326 | + @Endpoint() |
| 327 | + @ReadPermission |
| 328 | + def get_prometheus_api_url(self): |
| 329 | + prometheus_url = Settings.PROMETHEUS_API_HOST |
| 330 | + if prometheus_url is not None: |
| 331 | + # check if is url is already in IP format |
| 332 | + pattern = r'^(?:https?|http):\/\/(?:\d{1,3}\.){3}\d{1,3}:\d+$' |
| 333 | + valid_ip_url = bool(re.match(pattern, prometheus_url)) |
| 334 | + if not valid_ip_url: |
| 335 | + parsed_url = urlparse(prometheus_url) |
| 336 | + hostname = parsed_url.hostname |
| 337 | + orch = OrchClient.instance() |
| 338 | + inventory_hosts = [host.to_json() for host in orch.hosts.list()] |
| 339 | + for host in inventory_hosts: |
| 340 | + if host['hostname'] == hostname or host['hostname'] in hostname: |
| 341 | + node_ip = host['addr'] |
| 342 | + prometheus_url = prometheus_url.replace(hostname, node_ip) |
| 343 | + return prometheus_url |
| 344 | + |
323 | 345 |
|
324 | 346 | @UIRouter('/multi-cluster', Scope.CONFIG_OPT) |
325 | 347 | class MultiClusterUi(RESTController): |
|
0 commit comments