Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix URL parsing
  • Loading branch information
vvbogdanov87 committed Jan 18, 2023
commit 531157ed0a35296d8825d9f8166ef279b2e75361
9 changes: 6 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Client struct {
sessionKey string
auth [2]string
host string
path string
httpClient *http.Client
userAgent string
}
Expand Down Expand Up @@ -73,7 +74,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)
Expand Down Expand Up @@ -263,13 +264,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
Expand All @@ -278,13 +280,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
Expand Down
12 changes: 10 additions & 2 deletions splunk/provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package splunk

import (
"net/url"
"time"

"github.com/splunk/terraform-provider-splunk/client"
Expand Down Expand Up @@ -111,18 +112,25 @@ 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
}
} 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
Expand Down
2 changes: 1 addition & 1 deletion splunk/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func newTestClient() (*client.Client, error) {
[2]string{os.Getenv("SPLUNK_USERNAME"),
os.Getenv("SPLUNK_PASSWORD")},
os.Getenv("SPLUNK_URL"),

"",
http)
}

Expand Down