-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23052 from hashicorp/endpoint-envvars
provider: Add environment variable support for DynamoDB, IAM, S3, STS endpoints
- Loading branch information
Showing
5 changed files
with
266 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:enhancement | ||
provider: Add environment variables `TF_AWS_DYNAMODB_ENDPOINT`, `TF_AWS_IAM_ENDPOINT`, `TF_AWS_S3_ENDPOINT`, and `TF_AWS_STS_ENDPOINT`. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
package provider | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-provider-aws/internal/conns" | ||
) | ||
|
||
func TestExpandEndpoints(t *testing.T) { | ||
oldEnv := stashEnv() | ||
defer popEnv(oldEnv) | ||
|
||
endpoints := make(map[string]interface{}) | ||
for _, serviceKey := range conns.HCLKeys() { | ||
endpoints[serviceKey] = "" | ||
} | ||
endpoints["sts"] = "https://sts.fake.test" | ||
|
||
results := make(map[string]string) | ||
|
||
err := expandEndpoints([]interface{}{endpoints}, results) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %s", err) | ||
} | ||
|
||
if len(results) != 1 { | ||
t.Errorf("Expected 1 endpoint, got %d", len(results)) | ||
} | ||
|
||
if v := results["sts"]; v != "https://sts.fake.test" { | ||
t.Errorf("Expected endpoint %q, got %v", "https://sts.fake.test", results) | ||
} | ||
} | ||
|
||
func TestEndpointMultipleKeys(t *testing.T) { | ||
testcases := []struct { | ||
endpoints map[string]string | ||
expectedService string | ||
expectedEndpoint string | ||
}{ | ||
{ | ||
endpoints: map[string]string{ | ||
"transcribe": "https://transcribe.fake.test", | ||
}, | ||
expectedService: conns.Transcribe, | ||
expectedEndpoint: "https://transcribe.fake.test", | ||
}, | ||
{ | ||
endpoints: map[string]string{ | ||
"transcribeservice": "https://transcribe.fake.test", | ||
}, | ||
expectedService: conns.Transcribe, | ||
expectedEndpoint: "https://transcribe.fake.test", | ||
}, | ||
{ | ||
endpoints: map[string]string{ | ||
"transcribe": "https://transcribe.fake.test", | ||
"transcribeservice": "https://transcribeservice.fake.test", | ||
}, | ||
expectedService: conns.Transcribe, | ||
expectedEndpoint: "https://transcribe.fake.test", | ||
}, | ||
} | ||
|
||
for _, testcase := range testcases { | ||
oldEnv := stashEnv() | ||
defer popEnv(oldEnv) | ||
|
||
endpoints := make(map[string]interface{}) | ||
for _, serviceKey := range conns.HCLKeys() { | ||
endpoints[serviceKey] = "" | ||
} | ||
for k, v := range testcase.endpoints { | ||
endpoints[k] = v | ||
} | ||
|
||
results := make(map[string]string) | ||
|
||
err := expandEndpoints([]interface{}{endpoints}, results) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %s", err) | ||
} | ||
|
||
if a, e := len(results), 1; a != e { | ||
t.Errorf("Expected 1 endpoint, got %d", len(results)) | ||
} | ||
|
||
if v := results[testcase.expectedService]; v != testcase.expectedEndpoint { | ||
t.Errorf("Expected endpoint[%s] to be %q, got %v", testcase.expectedService, testcase.expectedEndpoint, results) | ||
} | ||
} | ||
} | ||
|
||
func TestEndpointEnvVarPrecedence(t *testing.T) { | ||
testcases := []struct { | ||
endpoints map[string]string | ||
envvars map[string]string | ||
expectedService string | ||
expectedEndpoint string | ||
}{ | ||
{ | ||
endpoints: map[string]string{}, | ||
envvars: map[string]string{ | ||
"TF_AWS_STS_ENDPOINT": "https://sts.fake.test", | ||
}, | ||
expectedService: conns.STS, | ||
expectedEndpoint: "https://sts.fake.test", | ||
}, | ||
{ | ||
endpoints: map[string]string{}, | ||
envvars: map[string]string{ | ||
"AWS_STS_ENDPOINT": "https://sts-deprecated.fake.test", | ||
}, | ||
expectedService: conns.STS, | ||
expectedEndpoint: "https://sts-deprecated.fake.test", | ||
}, | ||
{ | ||
endpoints: map[string]string{}, | ||
envvars: map[string]string{ | ||
"TF_AWS_STS_ENDPOINT": "https://sts.fake.test", | ||
"AWS_STS_ENDPOINT": "https://sts-deprecated.fake.test", | ||
}, | ||
expectedService: conns.STS, | ||
expectedEndpoint: "https://sts.fake.test", | ||
}, | ||
{ | ||
endpoints: map[string]string{ | ||
"sts": "https://sts-config.fake.test", | ||
}, | ||
envvars: map[string]string{ | ||
"TF_AWS_STS_ENDPOINT": "https://sts-env.fake.test", | ||
}, | ||
expectedService: conns.STS, | ||
expectedEndpoint: "https://sts-config.fake.test", | ||
}, | ||
} | ||
|
||
for _, testcase := range testcases { | ||
oldEnv := stashEnv() | ||
defer popEnv(oldEnv) | ||
|
||
for k, v := range testcase.envvars { | ||
os.Setenv(k, v) | ||
} | ||
|
||
endpoints := make(map[string]interface{}) | ||
for _, serviceKey := range conns.HCLKeys() { | ||
endpoints[serviceKey] = "" | ||
} | ||
for k, v := range testcase.endpoints { | ||
endpoints[k] = v | ||
} | ||
|
||
results := make(map[string]string) | ||
|
||
err := expandEndpoints([]interface{}{endpoints}, results) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %s", err) | ||
} | ||
|
||
if a, e := len(results), 1; a != e { | ||
t.Errorf("Expected 1 endpoint, got %d", len(results)) | ||
} | ||
|
||
if v := results[testcase.expectedService]; v != testcase.expectedEndpoint { | ||
t.Errorf("Expected endpoint[%s] to be %q, got %v", testcase.expectedService, testcase.expectedEndpoint, results) | ||
} | ||
} | ||
} | ||
|
||
func stashEnv() []string { | ||
env := os.Environ() | ||
os.Clearenv() | ||
return env | ||
} | ||
|
||
func popEnv(env []string) { | ||
os.Clearenv() | ||
|
||
for _, e := range env { | ||
p := strings.SplitN(e, "=", 2) | ||
k, v := p[0], "" | ||
if len(p) > 1 { | ||
v = p[1] | ||
} | ||
os.Setenv(k, v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters