-
Notifications
You must be signed in to change notification settings - Fork 1
/
service_knowledge.go
91 lines (74 loc) · 2.32 KB
/
service_knowledge.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
package splunk
import (
"encoding/json"
"fmt"
"net/url"
"path"
"strings"
)
// KnowledgeService is an interface for interacting with the splunk knowledge API
type KnowledgeService interface {
ListExtractions(opts *ListOptions) ([]map[string]interface{}, error)
ListTransforms(opts *ListOptions) ([]map[string]interface{}, error)
CreateExtraction(name, stanza, extractionType, value string, acl *ACL) error
DeleteExtraction(stanza, extractionType, value string) error
}
type knowledgeService service
// TODO: pagination
func (ks *knowledgeService) ListExtractions(opts *ListOptions) ([]map[string]interface{}, error) {
data, err := ks.client.NewRequest("GET", "/services/data/props/extractions", nil)
if err != nil {
return nil, err
}
var extractions []map[string]interface{}
err = json.Unmarshal(data.Entry, &extractions)
if err != nil {
return nil, err
}
return extractions, nil
}
// TODO: pagination
func (ks *knowledgeService) ListTransforms(opts *ListOptions) ([]map[string]interface{}, error) {
data, err := ks.client.NewRequest("GET", "/services/data/transforms/extractions", nil)
if err != nil {
return nil, err
}
var transforms []map[string]interface{}
err = json.Unmarshal(data.Entry, &transforms)
if err != nil {
return nil, err
}
return transforms, nil
}
func (ks *knowledgeService) CreateExtraction(name, stanza, extractionType, value string, acl *ACL) error {
form := url.Values{}
form.Set("name", name)
form.Set("stanza", stanza)
form.Set("type", extractionType)
form.Set("value", value)
_, err := ks.client.NewRequest("POST", "/services/data/props/extractions", strings.NewReader(form.Encode()))
if err != nil {
return err
}
if acl != nil {
uri := path.Join(encodedExtractionURI(stanza, extractionType, value), "acl")
_, err := ks.client.NewRequest("POST", uri, strings.NewReader(acl.Encode()))
if err != nil {
return err
}
}
return nil
}
func (ks *knowledgeService) DeleteExtraction(stanza, extractionType, value string) error {
_, err := ks.client.NewRequest("DELETE", encodedExtractionURI(stanza, extractionType, value), nil)
if err != nil {
return err
}
return nil
}
func encodedExtractionURI(stanza, extractionType, value string) string {
return path.Join(
"/services/data/props/extractions",
url.PathEscape(fmt.Sprintf("%s : %s-%s", stanza, extractionType, value)),
)
}