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

b/aws_caller_identity: fix error when sts_region is different from default_region #35860

Merged
merged 5 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
47 changes: 47 additions & 0 deletions internal/service/sts/caller_identity_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
package sts_test

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/envvar"
"github.com/hashicorp/terraform-provider-aws/names"
)

Expand All @@ -28,6 +31,50 @@ func TestAccSTSCallerIdentityDataSource_basic(t *testing.T) {
})
}

func TestAccSTSCallerIdentityDataSource_alternateRegion(t *testing.T) {
ctx := acctest.Context(t)

defaultRegion := os.Getenv(envvar.DefaultRegion)
if defaultRegion == "" {
t.Skipf("Skipping test due to missing %s", envvar.DefaultRegion)
}

alternateRegion := os.Getenv(envvar.AlternateRegion)
if alternateRegion == "" {
t.Skipf("Skipping test due to missing %s", envvar.AlternateRegion)
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.STSEndpointID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCallerIdentityConfig_alternateRegion(defaultRegion, alternateRegion),
Check: resource.ComposeTestCheckFunc(
acctest.CheckCallerIdentityAccountID("data.aws_caller_identity.current"),
),
},
},
})
}

const testAccCallerIdentityConfig_basic = `
data "aws_caller_identity" "current" {}
`

func testAccCallerIdentityConfig_alternateRegion(defaultRegion, alternateRegion string) string {
//lintignore:AT004
return fmt.Sprintf(`
provider "aws" {
region = %[1]q

sts_region = %[2]q
endpoints {
sts = "https://sts.%[2]s.amazonaws.com"
}
}

data "aws_caller_identity" "current" {}
`, defaultRegion, alternateRegion)
}
4 changes: 3 additions & 1 deletion internal/service/sts/service_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func (p *servicePackage) NewClient(ctx context.Context, config map[string]any) (
return sts_sdkv2.NewFromConfig(cfg, func(o *sts_sdkv2.Options) {
if endpoint := config["endpoint"].(string); endpoint != "" {
o.BaseEndpoint = aws_sdkv2.String(endpoint)
} else if stsRegion := config["sts_region"].(string); stsRegion != "" {
}

if stsRegion := config["sts_region"].(string); stsRegion != "" {
o.Region = stsRegion
}
}), nil
Expand Down
Loading