|
5 | 5 | "crypto/sha256" |
6 | 6 | "encoding/json" |
7 | 7 | "fmt" |
8 | | - "net/url" |
9 | 8 | "strconv" |
10 | 9 | "strings" |
11 | 10 |
|
@@ -47,12 +46,6 @@ Manages Grafana dashboards. |
47 | 46 | "It's automatically generated if not provided when creating a dashboard. " + |
48 | 47 | "The uid allows having consistent URLs for accessing dashboards and when syncing dashboards between multiple Grafana installs. ", |
49 | 48 | }, |
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 | | - }, |
56 | 49 | "dashboard_id": { |
57 | 50 | Type: schema.TypeInt, |
58 | 51 | Computed: true, |
@@ -96,87 +89,8 @@ Manages Grafana dashboards. |
96 | 89 | Description: "Set a commit message for the version history.", |
97 | 90 | }, |
98 | 91 | }, |
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. |
178 | 93 | } |
179 | | - return rawState, nil |
180 | 94 | } |
181 | 95 |
|
182 | 96 | 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{} |
215 | 129 | } |
216 | 130 |
|
217 | 131 | d.Set("uid", dashboard.Model["uid"].(string)) |
218 | | - d.Set("slug", dashboard.Meta.Slug) |
219 | 132 | d.Set("dashboard_id", int64(dashboard.Model["id"].(float64))) |
220 | 133 | d.Set("version", int64(dashboard.Model["version"].(float64))) |
221 | 134 | d.Set("url", strings.TrimRight(gapiURL, "/")+dashboard.Meta.URL) |
|
0 commit comments