-
Notifications
You must be signed in to change notification settings - Fork 13
/
filters.go
163 lines (138 loc) · 3.87 KB
/
filters.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
package opslevel
import (
"fmt"
"github.com/gosimple/slug"
)
type Predicate struct {
Type PredicateTypeEnum `graphql:"type"`
Value string `graphql:"value"`
}
type PredicateInput struct {
Type PredicateTypeEnum `json:"type"`
Value string `json:"value,omitempty"`
}
type PredicateUpdateInput struct {
Type PredicateTypeEnum `json:"type,omitempty"`
Value string `json:"value,omitempty"`
}
type FilterId struct {
Id ID
Name string
}
type Filter struct {
Connective ConnectiveEnum
HtmlURL string
FilterId
Predicates []FilterPredicate
}
type FilterPredicate struct {
Key PredicateKeyEnum `json:"key"`
KeyData string `json:"keyData,omitempty"`
Type PredicateTypeEnum `json:"type"`
Value string `json:"value,omitempty"`
}
type FilterConnection struct {
Nodes []Filter
PageInfo PageInfo
TotalCount int
}
type FilterCreateInput struct {
Name string `json:"name"`
Predicates []FilterPredicate `json:"predicates"`
Connective ConnectiveEnum `json:"connective,omitempty"`
}
type FilterUpdateInput struct {
Id ID `json:"id"`
Name string `json:"name,omitempty"`
Predicates []FilterPredicate `json:"predicates"` // The list of predicates used to select which services apply to the filter. All existing predicates will be replaced by these predicates.
Connective ConnectiveEnum `json:"connective,omitempty"`
}
func (self *Filter) Alias() string {
return slug.Make(self.Name)
}
//#region Create
func (client *Client) CreateFilter(input FilterCreateInput) (*Filter, error) {
var m struct {
Payload struct {
Filter Filter
Errors []OpsLevelErrors
} `graphql:"filterCreate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("FilterCreate"))
return &m.Payload.Filter, HandleErrors(err, m.Payload.Errors)
}
//#endregion
//#region Retrieve
func (client *Client) GetFilter(id ID) (*Filter, error) {
var q struct {
Account struct {
Filter Filter `graphql:"filter(id: $id)"`
}
}
v := PayloadVariables{
"id": id,
}
err := client.Query(&q, v, WithName("FilterGet"))
if q.Account.Filter.Id == "" {
err = fmt.Errorf("Filter with ID '%s' not found!", id)
}
return &q.Account.Filter, HandleErrors(err, nil)
}
func (client *Client) ListFilters(variables *PayloadVariables) (FilterConnection, error) {
var q struct {
Account struct {
Filters FilterConnection `graphql:"filters(after: $after, first: $first)"`
}
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
if err := client.Query(&q, *variables, WithName("FilterList")); err != nil {
return FilterConnection{}, err
}
for q.Account.Filters.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.Filters.PageInfo.End
resp, err := client.ListFilters(variables)
if err != nil {
return FilterConnection{}, err
}
q.Account.Filters.Nodes = append(q.Account.Filters.Nodes, resp.Nodes...)
q.Account.Filters.PageInfo = resp.PageInfo
q.Account.Filters.TotalCount += resp.TotalCount
}
return q.Account.Filters, nil
}
//#endregion
//#region Update
func (client *Client) UpdateFilter(input FilterUpdateInput) (*Filter, error) {
var m struct {
Payload struct {
Filter Filter
Errors []OpsLevelErrors
} `graphql:"filterUpdate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("FilterUpdate"))
return &m.Payload.Filter, HandleErrors(err, m.Payload.Errors)
}
//#endregion
//#region Delete
func (client *Client) DeleteFilter(id ID) error {
var m struct {
Payload struct {
Id ID `graphql:"deletedId"`
Errors []OpsLevelErrors
} `graphql:"filterDelete(input: $input)"`
}
v := PayloadVariables{
"input": DeleteInput{Id: id},
}
err := client.Mutate(&m, v, WithName("FilterDelete"))
return HandleErrors(err, m.Payload.Errors)
}
//#endregion