2015-01-15 10:04:09 +01:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccCloudStackEgressFirewall_basic(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckCloudStackEgressFirewallDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCloudStackEgressFirewall_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStackEgressFirewallRulesExist("cloudstack_egress_firewall.foo"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-04-11 17:14:19 +02:00
|
|
|
"cloudstack_egress_firewall.foo", "network_id", CLOUDSTACK_NETWORK_1),
|
2015-12-03 11:10:42 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-25 19:42:01 +02:00
|
|
|
"cloudstack_egress_firewall.foo", "rule.#", "1"),
|
2015-01-15 10:04:09 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-01-15 21:46:06 +01:00
|
|
|
"cloudstack_egress_firewall.foo",
|
2016-06-25 19:42:01 +02:00
|
|
|
"rule.2905891128.cidr_list.3378711023",
|
2015-12-03 11:10:42 +01:00
|
|
|
CLOUDSTACK_NETWORK_1_IPADDRESS1+"/32"),
|
2015-01-15 10:04:09 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-25 19:42:01 +02:00
|
|
|
"cloudstack_egress_firewall.foo", "rule.2905891128.protocol", "tcp"),
|
2015-01-15 10:04:09 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-25 19:42:01 +02:00
|
|
|
"cloudstack_egress_firewall.foo", "rule.2905891128.ports.32925333", "8080"),
|
2015-01-15 10:04:09 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccCloudStackEgressFirewall_update(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckCloudStackEgressFirewallDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCloudStackEgressFirewall_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStackEgressFirewallRulesExist("cloudstack_egress_firewall.foo"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-04-11 17:14:19 +02:00
|
|
|
"cloudstack_egress_firewall.foo", "network_id", CLOUDSTACK_NETWORK_1),
|
2015-01-15 10:04:09 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-25 19:42:01 +02:00
|
|
|
"cloudstack_egress_firewall.foo", "rule.#", "1"),
|
2015-01-15 10:04:09 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-01-15 21:46:06 +01:00
|
|
|
"cloudstack_egress_firewall.foo",
|
2016-06-25 19:42:01 +02:00
|
|
|
"rule.2905891128.cidr_list.3378711023",
|
2015-12-03 11:10:42 +01:00
|
|
|
CLOUDSTACK_NETWORK_1_IPADDRESS1+"/32"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-25 19:42:01 +02:00
|
|
|
"cloudstack_egress_firewall.foo", "rule.2905891128.protocol", "tcp"),
|
2015-01-15 10:04:09 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-25 19:42:01 +02:00
|
|
|
"cloudstack_egress_firewall.foo", "rule.2905891128.ports.32925333", "8080"),
|
2015-01-15 10:04:09 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCloudStackEgressFirewall_update,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStackEgressFirewallRulesExist("cloudstack_egress_firewall.foo"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-04-11 17:14:19 +02:00
|
|
|
"cloudstack_egress_firewall.foo", "network_id", CLOUDSTACK_NETWORK_1),
|
2015-01-15 10:04:09 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-25 19:42:01 +02:00
|
|
|
"cloudstack_egress_firewall.foo", "rule.#", "2"),
|
2015-12-03 11:10:42 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"cloudstack_egress_firewall.foo",
|
2016-06-25 19:42:01 +02:00
|
|
|
"rule.3593527682.cidr_list.1910468234",
|
2015-12-03 11:10:42 +01:00
|
|
|
CLOUDSTACK_NETWORK_1_IPADDRESS2+"/32"),
|
2015-01-15 10:04:09 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-01-15 21:46:06 +01:00
|
|
|
"cloudstack_egress_firewall.foo",
|
2016-06-25 19:42:01 +02:00
|
|
|
"rule.3593527682.cidr_list.3378711023",
|
2015-12-03 11:10:42 +01:00
|
|
|
CLOUDSTACK_NETWORK_1_IPADDRESS1+"/32"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-25 19:42:01 +02:00
|
|
|
"cloudstack_egress_firewall.foo", "rule.3593527682.protocol", "tcp"),
|
2015-01-15 10:04:09 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-25 19:42:01 +02:00
|
|
|
"cloudstack_egress_firewall.foo", "rule.3593527682.ports.32925333", "8080"),
|
2015-01-15 10:04:09 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-12-03 11:10:42 +01:00
|
|
|
"cloudstack_egress_firewall.foo",
|
2016-06-25 19:42:01 +02:00
|
|
|
"rule.739924765.cidr_list.3378711023",
|
2015-12-03 11:10:42 +01:00
|
|
|
CLOUDSTACK_NETWORK_1_IPADDRESS1+"/32"),
|
2015-01-15 10:04:09 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-25 19:42:01 +02:00
|
|
|
"cloudstack_egress_firewall.foo", "rule.739924765.protocol", "tcp"),
|
2015-01-15 10:04:09 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-25 19:42:01 +02:00
|
|
|
"cloudstack_egress_firewall.foo", "rule.739924765.ports.1889509032", "80"),
|
2015-01-15 10:04:09 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckCloudStackEgressFirewallRulesExist(n string) resource.TestCheckFunc {
|
|
|
|
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 firewall ID is set")
|
|
|
|
}
|
|
|
|
|
2015-10-05 14:05:21 +02:00
|
|
|
for k, id := range rs.Primary.Attributes {
|
2016-06-25 19:42:01 +02:00
|
|
|
if !strings.Contains(k, ".uuids.") || strings.HasSuffix(k, ".uuids.%") {
|
2015-01-15 10:04:09 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
|
2015-10-05 14:05:21 +02:00
|
|
|
_, count, err := cs.Firewall.GetEgressFirewallRuleByID(id)
|
2015-01-15 10:04:09 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if count == 0 {
|
|
|
|
return fmt.Errorf("Firewall rule for %s not found", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckCloudStackEgressFirewallDestroy(s *terraform.State) error {
|
|
|
|
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "cloudstack_egress_firewall" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No instance ID is set")
|
|
|
|
}
|
|
|
|
|
2015-10-05 14:05:21 +02:00
|
|
|
for k, id := range rs.Primary.Attributes {
|
2016-06-25 19:42:01 +02:00
|
|
|
if !strings.Contains(k, ".uuids.") || strings.HasSuffix(k, ".uuids.%") {
|
2015-01-15 10:04:09 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-10-05 14:05:21 +02:00
|
|
|
_, _, err := cs.Firewall.GetEgressFirewallRuleByID(id)
|
2015-06-09 12:38:05 +02:00
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Egress rule %s still exists", rs.Primary.ID)
|
2015-01-15 10:04:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccCloudStackEgressFirewall_basic = fmt.Sprintf(`
|
|
|
|
resource "cloudstack_egress_firewall" "foo" {
|
2016-04-11 17:14:19 +02:00
|
|
|
network_id = "%s"
|
2015-01-15 10:04:09 +01:00
|
|
|
|
2015-12-03 11:10:42 +01:00
|
|
|
rule {
|
|
|
|
cidr_list = ["%s/32"]
|
|
|
|
protocol = "tcp"
|
|
|
|
ports = ["8080"]
|
|
|
|
}
|
2015-01-15 11:29:04 +01:00
|
|
|
}`,
|
|
|
|
CLOUDSTACK_NETWORK_1,
|
2015-12-03 11:10:42 +01:00
|
|
|
CLOUDSTACK_NETWORK_1_IPADDRESS1)
|
2015-01-15 10:04:09 +01:00
|
|
|
|
|
|
|
var testAccCloudStackEgressFirewall_update = fmt.Sprintf(`
|
|
|
|
resource "cloudstack_egress_firewall" "foo" {
|
2016-04-11 17:14:19 +02:00
|
|
|
network_id = "%s"
|
2015-01-15 21:46:06 +01:00
|
|
|
|
2015-12-03 11:10:42 +01:00
|
|
|
rule {
|
|
|
|
cidr_list = ["%s/32", "%s/32"]
|
|
|
|
protocol = "tcp"
|
|
|
|
ports = ["8080"]
|
|
|
|
}
|
|
|
|
|
2015-01-15 21:46:06 +01:00
|
|
|
rule {
|
2016-06-25 19:42:01 +02:00
|
|
|
cidr_list = ["%s/32"]
|
2015-01-15 21:46:06 +01:00
|
|
|
protocol = "tcp"
|
|
|
|
ports = ["80", "1000-2000"]
|
|
|
|
}
|
2015-01-15 11:29:04 +01:00
|
|
|
}`,
|
|
|
|
CLOUDSTACK_NETWORK_1,
|
2015-12-03 11:10:42 +01:00
|
|
|
CLOUDSTACK_NETWORK_1_IPADDRESS1,
|
|
|
|
CLOUDSTACK_NETWORK_1_IPADDRESS2,
|
|
|
|
CLOUDSTACK_NETWORK_1_IPADDRESS1)
|