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