forked from OpsLevel/opslevel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration.go
197 lines (167 loc) · 5.58 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
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
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"`
NewRelicIntegrationFragment `graphql:"... on NewRelicIntegration"`
}
type AWSIntegrationFragment struct {
IAMRole string `graphql:"iamRole"`
ExternalID string `graphql:"externalId"`
OwnershipTagOverride bool `graphql:"awsTagsOverrideOwnership"`
OwnershipTagKeys []string `graphql:"ownershipTagKeys"`
}
type NewRelicIntegrationFragment struct {
BaseUrl string `graphql:"baseUrl"`
AccountKey string `graphql:"accountKey"`
}
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"`
}
type NewRelicIntegrationInput struct {
ApiKey *string `json:"apiKey,omitempty"`
BaseUrl *string `json:"baseUrl,omitempty"`
AccountKey *string `json:"accountKey,omitempty"`
}
func (s AWSIntegrationInput) GetGraphQLType() string { return "AwsIntegrationInput" }
func (s NewRelicIntegrationInput) GetGraphQLType() string { return "NewRelicIntegrationInput" }
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)
}
func (client *Client) CreateIntegrationNewRelic(input NewRelicIntegrationInput) (*Integration, error) {
var m struct {
Payload struct {
Integration *Integration
Errors []OpsLevelErrors
} `graphql:"newRelicIntegrationCreate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("NewRelicIntegrationCreate"))
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)
}
func (client *Client) UpdateIntegrationNewRelic(identifier string, input NewRelicIntegrationInput) (*Integration, error) {
var m struct {
Payload struct {
Integration *Integration
Errors []OpsLevelErrors
} `graphql:"newRelicIntegrationUpdate(input: $input resource: $resource)"`
}
v := PayloadVariables{
"resource": *NewIdentifier(identifier),
"input": input,
}
err := client.Mutate(&m, v, WithName("NewRelicIntegrationUpdate"))
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