-
Notifications
You must be signed in to change notification settings - Fork 13
/
integration.go
153 lines (129 loc) · 4.16 KB
/
integration.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
package opslevel
import (
"fmt"
"github.com/relvacode/iso8601"
"github.com/gosimple/slug"
)
type IntegrationId struct {
Id ID `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
}
type Integration struct {
IntegrationId
CreatedAt iso8601.Time `graphql:"createdAt"`
InstalledAt iso8601.Time `graphql:"installedAt"`
AWSIntegrationFragment `graphql:"... on AwsIntegration"`
}
type AWSIntegrationFragment struct {
IAMRole string `graphql:"iamRole"`
ExternalID string `graphql:"externalId"`
OwnershipTagOverride bool `graphql:"awsTagsOverrideOwnership"`
OwnershipTagKeys []string `graphql:"ownershipTagKeys"`
}
type IntegrationConnection struct {
Nodes []Integration
PageInfo PageInfo
TotalCount int
}
type AWSIntegrationInput struct {
Name *string `json:"name,omitempty"`
IAMRole *string `json:"iamRole,omitempty"`
ExternalID *string `json:"externalId,omitempty"`
OwnershipTagOverride *bool `json:"awsTagsOverrideOwnership,omitempty"`
OwnershipTagKeys []string `json:"ownershipTagKeys"`
}
func (s AWSIntegrationInput) GetGraphQLType() string { return "AwsIntegrationInput" }
func (self *IntegrationId) Alias() string {
return fmt.Sprintf("%s-%s", slug.Make(self.Type), slug.Make(self.Name))
}
//#region Create
func (client *Client) CreateIntegrationAWS(input AWSIntegrationInput) (*Integration, error) {
var m struct {
Payload struct {
Integration *Integration
Errors []OpsLevelErrors
} `graphql:"awsIntegrationCreate(input: $input)"`
}
// This is a default in the UI, so we must maintain it
if len(input.OwnershipTagKeys) == 0 {
input.OwnershipTagKeys = append(input.OwnershipTagKeys, "owner")
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("AWSIntegrationCreate"))
return m.Payload.Integration, HandleErrors(err, m.Payload.Errors)
}
//#endregion
//#region Retrieve
func (client *Client) GetIntegration(id ID) (*Integration, error) {
var q struct {
Account struct {
Integration Integration `graphql:"integration(id: $id)"`
}
}
v := PayloadVariables{
"id": id,
}
err := client.Query(&q, v, WithName("IntegrationGet"))
if q.Account.Integration.Id == "" {
err = fmt.Errorf("Integration with ID '%s' not found!", id)
}
return &q.Account.Integration, HandleErrors(err, nil)
}
func (client *Client) ListIntegrations(variables *PayloadVariables) (IntegrationConnection, error) {
var q struct {
Account struct {
Integrations IntegrationConnection `graphql:"integrations(after: $after, first: $first)"`
}
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
if err := client.Query(&q, *variables, WithName("IntegrationList")); err != nil {
return IntegrationConnection{}, err
}
for q.Account.Integrations.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.Integrations.PageInfo.End
resp, err := client.ListIntegrations(variables)
if err != nil {
return IntegrationConnection{}, err
}
q.Account.Integrations.Nodes = append(q.Account.Integrations.Nodes, resp.Nodes...)
q.Account.Integrations.PageInfo = resp.PageInfo
q.Account.Integrations.TotalCount += resp.TotalCount
}
return q.Account.Integrations, nil
}
//#endregion
//#region Update
func (client *Client) UpdateIntegrationAWS(identifier string, input AWSIntegrationInput) (*Integration, error) {
var m struct {
Payload struct {
Integration *Integration
Errors []OpsLevelErrors
} `graphql:"awsIntegrationUpdate(integration: $integration input: $input)"`
}
v := PayloadVariables{
"integration": *NewIdentifier(identifier),
"input": input,
}
err := client.Mutate(&m, v, WithName("AWSIntegrationUpdate"))
return m.Payload.Integration, HandleErrors(err, m.Payload.Errors)
}
//#endregion
//#region Delete
func (client *Client) DeleteIntegration(identifier string) error {
var m struct {
Payload struct {
Errors []OpsLevelErrors `graphql:"errors"`
} `graphql:"integrationDelete(resource: $input)"`
}
v := PayloadVariables{
"input": *NewIdentifier(identifier),
}
err := client.Mutate(&m, v, WithName("IntegrationDelete"))
return HandleErrors(err, m.Payload.Errors)
}
//#endregion