Browse Source

Compute slug in CreateOrUpdateDashboard

Brendan Abolivier 6 years ago
parent
commit
b79da1b71c
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
2 changed files with 9 additions and 8 deletions
  1. 8
    4
      src/grafana/dashboards.go
  2. 1
    4
      src/pusher/webhook.go

+ 8
- 4
src/grafana/dashboards.go View File

3
 import (
3
 import (
4
 	"encoding/json"
4
 	"encoding/json"
5
 	"fmt"
5
 	"fmt"
6
+
7
+	"grafana/helpers"
6
 )
8
 )
7
 
9
 
8
 // dbSearchResponse represents an element of the response to a dashboard search
10
 // dbSearchResponse represents an element of the response to a dashboard search
129
 // existing one. The Grafana API decides whether to create or update based on the
131
 // existing one. The Grafana API decides whether to create or update based on the
130
 // "id" attribute in the dashboard's JSON: If it's unkown or null, it's a
132
 // "id" attribute in the dashboard's JSON: If it's unkown or null, it's a
131
 // creation, else it's an update.
133
 // creation, else it's an update.
132
-// The slug is only used for error reporting, and can differ from the
133
-// dashboard's actual slug in its JSON content. However, it's recommended to use
134
-// the same in both places.
135
 // Returns an error if there was an issue generating the request body, performing
134
 // Returns an error if there was an issue generating the request body, performing
136
 // the request or decoding the response's body.
135
 // the request or decoding the response's body.
137
-func (c *Client) CreateOrUpdateDashboard(slug string, contentJSON []byte) (err error) {
136
+func (c *Client) CreateOrUpdateDashboard(contentJSON []byte) (err error) {
138
 	reqBody := dbCreateOrUpdateRequest{
137
 	reqBody := dbCreateOrUpdateRequest{
139
 		Dashboard: rawJSON(contentJSON),
138
 		Dashboard: rawJSON(contentJSON),
140
 		Overwrite: true,
139
 		Overwrite: true,
161
 		return
160
 		return
162
 	}
161
 	}
163
 
162
 
163
+	slug, err := helpers.GetDashboardSlug(contentJSON)
164
+	if err != nil {
165
+		return
166
+	}
167
+
164
 	if respBody.Status != "success" && isHttpUnknownError {
168
 	if respBody.Status != "success" && isHttpUnknownError {
165
 		return fmt.Errorf(
169
 		return fmt.Errorf(
166
 			"Failed to update dashboard %s (%d %s): %s",
170
 			"Failed to update dashboard %s (%d %s): %s",

+ 1
- 4
src/pusher/webhook.go View File

154
 		return err
154
 		return err
155
 	}
155
 	}
156
 
156
 
157
-	// Remove the .json part
158
-	slug := strings.Split(filename, ".json")[0]
159
-
160
-	return grafanaClient.CreateOrUpdateDashboard(slug, fileContent)
157
+	return grafanaClient.CreateOrUpdateDashboard(fileContent)
161
 }
158
 }
162
 
159
 
163
 // isIgnored checks whether the file must be ignored, by checking if there's an
160
 // isIgnored checks whether the file must be ignored, by checking if there's an