From edae1dde071cfeaa48dc35941ffc26bd3b06cb9a Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Tue, 24 Oct 2023 14:03:46 -0500 Subject: [PATCH 1/8] aws_autoscaling_group: create custom diff for launch template --- internal/service/autoscaling/group.go | 95 ++++++++++++++++++++++++--- 1 file changed, 87 insertions(+), 8 deletions(-) diff --git a/internal/service/autoscaling/group.go b/internal/service/autoscaling/group.go index 90e01c8a1cc..b5c33b9ed6b 100644 --- a/internal/service/autoscaling/group.go +++ b/internal/service/autoscaling/group.go @@ -253,6 +253,7 @@ func ResourceGroup() *schema.Resource { Type: schema.TypeList, MaxItems: 1, Optional: true, + Computed: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "id": { @@ -309,6 +310,7 @@ func ResourceGroup() *schema.Resource { "mixed_instances_policy": { Type: schema.TypeList, Optional: true, + Computed: true, MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -865,16 +867,93 @@ func ResourceGroup() *schema.Resource { }, CustomizeDiff: customdiff.Sequence( - customdiff.ComputedIf("launch_template.0.id", func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) bool { - return diff.HasChange("launch_template.0.name") - }), - customdiff.ComputedIf("launch_template.0.name", func(_ context.Context, diff *schema.ResourceDiff, meta interface{}) bool { - return diff.HasChange("launch_template.0.id") - }), + launchTemplateCustomDiff, + // mixedInstancesPolicyLaunchTemplateCustomDiff, + launchTemplateDiff("mixed_instances_policy", "mixed_instances_policy.0.launch_template.0.launch_template_specification.0.launch_template_name"), ), } } +func launchTemplateCustomDiff(_ context.Context, diff *schema.ResourceDiff, _ interface{}) error { + expandMap := func(in cty.Value, attributeName, keyToSet, valueToSet string) error { + if !in.IsNull() && len(in.AsValueSlice()) > 0 { + m := in.AsValueSlice()[0].AsValueMap() + tfMap := make(map[string]string) + for k, val := range m { + if !val.IsNull() && val.IsKnown() { + tfMap[k] = val.AsString() + } + } + + tfMap[keyToSet] = valueToSet + + if err := diff.SetNew(attributeName, []interface{}{tfMap}); err != nil { + return err + } + } + + return nil + } + + if diff.HasChange("launch_template.0.name") { + lt := diff.GetRawPlan().GetAttr("launch_template") + if err := expandMap(lt, "launch_template", "id", "unknown"); err != nil { + return err + } + } + + return nil +} + +func mixedInstancesPolicyLaunchTemplateCustomDiff(_ context.Context, diff *schema.ResourceDiff, _ interface{}) error { + if diff.HasChange("mixed_instances_policy.0.launch_template.0.launch_template_specification.0.launch_template_name") { + n := diff.Get("mixed_instances_policy") + nmip := n.([]interface{}) + + //parts := strings.Split("mixed_instances_policy.0.launch_template.0.launch_template_specification.0.launch_template_name", ".") + launchTemplate := nmip[0].(map[string]interface{})["launch_template"].([]interface{})[0].(map[string]interface{})["launch_template_specification"].([]interface{})[0] + launchTemplateSpecification := launchTemplate.(map[string]interface{}) + + launchTemplateSpecification["launch_template_id"] = "unknown" + + launchTemplate = launchTemplateSpecification + + if err := diff.SetNew("mixed_instances_policy", nmip); err != nil { + return err + } + } + + return nil +} + +func launchTemplateDiff(baseAttribute, subAttribute string) schema.CustomizeDiffFunc { + return func(_ context.Context, diff *schema.ResourceDiff, _ interface{}) error { + if diff.HasChange(subAttribute) { + n := diff.Get(baseAttribute) + nmip, ok := n.([]interface{}) + if !ok { + return nil + } + + if baseAttribute == "mixed_instances_policy" { + launchTemplate := nmip[0].(map[string]interface{})["launch_template"].([]interface{})[0].(map[string]interface{})["launch_template_specification"].([]interface{})[0] + launchTemplateSpecification := launchTemplate.(map[string]interface{}) + + launchTemplateSpecification["launch_template_id"] = "unknown" + + launchTemplate = launchTemplateSpecification + + if err := diff.SetNew("mixed_instances_policy", nmip); err != nil { + return err + } + } + + } + + return nil + } +} + func resourceGroupCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics conn := meta.(*conns.AWSClient).AutoScalingConn(ctx) @@ -3035,7 +3114,7 @@ func expandLaunchTemplateSpecificationForMixedInstancesPolicy(tfMap map[string]i // API returns both ID and name, which Terraform saves to state. Next update returns: // ValidationError: Valid requests must contain either launchTemplateId or LaunchTemplateName // Prefer the ID if we have both. - if v, ok := tfMap["launch_template_id"]; ok && v != "" { + if v, ok := tfMap["launch_template_id"]; ok && v != "" && v != "unknown" { apiObject.LaunchTemplateId = aws.String(v.(string)) } else if v, ok := tfMap["launch_template_name"]; ok && v != "" { apiObject.LaunchTemplateName = aws.String(v.(string)) @@ -3057,7 +3136,7 @@ func expandLaunchTemplateSpecification(tfMap map[string]interface{}) *autoscalin // DescribeAutoScalingGroups returns both name and id but LaunchTemplateSpecification // allows only one of them to be set. - if v, ok := tfMap["id"]; ok && v != "" { + if v, ok := tfMap["id"]; ok && v != "" && v != "unknown" { apiObject.LaunchTemplateId = aws.String(v.(string)) } else if v, ok := tfMap["name"]; ok && v != "" { apiObject.LaunchTemplateName = aws.String(v.(string)) From 1ed5cab460e0e35c72c500ae45d38128ac6e281a Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Tue, 24 Oct 2023 14:15:58 -0500 Subject: [PATCH 2/8] aws_autoscaling_group: cleaup --- internal/service/autoscaling/group.go | 70 +++++---------------------- 1 file changed, 13 insertions(+), 57 deletions(-) diff --git a/internal/service/autoscaling/group.go b/internal/service/autoscaling/group.go index b5c33b9ed6b..86ddcdb725d 100644 --- a/internal/service/autoscaling/group.go +++ b/internal/service/autoscaling/group.go @@ -867,66 +867,13 @@ func ResourceGroup() *schema.Resource { }, CustomizeDiff: customdiff.Sequence( - launchTemplateCustomDiff, - // mixedInstancesPolicyLaunchTemplateCustomDiff, - launchTemplateDiff("mixed_instances_policy", "mixed_instances_policy.0.launch_template.0.launch_template_specification.0.launch_template_name"), + launchTemplateCustomDiff("launch_template", "launch_template.0.name"), + launchTemplateCustomDiff("mixed_instances_policy", "mixed_instances_policy.0.launch_template.0.launch_template_specification.0.launch_template_name"), ), } } -func launchTemplateCustomDiff(_ context.Context, diff *schema.ResourceDiff, _ interface{}) error { - expandMap := func(in cty.Value, attributeName, keyToSet, valueToSet string) error { - if !in.IsNull() && len(in.AsValueSlice()) > 0 { - m := in.AsValueSlice()[0].AsValueMap() - tfMap := make(map[string]string) - for k, val := range m { - if !val.IsNull() && val.IsKnown() { - tfMap[k] = val.AsString() - } - } - - tfMap[keyToSet] = valueToSet - - if err := diff.SetNew(attributeName, []interface{}{tfMap}); err != nil { - return err - } - } - - return nil - } - - if diff.HasChange("launch_template.0.name") { - lt := diff.GetRawPlan().GetAttr("launch_template") - if err := expandMap(lt, "launch_template", "id", "unknown"); err != nil { - return err - } - } - - return nil -} - -func mixedInstancesPolicyLaunchTemplateCustomDiff(_ context.Context, diff *schema.ResourceDiff, _ interface{}) error { - if diff.HasChange("mixed_instances_policy.0.launch_template.0.launch_template_specification.0.launch_template_name") { - n := diff.Get("mixed_instances_policy") - nmip := n.([]interface{}) - - //parts := strings.Split("mixed_instances_policy.0.launch_template.0.launch_template_specification.0.launch_template_name", ".") - launchTemplate := nmip[0].(map[string]interface{})["launch_template"].([]interface{})[0].(map[string]interface{})["launch_template_specification"].([]interface{})[0] - launchTemplateSpecification := launchTemplate.(map[string]interface{}) - - launchTemplateSpecification["launch_template_id"] = "unknown" - - launchTemplate = launchTemplateSpecification - - if err := diff.SetNew("mixed_instances_policy", nmip); err != nil { - return err - } - } - - return nil -} - -func launchTemplateDiff(baseAttribute, subAttribute string) schema.CustomizeDiffFunc { +func launchTemplateCustomDiff(baseAttribute, subAttribute string) schema.CustomizeDiffFunc { return func(_ context.Context, diff *schema.ResourceDiff, _ interface{}) error { if diff.HasChange(subAttribute) { n := diff.Get(baseAttribute) @@ -935,6 +882,15 @@ func launchTemplateDiff(baseAttribute, subAttribute string) schema.CustomizeDiff return nil } + if baseAttribute == "launch_template" { + launchTemplate := nmip[0].(map[string]interface{}) + launchTemplate["id"] = "unknown" + + if err := diff.SetNew(baseAttribute, nmip); err != nil { + return err + } + } + if baseAttribute == "mixed_instances_policy" { launchTemplate := nmip[0].(map[string]interface{})["launch_template"].([]interface{})[0].(map[string]interface{})["launch_template_specification"].([]interface{})[0] launchTemplateSpecification := launchTemplate.(map[string]interface{}) @@ -943,7 +899,7 @@ func launchTemplateDiff(baseAttribute, subAttribute string) schema.CustomizeDiff launchTemplate = launchTemplateSpecification - if err := diff.SetNew("mixed_instances_policy", nmip); err != nil { + if err := diff.SetNew(baseAttribute, nmip); err != nil { return err } } From 7015fdd9dd9e76ff426edc9b97f6bcb89ad97d84 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Tue, 24 Oct 2023 14:20:51 -0500 Subject: [PATCH 3/8] aws_autoscaling_group: const for unknown value --- internal/service/autoscaling/consts.go | 4 ++++ internal/service/autoscaling/group.go | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/internal/service/autoscaling/consts.go b/internal/service/autoscaling/consts.go index 84b7d8a2dff..2c3ccd07169 100644 --- a/internal/service/autoscaling/consts.go +++ b/internal/service/autoscaling/consts.go @@ -85,3 +85,7 @@ const ( TrafficSourceStateRemoving = "Removing" TrafficSourceStateRemoved = "Removed" ) + +const ( + launchTemplateIDUnknown = "unknown" +) diff --git a/internal/service/autoscaling/group.go b/internal/service/autoscaling/group.go index 86ddcdb725d..834bb920b8b 100644 --- a/internal/service/autoscaling/group.go +++ b/internal/service/autoscaling/group.go @@ -884,7 +884,7 @@ func launchTemplateCustomDiff(baseAttribute, subAttribute string) schema.Customi if baseAttribute == "launch_template" { launchTemplate := nmip[0].(map[string]interface{}) - launchTemplate["id"] = "unknown" + launchTemplate["id"] = launchTemplateIDUnknown if err := diff.SetNew(baseAttribute, nmip); err != nil { return err @@ -895,7 +895,7 @@ func launchTemplateCustomDiff(baseAttribute, subAttribute string) schema.Customi launchTemplate := nmip[0].(map[string]interface{})["launch_template"].([]interface{})[0].(map[string]interface{})["launch_template_specification"].([]interface{})[0] launchTemplateSpecification := launchTemplate.(map[string]interface{}) - launchTemplateSpecification["launch_template_id"] = "unknown" + launchTemplateSpecification["launch_template_id"] = launchTemplateIDUnknown launchTemplate = launchTemplateSpecification @@ -3070,7 +3070,7 @@ func expandLaunchTemplateSpecificationForMixedInstancesPolicy(tfMap map[string]i // API returns both ID and name, which Terraform saves to state. Next update returns: // ValidationError: Valid requests must contain either launchTemplateId or LaunchTemplateName // Prefer the ID if we have both. - if v, ok := tfMap["launch_template_id"]; ok && v != "" && v != "unknown" { + if v, ok := tfMap["launch_template_id"]; ok && v != "" && v != launchTemplateIDUnknown { apiObject.LaunchTemplateId = aws.String(v.(string)) } else if v, ok := tfMap["launch_template_name"]; ok && v != "" { apiObject.LaunchTemplateName = aws.String(v.(string)) @@ -3092,7 +3092,7 @@ func expandLaunchTemplateSpecification(tfMap map[string]interface{}) *autoscalin // DescribeAutoScalingGroups returns both name and id but LaunchTemplateSpecification // allows only one of them to be set. - if v, ok := tfMap["id"]; ok && v != "" && v != "unknown" { + if v, ok := tfMap["id"]; ok && v != "" && v != launchTemplateIDUnknown { apiObject.LaunchTemplateId = aws.String(v.(string)) } else if v, ok := tfMap["name"]; ok && v != "" { apiObject.LaunchTemplateName = aws.String(v.(string)) From 861c605fba83528ed96d903b7e53cb39780a832d Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Tue, 24 Oct 2023 14:56:17 -0500 Subject: [PATCH 4/8] aws_autoscaling_group: update tests --- internal/service/autoscaling/group.go | 13 +++----- internal/service/autoscaling/group_test.go | 36 +++++++++++++++++----- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/internal/service/autoscaling/group.go b/internal/service/autoscaling/group.go index 834bb920b8b..85dfc94b0dd 100644 --- a/internal/service/autoscaling/group.go +++ b/internal/service/autoscaling/group.go @@ -877,33 +877,30 @@ func launchTemplateCustomDiff(baseAttribute, subAttribute string) schema.Customi return func(_ context.Context, diff *schema.ResourceDiff, _ interface{}) error { if diff.HasChange(subAttribute) { n := diff.Get(baseAttribute) - nmip, ok := n.([]interface{}) + ba, ok := n.([]interface{}) if !ok { return nil } if baseAttribute == "launch_template" { - launchTemplate := nmip[0].(map[string]interface{}) + launchTemplate := ba[0].(map[string]interface{}) launchTemplate["id"] = launchTemplateIDUnknown - if err := diff.SetNew(baseAttribute, nmip); err != nil { + if err := diff.SetNew(baseAttribute, ba); err != nil { return err } } if baseAttribute == "mixed_instances_policy" { - launchTemplate := nmip[0].(map[string]interface{})["launch_template"].([]interface{})[0].(map[string]interface{})["launch_template_specification"].([]interface{})[0] + launchTemplate := ba[0].(map[string]interface{})["launch_template"].([]interface{})[0].(map[string]interface{})["launch_template_specification"].([]interface{})[0] launchTemplateSpecification := launchTemplate.(map[string]interface{}) launchTemplateSpecification["launch_template_id"] = launchTemplateIDUnknown - launchTemplate = launchTemplateSpecification - - if err := diff.SetNew(baseAttribute, nmip); err != nil { + if err := diff.SetNew(baseAttribute, ba); err != nil { return err } } - } return nil diff --git a/internal/service/autoscaling/group_test.go b/internal/service/autoscaling/group_test.go index e16b9429a31..af591cc2c91 100644 --- a/internal/service/autoscaling/group_test.go +++ b/internal/service/autoscaling/group_test.go @@ -1098,6 +1098,7 @@ func TestAccAutoScalingGroup_LaunchTemplate_update(t *testing.T) { ctx := acctest.Context(t) var group autoscaling.Group rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + launchTemplateNameUpdated := fmt.Sprintf("%s_updated", rName) resourceName := "aws_autoscaling_group.test" resource.ParallelTest(t, resource.TestCase{ @@ -1127,7 +1128,7 @@ func TestAccAutoScalingGroup_LaunchTemplate_update(t *testing.T) { ), }, { - Config: testAccGroupConfig_launchTemplateName(rName), + Config: testAccGroupConfig_launchTemplateName(rName, rName), Check: resource.ComposeTestCheckFunc( testAccCheckGroupExists(ctx, resourceName, &group), resource.TestCheckResourceAttr(resourceName, "launch_configuration", ""), @@ -1159,6 +1160,17 @@ func TestAccAutoScalingGroup_LaunchTemplate_update(t *testing.T) { resource.TestCheckResourceAttrPair(resourceName, "launch_template.0.version", "aws_launch_template.test", "default_version"), ), }, + { + Config: testAccGroupConfig_launchTemplateName(rName, launchTemplateNameUpdated), + Check: resource.ComposeTestCheckFunc( + testAccCheckGroupExists(ctx, resourceName, &group), + resource.TestCheckResourceAttr(resourceName, "launch_configuration", ""), + resource.TestCheckResourceAttr(resourceName, "launch_template.#", "1"), + resource.TestCheckResourceAttrPair(resourceName, "launch_template.0.id", "aws_launch_template.test", "id"), + resource.TestCheckResourceAttrPair(resourceName, "launch_template.0.name", "aws_launch_template.test", "name"), + resource.TestCheckResourceAttr(resourceName, "launch_template.0.version", ""), + ), + }, }, }) } @@ -1974,7 +1986,7 @@ func TestAccAutoScalingGroup_MixedInstancesPolicyLaunchTemplateLaunchTemplateSpe var group autoscaling.Group resourceName := "aws_autoscaling_group.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) - + launchTemplateNameUpdated := fmt.Sprintf("%s_updated", rName) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, ErrorCheck: acctest.ErrorCheck(t, autoscaling.EndpointsID), @@ -1982,7 +1994,7 @@ func TestAccAutoScalingGroup_MixedInstancesPolicyLaunchTemplateLaunchTemplateSpe CheckDestroy: testAccCheckGroupDestroy(ctx), Steps: []resource.TestStep{ { - Config: testAccGroupConfig_mixedInstancesPolicyLaunchTemplateLaunchTemplateSpecificationLaunchTemplateName(rName), + Config: testAccGroupConfig_mixedInstancesPolicyLaunchTemplateLaunchTemplateSpecificationLaunchTemplateName(rName, rName), Check: resource.ComposeTestCheckFunc( testAccCheckGroupExists(ctx, resourceName, &group), resource.TestCheckResourceAttr(resourceName, "mixed_instances_policy.#", "1"), @@ -1992,6 +2004,16 @@ func TestAccAutoScalingGroup_MixedInstancesPolicyLaunchTemplateLaunchTemplateSpe ), }, testAccGroupImportStep(resourceName), + { + Config: testAccGroupConfig_mixedInstancesPolicyLaunchTemplateLaunchTemplateSpecificationLaunchTemplateName(rName, launchTemplateNameUpdated), + Check: resource.ComposeTestCheckFunc( + testAccCheckGroupExists(ctx, resourceName, &group), + resource.TestCheckResourceAttr(resourceName, "mixed_instances_policy.#", "1"), + resource.TestCheckResourceAttr(resourceName, "mixed_instances_policy.0.launch_template.#", "1"), + resource.TestCheckResourceAttr(resourceName, "mixed_instances_policy.0.launch_template.0.launch_template_specification.#", "1"), + resource.TestCheckResourceAttrSet(resourceName, "mixed_instances_policy.0.launch_template.0.launch_template_specification.0.launch_template_name"), + ), + }, }, }) } @@ -4801,8 +4823,8 @@ resource "aws_autoscaling_group" "test" { `, rName)) } -func testAccGroupConfig_launchTemplateName(rName string) string { - return acctest.ConfigCompose(testAccGroupConfig_launchTemplateBase(rName, "t2.micro"), fmt.Sprintf(` +func testAccGroupConfig_launchTemplateName(rName, launchTemplateName string) string { + return acctest.ConfigCompose(testAccGroupConfig_launchTemplateBase(launchTemplateName, "t2.micro"), fmt.Sprintf(` resource "aws_autoscaling_group" "test" { availability_zones = [data.aws_availability_zones.available.names[0]] max_size = 0 @@ -5718,8 +5740,8 @@ resource "aws_autoscaling_group" "test" { `, rName, spotMaxPrice)) } -func testAccGroupConfig_mixedInstancesPolicyLaunchTemplateLaunchTemplateSpecificationLaunchTemplateName(rName string) string { - return acctest.ConfigCompose(testAccGroupConfig_launchTemplateBase(rName, "t3.micro"), fmt.Sprintf(` +func testAccGroupConfig_mixedInstancesPolicyLaunchTemplateLaunchTemplateSpecificationLaunchTemplateName(rName, launchTemplateName string) string { + return acctest.ConfigCompose(testAccGroupConfig_launchTemplateBase(launchTemplateName, "t3.micro"), fmt.Sprintf(` resource "aws_autoscaling_group" "test" { availability_zones = [data.aws_availability_zones.available.names[0]] desired_capacity = 0 From 4e8a634dc851d5204ad4cf15bbef66cc2aa6ebb5 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Tue, 24 Oct 2023 14:59:12 -0500 Subject: [PATCH 5/8] aws_autoscaling_group: remove unnecessary spacing --- internal/service/autoscaling/group.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/service/autoscaling/group.go b/internal/service/autoscaling/group.go index 85dfc94b0dd..3ebda6b4fa5 100644 --- a/internal/service/autoscaling/group.go +++ b/internal/service/autoscaling/group.go @@ -894,7 +894,6 @@ func launchTemplateCustomDiff(baseAttribute, subAttribute string) schema.Customi if baseAttribute == "mixed_instances_policy" { launchTemplate := ba[0].(map[string]interface{})["launch_template"].([]interface{})[0].(map[string]interface{})["launch_template_specification"].([]interface{})[0] launchTemplateSpecification := launchTemplate.(map[string]interface{}) - launchTemplateSpecification["launch_template_id"] = launchTemplateIDUnknown if err := diff.SetNew(baseAttribute, ba); err != nil { From 98de67aabdd00e8d9975c90e2aad10bddbfffb0e Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Tue, 24 Oct 2023 15:00:38 -0500 Subject: [PATCH 6/8] add CHANGELOG entry --- .changelog/34086.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/34086.txt diff --git a/.changelog/34086.txt b/.changelog/34086.txt new file mode 100644 index 00000000000..30c2b1ab274 --- /dev/null +++ b/.changelog/34086.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/aws_autoscaling_group: Fix error when `launch_template` name is updated. +``` \ No newline at end of file From b32f9ecec49543f43de5ac22d38bae5770190428 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Wed, 25 Oct 2023 08:13:17 -0500 Subject: [PATCH 7/8] aws_autoscaling_group: custom diff for overrides --- internal/service/autoscaling/group.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/internal/service/autoscaling/group.go b/internal/service/autoscaling/group.go index 3ebda6b4fa5..2c9005d4a7c 100644 --- a/internal/service/autoscaling/group.go +++ b/internal/service/autoscaling/group.go @@ -398,6 +398,7 @@ func ResourceGroup() *schema.Resource { "override": { Type: schema.TypeList, Optional: true, + Computed: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "instance_requirements": { @@ -869,6 +870,7 @@ func ResourceGroup() *schema.Resource { CustomizeDiff: customdiff.Sequence( launchTemplateCustomDiff("launch_template", "launch_template.0.name"), launchTemplateCustomDiff("mixed_instances_policy", "mixed_instances_policy.0.launch_template.0.launch_template_specification.0.launch_template_name"), + launchTemplateCustomDiff("mixed_instances_policy", "mixed_instances_policy.0.launch_template.0.override"), ), } } @@ -891,7 +893,7 @@ func launchTemplateCustomDiff(baseAttribute, subAttribute string) schema.Customi } } - if baseAttribute == "mixed_instances_policy" { + if baseAttribute == "mixed_instances_policy" && !strings.Contains(subAttribute, "override") { launchTemplate := ba[0].(map[string]interface{})["launch_template"].([]interface{})[0].(map[string]interface{})["launch_template_specification"].([]interface{})[0] launchTemplateSpecification := launchTemplate.(map[string]interface{}) launchTemplateSpecification["launch_template_id"] = launchTemplateIDUnknown @@ -900,6 +902,24 @@ func launchTemplateCustomDiff(baseAttribute, subAttribute string) schema.Customi return err } } + + if baseAttribute == "mixed_instances_policy" && strings.Contains(subAttribute, "override") { + log.Printf("[DEBUG] we got here") + launchTemplate := ba[0].(map[string]interface{})["launch_template"].([]interface{})[0].(map[string]interface{})["override"].([]interface{}) + + for i := range launchTemplate { + key := fmt.Sprintf("mixed_instances_policy.0.launch_template.0.override.%d.launch_template_specification.0.launch_template_name", i) + + if diff.HasChange(key) { + launchTemplateSpecification := launchTemplate[i].(map[string]interface{})["launch_template_specification"].([]interface{})[0].(map[string]interface{}) + launchTemplateSpecification["launch_template_id"] = launchTemplateIDUnknown + } + } + + if err := diff.SetNew(baseAttribute, ba); err != nil { + return err + } + } } return nil From c48825e97e9e87bb6cb088e7f83415990c4ccfb7 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Wed, 25 Oct 2023 08:13:46 -0500 Subject: [PATCH 8/8] cleanup debug logs --- internal/service/autoscaling/group.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/service/autoscaling/group.go b/internal/service/autoscaling/group.go index 2c9005d4a7c..ccd6598eca9 100644 --- a/internal/service/autoscaling/group.go +++ b/internal/service/autoscaling/group.go @@ -904,7 +904,6 @@ func launchTemplateCustomDiff(baseAttribute, subAttribute string) schema.Customi } if baseAttribute == "mixed_instances_policy" && strings.Contains(subAttribute, "override") { - log.Printf("[DEBUG] we got here") launchTemplate := ba[0].(map[string]interface{})["launch_template"].([]interface{})[0].(map[string]interface{})["override"].([]interface{}) for i := range launchTemplate {