-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathfilter-alerts.go
More file actions
124 lines (110 loc) · 4.13 KB
/
Copy pathfilter-alerts.go
File metadata and controls
124 lines (110 loc) · 4.13 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
package api
import (
"context"
"fmt"
"github.com/humio/cli/internal/api/humiographql"
)
type FilterAlert struct {
ID string `yaml:"-"`
Name string
Description *string
QueryString string `yaml:"queryString"`
ActionNames []string `yaml:"actionNames"`
Labels []string
Enabled bool
QueryOwnershipType string `yaml:"queryOwnershipType"`
ThrottleTimeSeconds *int64 `yaml:"throttleTimeSeconds"`
ThrottleField *string `yaml:"throttleField"`
OwnershipRunAsID string `yaml:"ownershipRunAsID"`
}
type FilterAlerts struct {
client *Client
}
func (c *Client) FilterAlerts() *FilterAlerts { return &FilterAlerts{client: c} }
func (fa *FilterAlerts) List(searchDomainName string) ([]FilterAlert, error) {
if searchDomainName == "" {
return nil, fmt.Errorf("searchDomainName must not be empty")
}
resp, err := humiographql.ListFilterAlerts(context.Background(), fa.client, searchDomainName)
if err != nil {
return nil, err
}
respSearchDomain := resp.GetSearchDomain()
respFilterAlerts := respSearchDomain.GetFilterAlerts()
filterAlerts := make([]FilterAlert, len(respFilterAlerts))
for idx, filterAlert := range respFilterAlerts {
actionNames := make([]string, len(filterAlert.GetActions()))
for kdx, action := range filterAlert.GetActions() {
actionNames[kdx] = action.GetName()
}
filterAlerts[idx] = FilterAlert{
ID: filterAlert.GetId(),
Name: filterAlert.GetName(),
Description: filterAlert.GetDescription(),
QueryString: filterAlert.GetQueryString(),
ActionNames: actionNames,
Labels: filterAlert.GetLabels(),
Enabled: filterAlert.GetEnabled(),
ThrottleField: filterAlert.GetThrottleField(),
ThrottleTimeSeconds: filterAlert.GetThrottleTimeSeconds(),
QueryOwnershipType: string(queryOwnershipToQueryOwnershipType(filterAlert.GetQueryOwnership())),
OwnershipRunAsID: filterAlert.GetQueryOwnership().GetId(),
}
}
return filterAlerts, nil
}
func (fa *FilterAlerts) Create(searchDomainName string, newFilterAlert *FilterAlert) (*FilterAlert, error) {
if searchDomainName == "" {
return nil, fmt.Errorf("searchDomainName must not be empty")
}
if newFilterAlert == nil {
return nil, fmt.Errorf("newFilterAlert must not be nil")
}
var ownershipRunAsID *string
if humiographql.QueryOwnershipType(newFilterAlert.QueryOwnershipType) == humiographql.QueryOwnershipTypeUser {
ownershipRunAsID = &newFilterAlert.OwnershipRunAsID
}
resp, err := humiographql.CreateFilterAlert(
context.Background(),
fa.client,
searchDomainName,
newFilterAlert.Name,
newFilterAlert.Description,
newFilterAlert.QueryString,
newFilterAlert.ActionNames,
newFilterAlert.Labels,
newFilterAlert.Enabled,
ownershipRunAsID,
newFilterAlert.ThrottleField,
newFilterAlert.ThrottleTimeSeconds,
humiographql.QueryOwnershipType(newFilterAlert.QueryOwnershipType),
)
if err != nil {
return nil, err
}
respFilterAlert := resp.GetCreateFilterAlert()
actionNames := make([]string, len(respFilterAlert.GetActions()))
for kdx, action := range respFilterAlert.GetActions() {
actionNames[kdx] = action.GetName()
}
return &FilterAlert{
ID: respFilterAlert.GetId(),
Name: respFilterAlert.GetName(),
Description: respFilterAlert.GetDescription(),
QueryString: respFilterAlert.GetQueryString(),
ActionNames: actionNames,
Labels: respFilterAlert.GetLabels(),
Enabled: respFilterAlert.GetEnabled(),
ThrottleField: respFilterAlert.ThrottleField,
ThrottleTimeSeconds: respFilterAlert.GetThrottleTimeSeconds(),
QueryOwnershipType: string(queryOwnershipToQueryOwnershipType(respFilterAlert.GetQueryOwnership())),
OwnershipRunAsID: respFilterAlert.GetQueryOwnership().GetId(),
}, nil
}
func (fa *FilterAlerts) Delete(searchDomainName, filterAlertID string) error {
if filterAlertID == "" {
return fmt.Errorf("filterAlertID is empty")
}
_, err := humiographql.DeleteFilterAlert(context.Background(), fa.client, searchDomainName, filterAlertID)
return err
}