Jamie Hannaford | c9da4b4 | 2014-11-05 16:34:56 +0100 | [diff] [blame^] | 1 | // +build acceptance lbs |
| 2 | |
| 3 | package v1 |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | "github.com/rackspace/gophercloud/rackspace/lb/v1/lbs" |
| 11 | "github.com/rackspace/gophercloud/rackspace/lb/v1/vips" |
| 12 | th "github.com/rackspace/gophercloud/testhelper" |
| 13 | ) |
| 14 | |
| 15 | func TestVIPs(t *testing.T) { |
| 16 | client := setup(t) |
| 17 | |
| 18 | ids := createLB(t, client, 1) |
| 19 | lbID := ids[0] |
| 20 | |
| 21 | listVIPs(t, client, lbID) |
| 22 | |
| 23 | vipIDs := addVIPs(t, client, lbID, 3) |
| 24 | |
| 25 | deleteVIP(t, client, lbID, vipIDs[0]) |
| 26 | |
| 27 | bulkDeleteVIPs(t, client, lbID, vipIDs[1:]) |
| 28 | } |
| 29 | |
| 30 | func listVIPs(t *testing.T, client *gophercloud.ServiceClient, lbID int) { |
| 31 | err := vips.List(client, lbID).EachPage(func(page pagination.Page) (bool, error) { |
| 32 | vipList, err := vips.ExtractVIPs(page) |
| 33 | th.AssertNoErr(t, err) |
| 34 | |
| 35 | for _, vip := range vipList { |
| 36 | t.Logf("Listing VIP: ID [%s] Address [%s] Type [%s] Version [%s]", |
| 37 | vip.ID, vip.Address, vip.Type, vip.Version) |
| 38 | } |
| 39 | |
| 40 | return true, nil |
| 41 | }) |
| 42 | th.AssertNoErr(t, err) |
| 43 | } |
| 44 | |
| 45 | func addVIPs(t *testing.T, client *gophercloud.ServiceClient, lbID, count int) []int { |
| 46 | ids := []int{} |
| 47 | |
| 48 | for i := 0; i < count; i++ { |
| 49 | opts := vips.CreateOpts{ |
| 50 | Type: vips.PUBLIC, |
| 51 | Version: vips.IPV6, |
| 52 | } |
| 53 | |
| 54 | vip, err := vips.Create(client, lbID, opts).Extract() |
| 55 | th.AssertNoErr(t, err) |
| 56 | |
| 57 | t.Logf("Created VIP %d", vip.ID) |
| 58 | |
| 59 | waitForLB(client, lbID, lbs.ACTIVE) |
| 60 | |
| 61 | ids = append(ids, vip.ID) |
| 62 | } |
| 63 | |
| 64 | return ids |
| 65 | } |
| 66 | |
| 67 | func deleteVIP(t *testing.T, client *gophercloud.ServiceClient, lbID, vipID int) { |
| 68 | err := vips.Delete(client, lbID, vipID).ExtractErr() |
| 69 | th.AssertNoErr(t, err) |
| 70 | |
| 71 | t.Logf("Deleted VIP %d", vipID) |
| 72 | |
| 73 | waitForLB(client, lbID, lbs.ACTIVE) |
| 74 | } |
| 75 | |
| 76 | func bulkDeleteVIPs(t *testing.T, client *gophercloud.ServiceClient, lbID int, ids []int) { |
| 77 | err := vips.BulkDelete(client, lbID, ids).ExtractErr() |
| 78 | th.AssertNoErr(t, err) |
| 79 | t.Logf("Deleted VIPs %s", intsToStr(ids)) |
| 80 | } |