Skip to content

Add client nil checks to avoid panics #2008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading