-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
r/aws_config_aggregator: New resource
- Loading branch information
Showing
7 changed files
with
653 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.