Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 1 | // +build acceptance lbs |
| 2 | |
| 3 | package v1 |
| 4 | |
| 5 | import ( |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 6 | "testing" |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 7 | |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | "github.com/rackspace/gophercloud/rackspace/lb/v1/acl" |
| 11 | "github.com/rackspace/gophercloud/rackspace/lb/v1/lbs" |
| 12 | th "github.com/rackspace/gophercloud/testhelper" |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 13 | ) |
| 14 | |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 15 | func TestACL(t *testing.T) { |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 16 | return |
| 17 | client := setup(t) |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 18 | |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 19 | ids := createLB(t, client, 1) |
| 20 | lbID := ids[0] |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 21 | |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 22 | createACL(t, client, lbID) |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 23 | |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 24 | waitForLB(client, lbID, lbs.ACTIVE) |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 25 | |
| 26 | networkIDs := showACL(t, client, lbID) |
| 27 | |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 28 | deleteNetworkItem(t, client, lbID, networkIDs[0]) |
| 29 | waitForLB(client, lbID, lbs.ACTIVE) |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 30 | |
| 31 | bulkDeleteACL(t, client, lbID, networkIDs[1:2]) |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 32 | waitForLB(client, lbID, lbs.ACTIVE) |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 33 | |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 34 | deleteACL(t, client, lbID) |
| 35 | waitForLB(client, lbID, lbs.ACTIVE) |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 36 | |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 37 | deleteLB(t, client, lbID) |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | func createACL(t *testing.T, client *gophercloud.ServiceClient, lbID int) { |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 41 | opts := acl.CreateOpts{ |
| 42 | acl.CreateOpt{Address: "206.160.163.21", Type: acl.DENY}, |
| 43 | acl.CreateOpt{Address: "206.160.165.11", Type: acl.DENY}, |
| 44 | acl.CreateOpt{Address: "206.160.165.12", Type: acl.DENY}, |
| 45 | acl.CreateOpt{Address: "206.160.165.13", Type: acl.ALLOW}, |
| 46 | } |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 47 | |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 48 | err := acl.Create(client, lbID, opts).ExtractErr() |
| 49 | th.AssertNoErr(t, err) |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 50 | |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 51 | t.Logf("Created ACL items for LB %d", lbID) |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | func showACL(t *testing.T, client *gophercloud.ServiceClient, lbID int) []int { |
| 55 | ids := []int{} |
| 56 | |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 57 | err := acl.List(client, lbID).EachPage(func(page pagination.Page) (bool, error) { |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 58 | accessList, err := acl.ExtractAccessList(page) |
| 59 | th.AssertNoErr(t, err) |
| 60 | |
| 61 | for _, i := range accessList { |
| 62 | t.Logf("Listing network item: ID [%s] Address [%s] Type [%s]", i.ID, i.Address, i.Type) |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 63 | ids = append(ids, i.ID) |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | return true, nil |
| 67 | }) |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 68 | th.AssertNoErr(t, err) |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 69 | |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 70 | return ids |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | func deleteNetworkItem(t *testing.T, client *gophercloud.ServiceClient, lbID, itemID int) { |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 74 | err := acl.Delete(client, lbID, itemID).ExtractErr() |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 75 | |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 76 | th.AssertNoErr(t, err) |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 77 | |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 78 | t.Logf("Deleted network item %d", itemID) |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | func bulkDeleteACL(t *testing.T, client *gophercloud.ServiceClient, lbID int, items []int) { |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 82 | err := acl.BulkDelete(client, lbID, items).ExtractErr() |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 83 | |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 84 | th.AssertNoErr(t, err) |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 85 | |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 86 | t.Logf("Deleted network items %s", intsToStr(items)) |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | func deleteACL(t *testing.T, client *gophercloud.ServiceClient, lbID int) { |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 90 | err := acl.DeleteAll(client, lbID).ExtractErr() |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 91 | |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 92 | th.AssertNoErr(t, err) |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 93 | |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 94 | t.Logf("Deleted ACL from LB %d", lbID) |
Jamie Hannaford | 07cf0ea | 2014-11-06 10:42:49 +0100 | [diff] [blame] | 95 | } |