@@ -3,6 +3,7 @@ package plugin
33import (
44 "context"
55 "encoding/json"
6+ "fmt"
67
78 "github.com/grafana/grafana-plugin-sdk-go/backend"
89 "github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
@@ -90,28 +91,29 @@ func (d *SampleDatasource) CheckHealth(_ context.Context, req *backend.CheckHeal
9091 dashboardTag := uiProperties .PushConfiguration .TagPattern
9192 privateKey := []byte (uiSecureProperties ["privateSshKey" ])
9293
93- // privateKeyFilePath := uiSecureProperties["privateKeyFilePath"]
94-
9594 grafanaApi := NewGrafanaApi (grafanaUrl , token )
9695
9796 gitApi := NewGitApi (uiProperties .GitUrl , privateKey )
9897 log .DefaultLogger .Info ("Using Git repository from: %s" , uiProperties .GitUrl )
9998
99+ // clone and fetch repo
100100 repository , err := gitApi .CloneRepo ()
101101 if err != nil {
102102 return nil , err
103103 }
104104 gitApi .FetchRepo (* repository )
105105
106+ // Pull
106107 if uiProperties .PullConfiguration .Enable {
107108 log .DefaultLogger .Info ("Pull from git repo" , "url" , gitUrl )
108109
109110 gitApi .PullRepo (* repository )
110111 fileMap := gitApi .GetFileContent ()
111112 grafanaApi .CreateDashboardObjects (fileMap )
112- log .DefaultLogger .Info ("Dashboards created" )
113+ log .DefaultLogger .Info ("Pull process finished and dashboards created" )
113114 }
114115
116+ // Push
115117 if uiProperties .PushConfiguration .Enable {
116118 log .DefaultLogger .Info ("Push to git repo" , "url" , gitUrl )
117119
@@ -133,19 +135,17 @@ func (d *SampleDatasource) CheckHealth(_ context.Context, req *backend.CheckHeal
133135 folderName := boardProperties .FolderTitle
134136 folderId := boardProperties .FolderID
135137
136- // update dashboard with deleted Tag in Grafana
137- _ , err = grafanaApi .UpdateDashboardObjectByID (dashboardWithDeletedTag , folderId )
138- if err != nil {
139- log .DefaultLogger .Error ("update dashboard" , "error" , err .Error ())
140- }
141-
142138 // get raw Json Dashboard, required for import and export
143- dashboardJson , _ , err := grafanaApi . GetRawDashboardByID ( dashboard . UID )
139+ dashboardJson , err := json . Marshal ( dashboardWithDeletedTag )
144140 if err != nil {
145141 log .DefaultLogger .Error ("get raw dashboard" , "error" , err .Error ())
146142 }
147143
144+ // update dashboard with deleted Tag in Grafana
145+ grafanaApi .CreateOrUpdateDashboardObjectByID (dashboardJson , folderId , fmt .Sprintf ("Deleted '%s' tag" , dashboardTag ))
148146 log .DefaultLogger .Debug ("Dashboard preparation successfully " )
147+
148+ // Add Dashboard to in memory file system
149149 gitApi .AddFileWithContent (folderName + "/" + dashboardObject .Title + ".json" , string (dashboardJson ))
150150 log .DefaultLogger .Debug ("Dashboard added to in memory file system" )
151151 }
@@ -155,7 +155,7 @@ func (d *SampleDatasource) CheckHealth(_ context.Context, req *backend.CheckHeal
155155 gitApi .PushRepo (* repository )
156156 }
157157
158- log .DefaultLogger .Info ("Dashboard pushed successfully" )
158+ log .DefaultLogger .Info ("Dashboards pushed successfully" )
159159 }
160160
161161 return & backend.CheckHealthResult {
0 commit comments