Skip to content
This repository was archived by the owner on Mar 15, 2024. It is now read-only.

Commit 576fa30

Browse files
author
Amey Bhide
committed
Add Get/Update methods for Properties API
1 parent 7519a6b commit 576fa30

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

clients/splunk/properties.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
}

clients/splunk/properties_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package splunk
2+
3+
import (
4+
"gotest.tools/assert"
5+
"testing"
6+
)
7+
8+
func TestPropertiesService_GetKey(t *testing.T) {
9+
propertiesSvc := TestGlobalSplunkClient(t).Properties
10+
11+
// Negative cases
12+
_, response, err := propertiesSvc.GetKey("foo", "bar", "key")
13+
assert.ErrorContains(t, err, "splunk: foo does not exist")
14+
assert.Equal(t, response.StatusCode, 404)
15+
_, response, err = propertiesSvc.UpdateKey("foo", "bar", "pass4SymmKey", "bar")
16+
assert.ErrorContains(t, err, "splunk: bar does not exist")
17+
assert.Equal(t, response.StatusCode, 404)
18+
19+
20+
_, response, err = propertiesSvc.GetKey("server", "general", "pass4SymmKey")
21+
assert.Equal(t, response.StatusCode, 200)
22+
23+
// Update value for pass4SymmKey and check if the new value is reflected
24+
_, response, err = propertiesSvc.UpdateKey("server", "general", "pass4SymmKey", "bar")
25+
assert.Equal(t, response.StatusCode, 200)
26+
currentValue, response, err := propertiesSvc.GetKey("server", "general", "pass4SymmKey")
27+
assert.Equal(t, response.StatusCode, 200)
28+
assert.Equal(t, *currentValue, "bar")
29+
}

clients/splunk/splunk.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type API struct {
1919
client *Client
2020
Introspection *IntrospectionService
2121
AccessControl *AccessControlService
22+
Properties *PropertiesService
2223
// XXX ...
2324
}
2425

@@ -37,6 +38,7 @@ func (params *APIParams) NewAPI(ctx context.Context) *API {
3738
client: client.Path("services/"),
3839
Introspection: newIntrospectionService(client.New()),
3940
AccessControl: newAccessControlService(client.New()),
41+
Properties: newPropertiesService(client.New()),
4042
}
4143
}
4244

0 commit comments

Comments
 (0)