Jamie Hannaford | 558572f | 2014-11-24 14:31:57 +0100 | [diff] [blame] | 1 | // +build acceptance compute defsecrules |
| 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | dsr "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/defsecrules" |
| 10 | "github.com/rackspace/gophercloud/pagination" |
| 11 | th "github.com/rackspace/gophercloud/testhelper" |
| 12 | ) |
| 13 | |
| 14 | func TestSecDefRules(t *testing.T) { |
| 15 | client, err := newClient() |
| 16 | th.AssertNoErr(t, err) |
| 17 | |
| 18 | id := createDefRule(t, client) |
| 19 | |
| 20 | listDefRules(t, client) |
| 21 | |
| 22 | getDefRule(t, client, id) |
| 23 | |
| 24 | deleteDefRule(t, client, id) |
| 25 | } |
| 26 | |
| 27 | func createDefRule(t *testing.T, client *gophercloud.ServiceClient) string { |
| 28 | opts := dsr.CreateOpts{ |
| 29 | FromPort: 80, |
| 30 | ToPort: 80, |
| 31 | IPProtocol: "TCP", |
| 32 | CIDR: "0.0.0.0/0", |
| 33 | } |
| 34 | |
| 35 | rule, err := dsr.Create(client, opts).Extract() |
| 36 | th.AssertNoErr(t, err) |
| 37 | |
| 38 | t.Logf("Created default rule %s", rule.ID) |
| 39 | |
| 40 | return rule.ID |
| 41 | } |
| 42 | |
| 43 | func listDefRules(t *testing.T, client *gophercloud.ServiceClient) { |
| 44 | err := dsr.List(client).EachPage(func(page pagination.Page) (bool, error) { |
| 45 | drList, err := dsr.ExtractDefaultRules(page) |
| 46 | th.AssertNoErr(t, err) |
| 47 | |
| 48 | for _, dr := range drList { |
| 49 | t.Logf("Listing default rule %s: Name [%s] From Port [%s] To Port [%s] Protocol [%s]", |
| 50 | dr.ID, dr.FromPort, dr.ToPort, dr.IPProtocol) |
| 51 | } |
| 52 | |
| 53 | return true, nil |
| 54 | }) |
| 55 | |
| 56 | th.AssertNoErr(t, err) |
| 57 | } |
| 58 | |
| 59 | func getDefRule(t *testing.T, client *gophercloud.ServiceClient, id string) { |
| 60 | rule, err := dsr.Get(client, id).Extract() |
| 61 | th.AssertNoErr(t, err) |
| 62 | |
| 63 | t.Logf("Getting %s: %#v", id, rule) |
| 64 | } |
| 65 | |
| 66 | func deleteDefRule(t *testing.T, client *gophercloud.ServiceClient, id string) { |
| 67 | err := dsr.Delete(client, id).ExtractErr() |
| 68 | th.AssertNoErr(t, err) |
| 69 | |
| 70 | t.Logf("Deleted rule %s", id) |
| 71 | } |