Skip to content

Commit

Permalink
r/aws_config_aggregator: New resource
Browse files Browse the repository at this point in the history
  • Loading branch information
gazoakley committed Apr 22, 2018
1 parent e7cf8f0 commit a3b5a6d
Show file tree
Hide file tree
Showing 7 changed files with 653 additions and 0 deletions.
30 changes: 30 additions & 0 deletions aws/import_aws_config_aggregator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package aws

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccConfigAggregator_import(t *testing.T) {
resourceName := "aws_config_aggregator.example"
rString := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckConfigAggregatorDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccConfigAggregatorConfig_account(rString),
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ func Provider() terraform.ResourceProvider {
"aws_cloudwatch_log_resource_policy": resourceAwsCloudWatchLogResourcePolicy(),
"aws_cloudwatch_log_stream": resourceAwsCloudWatchLogStream(),
"aws_cloudwatch_log_subscription_filter": resourceAwsCloudwatchLogSubscriptionFilter(),
"aws_config_aggregator": resourceAwsConfigAggregator(),
"aws_config_config_rule": resourceAwsConfigConfigRule(),
"aws_config_configuration_recorder": resourceAwsConfigConfigurationRecorder(),
"aws_config_configuration_recorder_status": resourceAwsConfigConfigurationRecorderStatus(),
Expand Down
193 changes: 193 additions & 0 deletions aws/resource_aws_config_aggregator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package aws

import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/configservice"

"github.com/hashicorp/terraform/helper/customdiff"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsConfigAggregator() *schema.Resource {
return &schema.Resource{
Create: resourceAwsConfigAggregatorPut,
Read: resourceAwsConfigAggregatorRead,
Update: resourceAwsConfigAggregatorPut,
Delete: resourceAwsConfigAggregatorDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

CustomizeDiff: customdiff.Sequence(
customdiff.ForceNewIfChange("account_aggregation_source", func(old, new, meta interface{}) bool {
return len(old.([]interface{})) == 0 && len(new.([]interface{})) > 0
}),
customdiff.ForceNewIfChange("organization_aggregation_source", func(old, new, meta interface{}) bool {
return len(old.([]interface{})) == 0 && len(new.([]interface{})) > 0
}),
),

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateMaxLength(256),
},
"account_aggregation_source": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"organization_aggregation_source"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"account_ids": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateAwsAccountId,
},
},
"all_regions": {
Type: schema.TypeBool,
Default: false,
Optional: true,
},
"regions": {
Type: schema.TypeList,
Optional: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"organization_aggregation_source": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"account_aggregation_source"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"all_regions": {
Type: schema.TypeBool,
Default: false,
Optional: true,
},
"regions": {
Type: schema.TypeList,
Optional: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"role_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},
},
},
},
},
}
}

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

name := d.Get("name").(string)

req := &configservice.PutConfigurationAggregatorInput{
ConfigurationAggregatorName: aws.String(name),
}

account_aggregation_sources := d.Get("account_aggregation_source").([]interface{})
if len(account_aggregation_sources) > 0 {
req.AccountAggregationSources = expandConfigAccountAggregationSources(account_aggregation_sources)
}

organization_aggregation_sources := d.Get("organization_aggregation_source").([]interface{})
if len(organization_aggregation_sources) > 0 {
req.OrganizationAggregationSource = expandConfigOrganizationAggregationSource(organization_aggregation_sources[0].(map[string]interface{}))
}

_, err := conn.PutConfigurationAggregator(req)
if err != nil {
return fmt.Errorf("Error creating aggregator: %s", err)
}

d.SetId(strings.ToLower(name))

return resourceAwsConfigAggregatorRead(d, meta)
}

func resourceAwsConfigAggregatorRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).configconn
req := &configservice.DescribeConfigurationAggregatorsInput{
ConfigurationAggregatorNames: []*string{aws.String(d.Id())},
}

res, err := conn.DescribeConfigurationAggregators(req)
if err != nil {
if isAWSErr(err, configservice.ErrCodeNoSuchConfigurationAggregatorException, "") {
log.Printf("[WARN] No such configuration aggregator (%s), removing from state", d.Id())
d.SetId("")
return nil
}
return err
}

if len(res.ConfigurationAggregators) == 0 {
log.Printf("[WARN] No aggregators returned (%s), removing from state", d.Id())
d.SetId("")
return nil
}

aggregator := res.ConfigurationAggregators[0]
d.Set("arn", aggregator.ConfigurationAggregatorArn)
d.Set("name", aggregator.ConfigurationAggregatorName)

if aggregator.AccountAggregationSources != nil {
d.Set("account_aggregation_source", flattenConfigAccountAggregationSources(aggregator.AccountAggregationSources))
} else {
d.Set("account_aggregation_source", nil)
}

if aggregator.OrganizationAggregationSource != nil {
d.Set("organization_aggregation_source", flattenConfigOrganizationAggregationSource(aggregator.OrganizationAggregationSource))
} else {
d.Set("organization_aggregation_source", nil)
}

return nil
}

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

req := &configservice.DeleteConfigurationAggregatorInput{
ConfigurationAggregatorName: aws.String(d.Id()),
}
_, err := conn.DeleteConfigurationAggregator(req)
if err != nil {
return err
}

d.SetId("")
return nil
}
Loading

0 comments on commit a3b5a6d

Please sign in to comment.