@cryptotaxi247 / CoPilot / commits / 12a7a6fe

Add grafana_url parameter to load_dashboard_json function (#185)

taylor_socfortress committed Mar 29, 2024 at 08:51 UTC 12a7a6fea0400d0b8f94c714a0b870e708981a15
5 files changed +34 -1
backend/app/connectors/grafana/schema/dashboards.py
+4
@@ -89,6 +89,10 @@ class DashboardProvisionRequest(BaseModel):
89 "uid-to-be-replaced",
90 description="Datasource UID to use for dashboards",
91 )
92 + grafana_url: str = Field(
93 + "https://grafana.company.local",
94 + description="URL of the Grafana instance for the links within the dashboards.",
95 + )
96
97 @validator("dashboards", each_item=True)
98 def check_dashboard_exists(cls, e):
backend/app/connectors/grafana/services/dashboards.py
+24 -1
@@ -31,7 +31,7 @@ def get_dashboard_path(dashboard_info: tuple) -> Path:
31 return base_dir / "dashboards" / folder_name / file_name
32
33
34 -def load_dashboard_json(dashboard_info: tuple, datasource_uid: str) -> dict:
34 +def load_dashboard_json(dashboard_info: tuple, datasource_uid: str, grafana_url: str) -> dict:
35 """
36 Load the JSON data of a dashboard from a file and replace the 'uid' value with the provided datasource UID.
37
@@ -53,6 +53,7 @@ def load_dashboard_json(dashboard_info: tuple, datasource_uid: str) -> dict:
53
54 # Search for 'uid' with 'wazuh_datasource_uid' and replace it
55 replace_uid_value(dashboard_data, datasource_uid)
56 + replace_grafana_url(dashboard_data, grafana_url)
57
58 return dashboard_data
59
@@ -90,6 +91,27 @@ def replace_uid_value(
91 replace_uid_value(item, new_value, key_to_replace, old_value)
92
93
94 +def replace_grafana_url(obj, new_value, key_to_replace="url", old_value="https://grafana.company.local"):
95 + """
96 + Recursively replaces the value of a specified key in a nested dictionary or list.
97 +
98 + Args:
99 + obj (dict or list): The object to be traversed and modified.
100 + new_value: The new value to replace the old value with.
101 + key_to_replace (str): The key to be replaced. Defaults to "url".
102 + old_value: The old value to be replaced. Defaults to "https://grafana.company.local".
103 + """
104 + if isinstance(obj, dict):
105 + for k, v in obj.items():
106 + if k == key_to_replace and isinstance(v, str) and v.startswith(old_value):
107 + obj[k] = v.replace(old_value, new_value)
108 + elif isinstance(v, (dict, list)):
109 + replace_grafana_url(v, new_value, key_to_replace, old_value)
110 + elif isinstance(obj, list):
111 + for item in obj:
112 + replace_grafana_url(item, new_value, key_to_replace, old_value)
113 +
114 +
115 async def update_dashboard(
116 dashboard_json: dict,
117 organization_id: int,
@@ -161,6 +183,7 @@ async def provision_dashboards(
183 dashboard_json = load_dashboard_json(
184 dashboard_enum.value,
185 datasource_uid=dashboard_request.datasourceUid,
186 + grafana_url=dashboard_request.grafana_url,
187 )
188 updated_dashboard = await update_dashboard(
189 dashboard_json=dashboard_json,
backend/app/customer_provisioning/routes/provision.py
+1
@@ -360,5 +360,6 @@ async def provision_dashboards_route(
360 organizationId=request.grafana_org_id,
361 folderId=request.grafana_folder_id,
362 datasourceUid=request.grafana_datasource_uid,
363 + grafana_url=request.grafana_url,
364 ),
365 )
backend/app/customer_provisioning/schema/provision.py
+4
@@ -248,6 +248,10 @@ class ProvisionDashboardRequest(BaseModel):
248 ...,
249 description="ID of the Grafana folder",
250 )
251 + grafana_url: str = Field(
252 + ...,
253 + description="URL of the Grafana instance",
254 + )
255
256
257 class ProvisionDashboardResponse(BaseModel):
backend/app/customer_provisioning/services/provision.py
+1
@@ -87,6 +87,7 @@ async def provision_wazuh_customer(
87 organizationId=provision_meta_data["grafana_organization_id"],
88 folderId=provision_meta_data["grafana_edr_folder_id"],
89 datasourceUid=provision_meta_data["wazuh_datasource_uid"],
90 + grafana_url=request.grafana_url,
91 ),
92 )
93