Skip to content
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.4.31
* Fix: URL parsing of Splunk client path

## 1.4.30
* Suppress defaults when planning changes to Jira service desk params

Expand Down
15 changes: 12 additions & 3 deletions splunk/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,15 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
if err != nil {
return nil, err
}

u, err := url.Parse(d.Get("url").(string))
// Ensure scheme to parse host and path
providerUrl := d.Get("url").(string)
if !hasScheme(providerUrl) {
providerUrl = "http://" + providerUrl // add http scheme so url.Parse works
}
u, err := url.Parse(providerUrl)
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)},
Expand Down Expand Up @@ -147,3 +150,9 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
provider.Client = splunkdClient
return provider, nil
}

// hasScheme checks if a URL has scheme and host
func hasScheme(providerUrl string) bool {
parsed, err := url.Parse(providerUrl)
return err == nil && parsed.Scheme != "" && parsed.Host != ""
}
68 changes: 67 additions & 1 deletion splunk/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package splunk

import (
"net/url"
"os"
"testing"
"time"
Expand All @@ -19,6 +20,60 @@ func TestProvider(t *testing.T) {
}
}

func getTestProviderURL(inputData map[string]interface{}, t *testing.T) url.URL {
providerSchema := providerSchema()
// Create a mock *schema.ResourceData
resourceData := schema.TestResourceDataRaw(t, providerSchema, inputData)
// Call the providerConfigure method
providerInterface, err := providerConfigure(resourceData)
if err != nil {
t.Error(err)
}
provider, ok := providerInterface.(*SplunkProvider) // type assertion
if !ok {
t.Error(ok)
}

url := provider.Client.BuildSplunkURL(nil)
return url
}

func TestProviderConfigure(t *testing.T) {
// Define the input data for the ResourceData
inputData := map[string]interface{}{
"url": "localhost",
"timeout": 60,
"insecure_skip_verify": true,
"auth_token": "aa",
}

url := getTestProviderURL(inputData, t)
if got, want := url.Host, "localhost"; got != want {
t.Errorf("hostname invalid, got %s, want %s", got, want)
}

inputData["url"] = "localhost:8089"
url = getTestProviderURL(inputData, t)
if got, want := url.Host, "localhost:8089"; got != want {
t.Errorf("url.Host invalid, got %s, want %s", got, want)
}

inputData["url"] = "https://localhost:8089"
url = getTestProviderURL(inputData, t)
if got, want := url.Host, "localhost:8089"; got != want {
t.Errorf("url.Host invalid, got %s, want %s", got, want)
}
if got, want := url.Scheme, "https"; got != want {
t.Errorf("url.Scheme invalid, got %s, want %s", got, want)
}
// Test URL with Path
inputData["url"] = "https://localhost:8089/test/path"
url = getTestProviderURL(inputData, t)
if got, want := url.Path, "/test/path"; got != want {
t.Errorf("url path invalid, got %s, want %s", got, want)
}
}

func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
Expand All @@ -31,11 +86,22 @@ func newTestClient() (*client.Client, error) {
if err != nil {
return nil, err
}
// Parse splunk url host
SPLUNK_URL := (os.Getenv("SPLUNK_URL"))
if !hasScheme(SPLUNK_URL) {
SPLUNK_URL = "http://" + SPLUNK_URL // add http scheme so url.Parse works
}
u, err := url.Parse(SPLUNK_URL)
if err != nil {
return nil, err
}
host := u.Host

return client.NewSplunkdClient(
"",
[2]string{os.Getenv("SPLUNK_USERNAME"),
os.Getenv("SPLUNK_PASSWORD")},
os.Getenv("SPLUNK_URL"),
host,
"",
http)
}
Expand Down
Loading