-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathproperty.go
More file actions
282 lines (261 loc) · 10.4 KB
/
property.go
File metadata and controls
282 lines (261 loc) · 10.4 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package opslevel
import "fmt"
// PropertyDefinition represents the definition of a property.
type PropertyDefinition struct {
Aliases []string `graphql:"aliases" json:"aliases"`
AllowedInConfigFiles bool `graphql:"allowedInConfigFiles"` // Whether or not the property is allowed to be set in opslevel.yml config files.
Id ID `graphql:"id" json:"id"`
Name string `graphql:"name" json:"name"`
Description string `graphql:"description" json:"description"`
DisplaySubtype PropertyDefinitionDisplayTypeEnum `graphql:"displaySubtype" json:"displaySubtype"`
DisplayType PropertyDefinitionDisplayTypeEnum `graphql:"displayType" json:"displayType"`
PropertyDisplayStatus PropertyDisplayStatusEnum `graphql:"propertyDisplayStatus" json:"propertyDisplayStatus"`
LockedStatus PropertyLockedStatusEnum `graphql:"lockedStatus" json:"lockedStatus"`
Schema JSONSchema `json:"schema" scalar:"true"`
}
type PropertyDefinitionId struct {
Id ID `json:"id"`
Aliases []string `json:"aliases,omitempty"`
}
// Property represents a custom property value assigned to an entity.
type Property struct {
Definition PropertyDefinitionId `graphql:"definition"`
Locked bool `graphql:"locked"`
Owner PropertyOwner `graphql:"owner"`
ValidationErrors []Error `graphql:"validationErrors"`
Value *JsonString `graphql:"value"`
}
type PropertiesConnection struct {
Nodes []Property
PageInfo PageInfo
TotalCount int `graphql:"-"`
}
func (client *Client) CreatePropertyDefinition(input PropertyDefinitionInput) (*PropertyDefinition, error) {
var m struct {
Payload PropertyDefinitionPayload `graphql:"propertyDefinitionCreate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("PropertyDefinitionCreate"))
return &m.Payload.Definition, HandleErrors(err, m.Payload.Errors)
}
func (client *Client) UpdatePropertyDefinition(identifier string, input PropertyDefinitionInput) (*PropertyDefinition, error) {
var m struct {
Payload PropertyDefinitionPayload `graphql:"propertyDefinitionUpdate(propertyDefinition: $propertyDefinition, input: $input)"`
}
v := PayloadVariables{
"propertyDefinition": *NewIdentifier(identifier),
"input": input,
}
err := client.Mutate(&m, v, WithName("PropertyDefinitionUpdate"))
return &m.Payload.Definition, HandleErrors(err, m.Payload.Errors)
}
func (client *Client) GetPropertyDefinition(input string) (*PropertyDefinition, error) {
var q struct {
Account struct {
Definition PropertyDefinition `graphql:"propertyDefinition(input: $input)"`
}
}
v := PayloadVariables{
"input": *NewIdentifier(input),
}
err := client.Query(&q, v, WithName("PropertyDefinitionGet"))
if q.Account.Definition.Id == "" {
err = fmt.Errorf("PropertyDefinition with ID or Alias matching '%s' not found", input)
}
return &q.Account.Definition, HandleErrors(err, nil)
}
func (client *Client) ListPropertyDefinitions(variables *PayloadVariables) (*PropertyDefinitionConnection, error) {
var q struct {
Account struct {
Definitions PropertyDefinitionConnection `graphql:"propertyDefinitions(after: $after, first: $first)"`
}
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
if err := client.Query(&q, *variables, WithName("PropertyDefinitionList")); err != nil {
return nil, err
}
if q.Account.Definitions.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.Definitions.PageInfo.End
resp, err := client.ListPropertyDefinitions(variables)
if err != nil {
return nil, err
}
q.Account.Definitions.Nodes = append(q.Account.Definitions.Nodes, resp.Nodes...)
q.Account.Definitions.PageInfo = resp.PageInfo
q.Account.Definitions.TotalCount += len(q.Account.Definitions.Nodes)
}
return &q.Account.Definitions, nil
}
func (client *Client) DeletePropertyDefinition(input string) error {
var m struct {
Payload BasePayload `graphql:"propertyDefinitionDelete(resource: $input)"`
}
v := PayloadVariables{
"input": *NewIdentifier(input),
}
err := client.Mutate(&m, v, WithName("PropertyDefinitionDelete"))
return HandleErrors(err, m.Payload.Errors)
}
func (client *Client) CreateTeamPropertyDefinition(input TeamPropertyDefinitionInput) (*TeamPropertyDefinition, error) {
var m struct {
Payload TeamPropertyDefinitionPayload `graphql:"teamPropertyDefinitionCreate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("TeamPropertyDefinitionCreate"))
return &m.Payload.Definition, HandleErrors(err, m.Payload.Errors)
}
func (client *Client) UpdateTeamPropertyDefinition(identifier string, input TeamPropertyDefinitionInput) (*TeamPropertyDefinition, error) {
var m struct {
Payload TeamPropertyDefinitionPayload `graphql:"teamPropertyDefinitionUpdate(propertyDefinition: $propertyDefinition, input: $input)"`
}
v := PayloadVariables{
"propertyDefinition": *NewIdentifier(identifier),
"input": input,
}
err := client.Mutate(&m, v, WithName("TeamPropertyDefinitionUpdate"))
return &m.Payload.Definition, HandleErrors(err, m.Payload.Errors)
}
func (client *Client) GetTeamPropertyDefinition(identifier string) (*TeamPropertyDefinition, error) {
var q struct {
Account struct {
Definition TeamPropertyDefinition `graphql:"teamPropertyDefinition(input: $input)"`
}
}
v := PayloadVariables{
"input": *NewIdentifier(identifier),
}
err := client.Query(&q, v, WithName("TeamPropertyDefinitionGet"))
if q.Account.Definition.Id == "" {
err = fmt.Errorf("TeamPropertyDefinition with ID or Alias matching '%s' not found", identifier)
}
return &q.Account.Definition, HandleErrors(err, nil)
}
func (client *Client) ListTeamPropertyDefinitions(variables *PayloadVariables) (*TeamPropertyDefinitionConnection, error) {
var q struct {
Account struct {
Definitions TeamPropertyDefinitionConnection `graphql:"teamPropertyDefinitions(after: $after, first: $first)"`
}
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
if err := client.Query(&q, *variables, WithName("TeamPropertyDefinitionList")); err != nil {
return nil, err
}
q.Account.Definitions.TotalCount = len(q.Account.Definitions.Nodes)
if q.Account.Definitions.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.Definitions.PageInfo.End
resp, err := client.ListTeamPropertyDefinitions(variables)
if err != nil {
return nil, err
}
q.Account.Definitions.Nodes = append(q.Account.Definitions.Nodes, resp.Nodes...)
q.Account.Definitions.PageInfo = resp.PageInfo
q.Account.Definitions.TotalCount += resp.TotalCount
}
return &q.Account.Definitions, nil
}
func (client *Client) AssignTeamPropertyDefinitions(input TeamPropertyDefinitionsAssignInput) (*TeamPropertyDefinitionConnection, error) {
var m struct {
Payload TeamPropertyDefinitionsAssignPayload `graphql:"teamPropertyDefinitionsAssign(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("TeamPropertyDefinitionsAssign"))
m.Payload.Properties.TotalCount = len(m.Payload.Properties.Nodes)
return &m.Payload.Properties, HandleErrors(err, m.Payload.Errors)
}
// Deprecated: Use [Service.GetProperty] or [Team.GetProperty] instead.
// This method only resolves service owners. Passing a team identifier will
// return an error from the API.
func (client *Client) GetProperty(owner string, definition string) (*Property, error) {
var q struct {
Account struct {
Property Property `graphql:"property(owner: $owner, definition: $definition)"`
}
}
v := PayloadVariables{
"owner": *NewIdentifier(owner),
"definition": *NewIdentifier(definition),
}
err := client.Query(&q, v, WithName("PropertyGet"))
return &q.Account.Property, HandleErrors(err, nil)
}
func (service *Service) GetProperty(client *Client, definition string) (*Property, error) {
var q struct {
Account struct {
Service struct {
Property Property `graphql:"property(definition: $definition)"`
} `graphql:"service(id: $service)"`
}
}
if service.Id == "" {
return nil, fmt.Errorf("unable to get property, invalid Service id: '%s'", service.Id)
}
v := PayloadVariables{
"service": service.Id,
"definition": *NewIdentifier(definition),
}
err := client.Query(&q, v, WithName("ServicePropertyGet"))
return &q.Account.Service.Property, HandleErrors(err, nil)
}
func (client *Client) PropertyAssign(input PropertyInput) (*Property, error) {
var m struct {
Payload PropertyPayload `graphql:"propertyAssign(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("PropertyAssign"))
return &m.Payload.Property, HandleErrors(err, m.Payload.Errors)
}
func (client *Client) PropertyUnassign(owner string, definition string) error {
var m struct {
Payload BasePayload `graphql:"propertyUnassign(owner: $owner, definition: $definition)"`
}
v := PayloadVariables{
"owner": *NewIdentifier(owner),
"definition": *NewIdentifier(definition),
}
err := client.Mutate(&m, v, WithName("PropertyUnassign"))
return HandleErrors(err, m.Payload.Errors)
}
func (service *Service) GetProperties(client *Client, variables *PayloadVariables) (*PropertiesConnection, error) {
var q struct {
Account struct {
Service struct {
Properties PropertiesConnection `graphql:"properties(after: $after, first: $first)"`
} `graphql:"service(id: $service)"`
}
}
if service.Id == "" {
return nil, fmt.Errorf("unable to get properties, invalid Service id: '%s'", service.Id)
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
(*variables)["service"] = service.Id
if err := client.Query(&q, *variables, WithName("ServicePropertiesList")); err != nil {
return nil, err
}
if service.Properties == nil {
service.Properties = &PropertiesConnection{}
}
service.Properties.Nodes = append(service.Properties.Nodes, q.Account.Service.Properties.Nodes...)
service.Properties.PageInfo = q.Account.Service.Properties.PageInfo
if service.Properties.PageInfo.HasNextPage {
(*variables)["after"] = service.Properties.PageInfo.End
if _, err := service.GetProperties(client, variables); err != nil {
return nil, err
}
}
service.Properties.TotalCount = len(service.Properties.Nodes)
return service.Properties, nil
}