Skip to content

Commit

Permalink
Custom folder if for dashboard push (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpostument authored Mar 8, 2021
1 parent dd0f8be commit ebe68a2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
22 changes: 20 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Keep your grafana dashboards in sync.
- [Push](#push)
- [Push dashboards to grafana](#push-dashboards-to-grafana)
- [Push folders to grafana](#push-folders-to-grafana)
- [Push folders to grafana in custom folder](#push-folders-to-grafana-in-custom-folder)
- [Push notifications to grafana](#push-notifications-to-grafana)
- [Push datasources to grafana](#push-datasources-to-grafana)
- [Global parameters](#global-parameters)
Expand All @@ -33,13 +34,13 @@ Download the latest binary from [releases](https://github.com/mpostument/grafana

#### Save all dashboards to directory

`grafana-sync pull-dashboards`
`grafana-sync pull-dashboards --directory="dashboards`
Example:
`grafana-sync pull-dashboards --apikey="eyJrIjoiOWJYTktGNFlCbFVMOG1LY3d6ekN4Mmw4MFgyYU44a1UiLCJuIjoiY29icmEiLCJpZCI6MX0=" --directory="dashboards" --url http://127.0.0.1:3000`

#### Save dashboards with specific tags to directory

`grafana-sync pull-dashboards`
`grafana-sync pull-dashboards --tag=export`
Example:
`grafana-sync pull-dashboards --apikey="eyJrIjoiOWJYTktGNFlCbFVMOG1LY3d6ekN4Mmw4MFgyYU44a1UiLCJuIjoiY29icmEiLCJpZCI6MX0=" --directory="dashboards" --url http://127.0.0.1:3000 --tag=export`

Expand Down Expand Up @@ -75,6 +76,23 @@ Example:
Example:
`grafana-sync push-folders --apikey="eyJrIjoiOWJYTktGNFlCbFVMOG1LY3d6ekN4Mmw4MFgyYU44a1UiLCJuIjoiY29icmEiLCJpZCI6MX0=" --directory="folders" --url http://127.0.0.1:3000`

#### Push folders to grafana in custom folder

Find grafana folder Id by opening /api/folders in browser.
Example:
`http://127.0.0.1:3000/api/folders`

Find required folderId in output

```json
[{"id":1,"uid":"RcuWg5UMz","title":"TestFolder"}]
```

Specify folder id in push command
`grafana-sync push-folders --folderId=<FolderId>`
Example:
`grafana-sync push-folders --apikey="eyJrIjoiOWJYTktGNFlCbFVMOG1LY3d6ekN4Mmw4MFgyYU44a1UiLCJuIjoiY29icmEiLCJpZCI6MX0=" --directory="folders" --url http://127.0.0.1:3000 --folderId=1`

#### Push notifications to grafana

`grafana-sync push-notifications`
Expand Down
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ var pushDashboardsCmd = &cobra.Command{
url, _ := cmd.Flags().GetString("url")
apiKey, _ := cmd.Flags().GetString("apikey")
directory, _ := cmd.Flags().GetString("directory")
if err := grafana.PushDashboard(url, apiKey, directory); err != nil {
folderId, _ := cmd.Flags().GetInt("folderId")
if err := grafana.PushDashboard(url, apiKey, directory, folderId); err != nil {
log.Fatalln("Push dashboards command failed", err)
}
},
Expand Down Expand Up @@ -167,7 +168,8 @@ func init() {
rootCmd.PersistentFlags().StringP("url", "u", "http://localhost:3000", "Grafana Url with port")
rootCmd.PersistentFlags().StringP("directory", "d", ".", "Directory where to save dashboards")
rootCmd.PersistentFlags().StringP("apikey", "a", "", "Grafana api key")
rootCmd.PersistentFlags().StringP("tag", "t", "", "Dashboard tag to read")
pullDataSourcesCmd.PersistentFlags().StringP("tag", "t", "", "Dashboard tag to read")
pushDashboardsCmd.PersistentFlags().IntP("folderId", "f", 0, "Directory Id to which push dashboards")

if err := viper.BindPFlag("apikey", rootCmd.PersistentFlags().Lookup("apikey")); err != nil {
log.Println(err)
Expand Down
4 changes: 2 additions & 2 deletions grafana/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func PullDashboard(grafanaURL string, apiKey string, directory string, tag strin
return nil
}

func PushDashboard(grafanaURL string, apiKey string, directory string) error {
func PushDashboard(grafanaURL string, apiKey string, directory string, folderId int) error {
var (
filesInDir []os.FileInfo
rawBoard []byte
Expand All @@ -68,7 +68,7 @@ func PushDashboard(grafanaURL string, apiKey string, directory string) error {
continue
}
params := sdk.SetDashboardParams{
FolderID: sdk.DefaultFolderId,
FolderID: folderId,
Overwrite: true,
}
if _, err := c.SetDashboard(ctx, board, params); err != nil {
Expand Down

0 comments on commit ebe68a2

Please sign in to comment.