1+ package splunk
2+
3+ import (
4+ "encoding/json"
5+ "fmt"
6+ "io/ioutil"
7+ "net/http"
8+ "net/url"
9+ "reflect"
10+ "strings"
11+ )
12+
13+ // PropertiesService encapsulates Splunk Properties API
14+
15+ type PropertiesService struct {
16+ client * Client
17+ }
18+
19+ func newPropertiesService (client * Client ) * PropertiesService {
20+ return & PropertiesService {
21+ client : client ,
22+ }
23+ }
24+
25+ type Entry struct {
26+ Value string
27+ }
28+
29+ // stringResponseDecoder decodes http response string
30+ // Properties API operates on particular key in the configuration file.
31+ // CRUD for properties API returns JSON/XML encoded response for error cases and returns a string response for success
32+ type stringResponseDecoder struct {
33+ }
34+
35+ func getPropertiesUri (file string , stanza string , key string ) (string ) {
36+ return fmt .Sprintf ("properties/%s/%s/%s" , url .PathEscape (file ), url .PathEscape (stanza ), url .PathEscape (key ))
37+ }
38+
39+ func (d stringResponseDecoder ) Decode (resp * http.Response , v interface {}) error {
40+ body , err := ioutil .ReadAll (resp .Body )
41+ if err != nil {
42+ return err
43+ }
44+ if 200 <= resp .StatusCode && resp .StatusCode <= 299 {
45+ tempEntry := & Entry {
46+ Value : string (body ),
47+ }
48+ vVal , tempVal := reflect .ValueOf (v ), reflect .ValueOf (tempEntry )
49+ vVal .Elem ().Set (tempVal .Elem ())
50+ return nil
51+ }
52+ return json .Unmarshal (body , v )
53+ }
54+
55+ // UpdateKey updates value for specified key from the specified stanza in the configuration file
56+ func (p * PropertiesService ) UpdateKey (file string , stanza string , key string , value string ) (* string , * http.Response , error ) {
57+ apiError := & APIError {}
58+ body := strings .NewReader (fmt .Sprintf ("value=%s" , value ))
59+ resp , err := p .client .New ().Post (
60+ getPropertiesUri (file , stanza , key )).Body (body ).ResponseDecoder (stringResponseDecoder {}).Receive (nil , apiError )
61+ if err != nil || ! apiError .Empty () {
62+ return nil , resp , relevantError (err , apiError )
63+ }
64+ return nil , resp , relevantError (err , apiError )
65+ }
66+
67+ // GetKey returns value for the given key from the specified stanza in the configuration file
68+ func (p * PropertiesService ) GetKey (file string , stanza string , key string ) (* string , * http.Response , error ) {
69+ apiError := & APIError {}
70+ output := & Entry {}
71+ resp , err := p .client .New ().Get (
72+ getPropertiesUri (file , stanza , key )).ResponseDecoder (stringResponseDecoder {}).Receive (output , apiError )
73+ if err != nil || ! apiError .Empty () {
74+ return nil , resp , relevantError (err , apiError )
75+ }
76+ return & output .Value , resp , relevantError (err , apiError )
77+ }
0 commit comments