Skip to content

Commit 7defb74

Browse files
authored
Fix URL parsing (#148)
1 parent b1987eb commit 7defb74

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

client/client.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Client struct {
4141
sessionKey string
4242
auth [2]string
4343
host string
44+
path string
4445
httpClient *http.Client
4546
userAgent string
4647
urlEncoded bool
@@ -77,7 +78,7 @@ func getEnv(key, defaultValue string) string {
7778
}
7879

7980
func (c *Client) BuildSplunkURL(queryValues url.Values, urlPathParts ...string) url.URL {
80-
buildPath := ""
81+
buildPath := c.path
8182
for _, pathPart := range urlPathParts {
8283
pathPart = strings.ReplaceAll(pathPart, " ", "+") // url parameters cannot have spaces
8384
buildPath = path.Join(buildPath, pathPart)
@@ -277,13 +278,14 @@ func NewDefaultSplunkdClient() (*Client, error) {
277278
}
278279

279280
// NewSplunkdClient creates a Client with custom values passed in
280-
func NewSplunkdClient(sessionKey string, auth [2]string, host string, httpClient *http.Client) (*Client, error) {
281+
func NewSplunkdClient(sessionKey string, auth [2]string, host string, path string, httpClient *http.Client) (*Client, error) {
281282
c, err := NewDefaultSplunkdClient()
282283
if err != nil {
283284
return nil, err
284285
}
285286
c.auth = auth
286287
c.host = host
288+
c.path = path
287289
c.sessionKey = sessionKey
288290
if httpClient != nil {
289291
c.httpClient = httpClient
@@ -292,13 +294,14 @@ func NewSplunkdClient(sessionKey string, auth [2]string, host string, httpClient
292294
}
293295

294296
// NewSplunkdClient creates a Client with custom values passed in
295-
func NewSplunkdClientWithAuthToken(authToken string, auth [2]string, host string, httpClient *http.Client) (*Client, error) {
297+
func NewSplunkdClientWithAuthToken(authToken string, auth [2]string, host string, path string, httpClient *http.Client) (*Client, error) {
296298
c, err := NewDefaultSplunkdClient()
297299
if err != nil {
298300
return nil, err
299301
}
300302
c.auth = auth
301303
c.host = host
304+
c.path = path
302305
c.authToken = authToken
303306
if httpClient != nil {
304307
c.httpClient = httpClient

splunk/provider.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package splunk
22

33
import (
4+
"net/url"
45
"time"
56

67
"github.com/splunk/terraform-provider-splunk/client"
@@ -113,18 +114,25 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
113114
return nil, err
114115
}
115116

117+
u, err := url.Parse(d.Get("url").(string))
118+
if err != nil {
119+
return nil, err
120+
}
121+
116122
if token, ok := d.GetOk("auth_token"); ok {
117123
splunkdClient, err = client.NewSplunkdClientWithAuthToken(token.(string),
118124
[2]string{d.Get("username").(string), d.Get("password").(string)},
119-
d.Get("url").(string),
125+
u.Host,
126+
u.Path,
120127
httpClient)
121128
if err != nil {
122129
return splunkdClient, err
123130
}
124131
} else {
125132
splunkdClient, err = client.NewSplunkdClient("",
126133
[2]string{d.Get("username").(string), d.Get("password").(string)},
127-
d.Get("url").(string),
134+
u.Host,
135+
u.Path,
128136
httpClient)
129137
if err != nil {
130138
return splunkdClient, err

splunk/provider_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func newTestClient() (*client.Client, error) {
3636
[2]string{os.Getenv("SPLUNK_USERNAME"),
3737
os.Getenv("SPLUNK_PASSWORD")},
3838
os.Getenv("SPLUNK_URL"),
39-
39+
"",
4040
http)
4141
}
4242

0 commit comments

Comments
 (0)