-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from mpostument/folders-notifications-support
Folders and notification support
- Loading branch information
Showing
10 changed files
with
392 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package grafana | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func writeToFile(directory string, content []byte, name string, tag string) error { | ||
var ( | ||
err error | ||
path string | ||
dashboardFile *os.File | ||
fileName string | ||
) | ||
|
||
path = directory | ||
if tag != "" { | ||
path = filepath.Join(path, tag) | ||
} | ||
|
||
if _, err = os.Stat(path); os.IsNotExist(err) { | ||
if err = os.MkdirAll(path, 0755); err != nil { | ||
return err | ||
} | ||
} | ||
fileName = fmt.Sprintf("%s/%s.json", path, name) | ||
dashboardFile, err = os.Create(fileName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer dashboardFile.Close() | ||
|
||
err = ioutil.WriteFile(dashboardFile.Name(), content, os.FileMode(0755)) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package grafana | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"github.com/grafana-tools/sdk" | ||
) | ||
|
||
func PullDashboard(grafanaURL string, apiKey string, directory string, tag string) error { | ||
var ( | ||
boardLinks []sdk.FoundBoard | ||
rawBoard sdk.Board | ||
meta sdk.BoardProperties | ||
err error | ||
) | ||
ctx := context.Background() | ||
|
||
c := sdk.NewClient(grafanaURL, apiKey, sdk.DefaultHTTPClient) | ||
|
||
if boardLinks, err = c.Search(ctx, sdk.SearchTag(tag), sdk.SearchType(sdk.SearchTypeDashboard)); err != nil { | ||
return err | ||
} | ||
|
||
for _, link := range boardLinks { | ||
if rawBoard, meta, err = c.GetDashboardByUID(ctx, link.UID); err != nil { | ||
log.Printf("%s for %s\n", err, link.URI) | ||
continue | ||
} | ||
rawBoard.ID = 0 | ||
b, err := json.Marshal(rawBoard) | ||
if err != nil { | ||
return err | ||
} | ||
if err = writeToFile(directory, b, meta.Slug, tag); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func PushDashboard(grafanaURL string, apiKey string, directory string) error { | ||
var ( | ||
filesInDir []os.FileInfo | ||
rawBoard []byte | ||
err error | ||
) | ||
|
||
ctx := context.Background() | ||
c := sdk.NewClient(grafanaURL, apiKey, sdk.DefaultHTTPClient) | ||
if filesInDir, err = ioutil.ReadDir(directory); err != nil { | ||
return err | ||
} | ||
for _, file := range filesInDir { | ||
if strings.HasSuffix(file.Name(), ".json") { | ||
if rawBoard, err = ioutil.ReadFile(fmt.Sprintf("%s/%s", directory, file.Name())); err != nil { | ||
log.Println(err) | ||
continue | ||
} | ||
var board sdk.Board | ||
if err = json.Unmarshal(rawBoard, &board); err != nil { | ||
log.Println(err) | ||
continue | ||
} | ||
params := sdk.SetDashboardParams{ | ||
FolderID: sdk.DefaultFolderId, | ||
Overwrite: true, | ||
} | ||
if _, err := c.SetDashboard(ctx, board, params); err != nil { | ||
log.Printf("error on importing dashboard %s", board.Title) | ||
continue | ||
} | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.