blob: 78b07986bd28f582dcb4889e37b7e387846bd512 [file] [log] [blame]
Jamie Hannaford558572f2014-11-24 14:31:57 +01001// +build acceptance compute defsecrules
2
3package v2
4
5import (
6 "testing"
7
8 "github.com/rackspace/gophercloud"
Jamie Hannaforddcda97f2014-11-24 14:39:24 +01009 "github.com/rackspace/gophercloud/acceptance/tools"
Jamie Hannaford558572f2014-11-24 14:31:57 +010010 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
15func 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 Hannafordaea96c62014-11-25 12:05:04 +010028func createDefRule(t *testing.T, client *gophercloud.ServiceClient) string {
Jamie Hannaford558572f2014-11-24 14:31:57 +010029 opts := dsr.CreateOpts{
Jamie Hannaforddcda97f2014-11-24 14:39:24 +010030 FromPort: tools.RandomInt(80, 89),
31 ToPort: tools.RandomInt(90, 99),
Jamie Hannaford558572f2014-11-24 14:31:57 +010032 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
44func 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 Hannafordaea96c62014-11-25 12:05:04 +010060func getDefRule(t *testing.T, client *gophercloud.ServiceClient, id string) {
Jamie Hannaford558572f2014-11-24 14:31:57 +010061 rule, err := dsr.Get(client, id).Extract()
62 th.AssertNoErr(t, err)
63
Jamie Hannafordaea96c62014-11-25 12:05:04 +010064 t.Logf("Getting rule %s: %#v", id, rule)
Jamie Hannaford558572f2014-11-24 14:31:57 +010065}
66
Jamie Hannafordaea96c62014-11-25 12:05:04 +010067func deleteDefRule(t *testing.T, client *gophercloud.ServiceClient, id string) {
Jamie Hannaford558572f2014-11-24 14:31:57 +010068 err := dsr.Delete(client, id).ExtractErr()
69 th.AssertNoErr(t, err)
70
Jamie Hannafordaea96c62014-11-25 12:05:04 +010071 t.Logf("Deleted rule %s", id)
Jamie Hannaford558572f2014-11-24 14:31:57 +010072}