Browse Source

provisioning: removes id from dashboard.json

if dashboard json files contains `id` we
will just remove it form the dashboard model
before importing it into the database.

closes #11138
bergquist 7 years ago
parent
commit
b61bc72e2a

+ 2 - 2
pkg/services/provisioning/dashboards/file_reader.go

@@ -170,8 +170,8 @@ func (fr *fileReader) saveDashboard(path string, folderId int64, fileInfo os.Fil
 	}
 
 	if dash.Dashboard.Id != 0 {
-		fr.log.Error("provisioned dashboard json files cannot contain id")
-		return provisioningMetadata, nil
+		dash.Dashboard.Data.Set("id", nil)
+		dash.Dashboard.Id = 0
 	}
 
 	if alreadyProvisioned {

+ 16 - 3
pkg/services/provisioning/dashboards/file_reader_test.go

@@ -15,9 +15,10 @@ import (
 )
 
 var (
-	defaultDashboards string = "./test-dashboards/folder-one"
-	brokenDashboards  string = "./test-dashboards/broken-dashboards"
-	oneDashboard      string = "./test-dashboards/one-dashboard"
+	defaultDashboards = "./test-dashboards/folder-one"
+	brokenDashboards  = "./test-dashboards/broken-dashboards"
+	oneDashboard      = "./test-dashboards/one-dashboard"
+	containingId      = "./test-dashboards/containing-id"
 
 	fakeService *fakeDashboardProvisioningService
 )
@@ -85,6 +86,18 @@ func TestDashboardFileReader(t *testing.T) {
 				So(len(fakeService.inserted), ShouldEqual, 1)
 			})
 
+			Convey("Overrides id from dashboard.json files", func() {
+				cfg.Options["path"] = containingId
+
+				reader, err := NewDashboardFileReader(cfg, logger)
+				So(err, ShouldBeNil)
+
+				err = reader.startWalkingDisk()
+				So(err, ShouldBeNil)
+
+				So(len(fakeService.inserted), ShouldEqual, 1)
+			})
+
 			Convey("Invalid configuration should return error", func() {
 				cfg := &DashboardsAsConfig{
 					Name:   "Default",

+ 68 - 0
pkg/services/provisioning/dashboards/test-dashboards/containing-id/dashboard1.json

@@ -0,0 +1,68 @@
+{
+    "title": "Grafana1",
+    "tags": [],
+    "id": 3,
+    "style": "dark",
+    "timezone": "browser",
+    "editable": true,
+    "rows": [
+      {
+        "title": "New row",
+        "height": "150px",
+        "collapse": false,
+        "editable": true,
+        "panels": [
+          {
+            "id": 1,
+            "span": 12,
+            "editable": true,
+            "type": "text",
+            "mode": "html",
+            "content": "<div class=\"text-center\" style=\"padding-top: 15px\">\n<img src=\"img/logo_transparent_200x.png\"> \n</div>",
+            "style": {},
+            "title": "Welcome to"
+          }
+        ]
+      }
+    ],
+    "nav": [
+      {
+        "type": "timepicker",
+        "collapse": false,
+        "enable": true,
+        "status": "Stable",
+        "time_options": [
+          "5m",
+          "15m",
+          "1h",
+          "6h",
+          "12h",
+          "24h",
+          "2d",
+          "7d",
+          "30d"
+        ],
+        "refresh_intervals": [
+          "5s",
+          "10s",
+          "30s",
+          "1m",
+          "5m",
+          "15m",
+          "30m",
+          "1h",
+          "2h",
+          "1d"
+        ],
+        "now": true
+      }
+    ],
+    "time": {
+      "from": "now-6h",
+      "to": "now"
+    },
+    "templating": {
+      "list": []
+    },
+    "version": 5
+  }