blob: 7a380273c629c6e7be5b671830711a73c80c909d [file] [log] [blame]
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +01001// +build acceptance lbs
2
3package v1
4
5import (
Jamie Hannafordbde72602014-11-10 10:32:26 +01006 "testing"
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +01007
Jamie Hannafordbde72602014-11-10 10:32:26 +01008 "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 Hannaford07cf0ea2014-11-06 10:42:49 +010013)
14
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010015func TestACL(t *testing.T) {
Jamie Hannafordbde72602014-11-10 10:32:26 +010016 client := setup(t)
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010017
Jamie Hannafordbde72602014-11-10 10:32:26 +010018 ids := createLB(t, client, 1)
19 lbID := ids[0]
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010020
Jamie Hannafordbde72602014-11-10 10:32:26 +010021 createACL(t, client, lbID)
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010022
Jamie Hannafordbde72602014-11-10 10:32:26 +010023 waitForLB(client, lbID, lbs.ACTIVE)
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010024
25 networkIDs := showACL(t, client, lbID)
26
Jamie Hannafordbde72602014-11-10 10:32:26 +010027 deleteNetworkItem(t, client, lbID, networkIDs[0])
28 waitForLB(client, lbID, lbs.ACTIVE)
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010029
30 bulkDeleteACL(t, client, lbID, networkIDs[1:2])
Jamie Hannafordbde72602014-11-10 10:32:26 +010031 waitForLB(client, lbID, lbs.ACTIVE)
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010032
Jamie Hannafordbde72602014-11-10 10:32:26 +010033 deleteACL(t, client, lbID)
34 waitForLB(client, lbID, lbs.ACTIVE)
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010035
Jamie Hannafordbde72602014-11-10 10:32:26 +010036 deleteLB(t, client, lbID)
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010037}
38
39func createACL(t *testing.T, client *gophercloud.ServiceClient, lbID int) {
Jamie Hannafordbde72602014-11-10 10:32:26 +010040 opts := acl.CreateOpts{
41 acl.CreateOpt{Address: "206.160.163.21", Type: acl.DENY},
42 acl.CreateOpt{Address: "206.160.165.11", Type: acl.DENY},
43 acl.CreateOpt{Address: "206.160.165.12", Type: acl.DENY},
44 acl.CreateOpt{Address: "206.160.165.13", Type: acl.ALLOW},
45 }
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010046
Jamie Hannafordbde72602014-11-10 10:32:26 +010047 err := acl.Create(client, lbID, opts).ExtractErr()
48 th.AssertNoErr(t, err)
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010049
Jamie Hannafordbde72602014-11-10 10:32:26 +010050 t.Logf("Created ACL items for LB %d", lbID)
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010051}
52
53func showACL(t *testing.T, client *gophercloud.ServiceClient, lbID int) []int {
54 ids := []int{}
55
Jamie Hannafordbde72602014-11-10 10:32:26 +010056 err := acl.List(client, lbID).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010057 accessList, err := acl.ExtractAccessList(page)
58 th.AssertNoErr(t, err)
59
60 for _, i := range accessList {
61 t.Logf("Listing network item: ID [%s] Address [%s] Type [%s]", i.ID, i.Address, i.Type)
Jamie Hannafordbde72602014-11-10 10:32:26 +010062 ids = append(ids, i.ID)
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010063 }
64
65 return true, nil
66 })
Jamie Hannafordbde72602014-11-10 10:32:26 +010067 th.AssertNoErr(t, err)
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010068
Jamie Hannafordbde72602014-11-10 10:32:26 +010069 return ids
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010070}
71
72func deleteNetworkItem(t *testing.T, client *gophercloud.ServiceClient, lbID, itemID int) {
Jamie Hannafordbde72602014-11-10 10:32:26 +010073 err := acl.Delete(client, lbID, itemID).ExtractErr()
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010074
Jamie Hannafordbde72602014-11-10 10:32:26 +010075 th.AssertNoErr(t, err)
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010076
Jamie Hannafordbde72602014-11-10 10:32:26 +010077 t.Logf("Deleted network item %d", itemID)
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010078}
79
80func bulkDeleteACL(t *testing.T, client *gophercloud.ServiceClient, lbID int, items []int) {
Jamie Hannafordbde72602014-11-10 10:32:26 +010081 err := acl.BulkDelete(client, lbID, items).ExtractErr()
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010082
Jamie Hannafordbde72602014-11-10 10:32:26 +010083 th.AssertNoErr(t, err)
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010084
Jamie Hannafordbde72602014-11-10 10:32:26 +010085 t.Logf("Deleted network items %s", intsToStr(items))
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010086}
87
88func deleteACL(t *testing.T, client *gophercloud.ServiceClient, lbID int) {
Jamie Hannafordbde72602014-11-10 10:32:26 +010089 err := acl.DeleteAll(client, lbID).ExtractErr()
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010090
Jamie Hannafordbde72602014-11-10 10:32:26 +010091 th.AssertNoErr(t, err)
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010092
Jamie Hannafordbde72602014-11-10 10:32:26 +010093 t.Logf("Deleted ACL from LB %d", lbID)
Jamie Hannaford07cf0ea2014-11-06 10:42:49 +010094}