Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug - subnet destroy retry when attached resources in use #2895

Merged
merged 1 commit into from
Jul 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion ibm/resource_ibm_is_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
isSubnetDeleting = "deleting"
isSubnetDeleted = "done"
isSubnetRoutingTableID = "routing_table"
isSubnetInUse = "resources_attached"
)

func resourceIBMISSubnet() *schema.Resource {
Expand Down Expand Up @@ -618,7 +619,11 @@ func subnetDelete(d *schema.ResourceData, meta interface{}, id string) error {
}
response, err = sess.DeleteSubnet(deleteSubnetOptions)
if err != nil {
return fmt.Errorf("Error Deleting Subnet : %s\n%s", err, response)
if response != nil && response.StatusCode == 409 {
_, err = isWaitForSubnetDeleteRetry(sess, d.Id(), d.Timeout(schema.TimeoutDelete))
} else {
return fmt.Errorf("Error Deleting Subnet : %s\n%s", err, response)
}
}
_, err = isWaitForSubnetDeleted(sess, d.Id(), d.Timeout(schema.TimeoutDelete))
if err != nil {
Expand All @@ -628,6 +633,34 @@ func subnetDelete(d *schema.ResourceData, meta interface{}, id string) error {
return nil
}

func isWaitForSubnetDeleteRetry(vpcClient *vpcv1.VpcV1, id string, timeout time.Duration) (interface{}, error) {
log.Printf("Retrying subnet (%s) delete", id)
stateConf := &resource.StateChangeConf{
Pending: []string{isSubnetInUse},
Target: []string{isSubnetDeleting, isSubnetDeleted, ""},
Refresh: func() (interface{}, string, error) {
deleteSubnetOptions := &vpcv1.DeleteSubnetOptions{
ID: &id,
}
log.Printf("Retrying subnet (%s) delete", id)
response, err := vpcClient.DeleteSubnet(deleteSubnetOptions)
if err != nil {
if response != nil && response.StatusCode == 409 {
return response, isSubnetInUse, nil
} else if response != nil && response.StatusCode == 404 {
return response, isSubnetDeleted, nil
}
return response, "", fmt.Errorf("Error deleting subnet: %s\n%s", err, response)
}
return response, isSubnetDeleting, nil
},
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 10 * time.Second,
}
return stateConf.WaitForState()
}

func isWaitForSubnetDeleted(subnetC *vpcv1.VpcV1, id string, timeout time.Duration) (interface{}, error) {
log.Printf("Waiting for subnet (%s) to be deleted.", id)

Expand Down