|
27 | 27 | DatasourceItem,
|
28 | 28 | GrafanaDataModel,
|
29 | 29 | )
|
30 |
| -from grafana_wtf.util import JsonPathFinder, as_bool |
| 30 | +from grafana_wtf.util import JsonPathFinder, as_bool, to_list |
31 | 31 |
|
32 | 32 | log = logging.getLogger(__name__)
|
33 | 33 |
|
@@ -499,28 +499,40 @@ def index(self):
|
499 | 499 | def collect_datasource_items(self, element):
|
500 | 500 | element = element or []
|
501 | 501 | items = []
|
| 502 | + |
| 503 | + def add(item): |
| 504 | + if item is not None and item not in items: |
| 505 | + items.append(item) |
| 506 | + |
502 | 507 | for node in element:
|
503 |
| - ds = None |
504 | 508 |
|
505 | 509 | # Directly defined datasources.
|
506 | 510 | if "datasource" in node and node["datasource"]:
|
507 | 511 | ds = node.datasource
|
508 |
| - if isinstance(ds, Munch): |
| 512 | + if isinstance(ds, str): |
| 513 | + add(ds) |
| 514 | + elif isinstance(ds, Munch): |
509 | 515 | ds = dict(ds)
|
| 516 | + add(ds) |
| 517 | + continue |
510 | 518 |
|
511 | 519 | # Datasources defined as variables.
|
512 | 520 | if "type" in node and node["type"] == "datasource":
|
513 |
| - ds_name = node.get("current", {}).get("value") |
514 |
| - datasource = self.datasource_by_name.get(ds_name, {}) |
515 |
| - ds = dict( |
516 |
| - type=datasource.get("type"), |
517 |
| - uid=datasource.get("uid"), |
518 |
| - name=datasource.get("name"), |
519 |
| - url=datasource.get("url"), |
520 |
| - ) |
| 521 | + values = to_list(node.get("current", {}).get("value")) |
| 522 | + for ds_name in values: |
| 523 | + datasource = self.datasource_by_name.get(ds_name) |
| 524 | + if datasource is None: |
| 525 | + log.warning(f"Data source '{ds_name}' not found") |
| 526 | + continue |
| 527 | + ds = dict( |
| 528 | + type=datasource.get("type"), |
| 529 | + uid=datasource.get("uid"), |
| 530 | + name=datasource.get("name"), |
| 531 | + url=datasource.get("url"), |
| 532 | + ) |
| 533 | + add(ds) |
| 534 | + continue |
521 | 535 |
|
522 |
| - if ds is not None and ds not in items: |
523 |
| - items.append(ds) |
524 | 536 | return items
|
525 | 537 |
|
526 | 538 | def index_dashboards(self):
|
|
0 commit comments