-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_checks.go
191 lines (168 loc) · 4.9 KB
/
command_checks.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
package main
import (
"fmt"
"github.com/1and1/soma/internal/help"
"github.com/1and1/soma/internal/adm"
"github.com/1and1/soma/internal/cmpl"
"github.com/1and1/soma/lib/proto"
"github.com/codegangsta/cli"
)
func registerChecks(app cli.App) *cli.App {
app.Commands = append(app.Commands,
[]cli.Command{
{
Name: "checks",
Usage: "SUBCOMMANDS for check configurations",
Subcommands: []cli.Command{
{
Name: "create",
Usage: "Create a new check configuration",
Description: help.CmdCheckAdd,
Action: runtime(cmdCheckAdd),
BashComplete: cmpl.CheckAdd,
},
{
Name: `delete`,
Usage: "Delete a check configuration",
Description: help.CmdCheckDelete,
Action: runtime(cmdCheckDelete),
BashComplete: cmpl.In,
},
{
Name: "list",
Usage: "List check configurations",
Description: help.CmdCheckList,
Action: runtime(cmdCheckList),
BashComplete: cmpl.In,
},
{
Name: "show",
Usage: "Show details about a check configuration",
Description: help.CmdCheckShow,
Action: runtime(cmdCheckShow),
BashComplete: cmpl.In,
},
},
},
}...,
)
return &app
}
func cmdCheckAdd(c *cli.Context) error {
utl.ValidateCliMinArgumentCount(c, 8)
opts, constraints, thresholds := utl.ParseVariadicCheckArguments(c.Args().Tail())
req := proto.Request{}
req.CheckConfig = &proto.CheckConfig{
Name: utl.ValidateRuneCount(c.Args().First(), 256),
Interval: utl.GetValidatedUint64(opts["interval"][0], 1),
BucketId: utl.BucketByUUIDOrName(Client, opts["in"][0]),
CapabilityId: utl.TryGetCapabilityByUUIDOrName(Client, opts["with"][0]),
ObjectType: opts["on/type"][0],
}
req.CheckConfig.RepositoryId = utl.GetRepositoryIdForBucket(
Client, req.CheckConfig.BucketId)
req.CheckConfig.ObjectId = utl.GetObjectIdForCheck(
Client,
opts["on/type"][0],
opts["on/object"][0],
req.CheckConfig.BucketId)
// clear bucketid if check is on a repository
if req.CheckConfig.ObjectType == "repository" {
req.CheckConfig.BucketId = ""
}
// optional argument: inheritance
if iv, ok := opts["inheritance"]; ok {
req.CheckConfig.Inheritance = utl.GetValidatedBool(iv[0])
} else {
// inheritance defaults to true
req.CheckConfig.Inheritance = true
}
// optional argument: childrenonly
if co, ok := opts["childrenonly"]; ok {
req.CheckConfig.ChildrenOnly = utl.GetValidatedBool(co[0])
} else {
// childrenonly defaults to false
req.CheckConfig.ChildrenOnly = false
}
// optional argument: extern
if ex, ok := opts["extern"]; ok {
req.CheckConfig.ExternalId = utl.ValidateRuneCount(ex[0], 64)
}
teamId := utl.GetTeamIdByRepositoryId(Client, req.CheckConfig.RepositoryId)
req.CheckConfig.Thresholds = utl.CleanThresholds(Client, thresholds)
req.CheckConfig.Constraints = utl.CleanConstraints(
Client,
constraints,
req.CheckConfig.RepositoryId,
teamId)
path := fmt.Sprintf("/checks/%s/", req.CheckConfig.RepositoryId)
if resp, err := adm.PostReqBody(req, path); err != nil {
return err
} else {
fmt.Println(resp)
}
return nil
}
func cmdCheckDelete(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 3)
multiple := []string{}
unique := []string{"in"}
required := []string{"in"}
opts := utl.ParseVariadicArguments(
multiple,
unique,
required,
c.Args().Tail())
bucketId := utl.BucketByUUIDOrName(Client, opts["in"][0])
repoId := utl.GetRepositoryIdForBucket(Client, bucketId)
checkId := utl.TryGetCheckByUUIDOrName(Client, c.Args().First(), repoId)
path := fmt.Sprintf("/checks/%s/%s", repoId, checkId)
if resp, err := adm.DeleteReq(path); err != nil {
return err
} else {
fmt.Println(resp)
}
return nil
}
func cmdCheckList(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 2)
multiple := []string{}
unique := []string{"in"}
required := []string{"in"}
opts := utl.ParseVariadicArguments(
multiple,
unique,
required,
c.Args())
bucketId := utl.BucketByUUIDOrName(Client, opts["in"][0])
repoId := utl.GetRepositoryIdForBucket(Client, bucketId)
path := fmt.Sprintf("/checks/%s/", repoId)
if resp, err := adm.GetReq(path); err != nil {
return err
} else {
fmt.Println(resp)
}
return nil
}
func cmdCheckShow(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 3)
multiple := []string{}
unique := []string{"in"}
required := []string{"in"}
opts := utl.ParseVariadicArguments(
multiple,
unique,
required,
c.Args().Tail())
bucketId := utl.BucketByUUIDOrName(Client, opts["in"][0])
repoId := utl.GetRepositoryIdForBucket(Client, bucketId)
checkId := utl.TryGetCheckByUUIDOrName(Client, c.Args().First(), repoId)
path := fmt.Sprintf("/checks/%s/%s", repoId, checkId)
if resp, err := adm.GetReq(path); err != nil {
return err
} else {
fmt.Println(resp)
}
return nil
}
// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix