2014-12-10 22:20:52 +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 TestAccCloudStackFirewall_basic(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckCloudStackFirewallDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCloudStackFirewall_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStackFirewallRulesExist("cloudstack_firewall.foo"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"cloudstack_firewall.foo", "ipaddress", CLOUDSTACK_PUBLIC_IPADDRESS),
|
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.1702320581.source_cidr", "10.0.0.0/24"),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.1702320581.protocol", "tcp"),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.1702320581.ports.#", "2"),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.1702320581.ports.1209010669", "1000-2000"),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.1702320581.ports.1889509032", "80"),
|
2014-12-10 22:20:52 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccCloudStackFirewall_update(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckCloudStackFirewallDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCloudStackFirewall_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStackFirewallRulesExist("cloudstack_firewall.foo"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"cloudstack_firewall.foo", "ipaddress", CLOUDSTACK_PUBLIC_IPADDRESS),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"cloudstack_firewall.foo", "rule.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.1702320581.source_cidr", "10.0.0.0/24"),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.1702320581.protocol", "tcp"),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.1702320581.ports.#", "2"),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.1702320581.ports.1209010669", "1000-2000"),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.1702320581.ports.1889509032", "80"),
|
2014-12-10 22:20:52 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCloudStackFirewall_update,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStackFirewallRulesExist("cloudstack_firewall.foo"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"cloudstack_firewall.foo", "ipaddress", CLOUDSTACK_PUBLIC_IPADDRESS),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"cloudstack_firewall.foo", "rule.#", "2"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.1702320581.source_cidr", "10.0.0.0/24"),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.1702320581.protocol", "tcp"),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.1702320581.ports.#", "2"),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.1702320581.ports.1209010669", "1000-2000"),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.1702320581.ports.1889509032", "80"),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.3779782959.source_cidr", "172.16.100.0/24"),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.3779782959.protocol", "tcp"),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.3779782959.ports.#", "2"),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.3779782959.ports.1889509032", "80"),
|
2014-12-10 22:20:52 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-12-12 23:21:20 +01:00
|
|
|
"cloudstack_firewall.foo", "rule.3779782959.ports.3638101695", "443"),
|
2014-12-10 22:20:52 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckCloudStackFirewallRulesExist(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 {
|
2015-01-15 11:29:04 +01:00
|
|
|
if !strings.Contains(k, ".uuids.") || strings.HasSuffix(k, ".uuids.#") {
|
2014-12-10 22:20:52 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
|
2015-10-05 14:05:21 +02:00
|
|
|
_, count, err := cs.Firewall.GetFirewallRuleByID(id)
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if count == 0 {
|
|
|
|
return fmt.Errorf("Firewall rule for %s not found", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckCloudStackFirewallDestroy(s *terraform.State) error {
|
|
|
|
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "cloudstack_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 {
|
2015-01-15 11:29:04 +01:00
|
|
|
if !strings.Contains(k, ".uuids.") || strings.HasSuffix(k, ".uuids.#") {
|
2014-12-10 22:20:52 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-10-05 14:05:21 +02:00
|
|
|
_, _, err := cs.Firewall.GetFirewallRuleByID(id)
|
2015-06-09 12:38:05 +02:00
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Firewall rule %s still exists", rs.Primary.ID)
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccCloudStackFirewall_basic = fmt.Sprintf(`
|
|
|
|
resource "cloudstack_firewall" "foo" {
|
|
|
|
ipaddress = "%s"
|
|
|
|
|
|
|
|
rule {
|
|
|
|
source_cidr = "10.0.0.0/24"
|
|
|
|
protocol = "tcp"
|
|
|
|
ports = ["80", "1000-2000"]
|
|
|
|
}
|
|
|
|
}`, CLOUDSTACK_PUBLIC_IPADDRESS)
|
|
|
|
|
|
|
|
var testAccCloudStackFirewall_update = fmt.Sprintf(`
|
|
|
|
resource "cloudstack_firewall" "foo" {
|
|
|
|
ipaddress = "%s"
|
|
|
|
|
|
|
|
rule {
|
|
|
|
source_cidr = "10.0.0.0/24"
|
|
|
|
protocol = "tcp"
|
|
|
|
ports = ["80", "1000-2000"]
|
|
|
|
}
|
|
|
|
|
|
|
|
rule {
|
|
|
|
source_cidr = "172.16.100.0/24"
|
|
|
|
protocol = "tcp"
|
|
|
|
ports = ["80", "443"]
|
|
|
|
}
|
|
|
|
}`, CLOUDSTACK_PUBLIC_IPADDRESS)
|