2015-02-05 13:49:23 +01:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"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-05 13:49:23 +01:00
|
|
|
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/fwaas/rules"
|
|
|
|
)
|
|
|
|
|
2015-06-08 01:18:14 +02:00
|
|
|
func TestAccFWRuleV1_basic(t *testing.T) {
|
2015-02-05 13:49:23 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2015-02-19 22:55:54 +01:00
|
|
|
CheckDestroy: testAccCheckFWRuleV1Destroy,
|
2015-02-05 13:49:23 +01:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testFirewallRuleMinimalConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-02-19 22:55:54 +01:00
|
|
|
testAccCheckFWRuleV1Exists(
|
|
|
|
"openstack_fw_rule_v1.accept_test_minimal",
|
2015-02-05 13:49:23 +01:00
|
|
|
&rules.Rule{
|
|
|
|
Protocol: "udp",
|
|
|
|
Action: "deny",
|
|
|
|
IPVersion: 4,
|
|
|
|
Enabled: true,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testFirewallRuleConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-02-19 22:55:54 +01:00
|
|
|
testAccCheckFWRuleV1Exists(
|
|
|
|
"openstack_fw_rule_v1.accept_test",
|
2015-02-05 13:49:23 +01:00
|
|
|
&rules.Rule{
|
|
|
|
Name: "accept_test",
|
|
|
|
Protocol: "udp",
|
|
|
|
Action: "deny",
|
|
|
|
Description: "Terraform accept test",
|
|
|
|
IPVersion: 4,
|
|
|
|
SourceIPAddress: "1.2.3.4",
|
|
|
|
DestinationIPAddress: "4.3.2.0/24",
|
|
|
|
SourcePort: "444",
|
|
|
|
DestinationPort: "555",
|
|
|
|
Enabled: true,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testFirewallRuleUpdateAllFieldsConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-02-19 22:55:54 +01:00
|
|
|
testAccCheckFWRuleV1Exists(
|
|
|
|
"openstack_fw_rule_v1.accept_test",
|
2015-02-05 13:49:23 +01:00
|
|
|
&rules.Rule{
|
|
|
|
Name: "accept_test_updated_2",
|
|
|
|
Protocol: "tcp",
|
|
|
|
Action: "allow",
|
|
|
|
Description: "Terraform accept test updated",
|
|
|
|
IPVersion: 4,
|
|
|
|
SourceIPAddress: "1.2.3.0/24",
|
|
|
|
DestinationIPAddress: "4.3.2.8",
|
|
|
|
SourcePort: "666",
|
|
|
|
DestinationPort: "777",
|
|
|
|
Enabled: false,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func testAccCheckFWRuleV1Destroy(s *terraform.State) error {
|
2015-02-05 13:49:23 +01:00
|
|
|
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("(testAccCheckOpenstackFirewallRuleDestroy) Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "openstack_firewall_rule" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err = rules.Get(networkingClient, rs.Primary.ID).Extract()
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Firewall rule (%s) still exists.", rs.Primary.ID)
|
|
|
|
}
|
2015-02-17 22:07:01 +01:00
|
|
|
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError)
|
2015-02-05 13:49:23 +01:00
|
|
|
if !ok || httpError.Actual != 404 {
|
|
|
|
return httpError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-19 22:55:54 +01:00
|
|
|
func testAccCheckFWRuleV1Exists(n string, expected *rules.Rule) resource.TestCheckFunc {
|
2015-02-05 13:49:23 +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("(testAccCheckFirewallRuleExists) Error creating OpenStack networking client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var found *rules.Rule
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
// Firewall rule creation is asynchronous. Retry some times
|
|
|
|
// if we get a 404 error. Fail on any other error.
|
|
|
|
found, err = rules.Get(networkingClient, rs.Primary.ID).Extract()
|
|
|
|
if err != nil {
|
2015-02-17 22:07:01 +01:00
|
|
|
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError)
|
2015-02-05 13:49:23 +01:00
|
|
|
if !ok || httpError.Actual != 404 {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
expected.ID = found.ID
|
2015-02-10 00:19:01 +01:00
|
|
|
// Erase the tenant id because we don't want to compare
|
|
|
|
// it as long it is not present in the expected
|
|
|
|
found.TenantID = ""
|
2015-02-05 13:49:23 +01:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, found) {
|
|
|
|
return fmt.Errorf("Expected:\n%#v\nFound:\n%#v", expected, found)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testFirewallRuleMinimalConfig = `
|
2015-02-19 22:55:54 +01:00
|
|
|
resource "openstack_fw_rule_v1" "accept_test_minimal" {
|
2015-02-17 22:07:01 +01:00
|
|
|
protocol = "udp"
|
|
|
|
action = "deny"
|
2015-02-05 13:49:23 +01:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testFirewallRuleConfig = `
|
2015-02-19 22:55:54 +01:00
|
|
|
resource "openstack_fw_rule_v1" "accept_test" {
|
2015-02-17 22:07:01 +01:00
|
|
|
name = "accept_test"
|
2015-02-05 13:49:23 +01:00
|
|
|
description = "Terraform accept test"
|
2015-02-17 22:07:01 +01:00
|
|
|
protocol = "udp"
|
|
|
|
action = "deny"
|
2015-02-05 13:49:23 +01:00
|
|
|
ip_version = 4
|
|
|
|
source_ip_address = "1.2.3.4"
|
|
|
|
destination_ip_address = "4.3.2.0/24"
|
|
|
|
source_port = "444"
|
|
|
|
destination_port = "555"
|
|
|
|
enabled = true
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testFirewallRuleUpdateAllFieldsConfig = `
|
2015-02-19 22:55:54 +01:00
|
|
|
resource "openstack_fw_rule_v1" "accept_test" {
|
2015-02-17 22:07:01 +01:00
|
|
|
name = "accept_test_updated_2"
|
2015-02-05 13:49:23 +01:00
|
|
|
description = "Terraform accept test updated"
|
2015-02-17 22:07:01 +01:00
|
|
|
protocol = "tcp"
|
|
|
|
action = "allow"
|
2015-02-05 13:49:23 +01:00
|
|
|
ip_version = 4
|
|
|
|
source_ip_address = "1.2.3.0/24"
|
|
|
|
destination_ip_address = "4.3.2.8"
|
|
|
|
source_port = "666"
|
|
|
|
destination_port = "777"
|
|
|
|
enabled = false
|
|
|
|
}
|
|
|
|
`
|