func waitForLoadBalancerDelete(networkingClient *gophercloud.ServiceClient, lbID string) resource.StateRefreshFunc { return func() (interface{}, string, error) { log.Printf("[DEBUG] Attempting to delete OpenStack LBaaSV2 LoadBalancer %s", lbID) lb, err := loadbalancers.Get(networkingClient, lbID).Extract() if err != nil { if _, ok := err.(gophercloud.ErrDefault404); ok { log.Printf("[DEBUG] Successfully deleted OpenStack LBaaSV2 LoadBalancer %s", lbID) return lb, "DELETED", nil } return lb, "ACTIVE", err } log.Printf("[DEBUG] Openstack LoadBalancerV2: %+v", lb) err = loadbalancers.Delete(networkingClient, lbID).ExtractErr() if err != nil { if _, ok := err.(gophercloud.ErrDefault404); ok { log.Printf("[DEBUG] Successfully deleted OpenStack LBaaSV2 LoadBalancer %s", lbID) return lb, "DELETED", nil } if errCode, ok := err.(gophercloud.ErrUnexpectedResponseCode); ok { if errCode.Actual == 409 { log.Printf("[DEBUG] OpenStack LBaaSV2 LoadBalancer (%s) is still in use.", lbID) return lb, "ACTIVE", nil } } return lb, "ACTIVE", err } log.Printf("[DEBUG] OpenStack LBaaSV2 LoadBalancer (%s) still active.", lbID) return lb, "ACTIVE", nil } }
func TestDeleteLoadbalancer(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() HandleLoadbalancerDeletionSuccessfully(t) res := loadbalancers.Delete(fake.ServiceClient(), "36e08a3e-a78f-4b40-a229-1e7e23eee1ab") th.AssertNoErr(t, res.Err) }
// DeleteLoadBalancer will delete a specified loadbalancer. A fatal error will // occur if the loadbalancer could not be deleted. This works best when used // as a deferred function. func DeleteLoadBalancer(t *testing.T, client *gophercloud.ServiceClient, lbID string) { t.Logf("Attempting to delete loadbalancer %s", lbID) if err := loadbalancers.Delete(client, lbID).ExtractErr(); err != nil { t.Fatalf("Unable to delete loadbalancer: %v", err) } t.Logf("Waiting for loadbalancer %s to delete", lbID) if err := WaitForLoadBalancerState(client, lbID, "DELETED", loadbalancerActiveTimeoutSeconds); err != nil { t.Fatalf("Loadbalancer did not delete in time.") } t.Logf("Successfully deleted loadbalancer %s", lbID) }