Skip to content

Commit

Permalink
Merge pull request #23040 from kamilturek/f-data-aws-imagebuilder-con…
Browse files Browse the repository at this point in the history
…tainer-recipe

d/aws_imagebuilder_container_recipe - new data source
  • Loading branch information
ewbankkit authored Feb 11, 2022
2 parents e4a6748 + 6471c93 commit d7da73b
Show file tree
Hide file tree
Showing 5 changed files with 395 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/23040.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_imagebuilder_container_recipe
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ func Provider() *schema.Provider {

"aws_imagebuilder_component": imagebuilder.DataSourceComponent(),
"aws_imagebuilder_components": imagebuilder.DataSourceComponents(),
"aws_imagebuilder_container_recipe": imagebuilder.DataSourceContainerRecipe(),
"aws_imagebuilder_distribution_configuration": imagebuilder.DataSourceDistributionConfiguration(),
"aws_imagebuilder_distribution_configurations": imagebuilder.DataSourceDistributionConfigurations(),
"aws_imagebuilder_image": imagebuilder.DataSourceImage(),
Expand Down
236 changes: 236 additions & 0 deletions internal/service/imagebuilder/container_recipe_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package imagebuilder

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/hashicorp/terraform-provider-aws/internal/conns"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

func DataSourceContainerRecipe() *schema.Resource {
return &schema.Resource{
Read: dataSourceContainerRecipeRead,
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: verify.ValidARN,
},
"component": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"component_arn": {
Type: schema.TypeString,
Computed: true,
},
"parameter": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"value": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
"container_type": {
Type: schema.TypeString,
Computed: true,
},
"date_created": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"dockerfile_template_data": {
Type: schema.TypeString,
Computed: true,
},
"encrypted": {
Type: schema.TypeBool,
Computed: true,
},
"instance_configuration": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"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,
},
},
},
},
"image": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"kms_key_id": {
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": tftags.TagsSchema(),
"target_repository": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"repository_name": {
Type: schema.TypeString,
Computed: true,
},
"service": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"version": {
Type: schema.TypeString,
Computed: true,
},
"working_directory": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

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

input := &imagebuilder.GetContainerRecipeInput{}

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

output, err := conn.GetContainerRecipe(input)

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

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

containerRecipe := output.ContainerRecipe

d.SetId(aws.StringValue(containerRecipe.Arn))
d.Set("arn", containerRecipe.Arn)
d.Set("component", flattenComponentConfigurations(containerRecipe.Components))
d.Set("container_type", containerRecipe.ContainerType)
d.Set("date_created", containerRecipe.DateCreated)
d.Set("description", containerRecipe.Description)
d.Set("dockerfile_template_data", containerRecipe.DockerfileTemplateData)
d.Set("encrypted", containerRecipe.Encrypted)

if containerRecipe.InstanceConfiguration != nil {
d.Set("instance_configuration", []interface{}{flattenInstanceConfiguration(containerRecipe.InstanceConfiguration)})
} else {
d.Set("instance_configuration", nil)
}

d.Set("kms_key_id", containerRecipe.KmsKeyId)
d.Set("name", containerRecipe.Name)
d.Set("owner", containerRecipe.Owner)
d.Set("parent_image", containerRecipe.ParentImage)
d.Set("platform", containerRecipe.Platform)
d.Set("tags", KeyValueTags(containerRecipe.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map())
d.Set("target_repository", []interface{}{flattenTargetContainerRepository(containerRecipe.TargetRepository)})
d.Set("version", containerRecipe.Version)
d.Set("working_directory", containerRecipe.WorkingDirectory)

return nil
}
90 changes: 90 additions & 0 deletions internal/service/imagebuilder/container_recipe_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package imagebuilder_test

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/imagebuilder"
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
)

func TestAccImageBuilderContainerRecipeDataSource_arn(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
dataSourceName := "data.aws_imagebuilder_container_recipe.test"
resourceName := "aws_imagebuilder_container_recipe.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, imagebuilder.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccCheckContainerRecipeDestroy,
Steps: []resource.TestStep{
{
Config: testAccContainerRecipeARNDataSourceConfig(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "component.#", resourceName, "component.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "component.0.component_arn", resourceName, "component.0.component_arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "component.0.parameter.#", resourceName, "component.0.parameter.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "container_type", resourceName, "container_type"),
resource.TestCheckResourceAttrPair(dataSourceName, "date_created", resourceName, "date_created"),
resource.TestCheckResourceAttrPair(dataSourceName, "description", resourceName, "description"),
resource.TestCheckResourceAttrPair(dataSourceName, "dockerfile_template_data", resourceName, "dockerfile_template_data"),
resource.TestCheckResourceAttrPair(dataSourceName, "encrypted", resourceName, "encrypted"),
resource.TestCheckResourceAttrPair(dataSourceName, "instance_configuration.#", resourceName, "instance_configuration.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "kms_key_id", resourceName, "kms_key_id"),
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, "target_repository.#", resourceName, "target_repository.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "target_repository.0.repository_name", resourceName, "target_repository.0.repository_name"),
resource.TestCheckResourceAttrPair(dataSourceName, "target_repository.0.service", resourceName, "target_repository.0.service"),
resource.TestCheckResourceAttrPair(dataSourceName, "version", resourceName, "version"),
resource.TestCheckResourceAttrPair(dataSourceName, "working_directory", resourceName, "working_directory"),
),
},
},
})
}

func testAccContainerRecipeARNDataSourceConfig(rName string) string {
return fmt.Sprintf(`
data "aws_region" "current" {}
data "aws_partition" "current" {}
resource "aws_ecr_repository" "test" {
name = %[1]q
}
resource "aws_imagebuilder_container_recipe" "test" {
name = %[1]q
container_type = "DOCKER"
parent_image = "arn:${data.aws_partition.current.partition}:imagebuilder:${data.aws_region.current.name}:aws:image/amazon-linux-x86-2/x.x.x"
version = "1.0.0"
component {
component_arn = "arn:${data.aws_partition.current.partition}:imagebuilder:${data.aws_region.current.name}:aws:component/update-linux/x.x.x"
}
dockerfile_template_data = <<EOF
FROM {{{ imagebuilder:parentImage }}}
{{{ imagebuilder:environments }}}
{{{ imagebuilder:components }}}
EOF
target_repository {
repository_name = aws_ecr_repository.test.name
service = "ECR"
}
}
data "aws_imagebuilder_container_recipe" "test" {
arn = aws_imagebuilder_container_recipe.test.arn
}
`, rName)
}
Loading

0 comments on commit d7da73b

Please sign in to comment.