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

d/aws_imagebuilder_container_recipes - new data source #23134

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .changelog/23134.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_imagebuilder_container_recipes
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ func Provider() *schema.Provider {
"aws_imagebuilder_component": imagebuilder.DataSourceComponent(),
"aws_imagebuilder_components": imagebuilder.DataSourceComponents(),
"aws_imagebuilder_container_recipe": imagebuilder.DataSourceContainerRecipe(),
"aws_imagebuilder_container_recipes": imagebuilder.DataSourceContainerRecipes(),
"aws_imagebuilder_distribution_configuration": imagebuilder.DataSourceDistributionConfiguration(),
"aws_imagebuilder_distribution_configurations": imagebuilder.DataSourceDistributionConfigurations(),
"aws_imagebuilder_image": imagebuilder.DataSourceImage(),
Expand Down
84 changes: 84 additions & 0 deletions internal/service/imagebuilder/container_recipes_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
)

func DataSourceContainerRecipes() *schema.Resource {
return &schema.Resource{
Read: dataSourceContainerRecipesRead,
Schema: map[string]*schema.Schema{
"arns": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"filter": dataSourceFiltersSchema(),
"names": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"owner": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"Self", "Shared", "Amazon"}, false),
},
},
}
}

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

input := &imagebuilder.ListContainerRecipesInput{}

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

if v, ok := d.GetOk("filter"); ok {
input.Filters = buildFiltersDataSource(v.(*schema.Set))
}

var results []*imagebuilder.ContainerRecipeSummary

err := conn.ListContainerRecipesPages(input, func(page *imagebuilder.ListContainerRecipesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, containerRecipeSummary := range page.ContainerRecipeSummaryList {
if containerRecipeSummary == nil {
continue
}

results = append(results, containerRecipeSummary)
}

return !lastPage
})

if err != nil {
return fmt.Errorf("error reading Image Builder Container Recipes: %w", err)
}

var arns, names []string

for _, r := range results {
arns = append(arns, aws.StringValue(r.Arn))
names = append(names, aws.StringValue(r.Name))
}

d.SetId(meta.(*conns.AWSClient).Region)
d.Set("arns", arns)
d.Set("names", names)

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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 TestAccImageBuilderContainerRecipesDataSource_filter(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
dataSourceName := "data.aws_imagebuilder_container_recipes.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: testAccContainerRecipesFilterDataSourceConfig(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "names.#", "1"),
resource.TestCheckResourceAttrPair(dataSourceName, "names.0", resourceName, "name"),
),
},
},
})
}

func testAccContainerRecipesFilterDataSourceConfig(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_recipes" "test" {
filter {
name = "name"
values = [aws_imagebuilder_container_recipe.test.name]
}
}
`, rName)
}
41 changes: 41 additions & 0 deletions website/docs/d/imagebuilder_container_recipes.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
subcategory: "Image Builder"
layout: "aws"
page_title: "AWS: aws_imagebuilder_container_recipes"
description: |-
Get information on Image Builder Container Recipes.
---

# Data Source: aws_imagebuilder_container_recipes

Use this data source to get the ARNs and names of Image Builder Container Recipes matching the specified criteria.

## Example Usage

```terraform
data "aws_imagebuilder_container_recipes" "example" {
owner = "Self"

filter {
name = "platform"
values = ["Linux"]
}
}
```

## Argument Reference

* `owner` - (Optional) The owner of the container recipes. Valid values are `Self`, `Shared` and `Amazon`. Defaults to `Self`.
* `filter` - (Optional) Configuration block(s) for filtering. Detailed below.

### filter Configuration Block

The following arguments are supported by the `filter` configuration block:

* `name` - (Required) The name of the filter field. Valid values can be found in the [Image Builder ListContainerRecipes API Reference](https://docs.aws.amazon.com/imagebuilder/latest/APIReference/API_ListContainerRecipes.html).
* `values` - (Required) Set of values that are accepted for the given filter field. Results will be selected if any given value matches.

## Attributes Reference

* `arns` - Set of ARNs of the matched Image Builder Container Recipes.
* `names` - Set of names of the matched Image Builder Container Recipes.