-
Notifications
You must be signed in to change notification settings - Fork 12
/
integration.go
165 lines (148 loc) · 5 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
package opslevel
import (
"fmt"
"github.com/gosimple/slug"
)
type IntegrationId struct {
Id ID `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
}
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"`
}
func (awsIntegrationInput AWSIntegrationInput) GetGraphQLType() string { return "AwsIntegrationInput" }
func (newRelicIntegrationInput NewRelicIntegrationInput) GetGraphQLType() string {
return "NewRelicIntegrationInput"
}
func (integrationId *IntegrationId) Alias() string {
return fmt.Sprintf("%s-%s", slug.Make(integrationId.Type), slug.Make(integrationId.Name))
}
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)
}
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 nil, err
}
for q.Account.Integrations.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.Integrations.PageInfo.End
resp, err := client.ListIntegrations(variables)
if err != nil {
return nil, 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
}
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)
}
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)
}