diff --git a/client/client.go b/client/client.go index 762ecca7..c2d8095b 100644 --- a/client/client.go +++ b/client/client.go @@ -42,6 +42,7 @@ type Client struct { sessionKey string auth [2]string host string + path string httpClient *http.Client userAgent string } @@ -74,7 +75,7 @@ func getEnv(key, defaultValue string) string { } func (c *Client) BuildSplunkURL(queryValues url.Values, urlPathParts ...string) url.URL { - buildPath := "" + buildPath := c.path for _, pathPart := range urlPathParts { pathPart = strings.ReplaceAll(pathPart, " ", "+") // url parameters cannot have spaces buildPath = path.Join(buildPath, pathPart) @@ -274,13 +275,14 @@ func NewDefaultSplunkdClient() (*Client, error) { } // NewSplunkdClient creates a Client with custom values passed in -func NewSplunkdClient(sessionKey string, auth [2]string, host string, httpClient *http.Client) (*Client, error) { +func NewSplunkdClient(sessionKey string, auth [2]string, host string, path string, httpClient *http.Client) (*Client, error) { c, err := NewDefaultSplunkdClient() if err != nil { return nil, err } c.auth = auth c.host = host + c.path = path c.sessionKey = sessionKey if httpClient != nil { c.httpClient = httpClient @@ -289,13 +291,14 @@ func NewSplunkdClient(sessionKey string, auth [2]string, host string, httpClient } // NewSplunkdClient creates a Client with custom values passed in -func NewSplunkdClientWithAuthToken(authToken string, auth [2]string, host string, httpClient *http.Client) (*Client, error) { +func NewSplunkdClientWithAuthToken(authToken string, auth [2]string, host string, path string, httpClient *http.Client) (*Client, error) { c, err := NewDefaultSplunkdClient() if err != nil { return nil, err } c.auth = auth c.host = host + c.path = path c.authToken = authToken if httpClient != nil { c.httpClient = httpClient diff --git a/splunk/provider.go b/splunk/provider.go index 2e59c495..aea9c755 100644 --- a/splunk/provider.go +++ b/splunk/provider.go @@ -1,6 +1,7 @@ package splunk import ( + "net/url" "time" "github.com/splunk/terraform-provider-splunk/client" @@ -111,10 +112,16 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) { return nil, err } + u, err := url.Parse(d.Get("url").(string)) + if err != nil { + return nil, err + } + if token, ok := d.GetOk("auth_token"); ok { splunkdClient, err = client.NewSplunkdClientWithAuthToken(token.(string), [2]string{d.Get("username").(string), d.Get("password").(string)}, - d.Get("url").(string), + u.Host, + u.Path, httpClient) if err != nil { return splunkdClient, err @@ -122,7 +129,8 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) { } else { splunkdClient, err = client.NewSplunkdClient("", [2]string{d.Get("username").(string), d.Get("password").(string)}, - d.Get("url").(string), + u.Host, + u.Path, httpClient) if err != nil { return splunkdClient, err diff --git a/splunk/provider_test.go b/splunk/provider_test.go index d8859741..dca0008f 100644 --- a/splunk/provider_test.go +++ b/splunk/provider_test.go @@ -36,7 +36,7 @@ func newTestClient() (*client.Client, error) { [2]string{os.Getenv("SPLUNK_USERNAME"), os.Getenv("SPLUNK_PASSWORD")}, os.Getenv("SPLUNK_URL"), - + "", http) }