-
Notifications
You must be signed in to change notification settings - Fork 13
/
domain.go
208 lines (189 loc) · 5.92 KB
/
domain.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
package opslevel
import (
"fmt"
"slices"
)
type DomainId Identifier
type Domain struct {
DomainId
Name string `graphql:"name"`
Description string `graphql:"description"`
HTMLUrl string `graphql:"htmlUrl"`
Owner EntityOwner `graphql:"owner"`
Note string `graphql:"note"`
}
type DomainConnection struct {
Nodes []Domain `json:"nodes"`
PageInfo PageInfo `json:"pageInfo"`
TotalCount int `json:"totalCount" graphql:"-"`
}
type DomainInput struct {
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
Owner *ID `json:"ownerId,omitempty"`
Note *string `json:"note,omitempty"`
}
func (d *DomainId) GetTags(client *Client, variables *PayloadVariables) (*TagConnection, error) {
return d.Tags(client, variables)
}
func (d *DomainId) ResourceId() ID {
return d.Id
}
func (d *DomainId) ResourceType() TaggableResource {
return TaggableResourceDomain
}
func (d *DomainId) ChildSystems(client *Client, variables *PayloadVariables) (*SystemConnection, error) {
var q struct {
Account struct {
Domain struct {
ChildSystems SystemConnection `graphql:"childSystems(after: $after, first: $first)"`
} `graphql:"domain(input: $domain)"`
}
}
if d.Id == "" {
return nil, fmt.Errorf("Unable to get Systems, invalid domain id: '%s'", d.Id)
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
(*variables)["domain"] = *NewIdentifier(string(d.Id))
if err := client.Query(&q, *variables, WithName("DomainChildSystemsList")); err != nil {
return nil, err
}
for q.Account.Domain.ChildSystems.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.Domain.ChildSystems.PageInfo.End
resp, err := d.ChildSystems(client, variables)
if err != nil {
return nil, err
}
q.Account.Domain.ChildSystems.Nodes = append(q.Account.Domain.ChildSystems.Nodes, resp.Nodes...)
q.Account.Domain.ChildSystems.PageInfo = resp.PageInfo
}
q.Account.Domain.ChildSystems.TotalCount = len(q.Account.Domain.ChildSystems.Nodes)
return &q.Account.Domain.ChildSystems, nil
}
// Deprecated: Please use GetTags instead
func (s *DomainId) Tags(client *Client, variables *PayloadVariables) (*TagConnection, error) {
var q struct {
Account struct {
Domain struct {
Tags TagConnection `graphql:"tags(after: $after, first: $first)"`
} `graphql:"domain(input: $domain)"`
}
}
if s.Id == "" {
return nil, fmt.Errorf("Unable to get Tags, invalid domain id: '%s'", s.Id)
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
(*variables)["domain"] = *NewIdentifier(string(s.Id))
if err := client.Query(&q, *variables, WithName("DomainTagsList")); err != nil {
return nil, err
}
for q.Account.Domain.Tags.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.Domain.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.Domain.Tags.Nodes, resp) {
q.Account.Domain.Tags.Nodes = append(q.Account.Domain.Tags.Nodes, resp)
}
}
q.Account.Domain.Tags.PageInfo = resp.PageInfo
q.Account.Domain.Tags.TotalCount += resp.TotalCount
}
return &q.Account.Domain.Tags, nil
}
func (s *DomainId) AssignSystem(client *Client, systems ...string) error {
var m struct {
Payload struct {
Domain Domain
Errors []OpsLevelErrors
} `graphql:"domainChildAssign(domain:$domain, childSystems:$childSystems)"`
}
v := PayloadVariables{
"domain": *NewIdentifier(string(s.Id)),
"childSystems": NewIdentifierArray(systems),
}
err := client.Mutate(&m, v, WithName("DomainAssignSystem"))
return HandleErrors(err, m.Payload.Errors)
}
func (c *Client) CreateDomain(input DomainInput) (*Domain, error) {
var m struct {
Payload struct {
Domain Domain
Errors []OpsLevelErrors
} `graphql:"domainCreate(input:$input)"`
}
v := PayloadVariables{
"input": input,
}
err := c.Mutate(&m, v, WithName("DomainCreate"))
return &m.Payload.Domain, HandleErrors(err, m.Payload.Errors)
}
func (c *Client) GetDomain(identifier string) (*Domain, error) {
var q struct {
Account struct {
Domain Domain `graphql:"domain(input: $input)"`
}
}
v := PayloadVariables{
"input": *NewIdentifier(identifier),
}
err := c.Query(&q, v, WithName("DomainGet"))
return &q.Account.Domain, HandleErrors(err, nil)
}
func (c *Client) ListDomains(variables *PayloadVariables) (*DomainConnection, error) {
var q struct {
Account struct {
Domains DomainConnection `graphql:"domains(after: $after, first: $first)"`
}
}
if variables == nil {
variables = c.InitialPageVariablesPointer()
}
if err := c.Query(&q, *variables, WithName("DomainsList")); err != nil {
return &DomainConnection{}, err
}
for q.Account.Domains.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.Domains.PageInfo.End
resp, err := c.ListDomains(variables)
if err != nil {
return &DomainConnection{}, err
}
q.Account.Domains.Nodes = append(q.Account.Domains.Nodes, resp.Nodes...)
q.Account.Domains.PageInfo = resp.PageInfo
}
q.Account.Domains.TotalCount = len(q.Account.Domains.Nodes)
return &q.Account.Domains, nil
}
func (c *Client) UpdateDomain(identifier string, input DomainInput) (*Domain, error) {
var m struct {
Payload struct {
Domain Domain
Errors []OpsLevelErrors
} `graphql:"domainUpdate(domain:$domain,input:$input)"`
}
v := PayloadVariables{
"domain": *NewIdentifier(identifier),
"input": input,
}
err := c.Mutate(&m, v, WithName("DomainUpdate"))
return &m.Payload.Domain, HandleErrors(err, m.Payload.Errors)
}
func (c *Client) DeleteDomain(identifier string) error {
var d struct {
Payload struct {
Errors []OpsLevelErrors `graphql:"errors"`
} `graphql:"domainDelete(resource: $input)"`
}
v := PayloadVariables{
"input": *NewIdentifier(identifier),
}
err := c.Mutate(&d, v, WithName("DomainDelete"))
return HandleErrors(err, d.Payload.Errors)
}