Skip to content

Commit 7ce0806

Browse files
Dashboard: Remove slug and its migration (#967)
* Dashboard: Remove `slug` and its migration This field is no longer used and the migration was needed for Grafana 7 -> 8 I don't think we need to carry this code anymore. Especially considering that users can do a two-step upgrade if they do have issues * docccsssss * Regen docs
1 parent 46dc450 commit 7ce0806

File tree

3 files changed

+1
-91
lines changed

3 files changed

+1
-91
lines changed

docs/data-sources/role.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,3 @@ Read-Only:
6969

7070
- `action` (String)
7171
- `scope` (String)
72-
73-

docs/resources/dashboard.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ resource "grafana_dashboard" "metrics" {
4040

4141
- `dashboard_id` (Number) The numeric ID of the dashboard computed by Grafana.
4242
- `id` (String) The ID of this resource.
43-
- `slug` (String, Deprecated) URL friendly version of the dashboard title. This field is deprecated, please use `uid` instead.
4443
- `uid` (String) The unique identifier of a dashboard. This is used to construct its URL. It's automatically generated if not provided when creating a dashboard. The uid allows having consistent URLs for accessing dashboards and when syncing dashboards between multiple Grafana installs.
4544
- `url` (String) The full URL of the dashboard.
4645
- `version` (Number) Whenever you save a version of your dashboard, a copy of that version is saved so that previous versions of your dashboard are not lost.

internal/resources/grafana/resource_dashboard.go

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"crypto/sha256"
66
"encoding/json"
77
"fmt"
8-
"net/url"
98
"strconv"
109
"strings"
1110

@@ -47,12 +46,6 @@ Manages Grafana dashboards.
4746
"It's automatically generated if not provided when creating a dashboard. " +
4847
"The uid allows having consistent URLs for accessing dashboards and when syncing dashboards between multiple Grafana installs. ",
4948
},
50-
"slug": {
51-
Type: schema.TypeString,
52-
Computed: true,
53-
Description: "URL friendly version of the dashboard title. This field is deprecated, please use `uid` instead.",
54-
Deprecated: "Use `uid` instead.",
55-
},
5649
"dashboard_id": {
5750
Type: schema.TypeInt,
5851
Computed: true,
@@ -96,87 +89,8 @@ Manages Grafana dashboards.
9689
Description: "Set a commit message for the version history.",
9790
},
9891
},
99-
SchemaVersion: 1,
100-
StateUpgraders: []schema.StateUpgrader{
101-
{
102-
Type: resourceDashboardV0().CoreConfigSchema().ImpliedType(),
103-
Upgrade: resourceDashboardStateUpgradeV0,
104-
Version: 0,
105-
},
106-
},
107-
}
108-
}
109-
110-
// resourceDashboardV0 is the original schema for this resource. For a long
111-
// time we relied on the `slug` field as our ID - even long after it was
112-
// deprecated in Grafana. In Grafana 8, slug endpoints were completely removed
113-
// so we had to finally move away from it and start using UID.
114-
func resourceDashboardV0() *schema.Resource {
115-
return &schema.Resource{
116-
Schema: map[string]*schema.Schema{
117-
"slug": {
118-
Type: schema.TypeString,
119-
Computed: true,
120-
},
121-
"dashboard_id": {
122-
Type: schema.TypeInt,
123-
Computed: true,
124-
},
125-
"folder": {
126-
Type: schema.TypeInt,
127-
Optional: true,
128-
ForceNew: true,
129-
},
130-
"config_json": {
131-
Type: schema.TypeString,
132-
Required: true,
133-
StateFunc: NormalizeDashboardConfigJSON,
134-
ValidateFunc: validateDashboardConfigJSON,
135-
},
136-
"overwrite": {
137-
Type: schema.TypeBool,
138-
Optional: true,
139-
},
140-
},
141-
}
142-
}
143-
144-
// resourceDashboardStateUpgradeV0 migrates from version 0 of this resource's
145-
// schema to version 1.
146-
// - Use UID instead of slug. Slug was deprecated in Grafana 5 in favor of UID.
147-
// Slug API endpoints were removed in Grafana 8.
148-
// - Version field added to schema.
149-
func resourceDashboardStateUpgradeV0(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
150-
client := meta.(*common.Client).GrafanaAPI
151-
dashboardID := int64(rawState["dashboard_id"].(float64))
152-
query := url.Values{
153-
"type": {"dash-db"},
154-
"dashboardIds": {strconv.FormatInt(dashboardID, 10)},
155-
}
156-
resp, err := client.FolderDashboardSearch(query)
157-
if err != nil {
158-
return nil, fmt.Errorf("error attempting to migrate state. Grafana returned an error while searching for dashboard with ID %s: %s", query.Get("dashboardIds"), err)
159-
}
160-
switch {
161-
case len(resp) > 1:
162-
// Search endpoint returned multiple dashboards. This is not likely.
163-
return nil, fmt.Errorf("error attempting to migrate state. Many dashboards returned by Grafana while searching for dashboard with ID, %s", query.Get("dashboardIds"))
164-
case len(resp) == 0:
165-
// Dashboard does not exist. Let Terraform recreate it.
166-
return rawState, nil
167-
}
168-
uid := resp[0].UID
169-
rawState["id"] = uid
170-
rawState["uid"] = uid
171-
dashboard, err := client.DashboardByUID(uid)
172-
// Set version if we can.
173-
// In the unlikely event that we don't get a dashboard back, we don't return
174-
// an error because Terraform will be able to reconcile this field without
175-
// much trouble.
176-
if err == nil && dashboard != nil {
177-
rawState["version"] = int64(dashboard.Model["version"].(float64))
92+
SchemaVersion: 1, // The state upgrader was removed in v2. To upgrade, users can first upgrade to the last v1 release, apply, then upgrade to v2.
17893
}
179-
return rawState, nil
18094
}
18195

18296
func CreateDashboard(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
@@ -215,7 +129,6 @@ func ReadDashboard(ctx context.Context, d *schema.ResourceData, meta interface{}
215129
}
216130

217131
d.Set("uid", dashboard.Model["uid"].(string))
218-
d.Set("slug", dashboard.Meta.Slug)
219132
d.Set("dashboard_id", int64(dashboard.Model["id"].(float64)))
220133
d.Set("version", int64(dashboard.Model["version"].(float64)))
221134
d.Set("url", strings.TrimRight(gapiURL, "/")+dashboard.Meta.URL)

0 commit comments

Comments
 (0)