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

feat: set format for AWS region value in the provider definition #1549

Merged
merged 6 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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: 2 additions & 1 deletion mongodbatlas/fw_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
sdkv2schema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

cstmvalidator "github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/framework/validator"
"github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/util"
"github.com/mongodb/terraform-provider-mongodbatlas/version"
)

Expand Down Expand Up @@ -221,7 +222,7 @@ func (p *MongodbtlasProvider) Configure(ctx context.Context, req provider.Config
if awsRoleDefined {
config.AssumeRole = parseTfModel(ctx, &assumeRoles[0])
secret := data.SecretName.ValueString()
region := data.Region.ValueString()
region := util.MongoDBRegionToAWSRegion(data.Region.ValueString())
awsAccessKeyID := data.AwsAccessKeyID.ValueString()
awsSecretAccessKey := data.AwsSecretAccessKeyID.ValueString()
awsSessionToken := data.AwsSessionToken.ValueString()
Expand Down
3 changes: 2 additions & 1 deletion mongodbatlas/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/util"
"github.com/mwielbut/pointy"
"github.com/spf13/cast"
"github.com/zclconf/go-cty/cty"
Expand Down Expand Up @@ -271,7 +272,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (any, diag.D
if awsRoleDefined {
config.AssumeRole = expandAssumeRole(assumeRoleValue.([]any)[0].(map[string]any))
secret := d.Get("secret_name").(string)
region := d.Get("region").(string)
region := util.MongoDBRegionToAWSRegion(d.Get("region").(string))
awsAccessKeyID := d.Get("aws_access_key_id").(string)
awsSecretAccessKey := d.Get("aws_secret_access_key").(string)
awsSessionToken := d.Get("aws_session_token").(string)
Expand Down
10 changes: 9 additions & 1 deletion mongodbatlas/util/type_conversion.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package util

import "time"
import (
"strings"
"time"
)

func SafeString(s *string) string {
if s != nil {
Expand Down Expand Up @@ -48,3 +51,8 @@ func IntPtrToInt64Ptr(i *int) *int64 {
func IsStringPresent(strPtr *string) bool {
return strPtr != nil && len(*strPtr) > 0
}

// MongoDBRegionToAWSRegion converts region in US_EAST_1-like format to us-east-1-like
func MongoDBRegionToAWSRegion(region string) string {
return strings.ReplaceAll(strings.ToLower(region), "_", "-")
}
17 changes: 17 additions & 0 deletions mongodbatlas/util/type_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,20 @@ func TestIsStringPresent(t *testing.T) {
}
}
}

func TestMongoDBRegionToAWSRegion(t *testing.T) {
marcosuma marked this conversation as resolved.
Show resolved Hide resolved
tests := []struct {
region string
expected string
}{
{"US_EAST_1", "us-east-1"},
{"us-east-1", "us-east-1"},
{"nothing", "nothing"},
}

for _, test := range tests {
if resp := util.MongoDBRegionToAWSRegion(test.region); resp != test.expected {
t.Errorf("MongoDBRegionToAWSRegion(%v) = %v; want %v", test.region, resp, test.expected)
}
}
}
Loading