Skip to content

Commit

Permalink
wait for instance status only after create/update (GoogleCloudPlatfor…
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottSuarez authored and khajduczenia committed Oct 12, 2021
1 parent 7da8912 commit e5acfcc
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func resourceComputeInstanceGroupManager() *schema.Resource {
State: resourceInstanceGroupManagerStateImporter,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(5 * time.Minute),
Create: schema.DefaultTimeout(15 * time.Minute),
Update: schema.DefaultTimeout(15 * time.Minute),
Delete: schema.DefaultTimeout(15 * time.Minute),
},

Expand Down Expand Up @@ -471,6 +471,13 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte
return err
}

if d.Get("wait_for_instances").(bool) {
err := computeIGMWaitForInstanceStatus(d, meta)
if err != nil {
return err
}
}

return resourceComputeInstanceGroupManagerRead(d, meta)
}

Expand Down Expand Up @@ -637,30 +644,24 @@ func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interf

// If unset in state set to default value
if d.Get("wait_for_instances_status").(string) == "" {
if err = d.Set("wait_for_instances_status", "STABLE"); err != nil {
if err := d.Set("wait_for_instances_status", "STABLE"); err != nil {
return fmt.Errorf("Error setting wait_for_instances_status in state: %s", err.Error())
}
}

return nil
}

func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

if d.Get("wait_for_instances").(bool) {
waitForUpdates := d.Get("wait_for_instances_status").(string) == "UPDATED"
conf := resource.StateChangeConf{
Pending: []string{"creating", "error", "updating per instance configs", "reaching version target"},
Target: []string{"created"},
Refresh: waitForInstancesRefreshFunc(getManager, waitForUpdates, d, meta),
Timeout: d.Timeout(schema.TimeoutCreate),
}
_, err := conf.WaitForState()
err := computeIGMWaitForInstanceStatus(d, meta)
if err != nil {
return err
}
}

return nil
}

func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
Expand Down Expand Up @@ -767,11 +768,26 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte

d.Partial(false)

if d.Get("wait_for_instances").(bool) {
err := computeIGMWaitForInstanceStatus(d, meta)
if err != nil {
return err
}
}

return resourceComputeInstanceGroupManagerRead(d, meta)
}

func resourceComputeInstanceGroupManagerDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

if d.Get("wait_for_instances").(bool) {
err := computeIGMWaitForInstanceStatus(d, meta)
if err != nil {
return err
}
}

userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
Expand Down Expand Up @@ -828,6 +844,21 @@ func resourceComputeInstanceGroupManagerDelete(d *schema.ResourceData, meta inte
return nil
}

func computeIGMWaitForInstanceStatus(d *schema.ResourceData, meta interface{}) error {
waitForUpdates := d.Get("wait_for_instances_status").(string) == "UPDATED"
conf := resource.StateChangeConf{
Pending: []string{"creating", "error", "updating per instance configs", "reaching version target"},
Target: []string{"created"},
Refresh: waitForInstancesRefreshFunc(getManager, waitForUpdates, d, meta),
Timeout: d.Timeout(schema.TimeoutCreate),
}
_, err := conf.WaitForState()
if err != nil {
return err
}
return nil
}

func expandAutoHealingPolicies(configured []interface{}) []*computeBeta.InstanceGroupManagerAutoHealingPolicy {
autoHealingPolicies := make([]*computeBeta.InstanceGroupManagerAutoHealingPolicy, 0, len(configured))
for _, raw := range configured {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func resourceComputeRegionInstanceGroupManager() *schema.Resource {
State: resourceRegionInstanceGroupManagerStateImporter,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(5 * time.Minute),
Create: schema.DefaultTimeout(15 * time.Minute),
Update: schema.DefaultTimeout(15 * time.Minute),
Delete: schema.DefaultTimeout(15 * time.Minute),
},

Expand Down Expand Up @@ -453,9 +453,32 @@ func resourceComputeRegionInstanceGroupManagerCreate(d *schema.ResourceData, met
if err != nil {
return err
}

if d.Get("wait_for_instances").(bool) {
err := computeRIGMWaitForInstanceStatus(d, meta)
if err != nil {
return err
}
}

return resourceComputeRegionInstanceGroupManagerRead(d, config)
}

func computeRIGMWaitForInstanceStatus(d *schema.ResourceData, meta interface{}) error {
waitForUpdates := d.Get("wait_for_instances_status").(string) == "UPDATED"
conf := resource.StateChangeConf{
Pending: []string{"creating", "error", "updating per instance configs", "reaching version target"},
Target: []string{"created"},
Refresh: waitForInstancesRefreshFunc(getRegionalManager, waitForUpdates, d, meta),
Timeout: d.Timeout(schema.TimeoutCreate),
}
_, err := conf.WaitForState()
if err != nil {
return err
}
return nil
}

type getInstanceManagerFunc func(*schema.ResourceData, interface{}) (*computeBeta.InstanceGroupManager, error)

func getRegionalManager(d *schema.ResourceData, meta interface{}) (*computeBeta.InstanceGroupManager, error) {
Expand Down Expand Up @@ -592,25 +615,19 @@ func resourceComputeRegionInstanceGroupManagerRead(d *schema.ResourceData, meta
}
}

return nil
}

func resourceComputeRegionInstanceGroupManagerUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

if d.Get("wait_for_instances").(bool) {
waitForUpdates := d.Get("wait_for_instances_status").(string) == "UPDATED"
conf := resource.StateChangeConf{
Pending: []string{"creating", "error", "updating per instance configs", "reaching version target"},
Target: []string{"created"},
Refresh: waitForInstancesRefreshFunc(getRegionalManager, waitForUpdates, d, meta),
Timeout: d.Timeout(schema.TimeoutCreate),
}
_, err := conf.WaitForState()
err := computeRIGMWaitForInstanceStatus(d, meta)
if err != nil {
return err
}
}

return nil
}

func resourceComputeRegionInstanceGroupManagerUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
Expand Down Expand Up @@ -711,11 +728,26 @@ func resourceComputeRegionInstanceGroupManagerUpdate(d *schema.ResourceData, met

d.Partial(false)

if d.Get("wait_for_instances").(bool) {
err := computeRIGMWaitForInstanceStatus(d, meta)
if err != nil {
return err
}
}

return resourceComputeRegionInstanceGroupManagerRead(d, meta)
}

func resourceComputeRegionInstanceGroupManagerDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

if d.Get("wait_for_instances").(bool) {
err := computeRIGMWaitForInstanceStatus(d, meta)
if err != nil {
return err
}
}

userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ The `per_instance_configs` block holds:
This resource provides the following
[Timeouts](/docs/configuration/resources.html#timeouts) configuration options:

- `create` - Default is 5 minutes.
- `update` - Default is 5 minutes.
- `create` - Default is 15 minutes.
- `update` - Default is 15 minutes.
- `delete` - Default is 15 minutes.


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ The `per_instance_configs` block holds:
This resource provides the following
[Timeouts](/docs/configuration/resources.html#timeouts) configuration options:

- `create` - Default is 5 minutes.
- `update` - Default is 5 minutes.
- `create` - Default is 15 minutes.
- `update` - Default is 15 minutes.
- `delete` - Default is 15 minutes.


Expand Down

0 comments on commit e5acfcc

Please sign in to comment.