From 09dfed01c89435ae3e14dad2e3d76ded9482ec41 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Wed, 12 Sep 2018 11:33:21 -0400 Subject: [PATCH] resource/aws_config_config_rule: Prevent panic when specifying empty scope Previously: ``` === RUN TestAccAWSConfig/Config/scopeTagKeyEmpty panic: interface conversion: interface {} is nil, not map[string]interface {} goroutine 1709 [running]: github.com/terraform-providers/terraform-provider-aws/aws.resourceAwsConfigConfigRulePut(0xc0002e8fc0, 0x3c46240, 0xc0000b2600, 0x24, 0x71ead40) /Users/bflad/go/src/github.com/terraform-providers/terraform-provider-aws/aws/resource_aws_config_config_rule.go:154 +0x7e6 ``` Now: ``` --- PASS: TestAccAWSConfig/Config/scopeTagKeyEmpty (101.17s) ``` --- aws/resource_aws_config_config_rule.go | 6 +- aws/resource_aws_config_config_rule_test.go | 155 ++++++++++++++++++++ aws/resource_aws_config_test.go | 13 +- aws/structure.go | 6 +- 4 files changed, 169 insertions(+), 11 deletions(-) diff --git a/aws/resource_aws_config_config_rule.go b/aws/resource_aws_config_config_rule.go index 9f055b5b6d6..08621f114b4 100644 --- a/aws/resource_aws_config_config_rule.go +++ b/aws/resource_aws_config_config_rule.go @@ -146,14 +146,10 @@ func resourceAwsConfigConfigRulePut(d *schema.ResourceData, meta interface{}) er name := d.Get("name").(string) ruleInput := configservice.ConfigRule{ ConfigRuleName: aws.String(name), + Scope: expandConfigRuleScope(d.Get("scope").([]interface{})), Source: expandConfigRuleSource(d.Get("source").([]interface{})), } - scopes := d.Get("scope").([]interface{}) - if len(scopes) > 0 { - ruleInput.Scope = expandConfigRuleScope(scopes[0].(map[string]interface{})) - } - if v, ok := d.GetOk("description"); ok { ruleInput.Description = aws.String(v.(string)) } diff --git a/aws/resource_aws_config_config_rule_test.go b/aws/resource_aws_config_config_rule_test.go index d8db29ba197..52e0802c195 100644 --- a/aws/resource_aws_config_config_rule_test.go +++ b/aws/resource_aws_config_config_rule_test.go @@ -159,6 +159,86 @@ func testAccConfigConfigRule_importLambda(t *testing.T) { }) } +func testAccConfigConfigRule_Scope_TagKey(t *testing.T) { + var configRule configservice.ConfigRule + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_config_config_rule.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckConfigConfigRuleDestroy, + Steps: []resource.TestStep{ + { + Config: testAccConfigConfigRuleConfig_Scope_TagKey(rName, "key1"), + Check: resource.ComposeTestCheckFunc( + testAccCheckConfigConfigRuleExists(resourceName, &configRule), + resource.TestCheckResourceAttr(resourceName, "scope.#", "1"), + resource.TestCheckResourceAttr(resourceName, "scope.0.tag_key", "key1"), + ), + }, + { + Config: testAccConfigConfigRuleConfig_Scope_TagKey(rName, "key2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckConfigConfigRuleExists(resourceName, &configRule), + resource.TestCheckResourceAttr(resourceName, "scope.#", "1"), + resource.TestCheckResourceAttr(resourceName, "scope.0.tag_key", "key2"), + ), + }, + }, + }) +} + +func testAccConfigConfigRule_Scope_TagKey_Empty(t *testing.T) { + var configRule configservice.ConfigRule + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_config_config_rule.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckConfigConfigRuleDestroy, + Steps: []resource.TestStep{ + { + Config: testAccConfigConfigRuleConfig_Scope_TagKey(rName, ""), + Check: resource.ComposeTestCheckFunc( + testAccCheckConfigConfigRuleExists(resourceName, &configRule), + ), + }, + }, + }) +} + +func testAccConfigConfigRule_Scope_TagValue(t *testing.T) { + var configRule configservice.ConfigRule + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_config_config_rule.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckConfigConfigRuleDestroy, + Steps: []resource.TestStep{ + { + Config: testAccConfigConfigRuleConfig_Scope_TagValue(rName, "value1"), + Check: resource.ComposeTestCheckFunc( + testAccCheckConfigConfigRuleExists(resourceName, &configRule), + resource.TestCheckResourceAttr(resourceName, "scope.#", "1"), + resource.TestCheckResourceAttr(resourceName, "scope.0.tag_value", "value1"), + ), + }, + { + Config: testAccConfigConfigRuleConfig_Scope_TagValue(rName, "value2"), + Check: resource.ComposeTestCheckFunc( + testAccCheckConfigConfigRuleExists(resourceName, &configRule), + resource.TestCheckResourceAttr(resourceName, "scope.#", "1"), + resource.TestCheckResourceAttr(resourceName, "scope.0.tag_value", "value2"), + ), + }, + }, + }) +} + func testAccCheckConfigConfigRuleName(n, desired string, obj *configservice.ConfigRule) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -224,6 +304,42 @@ func testAccCheckConfigConfigRuleDestroy(s *terraform.State) error { return nil } +func testAccConfigConfigRuleConfig_base(rName string) string { + return fmt.Sprintf(` +data "aws_partition" "current" {} + +resource "aws_config_configuration_recorder" "test" { + name = %q + role_arn = "${aws_iam_role.test.arn}" +} + +resource "aws_iam_role" "test" { + name = %q + + assume_role_policy = <