Skip to content

Commit

Permalink
Merge pull request #25671 from kamilturek/f-aws-imagebuilder-distribu…
Browse files Browse the repository at this point in the history
…tion-configuration-fast-launch-configuration

rd/aws_imagebuilder_distribution_configuration - support fastLaunchConfigurations
  • Loading branch information
ewbankkit authored Jul 5, 2022
2 parents 7d6492e + 5a9dd4e commit 753227d
Show file tree
Hide file tree
Showing 7 changed files with 648 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changelog/25671.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_imagebuilder_distribution_configuration: Add `fast_launch_configuration` argument to the `distribution` configuration block
```

```release-note:enhancement
data-source/aws_imagebuilder_distribution_configuration: Add `fast_launch_configuration` attribute to the `distribution` configuration block
```
246 changes: 246 additions & 0 deletions internal/service/imagebuilder/distribution_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,68 @@ func ResourceDistributionConfiguration() *schema.Resource {
},
},
},
"fast_launch_configuration": {
Type: schema.TypeSet,
Optional: true,
MaxItems: 1000,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"account_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: verify.ValidAccountID,
},
"enabled": {
Type: schema.TypeBool,
Required: true,
},
"launch_template": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"launch_template_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: verify.ValidLaunchTemplateID,
},
"launch_template_name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: verify.ValidLaunchTemplateName,
},
"launch_template_version": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 1024),
},
},
},
},
"max_parallel_launches": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
ValidateFunc: validation.IntBetween(1, 10000),
},
"snapshot_configuration": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"target_resource_count": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 10000),
},
},
},
},
},
},
},
"launch_template_configuration": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -461,6 +523,10 @@ func expandDistribution(tfMap map[string]interface{}) *imagebuilder.Distribution
apiObject.ContainerDistributionConfiguration = expandContainerDistributionConfiguration(v[0].(map[string]interface{}))
}

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

if v, ok := tfMap["launch_template_configuration"].(*schema.Set); ok && v.Len() > 0 {
apiObject.LaunchTemplateConfigurations = expandLaunchTemplateConfigurations(v.List())
}
Expand Down Expand Up @@ -553,6 +619,98 @@ func expandTargetContainerRepository(tfMap map[string]interface{}) *imagebuilder
return apiObject
}

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

var apiObjects []*imagebuilder.FastLaunchConfiguration

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

if !ok {
continue
}

apiObject := expandFastLaunchConfiguration(tfMap)

if apiObject == nil {
continue
}

apiObjects = append(apiObjects, apiObject)
}

return apiObjects
}

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

apiObject := &imagebuilder.FastLaunchConfiguration{}

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

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

if v, ok := tfMap["launch_template"].([]interface{}); ok && len(v) > 0 && v[0] != nil {
apiObject.LaunchTemplate = expandFastLaunchLaunchTemplateSpecification(v[0].(map[string]interface{}))
}

if v, ok := tfMap["max_parallel_launches"].(int); ok && v != 0 {
apiObject.MaxParallelLaunches = aws.Int64(int64(v))
}

if v, ok := tfMap["snapshot_configuration"].([]interface{}); ok && len(v) > 0 && v[0] != nil {
apiObject.SnapshotConfiguration = expandFastLaunchSnapshotConfiguration(v[0].(map[string]interface{}))
}

return apiObject
}

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

apiObject := &imagebuilder.FastLaunchLaunchTemplateSpecification{}

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

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

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

return apiObject
}

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

apiObject := &imagebuilder.FastLaunchSnapshotConfiguration{}

if v, ok := tfMap["target_resource_count"].(int); ok && v != 0 {
apiObject.TargetResourceCount = aws.Int64(int64(v))
}

return apiObject
}

func expandLaunchTemplateConfiguration(tfMap map[string]interface{}) *imagebuilder.LaunchTemplateConfiguration {
if tfMap == nil {
return nil
Expand Down Expand Up @@ -664,6 +822,10 @@ func flattenDistribution(apiObject *imagebuilder.Distribution) map[string]interf
tfMap["container_distribution_configuration"] = []interface{}{flattenContainerDistributionConfiguration(v)}
}

if v := apiObject.FastLaunchConfigurations; v != nil {
tfMap["fast_launch_configuration"] = flattenFastLaunchConfigurations(v)
}

if v := apiObject.LaunchTemplateConfigurations; v != nil {
tfMap["launch_template_configuration"] = flattenLaunchTemplateConfigurations(v)
}
Expand Down Expand Up @@ -762,3 +924,87 @@ func flattenLaunchTemplateConfiguration(apiObject *imagebuilder.LaunchTemplateCo

return tfMap
}

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

var tfList []interface{}

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

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

return tfList
}

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

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

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

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

if v := apiObject.LaunchTemplate; v != nil {
tfMap["launch_template"] = []interface{}{flattenFastLaunchLaunchTemplateSpecification(v)}
}

if v := apiObject.MaxParallelLaunches; v != nil {
tfMap["max_parallel_launches"] = aws.Int64Value(v)
}

if v := apiObject.SnapshotConfiguration; v != nil {
tfMap["snapshot_configuration"] = []interface{}{flattenFastLaunchSnapshotConfiguration(v)}
}

return tfMap
}

func flattenFastLaunchLaunchTemplateSpecification(apiObject *imagebuilder.FastLaunchLaunchTemplateSpecification) 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.LaunchTemplateName; v != nil {
tfMap["launch_template_name"] = aws.StringValue(v)
}

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

return tfMap
}

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

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

if v := apiObject.TargetResourceCount; v != nil {
tfMap["target_resource_count"] = aws.Int64Value(v)
}

return tfMap
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,58 @@ func DataSourceDistributionConfiguration() *schema.Resource {
},
},
},
"fast_launch_configuration": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"account_id": {
Type: schema.TypeString,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
"launch_template": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"launch_template_id": {
Type: schema.TypeString,
Computed: true,
},
"launch_template_name": {
Type: schema.TypeString,
Computed: true,
},
"launch_template_version": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"max_parallel_launches": {
Type: schema.TypeInt,
Computed: true,
},
"snapshot_configuration": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"target_resource_count": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
},
},
"launch_template_configuration": {
Type: schema.TypeSet,
Computed: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ func TestAccImageBuilderDistributionConfigurationDataSource_arn(t *testing.T) {
resource.TestCheckResourceAttrPair(dataSourceName, "distribution.0.container_distribution_configuration.0.target_repository.#", resourceName, "distribution.0.container_distribution_configuration.0.target_repository.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "distribution.0.container_distribution_configuration.0.target_repository.0.repository_name", resourceName, "distribution.0.container_distribution_configuration.0.target_repository.0.repository_name"),
resource.TestCheckResourceAttrPair(dataSourceName, "distribution.0.container_distribution_configuration.0.target_repository.0.service", resourceName, "distribution.0.container_distribution_configuration.0.target_repository.0.service"),
resource.TestCheckResourceAttrPair(dataSourceName, "distribution.0.fast_launch_configuration.#", resourceName, "distribution.0.fast_launch_configuration.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "distribution.0.fast_launch_configuration.0.account_id", resourceName, "distribution.0.fast_launch_configuration.0.account_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "distribution.0.fast_launch_configuration.0.enabled", resourceName, "distribution.0.fast_launch_configuration.0.enabled"),
resource.TestCheckResourceAttrPair(dataSourceName, "distribution.0.fast_launch_configuration.0.launch_template.#", resourceName, "distribution.0.fast_launch_configuration.0.launch_template.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "distribution.0.fast_launch_configuration.0.launch_template.0.launch_template_id", resourceName, "distribution.0.fast_launch_configuration.0.launch_template.0.launch_template_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "distribution.0.fast_launch_configuration.0.launch_template.0.launch_template_name", resourceName, "distribution.0.fast_launch_configuration.0.launch_template.0.launch_template_name"),
resource.TestCheckResourceAttrPair(dataSourceName, "distribution.0.fast_launch_configuration.0.launch_template.0.launch_template_version", resourceName, "distribution.0.fast_launch_configuration.0.launch_template.0.launch_template_version"),
resource.TestCheckResourceAttrPair(dataSourceName, "distribution.0.fast_launch_configuration.0.max_parallel_launches", resourceName, "distribution.0.fast_launch_configuration.0.max_parallel_launches"),
resource.TestCheckResourceAttrPair(dataSourceName, "distribution.0.fast_launch_configuration.0.snapshot_configuration.#", resourceName, "distribution.0.fast_launch_configuration.0.snapshot_configuration.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "distribution.0.fast_launch_configuration.0.snapshot_configuration.0.target_resource_count", resourceName, "distribution.0.fast_launch_configuration.0.snapshot_configuration.0.target_resource_count"),
resource.TestCheckResourceAttrPair(dataSourceName, "distribution.0.launch_template_configuration.#", resourceName, "distribution.0.launch_template_configuration.#"),
resource.TestCheckResourceAttrPair(dataSourceName, "distribution.0.launch_template_configuration.0.default", resourceName, "distribution.0.launch_template_configuration.0.default"),
resource.TestCheckResourceAttrPair(dataSourceName, "distribution.0.launch_template_configuration.0.launch_template_id", resourceName, "distribution.0.launch_template_configuration.0.launch_template_id"),
Expand Down Expand Up @@ -79,6 +89,22 @@ resource "aws_imagebuilder_distribution_configuration" "test" {
launch_template_id = aws_launch_template.test.id
}
fast_launch_configuration {
account_id = data.aws_caller_identity.current.account_id
enabled = true
launch_template {
launch_template_id = aws_launch_template.test.id
launch_template_version = "1"
}
max_parallel_launches = 1
snapshot_configuration {
target_resource_count = 1
}
}
region = data.aws_region.current.name
}
}
Expand Down
Loading

0 comments on commit 753227d

Please sign in to comment.