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_image_recipes - new data source #21814

Merged
merged 8 commits into from
Nov 19, 2021
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/21814.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_imagebuilder_image_recipes
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ func Provider() *schema.Provider {
"aws_imagebuilder_image": imagebuilder.DataSourceImage(),
"aws_imagebuilder_image_pipeline": imagebuilder.DataSourceImagePipeline(),
"aws_imagebuilder_image_recipe": imagebuilder.DataSourceImageRecipe(),
"aws_imagebuilder_image_recipes": imagebuilder.DataSourceImageRecipes(),
"aws_imagebuilder_infrastructure_configuration": imagebuilder.DataSourceInfrastructureConfiguration(),

"aws_inspector_rules_packages": inspector.DataSourceRulesPackages(),
Expand Down
44 changes: 44 additions & 0 deletions internal/service/imagebuilder/filters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package imagebuilder

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/imagebuilder"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func buildFiltersDataSource(set *schema.Set) []*imagebuilder.Filter {
var filters []*imagebuilder.Filter
for _, v := range set.List() {
m := v.(map[string]interface{})
var filterValues []*string
for _, e := range m["values"].([]interface{}) {
filterValues = append(filterValues, aws.String(e.(string)))
}
filters = append(filters, &imagebuilder.Filter{
Name: aws.String(m["name"].(string)),
Values: filterValues,
})
}
return filters
}

func dataSourceFiltersSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"values": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
}
}
84 changes: 84 additions & 0 deletions internal/service/imagebuilder/image_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 DataSourceImageRecipes() *schema.Resource {
return &schema.Resource{
Read: dataSourceImageRecipesRead,
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 dataSourceImageRecipesRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).ImageBuilderConn

input := &imagebuilder.ListImageRecipesInput{}

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.ImageRecipeSummary

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

for _, imageRecipeSummary := range page.ImageRecipeSummaryList {
if imageRecipeSummary == nil {
continue
}

results = append(results, imageRecipeSummary)
}

return !lastPage
})

if err != nil {
return fmt.Errorf("error reading Image Builder Image 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
}
143 changes: 143 additions & 0 deletions internal/service/imagebuilder/image_recipes_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
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 TestAccImageBuilderImageRecipesDataSource_owner(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
dataSourceNameOwnerAmazon := "data.aws_imagebuilder_image_recipes.amazon"
dataSourceNameOwnerSelf := "data.aws_imagebuilder_image_recipes.self"
resourceName := "aws_imagebuilder_image_recipe.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, imagebuilder.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccCheckImageRecipeDestroy,
Steps: []resource.TestStep{
{
Config: testAccImageRecipesOwnerDataSourceConfig(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceNameOwnerAmazon, "arns.#", "0"),
resource.TestCheckResourceAttr(dataSourceNameOwnerAmazon, "names.#", "0"),
resource.TestCheckResourceAttr(dataSourceNameOwnerSelf, "arns.#", "1"),
resource.TestCheckResourceAttr(dataSourceNameOwnerSelf, "names.#", "1"),
resource.TestCheckResourceAttrPair(dataSourceNameOwnerSelf, "arns.0", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceNameOwnerSelf, "names.0", resourceName, "name"),
),
},
},
})
}

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, imagebuilder.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccCheckImageRecipeDestroy,
Steps: []resource.TestStep{
{
Config: testAccImageRecipesFilterDataSourceConfig(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "names.#", "1"),
resource.TestCheckResourceAttrPair(dataSourceName, "names.0", resourceName, "name"),
),
},
},
})
}

func testAccImageRecipeDataSourceBaseConfig(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"
}
`, rName)
}

func testAccImageRecipesOwnerDataSourceConfig(rName string) string {
return acctest.ConfigCompose(
testAccImageRecipeDataSourceBaseConfig(rName),
fmt.Sprintf(`
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_recipes" "amazon" {
owner = "Amazon"

depends_on = [
aws_imagebuilder_image_recipe.test
]
}

data "aws_imagebuilder_image_recipes" "self" {
owner = "Self"

depends_on = [
aws_imagebuilder_image_recipe.test
]
}
`, rName))
}

func testAccImageRecipesFilterDataSourceConfig(rName string) string {
return acctest.ConfigCompose(
testAccImageRecipeDataSourceBaseConfig(rName),
fmt.Sprintf(`
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_recipes" "test" {
filter {
name = "name"
values = [aws_imagebuilder_image_recipe.test.name]
}
}
`, rName))
}
41 changes: 41 additions & 0 deletions website/docs/d/imagebuilder_image_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_image_recipes"
description: |-
Get information on Image Builder Image Recipes.
---

# Data Source: aws_imagebuilder_image_recipes

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

## Example Usage

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

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

## Argument Reference

* `owner` - (Optional) The owner of the image 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 ListImageRecipes API Reference](https://docs.aws.amazon.com/imagebuilder/latest/APIReference/API_ListImageRecipes.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 Image Recipes.
* `names` - Set of names of the matched Image Builder Image Recipes.