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

New Resource: aws_imagebuilder_image_recipe #16218

Merged
merged 29 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
96b4232
Add aws_imagebuilder_component
Dogers Feb 21, 2020
2d588c2
Add test for data_source_aws_imagebuilder_component
Dogers Feb 22, 2020
a5d9f8f
Add aws_imagebuilder_infrastructureconfiguration
Dogers Feb 22, 2020
6414616
Add aws_imagebuilder_recipe
Dogers Mar 3, 2020
c29b5da
Rename to aws_imagebuilder_infrastructure_configuration
Dogers May 6, 2020
8b53767
Fix issue with infraconfig logging
Dogers May 25, 2020
8ce7b6b
Fix incorrect MaxItems on res
Dogers May 25, 2020
fbe7510
Fix S024
Dogers May 25, 2020
28dfb17
Fix AWSR002
Dogers May 25, 2020
507fe42
Fix linting
Dogers May 25, 2020
ca89e2b
[#11084] Fix the imagebuilder resource, add test and docs
blckct Jul 12, 2020
195ba8a
Merge remote-tracking branch 'source/master' into image-builder
Dogers Aug 2, 2020
0d050cd
Merge branch 'master' into image-builder
Dogers Aug 19, 2020
26e1bb9
Fix incorrect import on error handling
Dogers Aug 19, 2020
df7016f
Update to v2 SDK
Dogers Aug 19, 2020
89e7bdd
Fix R004 lint issue
Dogers Aug 19, 2020
18dd1b0
Remove changes to aws.erb
Dogers Aug 19, 2020
1b85e94
Add imagebuilder_distribution_configuration
Dogers Aug 2, 2020
eb8e1ed
Add imagebuilder_image_pipeline
Dogers Aug 25, 2020
0402fe7
fixed some lint issues and fixed terminate_instance_on_failure not be…
wrschneider Aug 28, 2020
a20d120
description can be updated in place
wrschneider Aug 28, 2020
10c55b6
Merge pull request #1 from wrschneider/image-builder
Dogers Aug 28, 2020
7cfe977
Merge branch 'image-builder' of ssh://github.com/Dogers/terraform-pro…
bflad Nov 12, 2020
cd14a07
service/imagebuilder: Remove non-aws_imagebuilder_image_recipe
bflad Nov 13, 2020
0b2c308
service/imagebuilder: Fix incorrectly named resource files and functi…
bflad Nov 13, 2020
81ceabf
New Resource: aws_imagebuilder_image_recipe
bflad Nov 16, 2020
013f185
tests/service/imagebuilder: terrafmt fixes
bflad Nov 17, 2020
fea46f2
Merge branch 'master' into f-aws_imagebuilder_image_recipe
bflad Nov 18, 2020
fae63a9
Update aws/resource_aws_imagebuilder_image_recipe_test.go
bflad Nov 24, 2020
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
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))
}
Comment on lines +129 to +131
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check, since arn is required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment applies, I think. #16180 (comment)


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 @@ -706,6 +707,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