Skip to content

Commit

Permalink
fix: instance group manager policy synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
deepaksibm authored and hkantare committed May 17, 2021
1 parent 763474d commit 8a0c89b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
9 changes: 4 additions & 5 deletions ibm/resource_ibm_is_instance_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func resourceIBMISInstanceGroupCreate(d *schema.ResourceData, meta interface{})
}
d.SetId(*instanceGroup.ID)

_, healthError := waitForHealthyInstanceGroup(d, meta, d.Timeout(schema.TimeoutCreate))
_, healthError := waitForHealthyInstanceGroup(d.Id(), meta, d.Timeout(schema.TimeoutCreate))
if healthError != nil {
return healthError
}
Expand Down Expand Up @@ -339,7 +339,7 @@ func resourceIBMISInstanceGroupUpdate(d *schema.ResourceData, meta interface{})
}

// wait for instance group health update with update timeout configured.
_, healthError := waitForHealthyInstanceGroup(d, meta, d.Timeout(schema.TimeoutUpdate))
_, healthError := waitForHealthyInstanceGroup(instanceGroupID, meta, d.Timeout(schema.TimeoutUpdate))
if healthError != nil {
return healthError
}
Expand Down Expand Up @@ -445,7 +445,7 @@ func resourceIBMISInstanceGroupDelete(d *schema.ResourceData, meta interface{})
if err != nil {
return fmt.Errorf("Error updating instanceGroup's instance count to 0 : %s\n%s", err, response)
}
_, healthError := waitForHealthyInstanceGroup(d, meta, d.Timeout(schema.TimeoutUpdate))
_, healthError := waitForHealthyInstanceGroup(instanceGroupID, meta, d.Timeout(schema.TimeoutUpdate))
if healthError != nil {
return healthError
}
Expand Down Expand Up @@ -511,13 +511,12 @@ func resourceIBMISInstanceGroupExists(d *schema.ResourceData, meta interface{})
return true, nil
}

func waitForHealthyInstanceGroup(d *schema.ResourceData, meta interface{}, timeout time.Duration) (interface{}, error) {
func waitForHealthyInstanceGroup(instanceGroupID string, meta interface{}, timeout time.Duration) (interface{}, error) {
sess, err := vpcClient(meta)
if err != nil {
return nil, err
}

instanceGroupID := d.Id()
getInstanceGroupOptions := vpcv1.GetInstanceGroupOptions{ID: &instanceGroupID}

healthStateConf := &resource.StateChangeConf{
Expand Down
29 changes: 29 additions & 0 deletions ibm/resource_ibm_is_instance_group_manager_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ func resourceIBMISInstanceGroupManagerPolicyCreate(d *schema.ResourceData, meta
InstanceGroupManagerID: &instanceGroupManagerID,
InstanceGroupManagerPolicyPrototype: &instanceGroupManagerPolicyPrototype,
}

isInsGrpKey := "Instance_Group_Key_" + instanceGroupID
ibmMutexKV.Lock(isInsGrpKey)
defer ibmMutexKV.Unlock(isInsGrpKey)

_, healthError := waitForHealthyInstanceGroup(instanceGroupID, meta, d.Timeout(schema.TimeoutCreate))
if healthError != nil {
return healthError
}

data, response, err := sess.CreateInstanceGroupManagerPolicy(&createInstanceGroupManagerPolicyOptions)
if err != nil || data == nil {
return fmt.Errorf("Error Creating InstanceGroup Manager Policy: %s\n%s", err, response)
Expand Down Expand Up @@ -180,6 +190,15 @@ func resourceIBMISInstanceGroupManagerPolicyUpdate(d *schema.ResourceData, meta
updateInstanceGroupManagerPolicyOptions.InstanceGroupID = &instanceGroupID
updateInstanceGroupManagerPolicyOptions.InstanceGroupManagerID = &instanceGroupManagerID

isInsGrpKey := "Instance_Group_Key_" + instanceGroupID
ibmMutexKV.Lock(isInsGrpKey)
defer ibmMutexKV.Unlock(isInsGrpKey)

_, healthError := waitForHealthyInstanceGroup(instanceGroupID, meta, d.Timeout(schema.TimeoutUpdate))
if healthError != nil {
return healthError
}

_, response, err := sess.UpdateInstanceGroupManagerPolicy(&updateInstanceGroupManagerPolicyOptions)
if err != nil {
return fmt.Errorf("Error Updating InstanceGroup Manager Policy: %s\n%s", err, response)
Expand Down Expand Up @@ -245,6 +264,16 @@ func resourceIBMISInstanceGroupManagerPolicyDelete(d *schema.ResourceData, meta
InstanceGroupManagerID: &instanceGroupManagerID,
InstanceGroupID: &instanceGroupID,
}

isInsGrpKey := "Instance_Group_Key_" + instanceGroupID
ibmMutexKV.Lock(isInsGrpKey)
defer ibmMutexKV.Unlock(isInsGrpKey)

_, healthError := waitForHealthyInstanceGroup(instanceGroupID, meta, d.Timeout(schema.TimeoutDelete))
if healthError != nil {
return healthError
}

response, err := sess.DeleteInstanceGroupManagerPolicy(&deleteInstanceGroupManagerPolicyOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
Expand Down

0 comments on commit 8a0c89b

Please sign in to comment.