-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathintegration.go
More file actions
331 lines (295 loc) · 9.58 KB
/
integration.go
File metadata and controls
331 lines (295 loc) · 9.58 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package cmd
import (
"encoding/json"
"fmt"
"slices"
"github.com/rs/zerolog/log"
"github.com/mitchellh/mapstructure"
"github.com/opslevel/cli/common"
"github.com/opslevel/opslevel-go/v2025"
"github.com/spf13/cobra"
)
type IntegrationType string
const (
IntegrationTypeAWS IntegrationType = "aws"
IntegrationTypeAzure IntegrationType = "azure"
IntegrationTypeGCP IntegrationType = "googleCloud"
)
var AllIntegrationType = []IntegrationType{IntegrationTypeAWS, IntegrationTypeAzure, IntegrationTypeGCP}
var IntegrationConfigCurrentVersion = "1"
type EventIntegrationInputDTO struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Type opslevel.EventIntegrationEnum `json:"type" yaml:"type" example:"apiDoc"`
}
type IntegrationInputType struct {
Version string `yaml:"version"`
Kind IntegrationType
Spec map[string]interface{}
}
type IntegrationInput interface {
opslevel.AWSIntegrationInput |
opslevel.AzureResourcesIntegrationInput |
EventIntegrationInputDTO |
opslevel.GoogleCloudIntegrationInput
}
func validateIntegrationInput() (*IntegrationInputType, error) {
input, err := readResourceInput[IntegrationInputType]()
if err != nil {
return nil, err
}
if input.Version != CheckConfigCurrentVersion {
return nil, fmt.Errorf("supported config version is '%s' but found '%s'",
IntegrationConfigCurrentVersion, input.Version)
}
if slices.Contains(opslevel.AllEventIntegrationEnum, string(input.Kind)) {
return input, nil
}
switch input.Kind {
case IntegrationTypeAWS, IntegrationTypeAzure, IntegrationTypeGCP:
return input, nil
default:
return nil, fmt.Errorf("unsupported integration kind: '%s' (must be one of: %+v)",
input.Kind, AllIntegrationType)
}
}
func readIntegrationInput[T IntegrationInput](input *IntegrationInputType) (T, error) {
var output T
if err := mapstructure.Decode(input.Spec, &output); err != nil {
return output, err
}
return output, nil
}
var createIntegrationCmd = &cobra.Command{
Use: "integration",
Aliases: []string{"integrations", "int", "ints"},
Short: "Create an integration",
Long: `Create an integration`,
Example: `cat << EOF | opslevel create integration -f -
version: 1
kind: aws
spec:
name: "Prod"
iamRole: "arn:aws:iam::XXXXX:role/opslevel-integration"
externalId: "XXXXXX"
awsTagsOverrideOwnership: true
ownershipTagKeys: ["owner","service","app"]
regionOverride: ["us-east-1","eu-west-1"]
EOF
cat << EOF | opslevel create integration -f -
version: 1
kind: azure
spec:
name: "Azure New"
tenantId: "12345678-1234-1234-1234-123456789abc"
subscriptionId: "12345678-1234-1234-1234-123456789def"
clientId: "XXX_CLIENT_ID_XXX"
clientSecret: "XXX_CLIENT_SECRET_XXX"
EOF
cat << EOF | opslevel create integration -f -
version: 1
kind: googleCloud
spec:
name: "GCP New"
ownershipTagKeys:
- owner
- team
privateKey: "XXX_PRIVATE_KEY_XXX"
clientEmail: "service-account-123@appspot.gserviceaccount.com"
tagsOverrideOwnership: false
EOF
cat << EOF | opslevel create integration -f -
version: 1
kind: githubActions
spec:
name: "GHA New"
EOF
`,
Run: func(cmd *cobra.Command, args []string) {
input, validateErr := validateIntegrationInput()
cobra.CheckErr(validateErr)
var result *opslevel.Integration
if slices.Contains(opslevel.AllEventIntegrationEnum, string(input.Kind)) {
input.Spec["type"] = input.Kind
eventIntegrationInput, err := readIntegrationInput[EventIntegrationInputDTO](input)
cobra.CheckErr(err)
result, err = getClientGQL().CreateEventIntegration(opslevel.EventIntegrationInput{
Name: opslevel.NewNullableFrom(eventIntegrationInput.Name),
Type: eventIntegrationInput.Type,
})
cobra.CheckErr(err)
} else {
switch input.Kind {
case IntegrationTypeAWS:
awsInput, err := readIntegrationInput[opslevel.AWSIntegrationInput](input)
cobra.CheckErr(err)
result, err = getClientGQL().CreateIntegrationAWS(awsInput)
cobra.CheckErr(err)
case IntegrationTypeAzure:
azureInput, err := readIntegrationInput[opslevel.AzureResourcesIntegrationInput](input)
cobra.CheckErr(err)
result, err = getClientGQL().CreateIntegrationAzureResources(azureInput)
cobra.CheckErr(err)
case IntegrationTypeGCP:
gcpInput, err := readIntegrationInput[opslevel.GoogleCloudIntegrationInput](input)
cobra.CheckErr(err)
result, err = getClientGQL().CreateIntegrationGCP(gcpInput)
cobra.CheckErr(err)
default:
cobra.CheckErr(fmt.Errorf("cannot use unexpected input kind: '%s'", input.Kind))
}
}
fmt.Printf("Created %s integration '%s' with id '%s'\n", input.Kind, result.Name, result.Id)
},
}
var getIntegrationCmd = &cobra.Command{
Use: "integration ID",
Aliases: []string{"int"},
Short: "Get details about a integration",
Long: `Get details about a integration`,
Args: cobra.ExactArgs(1),
ArgAliases: []string{"ID"},
Run: func(cmd *cobra.Command, args []string) {
key := args[0]
integration, err := getClientGQL().GetIntegration(opslevel.ID(key))
cobra.CheckErr(err)
common.PrettyPrint(integration)
},
}
var listIntegrationCmd = &cobra.Command{
Use: "integration",
Aliases: []string{"integrations", "int", "ints"},
Short: "Lists integrations",
Long: `Lists integrations`,
Run: func(cmd *cobra.Command, args []string) {
resp, err := getClientGQL().ListIntegrations(nil)
cobra.CheckErr(err)
list := resp.Nodes
if isJsonOutput() {
common.JsonPrint(json.MarshalIndent(list, "", " "))
} else {
w := common.NewTabWriter("NAME", "TYPE", "ALIAS", "ID")
for _, item := range list {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t\n", item.Name, item.Type, item.Alias(), item.Id)
}
w.Flush()
}
},
}
var updateIntegrationCmd = &cobra.Command{
Use: "integration ID",
Aliases: []string{"int"},
Short: "Update an integration",
Long: `Update an integration`,
Example: `cat << EOF | opslevel update integration Z2lkOi8vb123456789 -f -
version: 1
kind: aws
spec:
awsTagsOverrideOwnership: true
ownershipTagKeys: ["owner","service","app"]
regionOverride: ["us-east-1"]
EOF
cat << EOF | opslevel update integration Z2lkOi8vb123456789 -f -
version: 1
kind: azure
spec:
name: "Azure Dev"
clientId: "XXX_CLIENT_ID_XXX"
clientSecret: "XXX_CLIENT_SECRET_XXX"
EOF
cat << EOF | opslevel update integration Z2lkOi8vb123456789 -f -
version: 1
kind: googleCloud
spec:
name: "GCP Dev"
ownershipTagKeys:
- opslevel_team
- team
privateKey: "XXX_NEW_PRIVATE_KEY_XXX"
tagsOverrideOwnership: true
EOF
cat << EOF | opslevel update integration Z2lkOi8vb123456789 -f -
version: 1
kind: githubActions
spec:
name: "GHA Updated"
EOF
`,
Args: cobra.ExactArgs(1),
ArgAliases: []string{"ID"},
Run: func(cmd *cobra.Command, args []string) {
input, validateErr := validateIntegrationInput()
cobra.CheckErr(validateErr)
var result *opslevel.Integration
if slices.Contains(opslevel.AllEventIntegrationEnum, string(input.Kind)) {
input.Spec["type"] = input.Kind
eventIntegrationInput, err := readIntegrationInput[EventIntegrationInputDTO](input)
cobra.CheckErr(err)
if eventIntegrationInput.Name != "" {
apiInput := opslevel.EventIntegrationUpdateInput{
Id: opslevel.ID(args[0]),
Name: eventIntegrationInput.Name,
}
result, err = getClientGQL().UpdateEventIntegration(apiInput)
cobra.CheckErr(err)
} else {
log.Warn().Msgf("event integration 'name' cannot be updated as no name field was provided")
return
}
} else {
switch input.Kind {
case IntegrationTypeAWS:
awsInput, err := readIntegrationInput[opslevel.AWSIntegrationInput](input)
cobra.CheckErr(err)
result, err = getClientGQL().UpdateIntegrationAWS(args[0], awsInput)
cobra.CheckErr(err)
case IntegrationTypeAzure:
azureInput, err := readIntegrationInput[opslevel.AzureResourcesIntegrationInput](input)
cobra.CheckErr(err)
result, err = getClientGQL().UpdateIntegrationAzureResources(args[0], azureInput)
cobra.CheckErr(err)
case IntegrationTypeGCP:
gcpInput, err := readIntegrationInput[opslevel.GoogleCloudIntegrationInput](input)
cobra.CheckErr(err)
result, err = getClientGQL().UpdateIntegrationGCP(args[0], gcpInput)
cobra.CheckErr(err)
default:
cobra.CheckErr(fmt.Errorf("cannot use unexpected input kind: '%s'", input.Kind))
}
}
fmt.Printf("Updated %s integration '%s' with id '%s'\n", input.Kind, result.Name, result.Id)
},
}
var deleteIntegrationCmd = &cobra.Command{
Use: "integration {ID|ALIAS}",
Short: "Delete an integration",
Example: `opslevel delete integration Z2lkOi8vb123456789`,
Args: cobra.ExactArgs(1),
ArgAliases: []string{"ID", "ALIAS"},
Run: func(cmd *cobra.Command, args []string) {
key := args[0]
err := getClientGQL().DeleteIntegration(key)
cobra.CheckErr(err)
fmt.Printf("integration '%s' deleted\n", key)
},
}
var reactivateIntegrationCmd = &cobra.Command{
Use: "reactivate ID",
Short: "Reactivate an integration",
Long: `Reactivate an integration that was invalidated or deactivated`,
Example: `opslevel update integration reactivate Z2lkOi8vb123456789`,
Args: cobra.ExactArgs(1),
ArgAliases: []string{"ID"},
Run: func(cmd *cobra.Command, args []string) {
integration, err := getClientGQL().IntegrationReactivate(args[0])
cobra.CheckErr(err)
fmt.Printf("reactivated integration '%s' with id '%s'", integration.Name, integration.Id)
},
}
func init() {
createCmd.AddCommand(createIntegrationCmd)
getCmd.AddCommand(getIntegrationCmd)
listCmd.AddCommand(listIntegrationCmd)
updateCmd.AddCommand(updateIntegrationCmd)
deleteCmd.AddCommand(deleteIntegrationCmd)
updateIntegrationCmd.AddCommand(reactivateIntegrationCmd)
}