Skip to content

Commit

Permalink
New Resource: aws_imagebuilder_infrastructure_configuration (#16186)
Browse files Browse the repository at this point in the history
* Add aws_imagebuilder_infrastructureconfiguration

* Fix issue with infraconfig logging

* [#11084] Fix the imagebuilder resource, add test and docs

* New Resource: aws_imagebuilder_infrastructure_configuration

Reference: #11084
Reference: #13485
Reference: #14347

Changes:

```
* **New Data Source:** `aws_imagebuilder_infrastructure_configuration`
* **New Resource:** `aws_imagebuilder_infrastructure_configuration`
```

Output from acceptance testing in AWS Commercial:

```
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_basic (47.25s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_Description (56.76s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_disappears (34.25s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_InstanceProfileName (75.52s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_InstanceTypes (63.65s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_KeyPair (63.67s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_Logging_S3Logs_S3BucketName (82.86s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_Logging_S3Logs_S3KeyPrefix (76.12s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_ResourceTags (62.22s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_SecurityGroupIds (76.90s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_SnsTopicArn (69.00s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_SubnetId (76.21s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_Tags (74.09s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_TerminateInstanceOnFailure (62.45s)

--- PASS: TestAccAwsImageBuilderInfrastructureConfigurationDataSource_Arn (26.62s)
```

Output from acceptance testing in AWS GovCloud (US):

```
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_basic (51.08s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_Description (66.77s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_disappears (44.79s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_InstanceProfileName (76.02s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_InstanceTypes (71.91s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_KeyPair (73.11s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_Logging_S3Logs_S3BucketName (88.93s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_Logging_S3Logs_S3KeyPrefix (84.10s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_ResourceTags (71.55s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_SecurityGroupIds (81.06s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_SnsTopicArn (69.43s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_SubnetId (83.06s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_Tags (87.65s)
--- PASS: TestAccAwsImageBuilderInfrastructureConfiguration_TerminateInstanceOnFailure (71.96s)

--- PASS: TestAccAwsImageBuilderInfrastructureConfigurationDataSource_Arn (33.51s)
```

Output from sweeper in AWS Commercial:

```
2020/11/13 14:24:06 [DEBUG] Running Sweepers for region (us-west-2):
2020/11/13 14:24:06 [DEBUG] Running Sweeper (aws_imagebuilder_infrastructure_configuration) in region (us-west-2)
2020/11/13 14:24:09 Sweeper Tests ran successfully:
  - aws_imagebuilder_infrastructure_configuration
2020/11/13 14:24:09 [DEBUG] Running Sweepers for region (us-east-1):
2020/11/13 14:24:09 [DEBUG] Running Sweeper (aws_imagebuilder_infrastructure_configuration) in region (us-east-1)
2020/11/13 14:24:11 Sweeper Tests ran successfully:
  - aws_imagebuilder_infrastructure_configuration
ok    github.com/terraform-providers/terraform-provider-aws/aws 7.692s
```
Output from sweeper in AWS GovCloud (US):

```
2020/11/13 14:24:10 [DEBUG] Running Sweepers for region (us-gov-west-1):
2020/11/13 14:24:10 [DEBUG] Running Sweeper (aws_imagebuilder_infrastructure_configuration) in region (us-gov-west-1)
2020/11/13 14:24:12 Sweeper Tests ran successfully:
  - aws_imagebuilder_infrastructure_configuration
ok    github.com/terraform-providers/terraform-provider-aws/aws 4.976s
```

Co-authored-by: Dogers <Dogers@users.noreply.github.com>
Co-authored-by: Jakub Kania <jakubkania@blackcat.rocks>
  • Loading branch information
3 people authored Nov 18, 2020
1 parent 31aaa62 commit 98d9708
Show file tree
Hide file tree
Showing 7 changed files with 1,811 additions and 0 deletions.
142 changes: 142 additions & 0 deletions aws/data_source_aws_imagebuilder_infrastructure_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/imagebuilder"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func datasourceAwsImageBuilderInfrastructureConfiguration() *schema.Resource {
return &schema.Resource{
Read: datasourceAwsImageBuilderInfrastructureConfigurationRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},
"date_created": {
Type: schema.TypeString,
Computed: true,
},
"date_updated": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"instance_profile_name": {
Type: schema.TypeString,
Computed: true,
},
"instance_types": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"key_pair": {
Type: schema.TypeString,
Computed: true,
},
"logging": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"s3_logs": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"s3_bucket_name": {
Type: schema.TypeString,
Computed: true,
},
"s3_key_prefix": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"resource_tags": tagsSchemaComputed(),
"security_group_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"sns_topic_arn": {
Type: schema.TypeString,
Computed: true,
},
"subnet_id": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaComputed(),
"terminate_instance_on_failure": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}

func datasourceAwsImageBuilderInfrastructureConfigurationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).imagebuilderconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

input := &imagebuilder.GetInfrastructureConfigurationInput{}

if v, ok := d.GetOk("arn"); ok {
input.InfrastructureConfigurationArn = aws.String(v.(string))
}

output, err := conn.GetInfrastructureConfiguration(input)

if err != nil {
return fmt.Errorf("error getting Image Builder Infrastructure Configuration (%s): %w", d.Id(), err)
}

if output == nil || output.InfrastructureConfiguration == nil {
return fmt.Errorf("error getting Image Builder Infrastructure Configuration (%s): empty response", d.Id())
}

infrastructureConfiguration := output.InfrastructureConfiguration

d.SetId(aws.StringValue(infrastructureConfiguration.Arn))
d.Set("arn", infrastructureConfiguration.Arn)
d.Set("date_created", infrastructureConfiguration.DateCreated)
d.Set("date_updated", infrastructureConfiguration.DateUpdated)
d.Set("description", infrastructureConfiguration.Description)
d.Set("instance_profile_name", infrastructureConfiguration.InstanceProfileName)
d.Set("instance_types", aws.StringValueSlice(infrastructureConfiguration.InstanceTypes))
d.Set("key_pair", infrastructureConfiguration.KeyPair)
if infrastructureConfiguration.Logging != nil {
d.Set("logging", []interface{}{flattenImageBuilderLogging(infrastructureConfiguration.Logging)})
} else {
d.Set("logging", nil)
}
d.Set("name", infrastructureConfiguration.Name)
d.Set("resource_tags", keyvaluetags.ImagebuilderKeyValueTags(infrastructureConfiguration.ResourceTags).Map())
d.Set("security_group_ids", aws.StringValueSlice(infrastructureConfiguration.SecurityGroupIds))
d.Set("sns_topic_arn", infrastructureConfiguration.SnsTopicArn)
d.Set("subnet_id", infrastructureConfiguration.SubnetId)
d.Set("tags", keyvaluetags.ImagebuilderKeyValueTags(infrastructureConfiguration.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map())
d.Set("terminate_instance_on_failure", infrastructureConfiguration.TerminateInstanceOnFailure)

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccAwsImageBuilderInfrastructureConfigurationDataSource_Arn(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_imagebuilder_infrastructure_configuration.test"
resourceName := "aws_imagebuilder_infrastructure_configuration.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckAwsImageBuilderInfrastructureConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsImageBuilderInfrastructureConfigurationDataSourceConfigArn(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "date_created", resourceName, "date_created"),
resource.TestCheckResourceAttrPair(dataSourceName, "date_updated", resourceName, "date_updated"),
resource.TestCheckResourceAttrPair(dataSourceName, "description", resourceName, "description"),
resource.TestCheckResourceAttrPair(dataSourceName, "instance_profile_name", resourceName, "instance_profile_name"),
resource.TestCheckResourceAttrPair(dataSourceName, "instance_types.#", resourceName, "instance_types.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "key_pair", resourceName, "key_pair"),
resource.TestCheckResourceAttrPair(dataSourceName, "logging.#", resourceName, "logging.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(dataSourceName, "resource_tags.%", resourceName, "resource_tags.%"),
resource.TestCheckResourceAttrPair(dataSourceName, "security_group_ids.#", resourceName, "security_group_ids.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "sns_topic_arn", resourceName, "sns_topic_arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "subnet_id", resourceName, "subnet_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "tags.%", resourceName, "tags.%"),
resource.TestCheckResourceAttrPair(dataSourceName, "terminate_instance_on_failure", resourceName, "terminate_instance_on_failure"),
),
},
},
})
}

func testAccAwsImageBuilderInfrastructureConfigurationDataSourceConfigArn(rName string) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}
resource "aws_iam_instance_profile" "test" {
name = aws_iam_role.role.name
role = aws_iam_role.role.name
}
resource "aws_iam_role" "role" {
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ec2.${data.aws_partition.current.dns_suffix}"
}
Sid = ""
}]
})
name = %[1]q
}
resource "aws_imagebuilder_infrastructure_configuration" "test" {
instance_profile_name = aws_iam_instance_profile.test.name
name = %[1]q
}
data "aws_imagebuilder_infrastructure_configuration" "test" {
arn = aws_imagebuilder_infrastructure_configuration.test.arn
}
`, rName)
}
2 changes: 2 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ func Provider() *schema.Provider {
"aws_iam_user": dataSourceAwsIAMUser(),
"aws_imagebuilder_component": dataSourceAwsImageBuilderComponent(),
"aws_imagebuilder_distribution_configuration": datasourceAwsImageBuilderDistributionConfiguration(),
"aws_imagebuilder_infrastructure_configuration": datasourceAwsImageBuilderInfrastructureConfiguration(),
"aws_internet_gateway": dataSourceAwsInternetGateway(),
"aws_iot_endpoint": dataSourceAwsIotEndpoint(),
"aws_inspector_rules_packages": dataSourceAwsInspectorRulesPackages(),
Expand Down Expand Up @@ -705,6 +706,7 @@ func Provider() *schema.Provider {
"aws_iam_user_login_profile": resourceAwsIamUserLoginProfile(),
"aws_imagebuilder_component": resourceAwsImageBuilderComponent(),
"aws_imagebuilder_distribution_configuration": resourceAwsImageBuilderDistributionConfiguration(),
"aws_imagebuilder_infrastructure_configuration": resourceAwsImageBuilderInfrastructureConfiguration(),
"aws_inspector_assessment_target": resourceAWSInspectorAssessmentTarget(),
"aws_inspector_assessment_template": resourceAWSInspectorAssessmentTemplate(),
"aws_inspector_resource_group": resourceAWSInspectorResourceGroup(),
Expand Down
Loading

0 comments on commit 98d9708

Please sign in to comment.