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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
package fwaas_v2
import (
"fmt"
"strconv"
"testing"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/acceptance/tools"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas_v2/groups"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas_v2/policies"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas_v2/rules"
th "github.com/gophercloud/gophercloud/testhelper"
)
// RemoveRule will remove a rule from the policy.
func RemoveRule(t *testing.T, client *gophercloud.ServiceClient, policyID string, ruleID string) {
t.Logf("Attempting to remove rule %s from policy %s", ruleID, policyID)
_, err := policies.RemoveRule(client, policyID, ruleID).Extract()
if err != nil {
t.Fatalf("Unable to remove rule %s from policy %s: %v", ruleID, policyID, err)
}
}
// AddRule will add a rule to to a policy.
func AddRule(t *testing.T, client *gophercloud.ServiceClient, policyID string, ruleID string, beforeRuleID string) {
t.Logf("Attempting to insert rule %s in to policy %s", ruleID, policyID)
addOpts := policies.InsertRuleOpts{
ID: ruleID,
InsertBefore: beforeRuleID,
}
_, err := policies.InsertRule(client, policyID, addOpts).Extract()
if err != nil {
t.Fatalf("Unable to insert rule %s before rule %s in policy %s: %v", ruleID, beforeRuleID, policyID, err)
}
}
// CreatePolicy will create a Firewall Policy with a random name and given
// rule. An error will be returned if the rule could not be created.
func CreatePolicy(t *testing.T, client *gophercloud.ServiceClient, ruleID string) (*policies.Policy, error) {
policyName := tools.RandomString("TESTACC-", 8)
policyDescription := tools.RandomString("TESTACC-DESC-", 8)
t.Logf("Attempting to create policy %s", policyName)
createOpts := policies.CreateOpts{
Name: policyName,
Description: policyDescription,
FirewallRules: []string{
ruleID,
},
}
policy, err := policies.Create(client, createOpts).Extract()
if err != nil {
return policy, err
}
t.Logf("Successfully created policy %s", policyName)
th.AssertEquals(t, policy.Name, policyName)
th.AssertEquals(t, policy.Description, policyDescription)
th.AssertEquals(t, len(policy.Rules), 1)
return policy, nil
}
// CreateRule will create a Firewall Rule with a random source address and
// source port, destination address and port. An error will be returned if
// the rule could not be created.
func CreateRule(t *testing.T, client *gophercloud.ServiceClient) (*rules.Rule, error) {
ruleName := tools.RandomString("TESTACC-", 8)
sourceAddress := fmt.Sprintf("192.168.1.%d", tools.RandomInt(1, 100))
sourcePortInt := strconv.Itoa(tools.RandomInt(1, 100))
sourcePort := fmt.Sprintf("%s:%s", sourcePortInt, sourcePortInt)
destinationAddress := fmt.Sprintf("192.168.2.%d", tools.RandomInt(1, 100))
destinationPortInt := strconv.Itoa(tools.RandomInt(1, 100))
destinationPort := fmt.Sprintf("%s:%s", destinationPortInt, destinationPortInt)
t.Logf("Attempting to create rule %s with source %s:%s and destination %s:%s",
ruleName, sourceAddress, sourcePort, destinationAddress, destinationPort)
createOpts := rules.CreateOpts{
Name: ruleName,
Protocol: rules.ProtocolTCP,
Action: rules.ActionAllow,
SourceIPAddress: sourceAddress,
SourcePort: sourcePort,
DestinationIPAddress: destinationAddress,
DestinationPort: destinationPort,
}
rule, err := rules.Create(client, createOpts).Extract()
if err != nil {
return rule, err
}
t.Logf("Rule %s successfully created", ruleName)
th.AssertEquals(t, rule.Name, ruleName)
th.AssertEquals(t, rule.Protocol, string(rules.ProtocolTCP))
th.AssertEquals(t, rule.Action, string(rules.ActionAllow))
th.AssertEquals(t, rule.SourceIPAddress, sourceAddress)
th.AssertEquals(t, rule.SourcePort, sourcePortInt)
th.AssertEquals(t, rule.DestinationIPAddress, destinationAddress)
th.AssertEquals(t, rule.DestinationPort, destinationPortInt)
return rule, nil
}
// DeletePolicy will delete a policy with a specified ID. A fatal error will
// occur if the delete was not successful. This works best when used as a
// deferred function.
func DeletePolicy(t *testing.T, client *gophercloud.ServiceClient, policyID string) {
t.Logf("Attempting to delete policy: %s", policyID)
err := policies.Delete(client, policyID).ExtractErr()
if err != nil {
t.Fatalf("Unable to delete policy %s: %v", policyID, err)
}
t.Logf("Deleted policy: %s", policyID)
}
// DeleteRule will delete a rule with a specified ID. A fatal error will occur
// if the delete was not successful. This works best when used as a deferred
// function.
func DeleteRule(t *testing.T, client *gophercloud.ServiceClient, ruleID string) {
t.Logf("Attempting to delete rule: %s", ruleID)
err := rules.Delete(client, ruleID).ExtractErr()
if err != nil {
t.Fatalf("Unable to delete rule %s: %v", ruleID, err)
}
t.Logf("Deleted rule: %s", ruleID)
}
// CreateGroup will create a Firewall Group. An error will be returned if the
// firewall group could not be created.
func CreateGroup(t *testing.T, client *gophercloud.ServiceClient) (*groups.Group, error) {
groupName := tools.RandomString("TESTACC-", 8)
description := tools.RandomString("TESTACC-", 8)
adminStateUp := true
shared := false
createOpts := groups.CreateOpts{
Name: groupName,
Description: description,
AdminStateUp: &adminStateUp,
Shared: &shared,
}
t.Logf("Attempting to create firewall group %s",
groupName)
group, err := groups.Create(client, createOpts).Extract()
if err != nil {
return group, err
}
t.Logf("firewall group %s successfully created", groupName)
th.AssertEquals(t, group.Name, groupName)
return group, nil
}
// DeleteGroup will delete a group with a specified ID. A fatal error will occur
// if the delete was not successful. This works best when used as a deferred
// function.
func DeleteGroup(t *testing.T, client *gophercloud.ServiceClient, groupId string) {
t.Logf("Attempting to delete firewall group %s", groupId)
err := groups.Delete(client, groupId).ExtractErr()
if err != nil {
t.Fatalf("Unable to delete firewall group %s: %v", groupId, err)
}
t.Logf("Deleted firewall group %s", groupId)
}
|