Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit f5d9e4b

Browse files
Clean up source code and restructuring API calls (#19)
1 parent b182994 commit f5d9e4b

File tree

3 files changed

+26
-34
lines changed

3 files changed

+26
-34
lines changed

pkg/plugin/git_api.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ func NewGitApi(gitUrl string, privateKey []byte) GitApi {
3939
// helper function to create the git authenticator
4040
func createAuthenticator(privateKey []byte) (*ssh.PublicKeys, error) {
4141
// git authentication with ssh
42-
// _, err := os.Stat(privateKeyFilePath)
43-
// if err != nil {
44-
// log.DefaultLogger.Error("read file failed", privateKeyFilePath, err.Error())
45-
// return nil, err
46-
// }
4742
authenticator, err := ssh.NewPublicKeys("git", privateKey, "")
4843
if err != nil {
4944
log.DefaultLogger.Error("generate public keys failed", "error", err.Error())

pkg/plugin/grafana_api.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,20 @@ func (grafanaApi GrafanaApi) GetDashboardObjectByID(uid string) (sdk.Board, sdk.
4343
return dashboardObject, props, err
4444
}
4545

46-
// UpdateDashboardObjectByID update the Dashboard with the given dashboard object
47-
func (grafanaApi GrafanaApi) UpdateDashboardObjectByID(dashboard sdk.Board, folderId int) (sdk.StatusMessage, error) {
48-
statusMessage, err := grafanaApi.grafanaClient.SetDashboard(context.Background(), dashboard, sdk.SetDashboardParams{
49-
Overwrite: false,
50-
FolderID: folderId,
46+
// CreateOrUpdateDashboardObjectByID create or update the Dashboard with the given dashboard object
47+
func (grafanaApi GrafanaApi) CreateOrUpdateDashboardObjectByID(rawDashboard []byte, folderId int, message string) (sdk.StatusMessage) {
48+
statusMessage, err := grafanaApi.grafanaClient.SetRawDashboardWithParam(context.Background(), sdk.RawBoardRequest{
49+
Dashboard: rawDashboard,
50+
Parameters: sdk.SetDashboardParams{
51+
Overwrite: true,
52+
FolderID: folderId,
53+
Message: message,
54+
},
5155
})
52-
return statusMessage, err
56+
if err != nil {
57+
log.DefaultLogger.Error("set dashboard error", "error", err.Error())
58+
}
59+
return statusMessage
5360
}
5461

5562
// CreateFolder create a folder in Grafana
@@ -130,25 +137,15 @@ func (grafanaApi GrafanaApi) parseGetDashboardError(error error, gitBoardID int,
130137
// CreateDashboardObjects set a Dashboard with the given raw dashboard object
131138
func (grafanaApi GrafanaApi) CreateDashboardObjects(fileMap map[string]map[string][]byte) {
132139
for dashboardDir, dashboardFile := range fileMap {
133-
dirID := grafanaApi.GetOrCreateFolderID(dashboardDir)
140+
folderID := grafanaApi.GetOrCreateFolderID(dashboardDir)
134141
for dashboardName, rawDashboard := range dashboardFile {
135142
gitDashboardObject := getDashboardObjectFromRawDashboard(rawDashboard)
136143
// get grafana dashboard to compare version with git dashboard
137144
grafanaDashboardObject, _, err := grafanaApi.GetDashboardObjectByID(gitDashboardObject.UID)
138145
grafanaDashboardVersionMessageId := grafanaApi.parseGetDashboardError(err, int(gitDashboardObject.ID), int(grafanaDashboardObject.ID))
139146

140147
if grafanaDashboardVersionMessageId != int(gitDashboardObject.Version) {
141-
_, err := grafanaApi.grafanaClient.SetRawDashboardWithParam(context.Background(), sdk.RawBoardRequest{
142-
Dashboard: rawDashboard,
143-
Parameters: sdk.SetDashboardParams{
144-
Overwrite: true,
145-
FolderID: dirID,
146-
Message: "Synchronized from Version " + strconv.Itoa(int(gitDashboardObject.Version)),
147-
},
148-
})
149-
if err != nil {
150-
log.DefaultLogger.Error("set dashboard error", "error", err.Error())
151-
}
148+
grafanaApi.CreateOrUpdateDashboardObjectByID(rawDashboard, folderID, "Synchronized from Version " + strconv.Itoa(int(gitDashboardObject.Version)))
152149
log.DefaultLogger.Debug("Dashboard created", "name", dashboardName)
153150
} else {
154151
log.DefaultLogger.Info("Dashboard already up-to-date", "name", dashboardName)

pkg/plugin/plugin.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package plugin
33
import (
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

Comments
 (0)