// Delete load balancer func (os *OpenStack) DeleteLoadBalancer(name string) error { vip, err := os.getVipByName(name) if err != nil && err != ErrNotFound { return err } // We have to delete the VIP before the pool can be deleted, // so no point continuing if this fails. if vip != nil { err = vips.Delete(os.network, vip.ID).ExtractErr() if err != nil && !isNotFound(err) { return err } } var pool *pools.Pool if vip != nil { pool, err = pools.Get(os.network, vip.PoolID).Extract() if err != nil && !isNotFound(err) { return err } } else { // The VIP is gone, but it is conceivable that a Pool // still exists that we failed to delete on some // previous occasion. Make a best effort attempt to // cleanup any pools with the same name as the VIP. pool, err = os.getPoolByName(name) if err != nil && err != ErrNotFound { return err } } if pool != nil { for _, monId := range pool.MonitorIDs { _, err = pools.DisassociateMonitor(os.network, pool.ID, monId).Extract() if err != nil { return err } err = monitors.Delete(os.network, monId).ExtractErr() if err != nil && !isNotFound(err) { return err } } err = pools.Delete(os.network, pool.ID).ExtractErr() if err != nil && !isNotFound(err) { return err } } return nil }
func TestDisassociateHealthMonitor(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/v2.0/lb/pools/332abe93-f488-41ba-870b-2ac66be7f853/health_monitors/b624decf-d5d3-4c66-9a3d-f047e7786181", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "DELETE") th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) w.WriteHeader(http.StatusNoContent) }) res := pools.DisassociateMonitor(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", "b624decf-d5d3-4c66-9a3d-f047e7786181") th.AssertNoErr(t, res.Err) }
func TestPoolsMonitors(t *testing.T) { client, err := clients.NewNetworkV2Client() if err != nil { t.Fatalf("Unable to create a network client: %v", err) } network, err := networking.CreateNetwork(t, client) if err != nil { t.Fatalf("Unable to create network: %v", err) } defer networking.DeleteNetwork(t, client, network.ID) subnet, err := networking.CreateSubnet(t, client, network.ID) if err != nil { t.Fatalf("Unable to create subnet: %v", err) } defer networking.DeleteSubnet(t, client, subnet.ID) pool, err := CreatePool(t, client, subnet.ID) if err != nil { t.Fatalf("Unable to create pool: %v", err) } defer DeletePool(t, client, pool.ID) monitor, err := CreateMonitor(t, client) if err != nil { t.Fatalf("Unable to create monitor: %v", err) } defer DeleteMonitor(t, client, monitor.ID) t.Logf("Associating monitor %s with pool %s", monitor.ID, pool.ID) if res := pools.AssociateMonitor(client, pool.ID, monitor.ID); res.Err != nil { t.Fatalf("Unable to associate monitor to pool") } t.Logf("Disassociating monitor %s with pool %s", monitor.ID, pool.ID) if res := pools.DisassociateMonitor(client, pool.ID, monitor.ID); res.Err != nil { t.Fatalf("Unable to disassociate monitor from pool") } }
func resourceLBPoolV1Delete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) networkingClient, err := config.networkingV2Client(d.Get("region").(string)) if err != nil { return fmt.Errorf("Error creating OpenStack networking client: %s", err) } // Make sure all monitors are disassociated first if v, ok := d.GetOk("monitor_ids"); ok { if monitorIDList, ok := v.([]interface{}); ok { for _, monitorID := range monitorIDList { mID := monitorID.(string) log.Printf("[DEBUG] Attempting to disassociate monitor %s from pool %s", mID, d.Id()) if res := pools.DisassociateMonitor(networkingClient, d.Id(), mID); res.Err != nil { return fmt.Errorf("Error disassociating monitor %s from pool %s: %s", mID, d.Id(), err) } } } } stateConf := &resource.StateChangeConf{ Pending: []string{"ACTIVE", "PENDING_DELETE"}, Target: []string{"DELETED"}, Refresh: waitForLBPoolDelete(networkingClient, d.Id()), Timeout: 2 * time.Minute, Delay: 5 * time.Second, MinTimeout: 3 * time.Second, } _, err = stateConf.WaitForState() if err != nil { return fmt.Errorf("Error deleting OpenStack LB Pool: %s", err) } d.SetId("") return nil }
func resourceLBPoolV1Update(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) networkingClient, err := config.networkingV2Client(d.Get("region").(string)) if err != nil { return fmt.Errorf("Error creating OpenStack networking client: %s", err) } var updateOpts pools.UpdateOpts // If either option changed, update both. // Gophercloud complains if one is empty. if d.HasChange("name") || d.HasChange("lb_method") { updateOpts.Name = d.Get("name").(string) lbMethod := resourceLBPoolV1DetermineLBMethod(d.Get("lb_method").(string)) updateOpts.LBMethod = lbMethod } log.Printf("[DEBUG] Updating OpenStack LB Pool %s with options: %+v", d.Id(), updateOpts) _, err = pools.Update(networkingClient, d.Id(), updateOpts).Extract() if err != nil { return fmt.Errorf("Error updating OpenStack LB Pool: %s", err) } if d.HasChange("monitor_ids") { oldMIDsRaw, newMIDsRaw := d.GetChange("security_groups") oldMIDsSet, newMIDsSet := oldMIDsRaw.(*schema.Set), newMIDsRaw.(*schema.Set) monitorsToAdd := newMIDsSet.Difference(oldMIDsSet) monitorsToRemove := oldMIDsSet.Difference(newMIDsSet) log.Printf("[DEBUG] Monitors to add: %v", monitorsToAdd) log.Printf("[DEBUG] Monitors to remove: %v", monitorsToRemove) for _, m := range monitorsToAdd.List() { _, err := pools.AssociateMonitor(networkingClient, d.Id(), m.(string)).Extract() if err != nil { return fmt.Errorf("Error associating monitor (%s) with OpenStack server (%s): %s", m.(string), d.Id(), err) } log.Printf("[DEBUG] Associated monitor (%s) with pool (%s)", m.(string), d.Id()) } for _, m := range monitorsToRemove.List() { _, err := pools.DisassociateMonitor(networkingClient, d.Id(), m.(string)).Extract() if err != nil { return fmt.Errorf("Error disassociating monitor (%s) from OpenStack server (%s): %s", m.(string), d.Id(), err) } log.Printf("[DEBUG] Disassociated monitor (%s) from pool (%s)", m.(string), d.Id()) } } if d.HasChange("member") { oldMembersRaw, newMembersRaw := d.GetChange("member") oldMembersSet, newMembersSet := oldMembersRaw.(*schema.Set), newMembersRaw.(*schema.Set) membersToAdd := newMembersSet.Difference(oldMembersSet) membersToRemove := oldMembersSet.Difference(newMembersSet) log.Printf("[DEBUG] Members to add: %v", membersToAdd) log.Printf("[DEBUG] Members to remove: %v", membersToRemove) for _, m := range membersToRemove.List() { oldMember := resourcePoolMemberV1(d, m) listOpts := members.ListOpts{ PoolID: d.Id(), Address: oldMember.Address, ProtocolPort: oldMember.ProtocolPort, } err = members.List(networkingClient, listOpts).EachPage(func(page pagination.Page) (bool, error) { extractedMembers, err := members.ExtractMembers(page) if err != nil { return false, err } for _, member := range extractedMembers { err := members.Delete(networkingClient, member.ID).ExtractErr() if err != nil { return false, fmt.Errorf("Error deleting member (%s) from OpenStack LB pool (%s): %s", member.ID, d.Id(), err) } log.Printf("[DEBUG] Deleted member (%s) from pool (%s)", member.ID, d.Id()) } return true, nil }) } for _, m := range membersToAdd.List() { createOpts := resourcePoolMemberV1(d, m) newMember, err := members.Create(networkingClient, createOpts).Extract() if err != nil { return fmt.Errorf("Error creating LB member: %s", err) } log.Printf("[DEBUG] Created member (%s) in OpenStack LB pool (%s)", newMember.ID, d.Id()) } } return resourceLBPoolV1Read(d, meta) }