github.com/gophercloud/gophercloud@v1.11.0/internal/acceptance/openstack/networking/v2/extensions/fwaas_v2/fwaas_v2.go (about) 1 package fwaas_v2 2 3 import ( 4 "fmt" 5 "strconv" 6 "testing" 7 8 "github.com/gophercloud/gophercloud" 9 "github.com/gophercloud/gophercloud/internal/acceptance/tools" 10 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas_v2/groups" 11 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas_v2/policies" 12 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas_v2/rules" 13 th "github.com/gophercloud/gophercloud/testhelper" 14 ) 15 16 // RemoveRule will remove a rule from the policy. 17 func RemoveRule(t *testing.T, client *gophercloud.ServiceClient, policyID string, ruleID string) { 18 t.Logf("Attempting to remove rule %s from policy %s", ruleID, policyID) 19 20 _, err := policies.RemoveRule(client, policyID, ruleID).Extract() 21 if err != nil { 22 t.Fatalf("Unable to remove rule %s from policy %s: %v", ruleID, policyID, err) 23 } 24 } 25 26 // AddRule will add a rule to to a policy. 27 func AddRule(t *testing.T, client *gophercloud.ServiceClient, policyID string, ruleID string, beforeRuleID string) { 28 t.Logf("Attempting to insert rule %s in to policy %s", ruleID, policyID) 29 30 addOpts := policies.InsertRuleOpts{ 31 ID: ruleID, 32 InsertBefore: beforeRuleID, 33 } 34 35 _, err := policies.InsertRule(client, policyID, addOpts).Extract() 36 if err != nil { 37 t.Fatalf("Unable to insert rule %s before rule %s in policy %s: %v", ruleID, beforeRuleID, policyID, err) 38 } 39 } 40 41 // CreatePolicy will create a Firewall Policy with a random name and given 42 // rule. An error will be returned if the rule could not be created. 43 func CreatePolicy(t *testing.T, client *gophercloud.ServiceClient, ruleID string) (*policies.Policy, error) { 44 policyName := tools.RandomString("TESTACC-", 8) 45 policyDescription := tools.RandomString("TESTACC-DESC-", 8) 46 47 t.Logf("Attempting to create policy %s", policyName) 48 49 createOpts := policies.CreateOpts{ 50 Name: policyName, 51 Description: policyDescription, 52 FirewallRules: []string{ 53 ruleID, 54 }, 55 } 56 57 policy, err := policies.Create(client, createOpts).Extract() 58 if err != nil { 59 return policy, err 60 } 61 62 t.Logf("Successfully created policy %s", policyName) 63 64 th.AssertEquals(t, policy.Name, policyName) 65 th.AssertEquals(t, policy.Description, policyDescription) 66 th.AssertEquals(t, len(policy.Rules), 1) 67 68 return policy, nil 69 } 70 71 // CreateRule will create a Firewall Rule with a random source address and 72 // source port, destination address and port. An error will be returned if 73 // the rule could not be created. 74 func CreateRule(t *testing.T, client *gophercloud.ServiceClient) (*rules.Rule, error) { 75 ruleName := tools.RandomString("TESTACC-", 8) 76 sourceAddress := fmt.Sprintf("192.168.1.%d", tools.RandomInt(1, 100)) 77 sourcePortInt := strconv.Itoa(tools.RandomInt(1, 100)) 78 sourcePort := fmt.Sprintf("%s:%s", sourcePortInt, sourcePortInt) 79 destinationAddress := fmt.Sprintf("192.168.2.%d", tools.RandomInt(1, 100)) 80 destinationPortInt := strconv.Itoa(tools.RandomInt(1, 100)) 81 destinationPort := fmt.Sprintf("%s:%s", destinationPortInt, destinationPortInt) 82 83 t.Logf("Attempting to create rule %s with source %s:%s and destination %s:%s", 84 ruleName, sourceAddress, sourcePort, destinationAddress, destinationPort) 85 86 createOpts := rules.CreateOpts{ 87 Name: ruleName, 88 Protocol: rules.ProtocolTCP, 89 Action: rules.ActionAllow, 90 SourceIPAddress: sourceAddress, 91 SourcePort: sourcePort, 92 DestinationIPAddress: destinationAddress, 93 DestinationPort: destinationPort, 94 } 95 96 rule, err := rules.Create(client, createOpts).Extract() 97 if err != nil { 98 return rule, err 99 } 100 101 t.Logf("Rule %s successfully created", ruleName) 102 103 th.AssertEquals(t, rule.Name, ruleName) 104 th.AssertEquals(t, rule.Protocol, string(rules.ProtocolTCP)) 105 th.AssertEquals(t, rule.Action, string(rules.ActionAllow)) 106 th.AssertEquals(t, rule.SourceIPAddress, sourceAddress) 107 th.AssertEquals(t, rule.SourcePort, sourcePortInt) 108 th.AssertEquals(t, rule.DestinationIPAddress, destinationAddress) 109 th.AssertEquals(t, rule.DestinationPort, destinationPortInt) 110 111 return rule, nil 112 } 113 114 // DeletePolicy will delete a policy with a specified ID. A fatal error will 115 // occur if the delete was not successful. This works best when used as a 116 // deferred function. 117 func DeletePolicy(t *testing.T, client *gophercloud.ServiceClient, policyID string) { 118 t.Logf("Attempting to delete policy: %s", policyID) 119 120 err := policies.Delete(client, policyID).ExtractErr() 121 if err != nil { 122 t.Fatalf("Unable to delete policy %s: %v", policyID, err) 123 } 124 125 t.Logf("Deleted policy: %s", policyID) 126 } 127 128 // DeleteRule will delete a rule with a specified ID. A fatal error will occur 129 // if the delete was not successful. This works best when used as a deferred 130 // function. 131 func DeleteRule(t *testing.T, client *gophercloud.ServiceClient, ruleID string) { 132 t.Logf("Attempting to delete rule: %s", ruleID) 133 134 err := rules.Delete(client, ruleID).ExtractErr() 135 if err != nil { 136 t.Fatalf("Unable to delete rule %s: %v", ruleID, err) 137 } 138 139 t.Logf("Deleted rule: %s", ruleID) 140 } 141 142 // CreateGroup will create a Firewall Group. An error will be returned if the 143 // firewall group could not be created. 144 func CreateGroup(t *testing.T, client *gophercloud.ServiceClient) (*groups.Group, error) { 145 146 groupName := tools.RandomString("TESTACC-", 8) 147 description := tools.RandomString("TESTACC-", 8) 148 adminStateUp := true 149 shared := false 150 151 createOpts := groups.CreateOpts{ 152 Name: groupName, 153 Description: description, 154 AdminStateUp: &adminStateUp, 155 Shared: &shared, 156 } 157 158 t.Logf("Attempting to create firewall group %s", 159 groupName) 160 161 group, err := groups.Create(client, createOpts).Extract() 162 if err != nil { 163 return group, err 164 } 165 166 t.Logf("firewall group %s successfully created", groupName) 167 168 th.AssertEquals(t, group.Name, groupName) 169 return group, nil 170 } 171 172 // DeleteGroup will delete a group with a specified ID. A fatal error will occur 173 // if the delete was not successful. This works best when used as a deferred 174 // function. 175 func DeleteGroup(t *testing.T, client *gophercloud.ServiceClient, groupId string) { 176 t.Logf("Attempting to delete firewall group %s", groupId) 177 178 err := groups.Delete(client, groupId).ExtractErr() 179 if err != nil { 180 t.Fatalf("Unable to delete firewall group %s: %v", groupId, err) 181 } 182 183 t.Logf("Deleted firewall group %s", groupId) 184 }