瀏覽代碼

Compute slug in CreateOrUpdateDashboard

Brendan Abolivier 6 年之前
父節點
當前提交
b79da1b71c
簽署人: Brendan Abolivier <contact@brendanabolivier.com> GPG 金鑰 ID: 8EF1500759F70623
共有 2 個文件被更改,包括 9 次插入8 次删除
  1. 8
    4
      src/grafana/dashboards.go
  2. 1
    4
      src/pusher/webhook.go

+ 8
- 4
src/grafana/dashboards.go 查看文件

@@ -3,6 +3,8 @@ package grafana
3 3
 import (
4 4
 	"encoding/json"
5 5
 	"fmt"
6
+
7
+	"grafana/helpers"
6 8
 )
7 9
 
8 10
 // dbSearchResponse represents an element of the response to a dashboard search
@@ -129,12 +131,9 @@ func (c *Client) GetDashboard(URI string) (db *Dashboard, err error) {
129 131
 // existing one. The Grafana API decides whether to create or update based on the
130 132
 // "id" attribute in the dashboard's JSON: If it's unkown or null, it's a
131 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 134
 // Returns an error if there was an issue generating the request body, performing
136 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 137
 	reqBody := dbCreateOrUpdateRequest{
139 138
 		Dashboard: rawJSON(contentJSON),
140 139
 		Overwrite: true,
@@ -161,6 +160,11 @@ func (c *Client) CreateOrUpdateDashboard(slug string, contentJSON []byte) (err e
161 160
 		return
162 161
 	}
163 162
 
163
+	slug, err := helpers.GetDashboardSlug(contentJSON)
164
+	if err != nil {
165
+		return
166
+	}
167
+
164 168
 	if respBody.Status != "success" && isHttpUnknownError {
165 169
 		return fmt.Errorf(
166 170
 			"Failed to update dashboard %s (%d %s): %s",

+ 1
- 4
src/pusher/webhook.go 查看文件

@@ -154,10 +154,7 @@ func pushFile(filename string) error {
154 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 160
 // isIgnored checks whether the file must be ignored, by checking if there's an