-
Notifications
You must be signed in to change notification settings - Fork 12
/
tools.go
186 lines (164 loc) · 4.4 KB
/
tools.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
package opslevel
import (
"github.com/shurcooL/graphql"
)
type Tool struct {
Category ToolCategory
CategoryAlias string `json:",omitempty"`
DisplayName string
Environment string `json:",omitempty"`
Id graphql.ID `json:",omitempty"`
Url string
Service ServiceId
}
type ToolConnection struct {
Nodes []Tool
PageInfo PageInfo
TotalCount int
}
type ToolCreateInput struct {
Category ToolCategory `json:"category"`
DisplayName string `json:"displayName"`
Url string `json:"url"`
Environment string `json:"environment,omitempty"`
ServiceId graphql.ID `json:"serviceId,omitempty"`
ServiceAlias string `json:"serviceAlias,omitempty"`
}
type ToolUpdateInput struct {
Id graphql.ID `json:"id"`
Category ToolCategory `json:"category,omitempty"`
DisplayName string `json:"displayName,omitempty"`
Url string `json:"url,omitempty"`
Environment string `json:"environment,omitempty"`
}
type ToolDeleteInput struct {
Id graphql.ID `json:"id"`
}
//#region Create
func (client *Client) CreateTool(input ToolCreateInput) (*Tool, error) {
// TODO: validate - Category, DisplayName & Url are non nil - or throw err
var m struct {
Payload struct {
Tool Tool
Errors []OpsLevelErrors
} `graphql:"toolCreate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
if err := client.Mutate(&m, v); err != nil {
return nil, err
}
return &m.Payload.Tool, FormatErrors(m.Payload.Errors)
}
//#endregion
//#region Retrieve
func (conn *ToolConnection) Hydrate(service graphql.ID, client *Client) error {
var q struct {
Account struct {
Service struct {
Tools ToolConnection `graphql:"tools(after: $after, first: $first)"`
} `graphql:"service(id: $service)"`
}
}
v := PayloadVariables{
"service": service,
"first": client.pageSize,
}
q.Account.Service.Tools.PageInfo = conn.PageInfo
for q.Account.Service.Tools.PageInfo.HasNextPage {
v["after"] = q.Account.Service.Tools.PageInfo.End
if err := client.Query(&q, v); err != nil {
return err
}
for _, item := range q.Account.Service.Tools.Nodes {
conn.Nodes = append(conn.Nodes, item)
}
}
return nil
}
func (client *Client) GetToolsForServiceWithAlias(alias string) ([]Tool, error) {
service, serviceErr := client.GetServiceIdWithAlias(alias)
if serviceErr != nil {
return nil, serviceErr
}
return client.GetToolsForService(service.Id)
}
// Deprecated: Use GetToolsForService instead
func (client *Client) GetToolsForServiceWithId(service graphql.ID) ([]Tool, error) {
return client.GetToolsForService(service)
}
func (client *Client) GetToolsForService(service graphql.ID) ([]Tool, error) {
var q struct {
Account struct {
Service struct {
Tools ToolConnection `graphql:"tools(after: $after, first: $first)"`
} `graphql:"service(id: $service)"`
}
}
v := PayloadVariables{
"service": service,
"after": graphql.String(""),
"first": client.pageSize,
}
if err := client.Query(&q, v); err != nil {
return q.Account.Service.Tools.Nodes, err
}
if err := q.Account.Service.Tools.Hydrate(service, client); err != nil {
return q.Account.Service.Tools.Nodes, err
}
return q.Account.Service.Tools.Nodes, nil
}
func (client *Client) GetToolCount(service graphql.ID) (int, error) {
var q struct {
Account struct {
Service struct {
Tools struct {
TotalCount int
}
} `graphql:"service(id: $service)"`
}
}
v := PayloadVariables{
"service": service,
}
if err := client.Query(&q, v); err != nil {
return 0, err
}
return int(q.Account.Service.Tools.TotalCount), nil
}
//#endregion
//#region Update
func (client *Client) UpdateTool(input ToolUpdateInput) (*Tool, error) {
var m struct {
Payload struct {
Tool Tool
Errors []OpsLevelErrors
} `graphql:"toolUpdate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
if err := client.Mutate(&m, v); err != nil {
return nil, err
}
return &m.Payload.Tool, FormatErrors(m.Payload.Errors)
}
//#endregion
//#region Delete
func (client *Client) DeleteTool(id graphql.ID) error {
var m struct {
Payload struct {
Id graphql.ID `graphql:"deletedToolId"`
Errors []OpsLevelErrors
} `graphql:"toolDelete(input: $input)"`
}
v := PayloadVariables{
"input": ToolDeleteInput{Id: id},
}
if err := client.Mutate(&m, v); err != nil {
return err
}
return FormatErrors(m.Payload.Errors)
}
//#endregion