Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix URL parsing #148

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Client struct {
sessionKey string
auth [2]string
host string
path string
httpClient *http.Client
userAgent string
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
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