From 05360e23cdb1b7cb372f7706a1ef8e86e9919e2c Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sun, 30 Jan 2022 12:34:55 +0100 Subject: [PATCH 1/8] Add parameter argument --- internal/service/imagebuilder/image_recipe.go | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/internal/service/imagebuilder/image_recipe.go b/internal/service/imagebuilder/image_recipe.go index ac813d8426d..212f10fde0b 100644 --- a/internal/service/imagebuilder/image_recipe.go +++ b/internal/service/imagebuilder/image_recipe.go @@ -134,6 +134,23 @@ func ResourceImageRecipe() *schema.Resource { Required: true, ValidateFunc: verify.ValidARN, }, + "parameter": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringLenBetween(1, 256), + }, + "value": { + Type: schema.TypeString, + Required: true, + }, + }, + }, + }, }, }, }, @@ -367,6 +384,56 @@ func expandComponentConfiguration(tfMap map[string]interface{}) *imagebuilder.Co apiObject.ComponentArn = aws.String(v) } + if v, ok := tfMap["parameter"].(*schema.Set); ok && v.Len() > 0 { + apiObject.Parameters = expandComponentParameters(v.List()) + } + + return apiObject +} + +func expandComponentParameters(tfList []interface{}) []*imagebuilder.ComponentParameter { + if len(tfList) == 0 { + return nil + } + + var apiObjects []*imagebuilder.ComponentParameter + + for _, tfMapRaw := range tfList { + tfMap, ok := tfMapRaw.(map[string]interface{}) + + if !ok { + continue + } + + apiObject := expandComponentParameter(tfMap) + + if apiObject == nil { + continue + } + + apiObjects = append(apiObjects, apiObject) + } + + return apiObjects +} + +func expandComponentParameter(tfMap map[string]interface{}) *imagebuilder.ComponentParameter { + if tfMap == nil { + return nil + } + + apiObject := &imagebuilder.ComponentParameter{} + + if v, ok := tfMap["name"].(string); ok && v != "" { + apiObject.Name = aws.String(v) + } + + if v, ok := tfMap["value"].(string); ok { + // ImageBuilder API quirk + // Even though Value is a slice, only one element is accepted. + apiObject.Value = aws.StringSlice([]string{v}) + } + return apiObject } @@ -499,6 +566,48 @@ func flattenComponentConfiguration(apiObject *imagebuilder.ComponentConfiguratio tfMap["component_arn"] = aws.StringValue(v) } + if v := apiObject.Parameters; v != nil { + tfMap["parameter"] = flattenComponentParameters(v) + } + + return tfMap +} + +func flattenComponentParameters(apiObjects []*imagebuilder.ComponentParameter) []interface{} { + if len(apiObjects) == 0 { + return nil + } + + var tfList []interface{} + + for _, apiObject := range apiObjects { + if apiObject == nil { + continue + } + + tfList = append(tfList, flattenComponentParameter(apiObject)) + } + + return tfList +} + +func flattenComponentParameter(apiObject *imagebuilder.ComponentParameter) map[string]interface{} { + if apiObject == nil { + return nil + } + + tfMap := map[string]interface{}{} + + if v := apiObject.Name; v != nil { + tfMap["name"] = aws.StringValue(v) + } + + if v := apiObject.Value; v != nil { + // ImageBuilder API quirk + // Even though Value is a slice, only first element is used. + tfMap["value"] = aws.StringValueSlice(v)[0] + } + return tfMap } From de96b35120f9d6bb854773b69355fa8addf7718e Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sun, 30 Jan 2022 12:35:24 +0100 Subject: [PATCH 2/8] Add acceptance tests --- .../service/imagebuilder/image_recipe_test.go | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/internal/service/imagebuilder/image_recipe_test.go b/internal/service/imagebuilder/image_recipe_test.go index 97e64a4654d..0f139b91910 100644 --- a/internal/service/imagebuilder/image_recipe_test.go +++ b/internal/service/imagebuilder/image_recipe_test.go @@ -421,6 +421,32 @@ func TestAccImageBuilderImageRecipe_component(t *testing.T) { }) } +func TestAccImageBuilderImageRecipe_componentParameter(t *testing.T) { + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_imagebuilder_image_recipe.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ErrorCheck: acctest.ErrorCheck(t, imagebuilder.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckImageRecipeDestroy, + Steps: []resource.TestStep{ + { + Config: testAccImageRecipeComponentParameterConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckImageRecipeExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "component.#", "1"), + resource.TestCheckResourceAttr(resourceName, "component.0.parameter.#", "2"), + resource.TestCheckResourceAttr(resourceName, "component.0.parameter.0.name", "Parameter1"), + resource.TestCheckResourceAttr(resourceName, "component.0.parameter.0.value", "Value1"), + resource.TestCheckResourceAttr(resourceName, "component.0.parameter.1.name", "Parameter2"), + resource.TestCheckResourceAttr(resourceName, "component.0.parameter.1.value", "Value2"), + ), + }, + }, + }) +} + func TestAccImageBuilderImageRecipe_description(t *testing.T) { rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_imagebuilder_image_recipe.test" @@ -910,6 +936,58 @@ resource "aws_imagebuilder_image_recipe" "test" { `, rName)) } +func testAccImageRecipeComponentParameterConfig(rName string) string { + return fmt.Sprintf(` +data "aws_region" "current" {} + +data "aws_partition" "current" {} + +resource "aws_imagebuilder_component" "test" { + data = < Date: Sun, 30 Jan 2022 15:05:24 +0100 Subject: [PATCH 3/8] Amend expand function --- internal/service/imagebuilder/image_recipe.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/imagebuilder/image_recipe.go b/internal/service/imagebuilder/image_recipe.go index 212f10fde0b..3a04a3e8fe2 100644 --- a/internal/service/imagebuilder/image_recipe.go +++ b/internal/service/imagebuilder/image_recipe.go @@ -428,7 +428,7 @@ func expandComponentParameter(tfMap map[string]interface{}) *imagebuilder.Compon apiObject.Name = aws.String(v) } - if v, ok := tfMap["value"].(string); ok { + if v, ok := tfMap["value"].(string); ok && v != "" { // ImageBuilder API quirk // Even though Value is a slice, only one element is accepted. apiObject.Value = aws.StringSlice([]string{v}) From 45049edd502ce0b640ffa4cf5cbd47e88ff0e694 Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sun, 30 Jan 2022 15:07:06 +0100 Subject: [PATCH 4/8] Add changelog --- .changelog/22837.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/22837.txt diff --git a/.changelog/22837.txt b/.changelog/22837.txt new file mode 100644 index 00000000000..ddd73962697 --- /dev/null +++ b/.changelog/22837.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_imagebuilder_image_recipe: Add support for component parameters +``` From ede9fe62e8b3e31136a25be26988a4b3cecd057e Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sun, 30 Jan 2022 15:25:12 +0100 Subject: [PATCH 5/8] Update docs --- .../r/imagebuilder_image_recipe.html.markdown | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/website/docs/r/imagebuilder_image_recipe.html.markdown b/website/docs/r/imagebuilder_image_recipe.html.markdown index 69b852903dc..4e902b5193c 100644 --- a/website/docs/r/imagebuilder_image_recipe.html.markdown +++ b/website/docs/r/imagebuilder_image_recipe.html.markdown @@ -26,6 +26,16 @@ resource "aws_imagebuilder_image_recipe" "example" { component { component_arn = aws_imagebuilder_component.example.arn + + parameter { + name = "Parameter1" + value = "Value1" + } + + parameter { + name = "Parameter2 + value = "Value2" + } } name = "example" @@ -74,9 +84,17 @@ The following arguments are optional: ### component -The following arguments are required: +The `component` block supports the following arguments: * `component_arn` - (Required) Amazon Resource Name (ARN) of the Image Builder Component to associate. +* `parameter` - (Optional) Configuration block(s) for parameters to configure the component. Detailed below. + +### parameter + +The following arguments are required: + +* `name` - (Required) The name of the component parameter. +* `value` - (Required) The value for the named component parameter. ## Attributes Reference From 92d3c7f2e039fe7d00afa33c5c7b1d5626410227 Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sun, 30 Jan 2022 15:27:07 +0100 Subject: [PATCH 6/8] Unify comments --- internal/service/imagebuilder/image_recipe.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/imagebuilder/image_recipe.go b/internal/service/imagebuilder/image_recipe.go index 3a04a3e8fe2..a0b9e07dd4f 100644 --- a/internal/service/imagebuilder/image_recipe.go +++ b/internal/service/imagebuilder/image_recipe.go @@ -604,7 +604,7 @@ func flattenComponentParameter(apiObject *imagebuilder.ComponentParameter) map[s if v := apiObject.Value; v != nil { // ImageBuilder API quirk - // Even though Value is a slice, only first element is used. + // Even though Value is a slice, only one element is accepted. tfMap["value"] = aws.StringValueSlice(v)[0] } From 853aa56714ae73150d39fcadaa1d1d1b48227235 Mon Sep 17 00:00:00 2001 From: Kamil Turek Date: Sun, 30 Jan 2022 15:31:11 +0100 Subject: [PATCH 7/8] Fix terrafmt error in documentation --- website/docs/r/imagebuilder_image_recipe.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/imagebuilder_image_recipe.html.markdown b/website/docs/r/imagebuilder_image_recipe.html.markdown index 4e902b5193c..dbd55c4c865 100644 --- a/website/docs/r/imagebuilder_image_recipe.html.markdown +++ b/website/docs/r/imagebuilder_image_recipe.html.markdown @@ -33,7 +33,7 @@ resource "aws_imagebuilder_image_recipe" "example" { } parameter { - name = "Parameter2 + name = "Parameter2" value = "Value2" } } From 719ecdbea1e484f9b9368f7a4c2c44d2ac5cf253 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 31 Jan 2022 07:51:14 -0500 Subject: [PATCH 8/8] Update 22837.txt --- .changelog/22837.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changelog/22837.txt b/.changelog/22837.txt index ddd73962697..911b55f42e5 100644 --- a/.changelog/22837.txt +++ b/.changelog/22837.txt @@ -1,3 +1,3 @@ ```release-note:enhancement -resource/aws_imagebuilder_image_recipe: Add support for component parameters +resource/aws_imagebuilder_image_recipe: Add `parameter` argument to the `component` configuration block ```