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

Add support resource tags for configservice #9561

Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 36 additions & 3 deletions aws/resource_aws_config_aggregate_authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func resourceAwsConfigAggregateAuthorization() *schema.Resource {
return &schema.Resource{
Create: resourceAwsConfigAggregateAuthorizationPut,
Read: resourceAwsConfigAggregateAuthorizationRead,
Update: resourceAwsConfigAggregateAuthorizationUpdate,
Delete: resourceAwsConfigAggregateAuthorizationDelete,

Importer: &schema.ResourceImporter{
Expand All @@ -37,6 +38,7 @@ func resourceAwsConfigAggregateAuthorization() *schema.Resource {
Required: true,
ForceNew: true,
},
"tags": tagsSchema(),
},
}
}
Expand All @@ -50,6 +52,7 @@ func resourceAwsConfigAggregateAuthorizationPut(d *schema.ResourceData, meta int
req := &configservice.PutAggregationAuthorizationInput{
AuthorizedAccountId: aws.String(accountId),
AuthorizedAwsRegion: aws.String(region),
Tags: tagsFromMapConfigService(d.Get("tags").(map[string]interface{})),
}

_, err := conn.PutAggregationAuthorization(req)
Expand All @@ -58,6 +61,7 @@ func resourceAwsConfigAggregateAuthorizationPut(d *schema.ResourceData, meta int
}

d.SetId(fmt.Sprintf("%s:%s", accountId, region))

return resourceAwsConfigAggregateAuthorizationRead(d, meta)
}

Expand All @@ -77,19 +81,48 @@ func resourceAwsConfigAggregateAuthorizationRead(d *schema.ResourceData, meta in
return fmt.Errorf("Error retrieving list of aggregate authorizations: %s", err)
}

var aggregationAuthorization *configservice.AggregationAuthorization
// Check for existing authorization
for _, auth := range aggregateAuthorizations {
if accountId == aws.StringValue(auth.AuthorizedAccountId) && region == aws.StringValue(auth.AuthorizedAwsRegion) {
d.Set("arn", auth.AggregationAuthorizationArn)
aggregationAuthorization = auth
}
}

if aggregationAuthorization == nil {
log.Printf("[WARN] Aggregate Authorization not found, removing from state: %s", d.Id())
d.SetId("")
return nil
}

d.Set("arn", aggregationAuthorization.AggregationAuthorizationArn)

if err := saveTagsConfigService(conn, d, aws.StringValue(aggregationAuthorization.AggregationAuthorizationArn)); err != nil {
if isAWSErr(err, configservice.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] Aggregate Authorization not found, removing from state: %s", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error setting tags for %s: %s", d.Id(), err)
}

log.Printf("[WARN] Aggregate Authorization not found, removing from state: %s", d.Id())
d.SetId("")
return nil
}

func resourceAwsConfigAggregateAuthorizationUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).configconn

if err := setTagsConfigService(conn, d, d.Get("arn").(string)); err != nil {
if isAWSErr(err, configservice.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] Aggregate Authorization not found, removing from state: %s", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error updating tags for %s: %s", d.Id(), err)
}
return resourceAwsConfigAggregateAuthorizationRead(d, meta)
}

func resourceAwsConfigAggregateAuthorizationDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).configconn

Expand Down
68 changes: 63 additions & 5 deletions aws/resource_aws_config_aggregate_authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func testSweepConfigAggregateAuthorizations(region string) error {

func TestAccAWSConfigAggregateAuthorization_basic(t *testing.T) {
rString := acctest.RandStringFromCharSet(12, "0123456789")
resourceName := "aws_config_aggregate_authorization.example"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -68,20 +69,62 @@ func TestAccAWSConfigAggregateAuthorization_basic(t *testing.T) {
{
Config: testAccAWSConfigAggregateAuthorizationConfig_basic(rString),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("aws_config_aggregate_authorization.example", "account_id", rString),
resource.TestCheckResourceAttr("aws_config_aggregate_authorization.example", "region", "eu-west-1"),
resource.TestMatchResourceAttr("aws_config_aggregate_authorization.example", "arn", regexp.MustCompile(`^arn:aws:config:[\w-]+:\d{12}:aggregation-authorization/\d{12}/[\w-]+$`)),
resource.TestCheckResourceAttr(resourceName, "account_id", rString),
resource.TestCheckResourceAttr(resourceName, "region", "eu-west-1"),
resource.TestMatchResourceAttr(resourceName, "arn", regexp.MustCompile(`^arn:aws:config:[\w-]+:\d{12}:aggregation-authorization/\d{12}/[\w-]+$`)),
),
},
{
ResourceName: "aws_config_aggregate_authorization.example",
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSConfigAggregateAuthorization_tags(t *testing.T) {
rString := acctest.RandStringFromCharSet(12, "0123456789")
resourceName := "aws_config_aggregate_authorization.example"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSConfigAggregateAuthorizationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSConfigAggregateAuthorizationConfig_tags(rString, "foo", "bar", "fizz", "buzz"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", rString),
resource.TestCheckResourceAttr(resourceName, "tags.foo", "bar"),
resource.TestCheckResourceAttr(resourceName, "tags.fizz", "buzz"),
),
},
{
Config: testAccAWSConfigAggregateAuthorizationConfig_tags(rString, "foo", "bar2", "fizz2", "buzz2"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", rString),
resource.TestCheckResourceAttr(resourceName, "tags.foo", "bar2"),
resource.TestCheckResourceAttr(resourceName, "tags.fizz2", "buzz2"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSConfigAggregateAuthorizationConfig_basic(rString),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
},
})
}

func testAccCheckAWSConfigAggregateAuthorizationDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).configconn

Expand Down Expand Up @@ -114,8 +157,23 @@ func testAccCheckAWSConfigAggregateAuthorizationDestroy(s *terraform.State) erro
func testAccAWSConfigAggregateAuthorizationConfig_basic(rString string) string {
return fmt.Sprintf(`
resource "aws_config_aggregate_authorization" "example" {
account_id = "%s"
account_id = %[1]q
region = "eu-west-1"
}
`, rString)
}

func testAccAWSConfigAggregateAuthorizationConfig_tags(rString, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return fmt.Sprintf(`
resource "aws_config_aggregate_authorization" "example" {
account_id = %[1]q
region = "eu-west-1"

tags = {
Name = %[1]q
%[2]s = %[3]q
%[4]s = %[5]q
}
}
`, rString, tagKey1, tagValue1, tagKey2, tagValue2)
}
22 changes: 22 additions & 0 deletions aws/resource_aws_config_config_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func resourceAwsConfigConfigRule() *schema.Resource {
},
},
},
"tags": tagsSchema(),
},
}
}
Expand All @@ -162,6 +163,7 @@ func resourceAwsConfigConfigRulePut(d *schema.ResourceData, meta interface{}) er

input := configservice.PutConfigRuleInput{
ConfigRule: &ruleInput,
Tags: tagsFromMapConfigService(d.Get("tags").(map[string]interface{})),
}
log.Printf("[DEBUG] Creating AWSConfig config rule: %s", input)
err := resource.Retry(2*time.Minute, func() *resource.RetryError {
Expand Down Expand Up @@ -190,6 +192,17 @@ func resourceAwsConfigConfigRulePut(d *schema.ResourceData, meta interface{}) er

log.Printf("[DEBUG] AWSConfig config rule %q created", name)

if !d.IsNewResource() {
if err := setTagsConfigService(conn, d, d.Get("arn").(string)); err != nil {
if isAWSErr(err, configservice.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] Config Rule not found: %s, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error updating tags for %s: %s", d.Id(), err)
}
}

return resourceAwsConfigConfigRuleRead(d, meta)
}

Expand Down Expand Up @@ -236,6 +249,15 @@ func resourceAwsConfigConfigRuleRead(d *schema.ResourceData, meta interface{}) e

d.Set("source", flattenConfigRuleSource(rule.Source))

if err := saveTagsConfigService(conn, d, aws.StringValue(rule.ConfigRuleArn)); err != nil {
if isAWSErr(err, configservice.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] Config Rule not found: %s, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error setting tags for %s: %s", d.Id(), err)
}

return nil
}

Expand Down
Loading