forked from OpsLevel/opslevel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependencies.go
149 lines (131 loc) · 4.16 KB
/
dependencies.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
package opslevel
import "fmt"
type ServiceDependency struct {
Id ID `graphql:"id"`
Service ServiceId `graphql:"sourceService"`
DependsOn ServiceId `graphql:"destinationService"`
Notes string `graphql:"notes"`
}
type ServiceDependenciesEdge struct {
Id ID `graphql:"id"`
Locked bool `graphql:"locked"`
Node *ServiceId `graphql:"node"`
Notes string `graphql:"notes"`
}
type ServiceDependentsEdge struct {
Id ID `graphql:"id"`
Locked bool `graphql:"locked"`
Node *ServiceId `graphql:"node"`
Notes string `graphql:"notes"`
}
type ServiceDependenciesConnection struct {
Edges []ServiceDependenciesEdge `graphql:"edges"`
PageInfo PageInfo
}
type ServiceDependentsConnection struct {
Edges []ServiceDependentsEdge `graphql:"edges"`
PageInfo PageInfo
}
type ServiceDependencyKey struct {
Service IdentifierInput `json:"sourceIdentifier"`
DependsOn IdentifierInput `json:"destinationIdentifier"`
}
type ServiceDependencyCreateInput struct {
Key ServiceDependencyKey `json:"dependencyKey"`
Notes string `json:"notes,omitempty"`
}
//#region Create
func (client *Client) CreateServiceDependency(input ServiceDependencyCreateInput) (*ServiceDependency, error) {
var m struct {
Payload struct {
ServiceDependency *ServiceDependency
Errors []OpsLevelErrors
} `graphql:"serviceDependencyCreate(inputV2: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("ServiceDependencyCreate"))
return m.Payload.ServiceDependency, HandleErrors(err, m.Payload.Errors)
}
//#endregion
//#region Retrieve
func (s *Service) GetDependencies(client *Client, variables *PayloadVariables) (*ServiceDependenciesConnection, error) {
var q struct {
Account struct {
Service struct {
Dependencies ServiceDependenciesConnection `graphql:"dependencies(after: $after, first: $first)"`
} `graphql:"service(id: $service)"`
}
}
if s.Id == "" {
return nil, fmt.Errorf("Unable to get Dependencies, invalid service id: '%s'", s.Id)
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
(*variables)["service"] = s.Id
if err := client.Query(&q, *variables, WithName("ServiceDependenciesList")); err != nil {
return nil, err
}
if s.Dependencies == nil {
s.Dependencies = &ServiceDependenciesConnection{}
}
s.Dependencies.Edges = append(s.Dependencies.Edges, q.Account.Service.Dependencies.Edges...)
s.Dependencies.PageInfo = q.Account.Service.Dependencies.PageInfo
for s.Dependencies.PageInfo.HasNextPage {
(*variables)["after"] = s.Dependencies.PageInfo.End
_, err := s.GetDependencies(client, variables)
if err != nil {
return nil, err
}
}
return s.Dependencies, nil
}
func (s *Service) GetDependents(client *Client, variables *PayloadVariables) (*ServiceDependentsConnection, error) {
var q struct {
Account struct {
Service struct {
Dependents ServiceDependentsConnection `graphql:"dependents(after: $after, first: $first)"`
} `graphql:"service(id: $service)"`
}
}
if s.Id == "" {
return nil, fmt.Errorf("Unable to get Dependents, invalid service id: '%s'", s.Id)
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
(*variables)["service"] = s.Id
if err := client.Query(&q, *variables, WithName("ServiceDependentsList")); err != nil {
return nil, err
}
if s.Dependents == nil {
s.Dependents = &ServiceDependentsConnection{}
}
s.Dependents.Edges = append(s.Dependents.Edges, q.Account.Service.Dependents.Edges...)
s.Dependents.PageInfo = q.Account.Service.Dependents.PageInfo
for s.Dependents.PageInfo.HasNextPage {
(*variables)["after"] = s.Dependents.PageInfo.End
_, err := s.GetDependents(client, variables)
if err != nil {
return nil, err
}
}
return s.Dependents, nil
}
//#endregion
//#region Delete
func (client *Client) DeleteServiceDependency(id ID) error {
var m struct {
Payload struct {
Errors []OpsLevelErrors
} `graphql:"serviceDependencyDelete(input: $input)"`
}
v := PayloadVariables{
"input": DeleteInput{Id: id},
}
err := client.Mutate(&m, v, WithName("ServiceDependencyDelete"))
return HandleErrors(err, m.Payload.Errors)
}
//#endregion