File: rule_test.go

package info (click to toggle)
golang-github-rackspace-gophercloud 1.0.0%2Bgit20161013.1012.e00690e8-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,148 kB
  • ctags: 6,414
  • sloc: sh: 16; makefile: 6
file content (84 lines) | stat: -rw-r--r-- 2,109 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// +build acceptance networking fwaas

package fwaas

import (
	"testing"

	base "github.com/rackspace/gophercloud/acceptance/openstack/networking/v2"
	"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/fwaas/rules"
	"github.com/rackspace/gophercloud/pagination"
	th "github.com/rackspace/gophercloud/testhelper"
)

func TestFirewallRules(t *testing.T) {
	base.Setup(t)
	defer base.Teardown()

	ruleID := createRule(t, &rules.CreateOpts{
		Name:                 "gophercloud_test",
		Description:          "acceptance test",
		Protocol:             "tcp",
		Action:               "allow",
		DestinationIPAddress: "192.168.0.0/24",
		DestinationPort:      "22",
	})

	listRules(t)

	destinationIPAddress := "192.168.1.0/24"
	destinationPort := ""
	sourcePort := "1234"

	updateRule(t, ruleID, &rules.UpdateOpts{
		DestinationIPAddress: &destinationIPAddress,
		DestinationPort:      &destinationPort,
		SourcePort:           &sourcePort,
	})

	getRule(t, ruleID)

	deleteRule(t, ruleID)
}

func createRule(t *testing.T, opts *rules.CreateOpts) string {
	r, err := rules.Create(base.Client, *opts).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Created rule: %#v", opts)
	return r.ID
}

func listRules(t *testing.T) {
	err := rules.List(base.Client, rules.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
		ruleList, err := rules.ExtractRules(page)
		if err != nil {
			t.Errorf("Failed to extract rules: %v", err)
			return false, err
		}

		for _, r := range ruleList {
			t.Logf("Listing rules: ID [%s]", r.ID)
		}

		return true, nil
	})
	th.AssertNoErr(t, err)
}

func updateRule(t *testing.T, ruleID string, opts *rules.UpdateOpts) {
	r, err := rules.Update(base.Client, ruleID, *opts).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Updated rule ID [%s]", r.ID)
}

func getRule(t *testing.T, ruleID string) {
	r, err := rules.Get(base.Client, ruleID).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Getting rule ID [%s]", r.ID)
}

func deleteRule(t *testing.T, ruleID string) {
	res := rules.Delete(base.Client, ruleID)
	th.AssertNoErr(t, res.Err)
	t.Logf("Deleted rule %s", ruleID)
}