/
main.go
136 lines (114 loc) · 2.85 KB
/
main.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"regexp"
"net/http"
"github.com/Sirupsen/logrus"
"github.com/meteor/datadog-sync/dd"
"github.com/meteor/datadog-sync/util"
)
var (
modeStr = flag.String("mode", "pull", "Mode: push or pull")
formatStr = flag.String("format", "yaml", "File format: yaml or json")
filter = flag.String("only", "", "Regex to restrict synced monitors to those whose name match")
dryRun = flag.Bool("dry-run", false, "Dry run: output which changes would be performed")
verbose = flag.Bool("verbose", false, "Verbose: print complete monitors during synchronization")
withIds = flag.Bool("ids", false, "Include monitor IDs in dump")
)
type mode int
const (
pull mode = iota
push
)
func filteredMonitors(from []dd.Monitor, filter string) ([]dd.Monitor, error) {
if filter == "" {
return from, nil
}
reg, err := regexp.Compile(filter)
if err != nil {
return nil, err
}
var res []dd.Monitor
for _, m := range from {
if reg.MatchString(m.Name) {
res = append(res, m)
}
}
return res, nil
}
func main() {
logrus.SetLevel(logrus.DebugLevel)
var action mode
var format util.Format
flag.Parse()
switch *modeStr {
case "pull":
action = pull
case "push":
action = push
default:
logrus.Fatalf("unsupported mode %v", *modeStr)
}
switch *formatStr {
case "json":
format = util.JSON
case "yaml":
format = util.YAML
default:
logrus.Fatalf("unsupported format %v", *formatStr)
}
if *dd.APIKey == "" {
var ok bool
if *dd.APIKey, ok = os.LookupEnv("DATADOG_API_KEY"); !ok {
logrus.Fatal("no API key provided")
}
}
if *dd.AppKey == "" {
var ok bool
if *dd.AppKey, ok = os.LookupEnv("DATADOG_APP_KEY"); !ok {
logrus.Fatal("no application key provided")
}
}
client := &http.Client{}
remote, err := dd.GetMonitors(client)
if err != nil {
logrus.WithError(err).Fatal("could not pull monitors")
}
remote, err = filteredMonitors(remote, *filter)
if err != nil {
logrus.WithError(err).Fatal("could not filter remote monitors")
}
switch action {
case pull:
var repr string
if !*withIds {
for i := range remote {
remote[i].ID = nil
}
}
repr, err = util.Marshal(remote, format)
if err != nil {
logrus.WithError(err).Fatal("could not serialize monitors")
}
fmt.Println(repr)
case push:
var local []dd.Monitor
repr, err := ioutil.ReadAll(os.Stdin)
if err != nil {
logrus.WithError(err).Fatal("could not read from standard input")
}
if err = util.Unmarshal(repr, &local, format); err != nil {
logrus.WithError(err).Fatal("could not deserialize monitors")
}
local, err = filteredMonitors(local, *filter)
if err != nil {
logrus.WithError(err).Fatal("could not filter local monitors")
}
if err = dd.SyncMonitors(local, remote, client, *dryRun, *verbose); err != nil {
logrus.WithError(err).Fatal("could not sync monitors: %v")
}
}
}