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

fix(public_gateway): added retry logic for public gateway deletion #4503

Merged
merged 1 commit into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
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
61 changes: 60 additions & 1 deletion ibm/service/vpc/resource_ibm_is_public_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"log"
"os"
"strings"
"time"

"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
Expand Down Expand Up @@ -457,7 +458,32 @@ func resourceIBMISPublicGatewayDelete(d *schema.ResourceData, meta interface{})
}
response, err = sess.DeletePublicGateway(deletePublicGatewayOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Deleting Public Gateway : %s\n%s", err, response)
if response.StatusCode == 409 && strings.Contains(strings.ToLower(err.Error()), strings.ToLower("The Public Gateway is in use by subnet")) {
listSubnetsOptions := &vpcv1.ListSubnetsOptions{}
subnets, _, _ := sess.ListSubnets(listSubnetsOptions)
for _, s := range subnets.Subnets {
if s.PublicGateway != nil && id == *s.PublicGateway.ID {
unsetSubnetPublicGatewayOptions := &vpcv1.UnsetSubnetPublicGatewayOptions{
ID: s.ID,
}
res, errSub := sess.UnsetSubnetPublicGateway(unsetSubnetPublicGatewayOptions)
if res.StatusCode == 204 {
_, err = isWaitForSubnetPublicGatewayUnset(sess, *s.ID, d.Timeout(schema.TimeoutDelete))
if err != nil {
return err
}
response, err = sess.DeletePublicGateway(deletePublicGatewayOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error Deleting Public Gateway : %s\n%s", err, response)
}
} else {
return fmt.Errorf("[ERROR] Error Unsetting Public Gateway : %s\n%s", errSub, res)
}
}
}
} else {
return fmt.Errorf("[ERROR] Error Deleting Public Gateway : error is %s\n%s", err, response)
}
}
_, err = isWaitForPublicGatewayDeleted(sess, id, d.Timeout(schema.TimeoutDelete))
if err != nil {
Expand Down Expand Up @@ -517,3 +543,36 @@ func resourceIBMISPublicGatewayExists(d *schema.ResourceData, meta interface{})
}
return true, nil
}

func isWaitForSubnetPublicGatewayUnset(subnetC *vpcv1.VpcV1, id string, timeout time.Duration) (interface{}, error) {
log.Printf("Waiting for public gateway (%s) to be unset.", id)

stateConf := &resource.StateChangeConf{
Pending: []string{"retry", "wait"},
Target: []string{"done", ""},
Refresh: isSubnetPublicGatewayUnsetRefreshFunc(subnetC, id),
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 10 * time.Second,
}

return stateConf.WaitForState()
}

func isSubnetPublicGatewayUnsetRefreshFunc(subnetC *vpcv1.VpcV1, id string) resource.StateRefreshFunc {
log.Printf("Waiting for public gateway (%s) to be unset.", id)
return func() (interface{}, string, error) {
getSubnetPublicGatewayOptions := &vpcv1.GetSubnetPublicGatewayOptions{
ID: &id,
}
subnetPublicGateway, response, err := subnetC.GetSubnetPublicGateway(getSubnetPublicGatewayOptions)
if err != nil {
if response.StatusCode == 404 {
return subnetPublicGateway, "done", nil
}
return subnetPublicGateway, "", fmt.Errorf("[ERROR] Error getting Subnet PublicGateway : %s\n%s", err, response)
}

return subnetPublicGateway, "wait", nil
}
}
58 changes: 58 additions & 0 deletions ibm/service/vpc/resource_ibm_is_public_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,41 @@ func TestAccIBMISPublicGateway_basic(t *testing.T) {
},
})
}
func TestAccIBMISPublicGateway_resource_group_change(t *testing.T) {
var publicgw string
vpcname := fmt.Sprintf("tfpgw-vpc-%d", acctest.RandIntRange(10, 100))
name1 := fmt.Sprintf("tf-create-name-%d", acctest.RandIntRange(10, 100))
subnetname := fmt.Sprintf("tfpgw-subnet-%d", acctest.RandIntRange(10, 100))
flag := true

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckIBMISPublicGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMISPublicGatewayRgChangeConfig(vpcname, subnetname, name1, !flag),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMISPublicGatewayExists("ibm_is_public_gateway.testacc_public_gateway", publicgw),
resource.TestCheckResourceAttr(
"ibm_is_public_gateway.testacc_public_gateway", "name", name1),
resource.TestCheckResourceAttr(
"ibm_is_public_gateway.testacc_public_gateway", "zone", acc.ISZoneName),
),
},
{
Config: testAccCheckIBMISPublicGatewayRgChangeConfig(vpcname, subnetname, name1, flag),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMISPublicGatewayExists("ibm_is_public_gateway.testacc_public_gateway", publicgw),
resource.TestCheckResourceAttr(
"ibm_is_public_gateway.testacc_public_gateway", "name", name1),
resource.TestCheckResourceAttr(
"ibm_is_public_gateway.testacc_public_gateway", "zone", acc.ISZoneName),
),
},
},
})
}

func testAccCheckIBMISPublicGatewayDestroy(s *terraform.State) error {
sess, _ := acc.TestAccProvider.Meta().(conns.ClientSession).VpcV1API()
Expand Down Expand Up @@ -115,3 +150,26 @@ resource "ibm_is_public_gateway" "testacc_public_gateway" {
}`, vpcname, name, zone)

}
func testAccCheckIBMISPublicGatewayRgChangeConfig(vpcname, subnetname, name string, flag bool) string {
return fmt.Sprintf(`
resource "ibm_is_vpc" "testacc_vpc" {
name = "%s"
}
resource ibm_is_subnet subnet {
name = "%s"
vpc = ibm_is_vpc.testacc_vpc.id
zone = "%s"
total_ipv4_address_count = 16
public_gateway = ibm_is_public_gateway.testacc_public_gateway.id
}

resource "ibm_is_public_gateway" "testacc_public_gateway" {
name = "%s"
resource_group = %t ? "%s": null
vpc = "${ibm_is_vpc.testacc_vpc.id}"
zone = "%s"
}

`, vpcname, subnetname, acc.ISZoneName, name, flag, acc.IsResourceGroupID, acc.ISZoneName)

}