Skip to content

Commit

Permalink
Add standby_policy along with suspended / stopped target size. (Googl…
Browse files Browse the repository at this point in the history
  • Loading branch information
chayankumar999 authored May 29, 2024
1 parent 229dc05 commit 6503631
Show file tree
Hide file tree
Showing 6 changed files with 533 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,49 @@ func ResourceComputeInstanceGroupManager() *schema.Resource {
},
},

<% unless version == "ga" -%>
"standby_policy": {
Computed: true,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: `Standby policy for stopped and suspended instances.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"initial_delay_sec": {
Computed: true,
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(0, 3600),
Description: `Specifies the number of seconds that the MIG should wait to suspend or stop a VM after that VM was created. The initial delay gives the initialization script the time to prepare your VM for a quick scale out. The value of initial delay must be between 0 and 3600 seconds. The default value is 0.`,
},

"mode": {
Computed: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"MANUAL", "SCALE_OUT_POOL"}, true),
Description: `Defines how a MIG resumes or starts VMs from a standby pool when the group scales out. The default mode is "MANUAL".`,
},
},
},
},

"target_suspended_size": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: `The target number of suspended instances for this managed instance group.`,
},

"target_stopped_size": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: `The target number of stopped instances for this managed instance group.`,
},
<% end -%>

"update_policy": {
Computed: true,
Type: schema.TypeList,
Expand Down Expand Up @@ -609,6 +652,11 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte
TargetPools: tpgresource.ConvertStringSet(d.Get("target_pools").(*schema.Set)),
AutoHealingPolicies: expandAutoHealingPolicies(d.Get("auto_healing_policies").([]interface{})),
Versions: expandVersions(d.Get("version").([]interface{})),
<% unless version == "ga" -%>
StandbyPolicy: expandStandbyPolicy(d),
TargetSuspendedSize: int64(d.Get("target_suspended_size").(int)),
TargetStoppedSize: int64(d.Get("target_stopped_size").(int)),
<% end -%>
UpdatePolicy: expandUpdatePolicy(d.Get("update_policy").([]interface{})),
InstanceLifecyclePolicy: expandInstanceLifecyclePolicy(d.Get("instance_lifecycle_policy").([]interface{})),
AllInstancesConfig: expandAllInstancesConfig(nil, d.Get("all_instances_config").([]interface{})),
Expand Down Expand Up @@ -832,6 +880,17 @@ func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interf
if err := d.Set("version", flattenVersions(manager.Versions)); err != nil {
return err
}
<% unless version == "ga" -%>
if err = d.Set("standby_policy", flattenStandbyPolicy(manager.StandbyPolicy)); err != nil {
return fmt.Errorf("Error setting standby_policy in state: %s", err.Error())
}
if err := d.Set("target_suspended_size", manager.TargetSuspendedSize); err != nil {
return fmt.Errorf("Error setting target_suspended_size: %s", err)
}
if err := d.Set("target_stopped_size", manager.TargetStoppedSize); err != nil {
return fmt.Errorf("Error setting target_stopped_size: %s", err)
}
<% end -%>
if err = d.Set("update_policy", flattenUpdatePolicy(manager.UpdatePolicy)); err != nil {
return fmt.Errorf("Error setting update_policy in state: %s", err.Error())
}
Expand Down Expand Up @@ -903,6 +962,25 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte
change = true
}

<% unless version == "ga" -%>
if d.HasChange("standby_policy") {
updatedManager.StandbyPolicy = expandStandbyPolicy(d)
change = true
}

if d.HasChange("target_suspended_size") {
updatedManager.TargetSuspendedSize = int64(d.Get("target_suspended_size").(int))
updatedManager.ForceSendFields = append(updatedManager.ForceSendFields, "TargetSuspendedSize")
change = true
}

if d.HasChange("target_stopped_size") {
updatedManager.TargetStoppedSize = int64(d.Get("target_stopped_size").(int))
updatedManager.ForceSendFields = append(updatedManager.ForceSendFields, "TargetStoppedSize")
change = true
}
<% end -%>

if d.HasChange("update_policy") {
updatedManager.UpdatePolicy = expandUpdatePolicy(d.Get("update_policy").([]interface{}))
change = true
Expand Down Expand Up @@ -1227,6 +1305,19 @@ func expandInstanceLifecyclePolicy(configured []interface{}) *compute.InstanceGr
return instanceLifecyclePolicy
}

<% unless version == "ga" -%>
func expandStandbyPolicy(d *schema.ResourceData) *compute.InstanceGroupManagerStandbyPolicy {
standbyPolicy := &compute.InstanceGroupManagerStandbyPolicy{}
for _, sp := range d.Get("standby_policy").([]any) {
spData := sp.(map[string]any)
standbyPolicy.InitialDelaySec = int64(spData["initial_delay_sec"].(int))
standbyPolicy.ForceSendFields = []string{"InitialDelaySec"}
standbyPolicy.Mode = spData["mode"].(string)
}
return standbyPolicy
}
<% end -%>

func expandUpdatePolicy(configured []interface{}) *compute.InstanceGroupManagerUpdatePolicy {
updatePolicy := &compute.InstanceGroupManagerUpdatePolicy{}

Expand Down Expand Up @@ -1363,6 +1454,19 @@ func flattenStatefulPolicyStatefulIps(d *schema.ResourceData, ipfieldName string
return sorted
}

<% unless version == "ga" -%>
func flattenStandbyPolicy(standbyPolicy *compute.InstanceGroupManagerStandbyPolicy) []map[string]any{
results := []map[string]any{}
if standbyPolicy != nil {
sp := map[string]any{}
sp["initial_delay_sec"] = standbyPolicy.InitialDelaySec
sp["mode"] = standbyPolicy.Mode
results = append(results, sp)
}
return results
}
<% end -%>

func flattenUpdatePolicy(updatePolicy *compute.InstanceGroupManagerUpdatePolicy) []map[string]interface{} {
results := []map[string]interface{}{}
if updatePolicy != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,42 @@ func TestAccInstanceGroupManager_stateful(t *testing.T) {
})
}

<% unless version == "ga" -%>
func TestAccInstanceGroupManager_stoppedSuspendedTargetSize(t *testing.T) {
t.Parallel()

template := fmt.Sprintf("tf-test-igm-%s", acctest.RandString(t, 10))
igm := fmt.Sprintf("tf-test-igm-%s", acctest.RandString(t, 10))
network := fmt.Sprintf("tf-test-igm-%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckInstanceGroupManagerDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccInstanceGroupManager_stoppedSuspendedTargetSize(template, network, igm),
},
{
ResourceName: "google_compute_instance_group_manager.sr-igm",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"status"},
},
{
Config: testAccInstanceGroupManager_stoppedSuspendedTargetSizeUpdate(template, network, igm),
},
{
ResourceName: "google_compute_instance_group_manager.sr-igm",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"status"},
},
},
})
}
<% end -%>

func TestAccInstanceGroupManager_waitForStatus(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1705,6 +1741,100 @@ resource "google_compute_instance_group_manager" "igm-basic" {
`, network, template, target, igm)
}

<% unless version == "ga" -%>
func testAccInstanceGroupManager_stoppedSuspendedTargetSize(network, template, igm string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}

resource "google_compute_network" "sr-igm" {
name = "%s"
}

resource "google_compute_instance_template" "sr-igm" {
name = "%s"
machine_type = "e2-medium"
disk {
source_image = data.google_compute_image.my_image.self_link
auto_delete = true
boot = true
device_name = "stateful-disk"
}
network_interface {
network = "default"
}
}

resource "google_compute_instance_group_manager" "sr-igm" {
description = "Terraform test instance group manager"
name = "%s"

version {
instance_template = google_compute_instance_template.sr-igm.self_link
name = "primary"
}

base_instance_name = "tf-test-sr-igm"
zone = "us-central1-c"
target_size = 2
standby_policy {
initial_delay_sec = 20
mode = "MANUAL"
}
target_suspended_size = 2
target_stopped_size = 1
}
`, network, template, igm)
}

func testAccInstanceGroupManager_stoppedSuspendedTargetSizeUpdate(network, template, igm string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}

resource "google_compute_network" "sr-igm" {
name = "%s"
}

resource "google_compute_instance_template" "sr-igm" {
name = "%s"
machine_type = "e2-medium"
disk {
source_image = data.google_compute_image.my_image.self_link
auto_delete = true
boot = true
device_name = "stateful-disk"
}
network_interface {
network = "default"
}
}

resource "google_compute_instance_group_manager" "sr-igm" {
description = "Terraform test instance group manager"
name = "%s"

version {
instance_template = google_compute_instance_template.sr-igm.self_link
name = "primary"
}

base_instance_name = "tf-test-sr-igm"
zone = "us-central1-c"
target_size = 2
standby_policy {
mode = "SCALE_OUT_POOL"
}
target_suspended_size = 1
}
`, network, template, igm)
}
<% end -%>

func testAccInstanceGroupManager_waitForStatus(template, target, igm, perInstanceConfig string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,49 @@ func ResourceComputeRegionInstanceGroupManager() *schema.Resource {
},
},

<% unless version == "ga" -%>
"standby_policy": {
Type: schema.TypeList,
Computed: true,
Optional: true,
MaxItems: 1,
Description: `Standby policy for stopped and suspended instances.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"initial_delay_sec": {
Computed: true,
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(0, 3600),
Description: `Specifies the number of seconds that the MIG should wait to suspend or stop a VM after that VM was created. The initial delay gives the initialization script the time to prepare your VM for a quick scale out. The value of initial delay must be between 0 and 3600 seconds. The default value is 0.`,
},

"mode": {
Computed: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"MANUAL", "SCALE_OUT_POOL"}, true),
Description: `Defines how a MIG resumes or starts VMs from a standby pool when the group scales out. The default mode is "MANUAL".`,
},
},
},
},

"target_suspended_size": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: `The target number of suspended instances for this managed instance group.`,
},

"target_stopped_size": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: `The target number of stopped instances for this managed instance group.`,
},
<% end -%>

"update_policy": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -583,6 +626,11 @@ func resourceComputeRegionInstanceGroupManagerCreate(d *schema.ResourceData, met
TargetPools: tpgresource.ConvertStringSet(d.Get("target_pools").(*schema.Set)),
AutoHealingPolicies: expandAutoHealingPolicies(d.Get("auto_healing_policies").([]interface{})),
Versions: expandVersions(d.Get("version").([]interface{})),
<% unless version == "ga" -%>
StandbyPolicy: expandStandbyPolicy(d),
TargetSuspendedSize: int64(d.Get("target_suspended_size").(int)),
TargetStoppedSize: int64(d.Get("target_stopped_size").(int)),
<% end -%>
UpdatePolicy: expandRegionUpdatePolicy(d.Get("update_policy").([]interface{})),
InstanceLifecyclePolicy: expandInstanceLifecyclePolicy(d.Get("instance_lifecycle_policy").([]interface{})),
AllInstancesConfig: expandAllInstancesConfig(nil, d.Get("all_instances_config").([]interface{})),
Expand Down Expand Up @@ -775,6 +823,17 @@ func resourceComputeRegionInstanceGroupManagerRead(d *schema.ResourceData, meta
if err := d.Set("version", flattenVersions(manager.Versions)); err != nil {
return err
}
<% unless version == "ga" -%>
if err = d.Set("standby_policy", flattenStandbyPolicy(manager.StandbyPolicy)); err != nil {
return fmt.Errorf("Error setting standby_policy in state: %s", err.Error())
}
if err := d.Set("target_suspended_size", manager.TargetSuspendedSize); err != nil {
return fmt.Errorf("Error setting target_suspended_size: %s", err)
}
if err := d.Set("target_stopped_size", manager.TargetStoppedSize); err != nil {
return fmt.Errorf("Error setting target_stopped_size: %s", err)
}
<% end -%>
if err := d.Set("update_policy", flattenRegionUpdatePolicy(manager.UpdatePolicy)); err != nil {
return fmt.Errorf("Error setting update_policy in state: %s", err.Error())
}
Expand Down Expand Up @@ -853,6 +912,25 @@ func resourceComputeRegionInstanceGroupManagerUpdate(d *schema.ResourceData, met
change = true
}

<% unless version == "ga" -%>
if d.HasChange("standby_policy") {
updatedManager.StandbyPolicy = expandStandbyPolicy(d)
change = true
}

if d.HasChange("target_suspended_size") {
updatedManager.TargetSuspendedSize = int64(d.Get("target_suspended_size").(int))
updatedManager.ForceSendFields = append(updatedManager.ForceSendFields, "TargetSuspendedSize")
change = true
}

if d.HasChange("target_stopped_size") {
updatedManager.TargetStoppedSize = int64(d.Get("target_stopped_size").(int))
updatedManager.ForceSendFields = append(updatedManager.ForceSendFields, "TargetStoppedSize")
change = true
}
<% end -%>

if d.HasChange("update_policy") {
updatedManager.UpdatePolicy = expandRegionUpdatePolicy(d.Get("update_policy").([]interface{}))
change = true
Expand Down
Loading

0 comments on commit 6503631

Please sign in to comment.