@@ -3,7 +3,9 @@ package sumologic
33import (
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
6264var 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+
6486func 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