mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
provider/google: Fix instance group manager instance restart policy
This commit is contained in:
parent
8935013f69
commit
7be90215bc
@ -134,6 +134,10 @@ func computeOperationWaitRegion(config *Config, op *compute.Operation, region, a
|
|||||||
}
|
}
|
||||||
|
|
||||||
func computeOperationWaitZone(config *Config, op *compute.Operation, zone, activity string) error {
|
func computeOperationWaitZone(config *Config, op *compute.Operation, zone, activity string) error {
|
||||||
|
return computeOperationWaitZoneTime(config, op, zone, 4, activity)
|
||||||
|
}
|
||||||
|
|
||||||
|
func computeOperationWaitZoneTime(config *Config, op *compute.Operation, zone string, minutes int, activity string) error {
|
||||||
w := &ComputeOperationWaiter{
|
w := &ComputeOperationWaiter{
|
||||||
Service: config.clientCompute,
|
Service: config.clientCompute,
|
||||||
Op: op,
|
Op: op,
|
||||||
@ -143,7 +147,7 @@ func computeOperationWaitZone(config *Config, op *compute.Operation, zone, activ
|
|||||||
}
|
}
|
||||||
state := w.Conf()
|
state := w.Conf()
|
||||||
state.Delay = 10 * time.Second
|
state.Delay = 10 * time.Second
|
||||||
state.Timeout = 4 * time.Minute
|
state.Timeout = time.Duration(minutes) * time.Minute
|
||||||
state.MinTimeout = 2 * time.Second
|
state.MinTimeout = 2 * time.Second
|
||||||
opRaw, err := state.WaitForState()
|
opRaw, err := state.WaitForState()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -53,6 +53,12 @@ func resourceComputeInstanceGroupManager() *schema.Resource {
|
|||||||
Required: true,
|
Required: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"update_strategy": &schema.Schema{
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Optional: true,
|
||||||
|
Default: "RESTART",
|
||||||
|
},
|
||||||
|
|
||||||
"target_pools": &schema.Schema{
|
"target_pools": &schema.Schema{
|
||||||
Type: schema.TypeSet,
|
Type: schema.TypeSet,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
@ -112,6 +118,11 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte
|
|||||||
manager.TargetPools = s
|
manager.TargetPools = s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateStrategy := d.Get("update_strategy").(string)
|
||||||
|
if !(updateStrategy == "NONE" || updateStrategy == "RESTART") {
|
||||||
|
return fmt.Errorf("Update strategy must be \"NONE\" or \"RESTART\"")
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("[DEBUG] InstanceGroupManager insert request: %#v", manager)
|
log.Printf("[DEBUG] InstanceGroupManager insert request: %#v", manager)
|
||||||
op, err := config.clientCompute.InstanceGroupManagers.Insert(
|
op, err := config.clientCompute.InstanceGroupManagers.Insert(
|
||||||
config.Project, d.Get("zone").(string), manager).Do()
|
config.Project, d.Get("zone").(string), manager).Do()
|
||||||
@ -209,6 +220,35 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if d.Get("update_strategy").(string) == "RESTART" {
|
||||||
|
managedInstances, err := config.clientCompute.InstanceGroupManagers.ListManagedInstances(
|
||||||
|
config.Project, d.Get("zone").(string), d.Id()).Do()
|
||||||
|
|
||||||
|
managedInstanceCount := len(managedInstances.ManagedInstances)
|
||||||
|
instances := make([]string, managedInstanceCount)
|
||||||
|
for i, v := range managedInstances.ManagedInstances {
|
||||||
|
instances[i] = v.Instance
|
||||||
|
}
|
||||||
|
|
||||||
|
recreateInstances := &compute.InstanceGroupManagersRecreateInstancesRequest{
|
||||||
|
Instances: instances,
|
||||||
|
}
|
||||||
|
|
||||||
|
op, err = config.clientCompute.InstanceGroupManagers.RecreateInstances(
|
||||||
|
config.Project, d.Get("zone").(string), d.Id(), recreateInstances).Do()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error restarting instance group managers instances: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for the operation to complete
|
||||||
|
err = computeOperationWaitZoneTime(config, op, d.Get("zone").(string),
|
||||||
|
managedInstanceCount * 4, "Restarting InstanceGroupManagers instances")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
d.SetPartial("instance_template")
|
d.SetPartial("instance_template")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ resource "google_compute_instance_group_manager" "foobar" {
|
|||||||
description = "Terraform test instance group manager"
|
description = "Terraform test instance group manager"
|
||||||
name = "terraform-test"
|
name = "terraform-test"
|
||||||
instance_template = "${google_compute_instance_template.foobar.self_link}"
|
instance_template = "${google_compute_instance_template.foobar.self_link}"
|
||||||
|
update_strategy= "NONE"
|
||||||
target_pools = ["${google_compute_target_pool.foobar.self_link}"]
|
target_pools = ["${google_compute_target_pool.foobar.self_link}"]
|
||||||
base_instance_name = "foobar"
|
base_instance_name = "foobar"
|
||||||
zone = "us-central1-a"
|
zone = "us-central1-a"
|
||||||
@ -41,7 +42,13 @@ instance name.
|
|||||||
group manager.
|
group manager.
|
||||||
|
|
||||||
* `instance_template` - (Required) The full URL to an instance template from
|
* `instance_template` - (Required) The full URL to an instance template from
|
||||||
which all new instances will be created.
|
which all new instances will be created.
|
||||||
|
|
||||||
|
* `update_strategy` - (Optional, Default `"RESTART"`) If the `instance_template` resource is
|
||||||
|
modified, a value of `"NONE"` will prevent any of the managed instances from
|
||||||
|
being restarted by Terraform. A value of `"RESTART"` will restart all of the
|
||||||
|
instances at once. In the future, as the GCE API matures we will support
|
||||||
|
`"ROLLING_UPDATE"` as well.
|
||||||
|
|
||||||
* `name` - (Required) The name of the instance group manager. Must be 1-63
|
* `name` - (Required) The name of the instance group manager. Must be 1-63
|
||||||
characters long and comply with [RFC1035](https://www.ietf.org/rfc/rfc1035.txt).
|
characters long and comply with [RFC1035](https://www.ietf.org/rfc/rfc1035.txt).
|
||||||
|
Loading…
Reference in New Issue
Block a user