forked from OpsLevel/opslevel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.go
210 lines (191 loc) · 6.05 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
package opslevel
import (
"fmt"
"slices"
)
type SystemId Identifier
type System struct {
SystemId
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:"-"`
}
type SystemInput struct {
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
Owner *ID `json:"ownerId,omitempty"`
Parent *IdentifierInput `json:"parent,omitempty"`
Note *string `json:"note,omitempty"`
}
func (s *SystemId) GetTags(client *Client, variables *PayloadVariables) (*TagConnection, error) {
return s.Tags(client, variables)
}
func (s *SystemId) ResourceId() ID {
return s.Id
}
func (s *SystemId) ResourceType() TaggableResource {
return TaggableResourceSystem
}
func (s *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 s.Id == "" {
return nil, fmt.Errorf("Unable to get Services, invalid system id: '%s'", s.Id)
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
(*variables)["system"] = *NewIdentifier(string(s.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 := s.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
}
// Deprecated: Please use GetTags instead
func (s *SystemId) Tags(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 s.Id == "" {
return nil, fmt.Errorf("Unable to get Tags, invalid system id: '%s'", s.Id)
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
(*variables)["system"] = *NewIdentifier(string(s.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 := s.Tags(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 (s *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(s.Id)),
"childServices": NewIdentifierArray(services),
}
err := client.Mutate(&m, v, WithName("SystemAssignService"))
return HandleErrors(err, m.Payload.Errors)
}
func (c *Client) CreateSystem(input SystemInput) (*System, error) {
var m struct {
Payload struct {
System System
Errors []OpsLevelErrors
} `graphql:"systemCreate(input:$input)"`
}
v := PayloadVariables{
"input": input,
}
err := c.Mutate(&m, v, WithName("SystemCreate"))
return &m.Payload.System, HandleErrors(err, m.Payload.Errors)
}
func (c *Client) GetSystem(identifier string) (*System, error) {
var q struct {
Account struct {
System System `graphql:"system(input: $input)"`
}
}
v := PayloadVariables{
"input": *NewIdentifier(identifier),
}
err := c.Query(&q, v, WithName("SystemGet"))
return &q.Account.System, HandleErrors(err, nil)
}
func (c *Client) ListSystems(variables *PayloadVariables) (*SystemConnection, error) {
var q struct {
Account struct {
Systems SystemConnection `graphql:"systems(after: $after, first: $first)"`
}
}
if variables == nil {
variables = c.InitialPageVariablesPointer()
}
if err := c.Query(&q, *variables, WithName("SystemsList")); err != nil {
return &SystemConnection{}, err
}
for q.Account.Systems.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.Systems.PageInfo.End
resp, err := c.ListSystems(variables)
if err != nil {
return &SystemConnection{}, 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 (c *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 := c.Mutate(&s, v, WithName("SystemUpdate"))
return &s.Payload.System, HandleErrors(err, s.Payload.Errors)
}
func (c *Client) DeleteSystem(identifier string) error {
var s struct {
Payload struct {
Errors []OpsLevelErrors `graphql:"errors"`
} `graphql:"systemDelete(resource: $input)"`
}
v := PayloadVariables{
"input": *NewIdentifier(identifier),
}
err := c.Mutate(&s, v, WithName("SystemDelete"))
return HandleErrors(err, s.Payload.Errors)
}