2015-02-09 22:53:09 +01:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-02-17 22:07:01 +01:00
|
|
|
"github.com/rackspace/gophercloud"
|
2015-02-09 22:53:09 +01:00
|
|
|
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/fwaas/policies"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccOpenstackFirewallPolicy(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckOpenstackFirewallPolicyDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testFirewallPolicyConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckFirewallPolicyExists(
|
|
|
|
"openstack_fw_policy_v2.accept_test",
|
2015-02-18 01:00:48 +01:00
|
|
|
"", "", 0),
|
2015-02-09 22:53:09 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testFirewallPolicyConfigAddRules,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckFirewallPolicyExists(
|
|
|
|
"openstack_fw_policy_v2.accept_test",
|
2015-02-18 01:00:48 +01:00
|
|
|
"accept_test", "terraform acceptance test", 2),
|
2015-02-09 22:53:09 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testFirewallPolicyUpdateDeleteRule,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckFirewallPolicyExists(
|
|
|
|
"openstack_fw_policy_v2.accept_test",
|
2015-02-18 01:00:48 +01:00
|
|
|
"accept_test", "terraform acceptance test", 1),
|
2015-02-09 22:53:09 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckOpenstackFirewallPolicyDestroy(s *terraform.State) error {
|
|
|
|
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("(testAccCheckOpenstackFirewallPolicyDestroy) Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "openstack_fw_policy_v2" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err = policies.Get(networkingClient, rs.Primary.ID).Extract()
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Firewall policy (%s) still exists.", rs.Primary.ID)
|
|
|
|
}
|
2015-02-17 22:07:01 +01:00
|
|
|
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError)
|
2015-02-09 22:53:09 +01:00
|
|
|
if !ok || httpError.Actual != 404 {
|
|
|
|
return httpError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-18 01:00:48 +01:00
|
|
|
func testAccCheckFirewallPolicyExists(n, name, description string, ruleCount int) resource.TestCheckFunc {
|
2015-02-09 22:53:09 +01:00
|
|
|
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("(testAccCheckFirewallPolicyExists) Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var found *policies.Policy
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
// Firewall policy creation is asynchronous. Retry some times
|
|
|
|
// if we get a 404 error. Fail on any other error.
|
|
|
|
found, err = policies.Get(networkingClient, rs.Primary.ID).Extract()
|
|
|
|
if err != nil {
|
2015-02-17 22:07:01 +01:00
|
|
|
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError)
|
2015-02-09 22:53:09 +01:00
|
|
|
if !ok || httpError.Actual != 404 {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-18 01:00:48 +01:00
|
|
|
if name != found.Name {
|
|
|
|
return fmt.Errorf("Expected name <%s>, but found <%s>", name, found.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if description != found.Description {
|
|
|
|
return fmt.Errorf("Expected description <%s>, but found <%s>", description, found.Description)
|
|
|
|
}
|
2015-02-09 22:53:09 +01:00
|
|
|
|
2015-02-18 01:00:48 +01:00
|
|
|
if ruleCount != len(found.Rules) {
|
|
|
|
return fmt.Errorf("Expected rule count <%d>, but found <%d>", ruleCount, len(found.Rules))
|
2015-02-09 22:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testFirewallPolicyConfig = `
|
|
|
|
resource "openstack_fw_policy_v2" "accept_test" {
|
|
|
|
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testFirewallPolicyConfigAddRules = `
|
|
|
|
resource "openstack_fw_policy_v2" "accept_test" {
|
|
|
|
name = "accept_test"
|
|
|
|
description = "terraform acceptance test"
|
|
|
|
rules = [
|
|
|
|
"${openstack_fw_rule_v2.accept_test_udp_deny.id}",
|
2015-02-18 01:00:48 +01:00
|
|
|
"${openstack_fw_rule_v2.accept_test_tcp_allow.id}"
|
2015-02-09 22:53:09 +01:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "openstack_fw_rule_v2" "accept_test_tcp_allow" {
|
|
|
|
protocol = "tcp"
|
|
|
|
action = "allow"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "openstack_fw_rule_v2" "accept_test_udp_deny" {
|
|
|
|
protocol = "udp"
|
|
|
|
action = "deny"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testFirewallPolicyUpdateDeleteRule = `
|
|
|
|
resource "openstack_fw_policy_v2" "accept_test" {
|
|
|
|
name = "accept_test"
|
|
|
|
description = "terraform acceptance test"
|
|
|
|
rules = [
|
|
|
|
"${openstack_fw_rule_v2.accept_test_udp_deny.id}"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "openstack_fw_rule_v2" "accept_test_udp_deny" {
|
|
|
|
protocol = "udp"
|
|
|
|
action = "deny"
|
|
|
|
}
|
|
|
|
`
|