-
Notifications
You must be signed in to change notification settings - Fork 13
/
system.go
235 lines (210 loc) · 6.9 KB
/
system.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
package opslevel
import (
"errors"
"fmt"
"slices"
)
type SystemId Identifier
type System struct {
SystemId
ManagedAliases []string `graphql:"managedAliases"`
Name string `graphql:"name"`
Description string `graphql:"description"`
HTMLUrl string `graphql:"htmlUrl"`
Owner EntityOwner `graphql:"owner"`
Parent Domain `graphql:"parent"`
Note string `graphql:"note"`
}
type SystemConnection struct {
Nodes []System `json:"nodes"`
PageInfo PageInfo `json:"pageInfo"`
TotalCount int `json:"totalCount" graphql:"-"`
}
func (systemId *SystemId) GetTags(client *Client, variables *PayloadVariables) (*TagConnection, error) {
var q struct {
Account struct {
System struct {
Tags TagConnection `graphql:"tags(after: $after, first: $first)"`
} `graphql:"system(input: $system)"`
}
}
if systemId.Id == "" {
return nil, fmt.Errorf("unable to get Tags, invalid system id: '%s'", systemId.Id)
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
(*variables)["system"] = *NewIdentifier(string(systemId.Id))
if err := client.Query(&q, *variables, WithName("SystemTagsList")); err != nil {
return nil, err
}
for q.Account.System.Tags.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.System.Tags.PageInfo.End
resp, err := systemId.GetTags(client, variables)
if err != nil {
return nil, err
}
// Add unique tags only
for _, resp := range resp.Nodes {
if !slices.Contains[[]Tag, Tag](q.Account.System.Tags.Nodes, resp) {
q.Account.System.Tags.Nodes = append(q.Account.System.Tags.Nodes, resp)
}
}
q.Account.System.Tags.PageInfo = resp.PageInfo
q.Account.System.Tags.TotalCount += resp.TotalCount
}
return &q.Account.System.Tags, nil
}
func (systemId *SystemId) GetAliases() []string {
return systemId.Aliases
}
func (systemId *SystemId) ResourceId() ID {
return systemId.Id
}
func (systemId *SystemId) ResourceType() TaggableResource {
return TaggableResourceSystem
}
func (systemId *SystemId) AliasableType() AliasOwnerTypeEnum {
return AliasOwnerTypeEnumSystem
}
// Returns unique identifiers created by OpsLevel, values in Aliases but not ManagedAliases
func (system *System) UniqueIdentifiers() []string {
uniqueIdentifiers := []string{}
for _, alias := range system.Aliases {
if !slices.Contains(system.ManagedAliases, alias) {
uniqueIdentifiers = append(uniqueIdentifiers, alias)
}
}
return uniqueIdentifiers
}
func (system *SystemId) ReconcileAliases(client *Client, aliasesWanted []string) error {
aliasesToCreate, aliasesToDelete := extractAliases(system.Aliases, aliasesWanted)
// reconcile wanted aliases with actual aliases
deleteErr := client.DeleteAliases(AliasOwnerTypeEnumSystem, aliasesToDelete)
_, createErr := client.CreateAliases(system.Id, aliasesToCreate)
// update system to reflect API updates
updatedSystem, getErr := client.GetSystem(string(system.Id))
if updatedSystem != nil {
system.Aliases = updatedSystem.Aliases
}
return errors.Join(deleteErr, createErr, getErr)
}
func (systemId *SystemId) ChildServices(client *Client, variables *PayloadVariables) (*ServiceConnection, error) {
var q struct {
Account struct {
System struct {
ChildServices ServiceConnection `graphql:"childServices(after: $after, first: $first)"`
} `graphql:"system(input: $system)"`
}
}
if systemId.Id == "" {
return nil, fmt.Errorf("unable to get Services, invalid system id: '%s'", systemId.Id)
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
(*variables)["system"] = *NewIdentifier(string(systemId.Id))
if err := client.Query(&q, *variables, WithName("SystemChildServicesList")); err != nil {
return nil, err
}
for q.Account.System.ChildServices.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.System.ChildServices.PageInfo.End
resp, err := systemId.ChildServices(client, variables)
if err != nil {
return nil, err
}
q.Account.System.ChildServices.Nodes = append(q.Account.System.ChildServices.Nodes, resp.Nodes...)
q.Account.System.ChildServices.PageInfo = resp.PageInfo
q.Account.System.ChildServices.TotalCount += resp.TotalCount
}
return &q.Account.System.ChildServices, nil
}
func (systemId *SystemId) AssignService(client *Client, services ...string) error {
var m struct {
Payload struct {
System System
Errors []OpsLevelErrors
} `graphql:"systemChildAssign(system:$system, childServices:$childServices)"`
}
v := PayloadVariables{
"system": *NewIdentifier(string(systemId.Id)),
"childServices": NewIdentifierArray(services),
}
err := client.Mutate(&m, v, WithName("SystemAssignService"))
return HandleErrors(err, m.Payload.Errors)
}
func (client *Client) CreateSystem(input SystemInput) (*System, error) {
var m struct {
Payload struct {
System System
Errors []OpsLevelErrors
} `graphql:"systemCreate(input:$input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("SystemCreate"))
return &m.Payload.System, HandleErrors(err, m.Payload.Errors)
}
func (client *Client) GetSystem(identifier string) (*System, error) {
var q struct {
Account struct {
System System `graphql:"system(input: $input)"`
}
}
v := PayloadVariables{
"input": *NewIdentifier(identifier),
}
err := client.Query(&q, v, WithName("SystemGet"))
return &q.Account.System, HandleErrors(err, nil)
}
func (client *Client) ListSystems(variables *PayloadVariables) (*SystemConnection, error) {
var q struct {
Account struct {
Systems SystemConnection `graphql:"systems(after: $after, first: $first)"`
}
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
if err := client.Query(&q, *variables, WithName("SystemsList")); err != nil {
return nil, err
}
for q.Account.Systems.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.Systems.PageInfo.End
resp, err := client.ListSystems(variables)
if err != nil {
return nil, err
}
q.Account.Systems.Nodes = append(q.Account.Systems.Nodes, resp.Nodes...)
q.Account.Systems.PageInfo = resp.PageInfo
}
q.Account.Systems.TotalCount = len(q.Account.Systems.Nodes)
return &q.Account.Systems, nil
}
func (client *Client) UpdateSystem(identifier string, input SystemInput) (*System, error) {
var s struct {
Payload struct {
System System
Errors []OpsLevelErrors
} `graphql:"systemUpdate(system:$system,input:$input)"`
}
v := PayloadVariables{
"system": *NewIdentifier(identifier),
"input": input,
}
err := client.Mutate(&s, v, WithName("SystemUpdate"))
return &s.Payload.System, HandleErrors(err, s.Payload.Errors)
}
func (client *Client) DeleteSystem(identifier string) error {
var s struct {
Payload struct {
Errors []OpsLevelErrors `graphql:"errors"`
} `graphql:"systemDelete(resource: $input)"`
}
v := PayloadVariables{
"input": *NewIdentifier(identifier),
}
err := client.Mutate(&s, v, WithName("SystemDelete"))
return HandleErrors(err, s.Payload.Errors)
}