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

r/aws_imagebuilder_distribution_configuration - add support for launch template configurations #22842

Merged
Show file tree
Hide file tree
Changes from 4 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/22842.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_imagebuilder_distribution_configuration: Add `launch_template_configuration` argument
```
107 changes: 107 additions & 0 deletions internal/service/imagebuilder/distribution_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,25 @@ func ResourceDistributionConfiguration() *schema.Resource {
},
},
},
"launch_template_configuration": {
Type: schema.TypeSet,
Optional: true,
MaxItems: 100,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"default": {
Type: schema.TypeBool,
Optional: true,
Default: true,
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
},
"launch_template_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: verify.ValidLaunchTemplateID,
},
},
},
},
"license_configuration_arns": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -380,6 +399,32 @@ func expandContainerDistributionConfiguration(tfMap map[string]interface{}) *ima
return apiObject
}

func expandLaunchTemplateConfigurations(tfList []interface{}) []*imagebuilder.LaunchTemplateConfiguration {
if len(tfList) == 0 {
return nil
}

var apiObjects []*imagebuilder.LaunchTemplateConfiguration

for _, tfMapRaw := range tfList {
tfMap, ok := tfMapRaw.(map[string]interface{})

if !ok {
continue
}

apiObject := expandLaunchTemplateConfiguration(tfMap)

if apiObject == nil {
continue
}

apiObjects = append(apiObjects, apiObject)
}

return apiObjects
}

func expandDistribution(tfMap map[string]interface{}) *imagebuilder.Distribution {
if tfMap == nil {
return nil
Expand All @@ -395,6 +440,10 @@ func expandDistribution(tfMap map[string]interface{}) *imagebuilder.Distribution
apiObject.ContainerDistributionConfiguration = expandContainerDistributionConfiguration(v[0].(map[string]interface{}))
}

if v, ok := tfMap["launch_template_configuration"].(*schema.Set); ok && v.Len() > 0 {
apiObject.LaunchTemplateConfigurations = expandLaunchTemplateConfigurations(v.List())
}

if v, ok := tfMap["license_configuration_arns"].(*schema.Set); ok && v.Len() > 0 {
apiObject.LicenseConfigurationArns = flex.ExpandStringSet(v)
}
Expand Down Expand Up @@ -475,6 +524,24 @@ func expandTargetContainerRepository(tfMap map[string]interface{}) *imagebuilder
return apiObject
}

func expandLaunchTemplateConfiguration(tfMap map[string]interface{}) *imagebuilder.LaunchTemplateConfiguration {
if tfMap == nil {
return nil
}

apiObject := &imagebuilder.LaunchTemplateConfiguration{}

if v, ok := tfMap["launch_template_id"].(string); ok && v != "" {
apiObject.LaunchTemplateId = aws.String(v)
}

if v, ok := tfMap["default"].(bool); ok {
apiObject.SetDefaultVersion = aws.Bool(v)
}

return apiObject
}

func flattenAMIDistributionConfiguration(apiObject *imagebuilder.AmiDistributionConfiguration) map[string]interface{} {
if apiObject == nil {
return nil
Expand Down Expand Up @@ -531,6 +598,24 @@ func flattenContainerDistributionConfiguration(apiObject *imagebuilder.Container
return tfMap
}

func flattenLaunchTemplateConfigurations(apiObjects []*imagebuilder.LaunchTemplateConfiguration) []interface{} {
if apiObjects == nil {
return nil
}

var tfList []interface{}

for _, apiObject := range apiObjects {
if apiObject == nil {
continue
}

tfList = append(tfList, flattenLaunchTemplateConfiguration(apiObject))
}

return tfList
}

func flattenDistribution(apiObject *imagebuilder.Distribution) map[string]interface{} {
if apiObject == nil {
return nil
Expand All @@ -546,6 +631,10 @@ func flattenDistribution(apiObject *imagebuilder.Distribution) map[string]interf
tfMap["container_distribution_configuration"] = []interface{}{flattenContainerDistributionConfiguration(v)}
}

if v := apiObject.LaunchTemplateConfigurations; v != nil {
tfMap["launch_template_configuration"] = flattenLaunchTemplateConfigurations(v)
}

if v := apiObject.LicenseConfigurationArns; v != nil {
tfMap["license_configuration_arns"] = aws.StringValueSlice(v)
}
Expand Down Expand Up @@ -610,3 +699,21 @@ func flattenTargetContainerRepository(apiObject *imagebuilder.TargetContainerRep

return tfMap
}

func flattenLaunchTemplateConfiguration(apiObject *imagebuilder.LaunchTemplateConfiguration) map[string]interface{} {
if apiObject == nil {
return nil
}

tfMap := map[string]interface{}{}

if v := apiObject.LaunchTemplateId; v != nil {
tfMap["launch_template_id"] = aws.StringValue(v)
}

if v := apiObject.SetDefaultVersion; v != nil {
tfMap["default"] = aws.BoolValue(v)
}

return tfMap
}
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,47 @@ func TestAccImageBuilderDistributionConfiguration_DistributionContainerDistribut
})
}

func TestAccImageBuilderDistributionConfiguration_Distribution_launchTemplateConfiguration(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
launchTemplateResourceName := "aws_launch_template.test"
resourceName := "aws_imagebuilder_distribution_configuration.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, imagebuilder.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccCheckDistributionConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccDistributionConfigurationDistributionLaunchTemplateConfigurationLaunchTemplateIDDefaultConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDistributionConfigurationExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "distribution.#", "1"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.launch_template_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.launch_template_configuration.0.default", "true"),
resource.TestCheckResourceAttrPair(resourceName, "distribution.0.launch_template_configuration.0.launch_template_id", launchTemplateResourceName, "id"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccDistributionConfigurationDistributionLaunchTemplateConfigurationLaunchTemplateIDNonDefaultConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDistributionConfigurationExists(resourceName),
acctest.CheckResourceAttrRFC3339(resourceName, "date_updated"),
resource.TestCheckResourceAttr(resourceName, "distribution.#", "1"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.launch_template_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "distribution.0.launch_template_configuration.0.default", "false"),
resource.TestCheckResourceAttrPair(resourceName, "distribution.0.launch_template_configuration.0.launch_template_id", launchTemplateResourceName, "id"),
),
},
},
})
}

func TestAccImageBuilderDistributionConfiguration_Distribution_licenseARNs(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
licenseConfigurationResourceName := "aws_licensemanager_license_configuration.test"
Expand Down Expand Up @@ -925,6 +966,54 @@ resource "aws_imagebuilder_distribution_configuration" "test" {
`, rName, containerTag)
}

func testAccDistributionConfigurationDistributionLaunchTemplateConfigurationLaunchTemplateIDDefaultConfig(rName string) string {
return fmt.Sprintf(`
data "aws_region" "current" {}

resource "aws_launch_template" "test" {
instance_type = "t2.micro"
name = %[1]q
}

resource "aws_imagebuilder_distribution_configuration" "test" {
name = %[1]q

distribution {
launch_template_configuration {
default = true
launch_template_id = aws_launch_template.test.id
}

region = data.aws_region.current.name
}
}
`, rName)
}

func testAccDistributionConfigurationDistributionLaunchTemplateConfigurationLaunchTemplateIDNonDefaultConfig(rName string) string {
return fmt.Sprintf(`
data "aws_region" "current" {}

resource "aws_launch_template" "test" {
instance_type = "t2.micro"
name = %[1]q
}

resource "aws_imagebuilder_distribution_configuration" "test" {
name = %[1]q

distribution {
launch_template_configuration {
default = false
launch_template_id = aws_launch_template.test.id
}

region = data.aws_region.current.name
}
}
`, rName)
}

func testAccDistributionConfigurationDistributionLicenseConfigurationARNs1Config(rName string) string {
return fmt.Sprintf(`
data "aws_region" "current" {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ resource "aws_imagebuilder_distribution_configuration" "example" {

name = "example-{{ imagebuilder:buildDate }}"

launch_template_configuration {
launch_template_id = "lt-0aaa1bcde2ff3456"
}

launch_permission {
user_ids = ["123456789012"]
}
Expand Down Expand Up @@ -57,6 +61,7 @@ The following arguments are optional:

* `ami_distribution_configuration` - (Optional) Configuration block with Amazon Machine Image (AMI) distribution settings. Detailed below.
* `container_distribution_configuration` - (Optional) Configuration block with container distribution settings. Detailed below.
* `launch_template_configuration` - (Optional) Set of launch template configuration settings that apply to image distribution. Detailed below.
* `license_configuration_arns` - (Optional) Set of Amazon Resource Names (ARNs) of License Manager License Configurations.

### ami_distribution_configuration
Expand Down Expand Up @@ -88,6 +93,11 @@ The following arguments are optional:
* `repository_name` - (Required) The name of the container repository where the output container image is stored. This name is prefixed by the repository location.
* `service` - (Required) The service in which this image is registered. Valid values: `ECR`.

### launch_template_configuration

* `default` - (Optional) Indicates whether to set the specified Amazon EC2 launch template as the default launch template. Defaults to `true`.
* `launch_template_id` - (Required) The ID of the Amazon EC2 launch template to use.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:
Expand Down