-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrafana.go
57 lines (50 loc) · 1.17 KB
/
grafana.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"fmt"
"github.com/grafana-tools/sdk"
)
func grafanaPostDatasources(c *config, resolvedValues []*resolved) error {
fmt.Println()
fmt.Printf("Going to create these Grafana datasources on %s :", c.grafanaURL)
fmt.Println()
fmt.Println()
for _, r := range resolvedValues {
fmt.Printf(" NAME: %s URL: %s", r.id, r.url)
fmt.Println()
}
fmt.Println()
fmt.Println("Is this OK? Press enter to continue or Ctrl-C to abort...")
fmt.Scanln()
grafana := sdk.NewClient(c.grafanaURL, c.grafanaKey, sdk.DefaultHTTPClient)
existingDatasources, err := grafana.GetAllDatasources()
if err != nil {
return err
}
for _, r := range resolvedValues {
fmt.Printf("%s... ", r.id)
exists := false
for _, ds := range existingDatasources {
if ds.Name == r.id {
exists = true
break
}
}
if exists {
fmt.Print("Already exists, skipping")
} else {
var newDS sdk.Datasource
newDS.Name = r.id
newDS.Type = "prometheus"
newDS.URL = r.url
newDS.Access = "proxy"
msg, err := grafana.CreateDatasource(newDS)
if err != nil {
return err
}
fmt.Printf(*msg.Message)
}
fmt.Println()
}
fmt.Println("Done!")
return nil
}