Skip to content

Commit

Permalink
New Resource: aws_imagebuilder_image_recipe (#16218)
Browse files Browse the repository at this point in the history
Reference: #11084
Reference: #13485
Reference: #16169 (required for `aws_imagebuilder_component` resource implementation)

Changes:

```
* **New Data Source:** `aws_imagebuilder_image_recipe`
* **New Resource:** `aws_imagebuilder_image_recipe`
```

Output from acceptance testing in AWS Commercial:

```
--- PASS: TestAccAwsImageBuilderImageRecipe_basic (38.20s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_DeviceName (35.14s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_Ebs_DeleteOnTermination (34.64s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_Ebs_Encrypted (33.35s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_Ebs_Iops (36.82s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_Ebs_KmsKeyId (35.17s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_Ebs_SnapshotId (57.27s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_Ebs_VolumeSize (35.26s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_Ebs_VolumeType (37.01s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_NoDevice (35.18s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_VirtualName (34.55s)
--- PASS: TestAccAwsImageBuilderImageRecipe_Description (34.98s)
--- PASS: TestAccAwsImageBuilderImageRecipe_disappears (31.67s)
--- PASS: TestAccAwsImageBuilderImageRecipe_Tags (59.07s)

--- PASS: TestAccAwsImageBuilderImageRecipeDataSource_Arn (19.90s)
```

Output from acceptance testing in AWS GovCloud (US):

```
--- PASS: TestAccAwsImageBuilderImageRecipe_basic (25.73s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_DeviceName (39.43s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_Ebs_DeleteOnTermination (39.12s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_Ebs_Encrypted (39.21s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_Ebs_Iops (39.20s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_Ebs_KmsKeyId (39.29s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_Ebs_SnapshotId (59.68s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_Ebs_VolumeSize (39.29s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_Ebs_VolumeType (38.94s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_NoDevice (38.10s)
--- PASS: TestAccAwsImageBuilderImageRecipe_BlockDeviceMapping_VirtualName (39.09s)
--- PASS: TestAccAwsImageBuilderImageRecipe_Description (38.84s)
--- PASS: TestAccAwsImageBuilderImageRecipe_disappears (34.09s)
--- PASS: TestAccAwsImageBuilderImageRecipe_Tags (68.88s)

--- PASS: TestAccAwsImageBuilderImageRecipeDataSource_Arn (24.71s)
```

Output from sweeper in AWS Commercial:

```
2020/11/16 15:31:54 [DEBUG] Running Sweepers for region (us-west-2):
2020/11/16 15:31:54 [DEBUG] Running Sweeper (aws_imagebuilder_image_recipe) in region (us-west-2)
2020/11/16 15:31:56 Sweeper Tests ran successfully:
  - aws_imagebuilder_image_recipe
2020/11/16 15:31:56 [DEBUG] Running Sweepers for region (us-east-1):
2020/11/16 15:31:56 [DEBUG] Running Sweeper (aws_imagebuilder_image_recipe) in region (us-east-1)
2020/11/16 15:31:57 Sweeper Tests ran successfully:
  - aws_imagebuilder_image_recipe
ok    github.com/terraform-providers/terraform-provider-aws/aws 6.199s
```

Output from sweeper in AWS GovCloud (US):

```
2020/11/16 15:32:04 [DEBUG] Running Sweepers for region (us-gov-west-1):
2020/11/16 15:32:04 [DEBUG] Running Sweeper (aws_imagebuilder_image_recipe) in region (us-gov-west-1)
2020/11/16 15:32:06 Sweeper Tests ran successfully:
  - aws_imagebuilder_image_recipe
ok    github.com/terraform-providers/terraform-provider-aws/aws 4.799s
```

Co-authored-by: Dogers <Dogers@users.noreply.github.com>
Co-authored-by: Jakub Kania <jakubkania@blackcat.rocks>
Co-authored-by: Bill Schneider <bill.schneider@optum.com>
Co-authored-by: Graham Davison <gdavison@hashicorp.com>
  • Loading branch information
5 people authored Nov 24, 2020
1 parent 6bbb20f commit 9b3a68d
Show file tree
Hide file tree
Showing 7 changed files with 1,796 additions and 0 deletions.
159 changes: 159 additions & 0 deletions aws/data_source_aws_imagebuilder_image_recipe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
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 dataSourceAwsImageBuilderImageRecipe() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsImageBuilderImageRecipeRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},
"block_device_mapping": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"device_name": {
Type: schema.TypeString,
Computed: true,
},
"ebs": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"delete_on_termination": {
Type: schema.TypeBool,
Computed: true,
},
"encrypted": {
Type: schema.TypeBool,
Computed: true,
},
"iops": {
Type: schema.TypeInt,
Computed: true,
},
"kms_key_id": {
Type: schema.TypeString,
Computed: true,
},
"snapshot_id": {
Type: schema.TypeString,
Computed: true,
},
"volume_size": {
Type: schema.TypeInt,
Computed: true,
},
"volume_type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"no_device": {
Type: schema.TypeString,
Computed: true,
},
"virtual_name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"component": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"component_arn": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"date_created": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"owner": {
Type: schema.TypeString,
Computed: true,
},
"parent_image": {
Type: schema.TypeString,
Computed: true,
},
"platform": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
"version": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

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

input := &imagebuilder.GetImageRecipeInput{}

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

output, err := conn.GetImageRecipe(input)

if err != nil {
return fmt.Errorf("error reading Image Builder Image Recipe (%s): %w", aws.StringValue(input.ImageRecipeArn), err)
}

if output == nil || output.ImageRecipe == nil {
return fmt.Errorf("error reading Image Builder Image Recipe (%s): empty response", aws.StringValue(input.ImageRecipeArn))
}

imageRecipe := output.ImageRecipe

d.SetId(aws.StringValue(imageRecipe.Arn))
d.Set("arn", imageRecipe.Arn)
d.Set("block_device_mapping", flattenImageBuilderInstanceBlockDeviceMappings(imageRecipe.BlockDeviceMappings))
d.Set("component", flattenImageBuilderComponentConfigurations(imageRecipe.Components))
d.Set("date_created", imageRecipe.DateCreated)
d.Set("description", imageRecipe.Description)
d.Set("name", imageRecipe.Name)
d.Set("owner", imageRecipe.Owner)
d.Set("parent_image", imageRecipe.ParentImage)
d.Set("platform", imageRecipe.Platform)
d.Set("tags", keyvaluetags.ImagebuilderKeyValueTags(imageRecipe.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map())
d.Set("version", imageRecipe.Version)

return nil
}
81 changes: 81 additions & 0 deletions aws/data_source_aws_imagebuilder_image_recipe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package aws

import (
"fmt"
"testing"

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

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckAwsImageBuilderImageRecipeDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsImageBuilderImageRecipeDataSourceConfigArn(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "block_device_mapping.#", resourceName, "block_device_mapping.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "component.#", resourceName, "component.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "date_created", resourceName, "date_created"),
resource.TestCheckResourceAttrPair(dataSourceName, "description", resourceName, "description"),
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(dataSourceName, "owner", resourceName, "owner"),
resource.TestCheckResourceAttrPair(dataSourceName, "parent_image", resourceName, "parent_image"),
resource.TestCheckResourceAttrPair(dataSourceName, "platform", resourceName, "platform"),
resource.TestCheckResourceAttrPair(dataSourceName, "tags.%", resourceName, "tags.%"),
resource.TestCheckResourceAttrPair(dataSourceName, "version", resourceName, "version"),
),
},
},
})
}

func testAccAwsImageBuilderImageRecipeDataSourceConfigArn(rName string) string {
return fmt.Sprintf(`
data "aws_region" "current" {}
data "aws_partition" "current" {}
resource "aws_imagebuilder_component" "test" {
data = yamlencode({
phases = [{
name = "build"
steps = [{
action = "ExecuteBash"
inputs = {
commands = ["echo 'hello world'"]
}
name = "example"
onFailure = "Continue"
}]
}]
schemaVersion = 1.0
})
name = %[1]q
platform = "Linux"
version = "1.0.0"
}
resource "aws_imagebuilder_image_recipe" "test" {
component {
component_arn = aws_imagebuilder_component.test.arn
}
name = %[1]q
parent_image = "arn:${data.aws_partition.current.partition}:imagebuilder:${data.aws_region.current.name}:aws:image/amazon-linux-2-x86/x.x.x"
version = "1.0.0"
}
data "aws_imagebuilder_image_recipe" "test" {
arn = aws_imagebuilder_image_recipe.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_image_recipe": dataSourceAwsImageBuilderImageRecipe(),
"aws_imagebuilder_infrastructure_configuration": datasourceAwsImageBuilderInfrastructureConfiguration(),
"aws_internet_gateway": dataSourceAwsInternetGateway(),
"aws_iot_endpoint": dataSourceAwsIotEndpoint(),
Expand Down Expand Up @@ -709,6 +710,7 @@ func Provider() *schema.Provider {
"aws_iam_user_login_profile": resourceAwsIamUserLoginProfile(),
"aws_imagebuilder_component": resourceAwsImageBuilderComponent(),
"aws_imagebuilder_distribution_configuration": resourceAwsImageBuilderDistributionConfiguration(),
"aws_imagebuilder_image_recipe": resourceAwsImageBuilderImageRecipe(),
"aws_imagebuilder_infrastructure_configuration": resourceAwsImageBuilderInfrastructureConfiguration(),
"aws_inspector_assessment_target": resourceAWSInspectorAssessmentTarget(),
"aws_inspector_assessment_template": resourceAWSInspectorAssessmentTemplate(),
Expand Down
Loading

0 comments on commit 9b3a68d

Please sign in to comment.