Skip to content

Commit

Permalink
Add grafana_url parameter to load_dashboard_json function (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorwalton authored Mar 29, 2024
1 parent 2241878 commit 12a7a6f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions backend/app/connectors/grafana/schema/dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class DashboardProvisionRequest(BaseModel):
"uid-to-be-replaced",
description="Datasource UID to use for dashboards",
)
grafana_url: str = Field(
"https://grafana.company.local",
description="URL of the Grafana instance for the links within the dashboards.",
)

@validator("dashboards", each_item=True)
def check_dashboard_exists(cls, e):
Expand Down
25 changes: 24 additions & 1 deletion backend/app/connectors/grafana/services/dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_dashboard_path(dashboard_info: tuple) -> Path:
return base_dir / "dashboards" / folder_name / file_name


def load_dashboard_json(dashboard_info: tuple, datasource_uid: str) -> dict:
def load_dashboard_json(dashboard_info: tuple, datasource_uid: str, grafana_url: str) -> dict:
"""
Load the JSON data of a dashboard from a file and replace the 'uid' value with the provided datasource UID.
Expand All @@ -53,6 +53,7 @@ def load_dashboard_json(dashboard_info: tuple, datasource_uid: str) -> dict:

# Search for 'uid' with 'wazuh_datasource_uid' and replace it
replace_uid_value(dashboard_data, datasource_uid)
replace_grafana_url(dashboard_data, grafana_url)

return dashboard_data

Expand Down Expand Up @@ -90,6 +91,27 @@ def replace_uid_value(
replace_uid_value(item, new_value, key_to_replace, old_value)


def replace_grafana_url(obj, new_value, key_to_replace="url", old_value="https://grafana.company.local"):
"""
Recursively replaces the value of a specified key in a nested dictionary or list.
Args:
obj (dict or list): The object to be traversed and modified.
new_value: The new value to replace the old value with.
key_to_replace (str): The key to be replaced. Defaults to "url".
old_value: The old value to be replaced. Defaults to "https://grafana.company.local".
"""
if isinstance(obj, dict):
for k, v in obj.items():
if k == key_to_replace and isinstance(v, str) and v.startswith(old_value):
obj[k] = v.replace(old_value, new_value)
elif isinstance(v, (dict, list)):
replace_grafana_url(v, new_value, key_to_replace, old_value)
elif isinstance(obj, list):
for item in obj:
replace_grafana_url(item, new_value, key_to_replace, old_value)


async def update_dashboard(
dashboard_json: dict,
organization_id: int,
Expand Down Expand Up @@ -161,6 +183,7 @@ async def provision_dashboards(
dashboard_json = load_dashboard_json(
dashboard_enum.value,
datasource_uid=dashboard_request.datasourceUid,
grafana_url=dashboard_request.grafana_url,
)
updated_dashboard = await update_dashboard(
dashboard_json=dashboard_json,
Expand Down
1 change: 1 addition & 0 deletions backend/app/customer_provisioning/routes/provision.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,5 +360,6 @@ async def provision_dashboards_route(
organizationId=request.grafana_org_id,
folderId=request.grafana_folder_id,
datasourceUid=request.grafana_datasource_uid,
grafana_url=request.grafana_url,
),
)
4 changes: 4 additions & 0 deletions backend/app/customer_provisioning/schema/provision.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ class ProvisionDashboardRequest(BaseModel):
...,
description="ID of the Grafana folder",
)
grafana_url: str = Field(
...,
description="URL of the Grafana instance",
)


class ProvisionDashboardResponse(BaseModel):
Expand Down
1 change: 1 addition & 0 deletions backend/app/customer_provisioning/services/provision.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ async def provision_wazuh_customer(
organizationId=provision_meta_data["grafana_organization_id"],
folderId=provision_meta_data["grafana_edr_folder_id"],
datasourceUid=provision_meta_data["wazuh_datasource_uid"],
grafana_url=request.grafana_url,
),
)

Expand Down

0 comments on commit 12a7a6f

Please sign in to comment.