-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathviews.go
More file actions
159 lines (137 loc) · 3.95 KB
/
Copy pathviews.go
File metadata and controls
159 lines (137 loc) · 3.95 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
package api
import (
"context"
"sort"
"strings"
"github.com/humio/cli/internal/api/humiographql"
)
type Views struct {
client *Client
}
type ViewConnection struct {
RepoName string
Filter string
}
type View struct {
Name string
Description string
Connections []ViewConnection
AutomaticSearch bool
}
func (c *Client) Views() *Views { return &Views{client: c} }
func (c *Views) Get(name string) (*View, error) {
resp, err := humiographql.GetSearchDomain(context.Background(), c.client, name)
if err != nil {
return nil, ViewNotFound(name)
}
searchDomain := resp.GetSearchDomain()
switch v := searchDomain.(type) {
case *humiographql.GetSearchDomainSearchDomainView:
connections := make([]ViewConnection, len(v.GetConnections()))
for i, data := range v.GetConnections() {
connections[i] = ViewConnection{
RepoName: data.Repository.Name,
Filter: data.Filter,
}
}
description := ""
if searchDomain.GetDescription() != nil {
description = *searchDomain.GetDescription()
}
return &View{
Name: searchDomain.GetName(),
Description: description,
Connections: connections,
AutomaticSearch: searchDomain.GetAutomaticSearch(),
}, nil
default:
return nil, ViewNotFound(name)
}
}
type ViewListItem struct {
Name string
Typename string
AutomaticSearch bool
}
func (c *Views) List() ([]ViewListItem, error) {
resp, err := humiographql.ListSearchDomains(context.Background(), c.client)
if err != nil {
return nil, err
}
searchDomains := resp.GetSearchDomains()
viewsList := []ViewListItem{}
for _, searchDomain := range searchDomains {
switch v := searchDomain.(type) {
case *humiographql.ListSearchDomainsSearchDomainsView:
typename := ""
if v.GetTypename() != nil {
typename = *v.GetTypename()
}
viewsList = append(viewsList, ViewListItem{
Name: v.GetName(),
Typename: typename,
AutomaticSearch: v.GetAutomaticSearch(),
})
default:
// ignore
}
}
sort.Slice(viewsList, func(i, j int) bool {
return strings.ToLower(viewsList[i].Name) < strings.ToLower(viewsList[j].Name)
})
return viewsList, nil
}
type ViewConnectionInput struct {
RepositoryName string
Filter string
}
func (c *Views) Create(name, description string, connections []ViewConnectionInput) error {
createDescription := ""
if description != "" {
createDescription = description
}
internalConnType := make([]humiographql.ViewConnectionInput, len(connections))
for i := range connections {
internalConnType[i] = humiographql.ViewConnectionInput{
RepositoryName: connections[i].RepositoryName,
Filter: connections[i].Filter,
}
}
_, err := humiographql.CreateView(context.Background(), c.client, name, &createDescription, internalConnType)
return err
}
func (c *Views) Delete(name, reason string) error {
_, err := c.Get(name)
if err != nil {
return err
}
_, err = humiographql.DeleteSearchDomain(context.Background(), c.client, name, reason)
return err
}
func (c *Views) UpdateConnections(name string, connections []ViewConnectionInput) error {
internalConnType := make([]humiographql.ViewConnectionInput, len(connections))
for i := range connections {
internalConnType[i] = humiographql.ViewConnectionInput{
RepositoryName: connections[i].RepositoryName,
Filter: connections[i].Filter,
}
}
_, err := humiographql.UpdateViewConnections(context.Background(), c.client, name, internalConnType)
return err
}
func (c *Views) UpdateDescription(name string, description string) error {
_, err := c.Get(name)
if err != nil {
return err
}
_, err = humiographql.UpdateDescriptionForSearchDomain(context.Background(), c.client, name, description)
return err
}
func (c *Views) UpdateAutomaticSearch(name string, automaticSearch bool) error {
_, err := c.Get(name)
if err != nil {
return err
}
_, err = humiographql.SetAutomaticSearching(context.Background(), c.client, name, automaticSearch)
return err
}