Skip to content

Commit

Permalink
r/aws_amplify_app: Send map with empty key and value to clear 'enviro…
Browse files Browse the repository at this point in the history
…nment_variables'.

Acceptance test output:

% make testacc TEST=./aws TESTARGS='-run=TestAccAWSAmplifyApp_EnvironmentVariables'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccAWSAmplifyApp_EnvironmentVariables -timeout 180m
=== RUN   TestAccAWSAmplifyApp_EnvironmentVariables
=== PAUSE TestAccAWSAmplifyApp_EnvironmentVariables
=== CONT  TestAccAWSAmplifyApp_EnvironmentVariables
--- PASS: TestAccAWSAmplifyApp_EnvironmentVariables (29.91s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	33.543s
  • Loading branch information
ewbankkit committed May 20, 2021
1 parent f3f3ac0 commit 4b781c8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
6 changes: 5 additions & 1 deletion aws/resource_aws_amplify_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,11 @@ func resourceAwsAmplifyAppUpdate(d *schema.ResourceData, meta interface{}) error
}

if d.HasChange("environment_variables") {
input.EnvironmentVariables = expandStringMap(d.Get("environment_variables").(map[string]interface{}))
if v := d.Get("environment_variables").(map[string]interface{}); len(v) > 0 {
input.EnvironmentVariables = expandStringMap(v)
} else {
input.EnvironmentVariables = aws.StringMap(map[string]string{"": ""})
}
}

if d.HasChange("iam_service_role_arn") {
Expand Down
76 changes: 40 additions & 36 deletions aws/resource_aws_amplify_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,9 @@ func TestAccAWSAmplifyApp_CustomRules(t *testing.T) {
})
}

func TestAccAWSAmplifyApp_Name(t *testing.T) {
func TestAccAWSAmplifyApp_EnvironmentVariables(t *testing.T) {
var app amplify.App
rName1 := acctest.RandomWithPrefix("tf-acc-test")
rName2 := acctest.RandomWithPrefix("tf-acc-test")
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_amplify_app.test"

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -338,10 +337,11 @@ func TestAccAWSAmplifyApp_Name(t *testing.T) {
CheckDestroy: testAccCheckAWSAmplifyAppDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAmplifyAppConfigName(rName1),
Config: testAccAWSAmplifyAppConfigEnvironmentVariables(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAmplifyAppExists(resourceName, &app),
resource.TestCheckResourceAttr(resourceName, "name", rName1),
resource.TestCheckResourceAttr(resourceName, "environment_variables.%", "1"),
resource.TestCheckResourceAttr(resourceName, "environment_variables.ENVVAR1", "1"),
),
},
{
Expand All @@ -350,18 +350,29 @@ func TestAccAWSAmplifyApp_Name(t *testing.T) {
ImportStateVerify: true,
},
{
Config: testAccAWSAmplifyAppConfigName(rName2),
Config: testAccAWSAmplifyAppConfigEnvironmentVariablesUpdated(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAmplifyAppExists(resourceName, &app),
resource.TestCheckResourceAttr(resourceName, "name", rName2),
resource.TestCheckResourceAttr(resourceName, "environment_variables.%", "2"),
resource.TestCheckResourceAttr(resourceName, "environment_variables.ENVVAR1", "2"),
resource.TestCheckResourceAttr(resourceName, "environment_variables.ENVVAR2", "2"),
),
},
{
Config: testAccAWSAmplifyAppConfigName(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAmplifyAppExists(resourceName, &app),
resource.TestCheckResourceAttr(resourceName, "environment_variables.%", "0"),
),
},
},
})
}

func TestAccAWSAmplifyApp_repository(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
func TestAccAWSAmplifyApp_Name(t *testing.T) {
var app amplify.App
rName1 := acctest.RandomWithPrefix("tf-acc-test")
rName2 := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_amplify_app.test"

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -371,24 +382,29 @@ func TestAccAWSAmplifyApp_repository(t *testing.T) {
CheckDestroy: testAccCheckAWSAmplifyAppDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAmplifyAppConfigRepository(rName),
Config: testAccAWSAmplifyAppConfigName(rName1),
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(resourceName, "repository", regexp.MustCompile("^https://github.com")),
testAccCheckAWSAmplifyAppExists(resourceName, &app),
resource.TestCheckResourceAttr(resourceName, "name", rName1),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
// access_token is ignored because AWS does not store access_token and oauth_token
// See https://docs.aws.amazon.com/sdk-for-go/api/service/amplify/#CreateAppInput
ImportStateVerifyIgnore: []string{"access_token"},
},
{
Config: testAccAWSAmplifyAppConfigName(rName2),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAmplifyAppExists(resourceName, &app),
resource.TestCheckResourceAttr(resourceName, "name", rName2),
),
},
},
})
}

func TestAccAWSAmplifyApp_environmentVariables(t *testing.T) {
func TestAccAWSAmplifyApp_repository(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_amplify_app.test"

Expand All @@ -399,30 +415,18 @@ func TestAccAWSAmplifyApp_environmentVariables(t *testing.T) {
CheckDestroy: testAccCheckAWSAmplifyAppDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAmplifyAppConfigEnvironmentVariables1(rName),
Config: testAccAWSAmplifyAppConfigRepository(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "environment_variables.%", "1"),
resource.TestCheckResourceAttr(resourceName, "environment_variables.ENVVAR1", "1"),
resource.TestMatchResourceAttr(resourceName, "repository", regexp.MustCompile("^https://github.com")),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSAmplifyAppConfigEnvironmentVariables2(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "environment_variables.%", "2"),
resource.TestCheckResourceAttr(resourceName, "environment_variables.ENVVAR1", "2"),
resource.TestCheckResourceAttr(resourceName, "environment_variables.ENVVAR2", "2"),
),
},
{
Config: testAccAWSAmplifyAppConfigName(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "environment_variables.%", "0"),
),
// access_token is ignored because AWS does not store access_token and oauth_token
// See https://docs.aws.amazon.com/sdk-for-go/api/service/amplify/#CreateAppInput
ImportStateVerifyIgnore: []string{"access_token"},
},
},
})
Expand Down Expand Up @@ -715,10 +719,10 @@ resource "aws_amplify_app" "test" {
`, rName)
}

func testAccAWSAmplifyAppConfigEnvironmentVariables1(rName string) string {
func testAccAWSAmplifyAppConfigEnvironmentVariables(rName string) string {
return fmt.Sprintf(`
resource "aws_amplify_app" "test" {
name = "%s"
name = %[1]q
environment_variables = {
ENVVAR1 = "1"
Expand All @@ -727,10 +731,10 @@ resource "aws_amplify_app" "test" {
`, rName)
}

func testAccAWSAmplifyAppConfigEnvironmentVariables2(rName string) string {
func testAccAWSAmplifyAppConfigEnvironmentVariablesUpdated(rName string) string {
return fmt.Sprintf(`
resource "aws_amplify_app" "test" {
name = "%s"
name = %[1]q
environment_variables = {
ENVVAR1 = "2",
Expand Down

0 comments on commit 4b781c8

Please sign in to comment.