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