Skip to content

Commit

Permalink
Add client nil checks to avoid panics (#2008)
Browse files Browse the repository at this point in the history
* Add client nil checks to avoid panics

* Update docs

* Fix docs again

* Update docs
  • Loading branch information
spinillos authored Jan 29, 2025
1 parent 8ad5c07 commit f572d44
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/resources/dashboard_public.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ subcategory: "Grafana OSS"
description: |-
Manages Grafana public dashboards.
Note: This resource is available only with Grafana 10.2+.
Official documentation https://grafana.com/docs/grafana/latest/dashboards/dashboard-public/HTTP API https://grafana.com/docs/grafana/next/developers/http_api/dashboard_public/
Official documentation https://grafana.com/docs/grafana/latest/dashboards/share-dashboards-panels/shared-dashboards/HTTP API https://grafana.com/docs/grafana/next/developers/http_api/dashboard_public/
---

# grafana_dashboard_public (Resource)
Expand All @@ -14,7 +14,7 @@ Manages Grafana public dashboards.

**Note:** This resource is available only with Grafana 10.2+.

* [Official documentation](https://grafana.com/docs/grafana/latest/dashboards/dashboard-public/)
* [Official documentation](https://grafana.com/docs/grafana/latest/dashboards/share-dashboards-panels/shared-dashboards/)
* [HTTP API](https://grafana.com/docs/grafana/next/developers/http_api/dashboard_public/)

## Example Usage
Expand Down
9 changes: 9 additions & 0 deletions internal/resources/cloud/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,14 @@ func (r *basePluginFrameworkResource) Configure(ctx context.Context, req resourc
return
}

if client.GrafanaCloudAPI == nil {
resp.Diagnostics.AddError(
"The Grafana Provider is missing a configuration for the Grafana Cloud API.",
"Please ensure that cloud_api_url and cloud_access_policy_token are set in the provider configuration.",
)

return
}

r.client = client.GrafanaCloudAPI
}
18 changes: 18 additions & 0 deletions internal/resources/grafana/common_plugin_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ func (r *basePluginFrameworkDataSource) Configure(ctx context.Context, req datas
return
}

if client.GrafanaAPI == nil || client.GrafanaAPIConfig == nil {
resp.Diagnostics.AddError(
"The Grafana Provider is missing a configuration for the Grafana API.",
"Please ensure that url and auth are set in the provider configuration.",
)

return
}

r.client = client.GrafanaAPI
r.config = client.GrafanaAPIConfig
}
Expand Down Expand Up @@ -81,6 +90,15 @@ func (r *basePluginFrameworkResource) Configure(ctx context.Context, req resourc
return
}

if client.GrafanaAPI == nil || client.GrafanaAPIConfig == nil {
resp.Diagnostics.AddError(
"The Grafana Provider is missing a configuration for the Grafana API.",
"Please ensure that url and auth are set in the provider configuration.",
)

return
}

r.client = client.GrafanaAPI
r.config = client.GrafanaAPIConfig
}
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/grafana/resource_dashboard_public.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Manages Grafana public dashboards.
**Note:** This resource is available only with Grafana 10.2+.
* [Official documentation](https://grafana.com/docs/grafana/latest/dashboards/dashboard-public/)
* [Official documentation](https://grafana.com/docs/grafana/latest/dashboards/share-dashboards-panels/shared-dashboards/)
* [HTTP API](https://grafana.com/docs/grafana/next/developers/http_api/dashboard_public/)
`,

Expand Down
9 changes: 9 additions & 0 deletions internal/resources/machinelearning/resource_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ func (r *alertResource) Configure(ctx context.Context, req resource.ConfigureReq
return
}

if client.MLAPI == nil {
resp.Diagnostics.AddError(
"The Grafana Provider is missing a configuration for the MLAPI API.",
"Please ensure that url and auth are set in the provider configuration.",
)

return
}

r.mlapi = client.MLAPI
}

Expand Down
18 changes: 18 additions & 0 deletions internal/resources/oncall/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ func (r *basePluginFrameworkResource) Configure(ctx context.Context, req resourc
return
}

if client.OnCallClient == nil {
resp.Diagnostics.AddError(
"The Grafana Provider is missing a configuration for the OnCall API.",
"Please ensure that oncall_url and oncall_access_token/auth are set in the provider configuration.",
)

return
}

r.client = client.OnCallClient
}

Expand All @@ -60,6 +69,15 @@ func (r *basePluginFrameworkDataSource) Configure(ctx context.Context, req datas
return
}

if client.OnCallClient == nil {
resp.Diagnostics.AddError(
"The Grafana Provider is missing a configuration for the OnCall API.",
"Please ensure that oncall_url and oncall_access_token/auth are set in the provider configuration.",
)

return
}

r.client = client.OnCallClient
}

Expand Down

0 comments on commit f572d44

Please sign in to comment.