2014-08-26 07:09:38 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.google.com/p/google-api-go-client/compute/v1"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccComputeFirewall_basic(t *testing.T) {
|
|
|
|
var firewall compute.Firewall
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeFirewallDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeFirewall_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeFirewallExists(
|
|
|
|
"google_compute_firewall.foobar", &firewall),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-08-26 21:50:08 +02:00
|
|
|
func TestAccComputeFirewall_update(t *testing.T) {
|
|
|
|
var firewall compute.Firewall
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeFirewallDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeFirewall_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeFirewallExists(
|
|
|
|
"google_compute_firewall.foobar", &firewall),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeFirewall_update,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeFirewallExists(
|
|
|
|
"google_compute_firewall.foobar", &firewall),
|
|
|
|
testAccCheckComputeFirewallPorts(
|
|
|
|
&firewall, "80-255"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-08-26 07:09:38 +02:00
|
|
|
func testAccCheckComputeFirewallDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
2014-09-17 02:16:53 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-08-26 07:09:38 +02:00
|
|
|
if rs.Type != "google_compute_firewall" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := config.clientCompute.Firewalls.Get(
|
2014-09-17 02:16:53 +02:00
|
|
|
config.Project, rs.Primary.ID).Do()
|
2014-08-26 07:09:38 +02:00
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Firewall still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckComputeFirewallExists(n string, firewall *compute.Firewall) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:16:53 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-08-26 07:09:38 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:16:53 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-08-26 07:09:38 +02:00
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
found, err := config.clientCompute.Firewalls.Get(
|
2014-09-17 02:16:53 +02:00
|
|
|
config.Project, rs.Primary.ID).Do()
|
2014-08-26 07:09:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:16:53 +02:00
|
|
|
if found.Name != rs.Primary.ID {
|
2014-08-26 07:09:38 +02:00
|
|
|
return fmt.Errorf("Firewall not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*firewall = *found
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 21:50:08 +02:00
|
|
|
func testAccCheckComputeFirewallPorts(
|
|
|
|
firewall *compute.Firewall, ports string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if len(firewall.Allowed) == 0 {
|
|
|
|
return fmt.Errorf("no allowed rules")
|
|
|
|
}
|
|
|
|
|
|
|
|
if firewall.Allowed[0].Ports[0] != ports {
|
|
|
|
return fmt.Errorf("bad: %#v", firewall.Allowed[0].Ports)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 07:09:38 +02:00
|
|
|
const testAccComputeFirewall_basic = `
|
|
|
|
resource "google_compute_network" "foobar" {
|
|
|
|
name = "terraform-test"
|
|
|
|
ipv4_range = "10.0.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_firewall" "foobar" {
|
|
|
|
name = "terraform-test"
|
|
|
|
network = "${google_compute_network.foobar.name}"
|
|
|
|
source_tags = ["foo"]
|
|
|
|
|
|
|
|
allow {
|
|
|
|
protocol = "icmp"
|
|
|
|
}
|
|
|
|
}`
|
2014-08-26 21:50:08 +02:00
|
|
|
|
|
|
|
const testAccComputeFirewall_update = `
|
|
|
|
resource "google_compute_network" "foobar" {
|
|
|
|
name = "terraform-test"
|
|
|
|
ipv4_range = "10.0.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_firewall" "foobar" {
|
|
|
|
name = "terraform-test"
|
|
|
|
network = "${google_compute_network.foobar.name}"
|
|
|
|
source_tags = ["foo"]
|
|
|
|
|
|
|
|
allow {
|
|
|
|
protocol = "tcp"
|
|
|
|
ports = ["80-255"]
|
|
|
|
}
|
|
|
|
}`
|