-
Notifications
You must be signed in to change notification settings - Fork 32
/
annotations.go
214 lines (192 loc) · 6.25 KB
/
annotations.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
mkr "github.com/mackerelio/mackerel-client-go"
"github.com/mackerelio/mkr/logger"
"gopkg.in/urfave/cli.v1"
)
var commandAnnotations = cli.Command{
Name: "annotations",
Usage: "Manipulate graph annotations",
Description: `
Manipulate graph annotations. Requests APIs under "/api/v0/graph-annotations".
See https://mackerel.io/api-docs/entry/graph-annotations .
`,
Subcommands: []cli.Command{
{
Name: "create",
Usage: "create a graph annotation",
ArgsUsage: "[--title <title>] [--description <descriptio>] [--from <from>] [--to <to>] [--service -s <service>] [--role -r <role>]",
Description: `
Creates a graph annotation.
`,
Action: doAnnotationsCreate,
Flags: []cli.Flag{
cli.StringFlag{Name: "title", Usage: "Title for annotation"},
cli.StringFlag{Name: "description", Usage: "Description for annotation"},
cli.IntFlag{Name: "from", Usage: "Starting time (epoch seconds)"},
cli.IntFlag{Name: "to", Usage: "Ending time (epoch seconds)"},
cli.StringFlag{Name: "service, s", Usage: "Service name for annotation"},
cli.StringSliceFlag{
Name: "role, r",
Value: &cli.StringSlice{},
Usage: "Roles for annotation. Multiple choices are allowed",
},
},
},
{
Name: "list",
Usage: "list annotations",
ArgsUsage: "[--from <from>] [--to <to>] [--service -s <service>]",
Description: `
Shows annotations by service name and duration (from and to)
`,
Action: doAnnotationsList,
Flags: []cli.Flag{
cli.StringFlag{Name: "service, s", Usage: "Service name for annotation"},
cli.IntFlag{Name: "from", Usage: "Starting time (epoch seconds)"},
cli.IntFlag{Name: "to", Usage: "Ending time (epoch seconds)"},
},
},
{
Name: "update",
Usage: "update annotation",
ArgsUsage: "[--id <id>] [--title <title>] [--description <descriptio>] [--from <from>] [--to <to>] [--service -s <service>] [--role -r <role>]",
Description: `
Updates an annotation
`,
Action: doAnnotationsUpdate,
Flags: []cli.Flag{
cli.StringFlag{Name: "id", Usage: "Annotation ID."},
cli.StringFlag{Name: "service, s", Usage: "Service name for annotation"},
cli.StringFlag{Name: "title", Usage: "Title for annotation"},
cli.StringFlag{Name: "description", Usage: "Description for annotation"},
cli.IntFlag{Name: "from", Usage: "Starting time (epoch seconds)"},
cli.IntFlag{Name: "to", Usage: "Ending time (epoch seconds)"},
cli.StringSliceFlag{
Name: "role, r",
Value: &cli.StringSlice{},
Usage: "Roles for annotation. Multiple choices are allowed",
},
},
},
{
Name: "delete",
Usage: "delete annotation",
ArgsUsage: "[--id <id>]",
Description: `
Delete graph annotation by annotation id
`,
Action: doAnnotationsDelete,
Flags: []cli.Flag{
cli.StringFlag{Name: "id", Usage: "Graph annotation ID"},
},
},
},
}
func doAnnotationsCreate(c *cli.Context) error {
title := c.String("title")
description := c.String("description")
from := c.Int64("from")
to := c.Int64("to")
service := c.String("service")
roles := c.StringSlice("role")
if title == "" {
_ = cli.ShowCommandHelp(c, "create")
return cli.NewExitError("`title` is a required field to create a graph annotation.", 1)
}
if service == "" {
_ = cli.ShowCommandHelp(c, "create")
return cli.NewExitError("`service` is a required field to create a graph annotation.", 1)
}
if from == 0 {
_ = cli.ShowCommandHelp(c, "create")
return cli.NewExitError("`from` is a required field to create a graph annotation.", 1)
}
if to == 0 {
_ = cli.ShowCommandHelp(c, "create")
return cli.NewExitError("`to` is a required field to create a graph annotation.", 1)
}
client := newMackerelFromContext(c)
annotation, err := client.CreateGraphAnnotation(&mkr.GraphAnnotation{
Title: title,
Description: description,
From: from,
To: to,
Service: service,
Roles: roles,
})
logger.DieIf(err)
PrettyPrintJSON(annotation)
return nil
}
func doAnnotationsList(c *cli.Context) error {
service := c.String("service")
from := c.Int64("from")
to := c.Int64("to")
if service == "" {
_ = cli.ShowCommandHelp(c, "list")
return cli.NewExitError("`service` is a required field to list graph annotations.", 1)
}
if from == 0 {
_ = cli.ShowCommandHelp(c, "list")
return cli.NewExitError("`from` is a required field to list graph annotations.", 1)
}
if to == 0 {
_ = cli.ShowCommandHelp(c, "list")
return cli.NewExitError("`to` is a required field to list graph annotations.", 1)
}
client := newMackerelFromContext(c)
annotations, err := client.FindGraphAnnotations(service, from, to)
logger.DieIf(err)
PrettyPrintJSON(annotations)
return nil
}
func doAnnotationsUpdate(c *cli.Context) error {
annotationID := c.String("id")
title := c.String("title")
description := c.String("description")
from := c.Int64("from")
to := c.Int64("to")
service := c.String("service")
roles := c.StringSlice("role")
if annotationID == "" {
_ = cli.ShowCommandHelp(c, "update")
return cli.NewExitError("`id` is a required field to delete a update annotation.", 1)
}
if service == "" {
_ = cli.ShowCommandHelp(c, "update")
return cli.NewExitError("`service` is a required field to update a graph annotation.", 1)
}
if from == 0 {
_ = cli.ShowCommandHelp(c, "update")
return cli.NewExitError("`from` is a required field to update a graph annotation.", 1)
}
if to == 0 {
_ = cli.ShowCommandHelp(c, "update")
return cli.NewExitError("`to` is a required field to update a graph annotation.", 1)
}
client := newMackerelFromContext(c)
annotation, err := client.UpdateGraphAnnotation(annotationID, &mkr.GraphAnnotation{
Title: title,
Description: description,
From: from,
To: to,
Service: service,
Roles: roles,
})
logger.DieIf(err)
PrettyPrintJSON(annotation)
return nil
}
func doAnnotationsDelete(c *cli.Context) error {
annotationID := c.String("id")
if annotationID == "" {
_ = cli.ShowCommandHelp(c, "delete")
return cli.NewExitError("`id` is a required field to delete a graph annotation.", 1)
}
client := newMackerelFromContext(c)
annotation, err := client.DeleteGraphAnnotation(annotationID)
logger.DieIf(err)
PrettyPrintJSON(annotation)
return nil
}