Skip to content

Commit 62d685d

Browse files
author
Ryan Miller
committed
Support endpoint redirection to make base_url optional
1 parent f6c76a6 commit 62d685d

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,4 @@ fabric.properties
9494

9595
terra.tf
9696
dist/
97+
go.sum

sumologic/provider.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package sumologic
33
import (
44
"fmt"
55
"log"
6+
"net/http"
67
"os"
8+
"strings"
79

810
"github.com/go-errors/errors"
911
"github.com/hashicorp/terraform/helper/mutexkv"
@@ -61,6 +63,26 @@ func Provider() terraform.ResourceProvider {
6163

6264
var SumoMutexKV = mutexkv.NewMutexKV()
6365

66+
func resolveRedirectURL(accessId string, accessKey string) (string, error) {
67+
req, err := http.NewRequest(http.MethodHead, "https://api.sumologic.com/api/v1/collectors", nil)
68+
if err != nil {
69+
return "", err
70+
}
71+
req.SetBasicAuth(accessId, accessKey)
72+
client := &http.Client{CheckRedirect: func(req *http.Request, via []*http.Request) error {
73+
return http.ErrUseLastResponse
74+
}}
75+
resp, err := client.Do(req)
76+
if err != nil {
77+
return "", err
78+
}
79+
location := resp.Header.Get("location")
80+
if location == "" {
81+
return location, fmt.Errorf("location header not found")
82+
}
83+
return strings.Split(location, "v1")[0], nil
84+
}
85+
6486
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
6587
accessId := d.Get("access_id").(string)
6688
accessKey := d.Get("access_key").(string)
@@ -77,9 +99,18 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
7799
}
78100

79101
if environment == "" && baseUrl == "" {
80-
environment = "us2"
81-
// baseUrl will be set accordingly in NewClient constructor
82-
log.Printf("[WARN] environment not set, setting to %s", environment)
102+
log.Printf("Attempting to resolve redirection URL from access key/id")
103+
url, err := resolveRedirectURL(accessId, accessKey)
104+
if err != nil {
105+
log.Printf("[WARN] Unable to resolve redirection URL, %s", err)
106+
environment = "us2"
107+
// baseUrl will be set accordingly in NewClient constructor
108+
log.Printf("[WARN] environment not set, defaulting to %s", environment)
109+
} else {
110+
baseUrl = url
111+
log.Printf("Resolved redirection URL %s", baseUrl)
112+
}
113+
83114
}
84115

85116
if msg != "" {

0 commit comments

Comments
 (0)