forked from OpsLevel/opslevel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.go
306 lines (273 loc) · 11.4 KB
/
check.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package opslevel
import (
"encoding/json"
"fmt"
"github.com/relvacode/iso8601"
)
type CheckOwner struct {
Team TeamId `graphql:"... on Team"`
// User User `graphql:"... on User"` // TODO: will this be public?
}
type Check struct {
Category Category `graphql:"category"`
Description string `graphql:"description"`
Enabled bool `graphql:"enabled"`
EnableOn iso8601.Time `graphql:"enableOn"`
Filter Filter `graphql:"filter"`
Id ID `graphql:"id"`
Level Level `graphql:"level"`
Name string `graphql:"name"`
Notes string `graphql:"notes: rawNotes"`
Owner CheckOwner `graphql:"owner"`
Type CheckType `graphql:"type"`
// TODO: resort these alphabetically - It will require fixing all the test fixtures
AlertSourceUsageCheckFragment `graphql:"... on AlertSourceUsageCheck"`
CustomEventCheckFragment `graphql:"... on CustomEventCheck"`
HasRecentDeployCheckFragment `graphql:"... on HasRecentDeployCheck"`
ManualCheckFragment `graphql:"... on ManualCheck"`
RepositoryFileCheckFragment `graphql:"... on RepositoryFileCheck"`
RepositoryGrepCheckFragment `graphql:"... on RepositoryGrepCheck"`
RepositorySearchCheckFragment `graphql:"... on RepositorySearchCheck"`
ServiceOwnershipCheckFragment `graphql:"... on ServiceOwnershipCheck"`
ServicePropertyCheckFragment `graphql:"... on ServicePropertyCheck"`
TagDefinedCheckFragment `graphql:"... on TagDefinedCheck"`
ToolUsageCheckFragment `graphql:"... on ToolUsageCheck"`
HasDocumentationCheckFragment `graphql:"... on HasDocumentationCheck"`
}
type CheckInputConstructor func() any
var CheckCreateConstructors = map[CheckType]CheckInputConstructor{
CheckTypeAlertSourceUsage: func() any { return &CheckAlertSourceUsageCreateInput{} },
CheckTypeCustom: func() any { return &CheckCreateInput{} },
CheckTypeGeneric: func() any { return &CheckCustomEventCreateInput{} },
CheckTypeGitBranchProtection: func() any { return &CheckGitBranchProtectionCreateInput{} },
CheckTypeHasDocumentation: func() any { return &CheckHasDocumentationCreateInput{} },
CheckTypeHasOwner: func() any { return &CheckServiceOwnershipCreateInput{} },
CheckTypeHasRecentDeploy: func() any { return &CheckHasRecentDeployCreateInput{} },
CheckTypeHasRepository: func() any { return &CheckRepositoryIntegratedCreateInput{} },
CheckTypeHasServiceConfig: func() any { return &CheckServiceConfigurationCreateInput{} },
CheckTypeManual: func() any { return &CheckManualCreateInput{} },
CheckTypePayload: func() any { return &CheckCreateInput{} },
CheckTypeRepoFile: func() any { return &CheckRepositoryFileCreateInput{} },
CheckTypeRepoGrep: func() any { return &CheckRepositoryGrepCreateInput{} },
CheckTypeRepoSearch: func() any { return &CheckRepositorySearchCreateInput{} },
CheckTypeServiceDependency: func() any { return &CheckServiceDependencyCreateInput{} },
CheckTypeServiceProperty: func() any { return &CheckServicePropertyCreateInput{} },
CheckTypeTagDefined: func() any { return &CheckTagDefinedCreateInput{} },
CheckTypeToolUsage: func() any { return &CheckToolUsageCreateInput{} },
}
var CheckUpdateConstructors = map[CheckType]CheckInputConstructor{
CheckTypeAlertSourceUsage: func() any { return &CheckAlertSourceUsageUpdateInput{} },
CheckTypeCustom: func() any { return &CheckUpdateInput{} },
CheckTypeGeneric: func() any { return &CheckCustomEventUpdateInput{} },
CheckTypeGitBranchProtection: func() any { return &CheckGitBranchProtectionUpdateInput{} },
CheckTypeHasDocumentation: func() any { return &CheckHasDocumentationUpdateInput{} },
CheckTypeHasOwner: func() any { return &CheckServiceOwnershipUpdateInput{} },
CheckTypeHasRecentDeploy: func() any { return &CheckHasRecentDeployUpdateInput{} },
CheckTypeHasRepository: func() any { return &CheckRepositoryIntegratedUpdateInput{} },
CheckTypeHasServiceConfig: func() any { return &CheckServiceConfigurationUpdateInput{} },
CheckTypeManual: func() any { return &CheckManualUpdateInput{} },
CheckTypePayload: func() any { return &CheckUpdateInput{} },
CheckTypeRepoFile: func() any { return &CheckRepositoryFileUpdateInput{} },
CheckTypeRepoGrep: func() any { return &CheckRepositoryGrepUpdateInput{} },
CheckTypeRepoSearch: func() any { return &CheckRepositorySearchUpdateInput{} },
CheckTypeServiceDependency: func() any { return &CheckServiceDependencyUpdateInput{} },
CheckTypeServiceProperty: func() any { return &CheckServicePropertyUpdateInput{} },
CheckTypeTagDefined: func() any { return &CheckTagDefinedUpdateInput{} },
CheckTypeToolUsage: func() any { return &CheckToolUsageUpdateInput{} },
}
func UnmarshalCheckCreateInput(checkType CheckType, data []byte) (any, error) {
output := CheckCreateConstructors[checkType]()
if err := json.Unmarshal(data, &output); err != nil {
return nil, err
}
return output, nil
}
func UnmarshalCheckUpdateInput(checkType CheckType, data []byte) (any, error) {
output := CheckUpdateConstructors[checkType]()
if err := json.Unmarshal(data, &output); err != nil {
return nil, err
}
return output, nil
}
type CheckConnection struct {
Nodes []Check
PageInfo PageInfo
TotalCount int
}
type CheckCreateInputProvider interface {
GetCheckCreateInput() *CheckCreateInput
}
type CheckCreateInput struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
EnableOn *iso8601.Time `json:"enableOn,omitempty"`
Category ID `json:"categoryId"`
Level ID `json:"levelId"`
Owner *ID `json:"ownerId,omitempty"`
Filter *ID `json:"filterId,omitempty"`
Notes string `json:"notes"`
}
func (c *CheckCreateInput) GetCheckCreateInput() *CheckCreateInput {
return c
}
type CheckUpdateInputProvider interface {
GetCheckUpdateInput() *CheckUpdateInput
}
type CheckUpdateInput struct {
Id ID `json:"id"`
Name string `json:"name,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
EnableOn *iso8601.Time `json:"enableOn,omitempty"`
Category ID `json:"categoryId,omitempty"`
Level ID `json:"levelId,omitempty"`
Owner *ID `json:"ownerId,omitempty"`
Filter *ID `json:"filterId,omitempty"`
Notes *string `json:"notes,omitempty"`
}
func (c *CheckUpdateInput) GetCheckUpdateInput() *CheckUpdateInput {
return c
}
type CheckDeleteInput struct {
Id ID `json:"id"`
}
// Encompass CheckCreatePayload and CheckUpdatePayload into 1 struct
type CheckResponsePayload struct {
Check Check
Errors []OpsLevelErrors
}
//#region Create
func (client *Client) CreateCheck(input any) (*Check, error) {
switch v := input.(type) {
case *CheckAlertSourceUsageCreateInput:
return client.CreateCheckAlertSourceUsage(*v)
case *CheckCustomEventCreateInput:
return client.CreateCheckCustomEvent(*v)
case *CheckGitBranchProtectionCreateInput:
return client.CreateCheckGitBranchProtection(*v)
case *CheckHasDocumentationCreateInput:
return client.CreateCheckHasDocumentation(*v)
case *CheckServiceOwnershipCreateInput:
return client.CreateCheckServiceOwnership(*v)
case *CheckHasRecentDeployCreateInput:
return client.CreateCheckHasRecentDeploy(*v)
case *CheckRepositoryIntegratedCreateInput:
return client.CreateCheckRepositoryIntegrated(*v)
case *CheckServiceConfigurationCreateInput:
return client.CreateCheckServiceConfiguration(*v)
case *CheckManualCreateInput:
return client.CreateCheckManual(*v)
case *CheckRepositoryFileCreateInput:
return client.CreateCheckRepositoryFile(*v)
case *CheckRepositoryGrepCreateInput:
return client.CreateCheckRepositoryGrep(*v)
case *CheckRepositorySearchCreateInput:
return client.CreateCheckRepositorySearch(*v)
case *CheckServiceDependencyCreateInput:
return client.CreateCheckServiceDependency(*v)
case *CheckServicePropertyCreateInput:
return client.CreateCheckServiceProperty(*v)
case *CheckTagDefinedCreateInput:
return client.CreateCheckTagDefined(*v)
case *CheckToolUsageCreateInput:
return client.CreateCheckToolUsage(*v)
}
return nil, fmt.Errorf("unknown input type %T", input)
}
// See files check_*.go
//#endregion
//#region Retrieve
func (client *Client) GetCheck(id ID) (*Check, error) {
var q struct {
Account struct {
Check Check `graphql:"check(id: $id)"`
}
}
v := PayloadVariables{
"id": id,
}
err := client.Query(&q, v, WithName("CheckGet"))
if q.Account.Check.Id == "" {
err = fmt.Errorf("check with ID '%s' not found", id)
}
return &q.Account.Check, HandleErrors(err, nil)
}
func (client *Client) ListChecks(variables *PayloadVariables) (CheckConnection, error) {
var q struct {
Account struct {
Rubric struct {
Checks CheckConnection `graphql:"checks(after: $after, first: $first)"`
}
}
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
if err := client.Query(&q, *variables, WithName("CheckList")); err != nil {
return CheckConnection{}, err
}
for q.Account.Rubric.Checks.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.Rubric.Checks.PageInfo.End
resp, err := client.ListChecks(variables)
if err != nil {
return CheckConnection{}, err
}
q.Account.Rubric.Checks.Nodes = append(q.Account.Rubric.Checks.Nodes, resp.Nodes...)
q.Account.Rubric.Checks.PageInfo = resp.PageInfo
q.Account.Rubric.Checks.TotalCount += resp.TotalCount
}
return q.Account.Rubric.Checks, nil
}
//#endregion
//#region Update
func (client *Client) UpdateCheck(input any) (*Check, error) {
switch v := input.(type) {
case *CheckAlertSourceUsageUpdateInput:
return client.UpdateCheckAlertSourceUsage(*v)
case *CheckCustomEventUpdateInput:
return client.UpdateCheckCustomEvent(*v)
case *CheckGitBranchProtectionUpdateInput:
return client.UpdateCheckGitBranchProtection(*v)
case *CheckHasDocumentationUpdateInput:
return client.UpdateCheckHasDocumentation(*v)
case *CheckServiceOwnershipUpdateInput:
return client.UpdateCheckServiceOwnership(*v)
case *CheckHasRecentDeployUpdateInput:
return client.UpdateCheckHasRecentDeploy(*v)
case *CheckRepositoryIntegratedUpdateInput:
return client.UpdateCheckRepositoryIntegrated(*v)
case *CheckServiceConfigurationUpdateInput:
return client.UpdateCheckServiceConfiguration(*v)
case *CheckManualUpdateInput:
return client.UpdateCheckManual(*v)
case *CheckRepositoryFileUpdateInput:
return client.UpdateCheckRepositoryFile(*v)
case *CheckRepositoryGrepUpdateInput:
return client.UpdateCheckRepositoryGrep(*v)
case *CheckRepositorySearchUpdateInput:
return client.UpdateCheckRepositorySearch(*v)
case *CheckServiceDependencyUpdateInput:
return client.UpdateCheckServiceDependency(*v)
case *CheckServicePropertyUpdateInput:
return client.UpdateCheckServiceProperty(*v)
case *CheckTagDefinedUpdateInput:
return client.UpdateCheckTagDefined(*v)
case *CheckToolUsageUpdateInput:
return client.UpdateCheckToolUsage(*v)
}
return nil, fmt.Errorf("unknown input type %T", input)
}
//#endregion
//#region Delete
func (client *Client) DeleteCheck(id ID) error {
var m struct {
Payload struct {
Errors []OpsLevelErrors
} `graphql:"checkDelete(input: $input)"`
}
v := PayloadVariables{
"input": CheckDeleteInput{Id: id},
}
err := client.Mutate(&m, v, WithName("CheckDelete"))
return HandleErrors(err, m.Payload.Errors)
}
//#endregion