2014-11-20 17:40:17 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-05 21:47:10 +00:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2014-11-20 17:40:17 +00:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccComputeTargetPool_basic(t *testing.T) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
2014-11-20 17:40:17 +00:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeTargetPoolDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-11-28 00:32:00 +00:00
|
|
|
Config: testAccComputeTargetPool_basic(),
|
2014-11-20 17:40:17 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeTargetPoolExists(
|
2017-11-08 17:11:17 +00:00
|
|
|
"google_compute_target_pool.foo"),
|
|
|
|
testAccCheckComputeTargetPoolHealthCheck("google_compute_target_pool.foo", "google_compute_http_health_check.foobar"),
|
|
|
|
testAccCheckComputeTargetPoolExists(
|
|
|
|
"google_compute_target_pool.bar"),
|
|
|
|
testAccCheckComputeTargetPoolHealthCheck("google_compute_target_pool.bar", "google_compute_http_health_check.foobar"),
|
2014-11-20 17:40:17 +00:00
|
|
|
),
|
|
|
|
},
|
2018-01-03 14:26:55 +00:00
|
|
|
resource.TestStep{
|
|
|
|
ResourceName: "google_compute_target_pool.foo",
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
2014-11-20 17:40:17 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckComputeTargetPoolDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "google_compute_target_pool" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := config.clientCompute.TargetPools.Get(
|
|
|
|
config.Project, config.Region, rs.Primary.ID).Do()
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("TargetPool still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckComputeTargetPoolExists(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 ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
found, err := config.clientCompute.TargetPools.Get(
|
|
|
|
config.Project, config.Region, rs.Primary.ID).Do()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if found.Name != rs.Primary.ID {
|
|
|
|
return fmt.Errorf("TargetPool not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 17:11:17 +00:00
|
|
|
func testAccCheckComputeTargetPoolHealthCheck(targetPool, healthCheck string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
targetPoolRes, ok := s.RootModule().Resources[targetPool]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", targetPool)
|
|
|
|
}
|
|
|
|
|
|
|
|
healthCheckRes, ok := s.RootModule().Resources[healthCheck]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", healthCheck)
|
|
|
|
}
|
|
|
|
|
|
|
|
hcLink := healthCheckRes.Primary.Attributes["self_link"]
|
|
|
|
if targetPoolRes.Primary.Attributes["health_checks.0"] != hcLink {
|
|
|
|
return fmt.Errorf("Health check not set up. Expected %q", hcLink)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:32:00 +00:00
|
|
|
func testAccComputeTargetPool_basic() string {
|
|
|
|
return fmt.Sprintf(`
|
2016-08-26 17:45:59 +00:00
|
|
|
resource "google_compute_http_health_check" "foobar" {
|
|
|
|
name = "healthcheck-test-%s"
|
|
|
|
host = "example.com"
|
|
|
|
}
|
|
|
|
|
2017-11-04 00:02:02 +00:00
|
|
|
resource "google_compute_instance" "foobar" {
|
|
|
|
name = "inst-tp-test-%s"
|
|
|
|
machine_type = "n1-standard-1"
|
|
|
|
zone = "us-central1-a"
|
|
|
|
|
|
|
|
boot_disk {
|
|
|
|
initialize_params{
|
|
|
|
image = "debian-8-jessie-v20160803"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
network_interface {
|
|
|
|
network = "default"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 17:11:17 +00:00
|
|
|
resource "google_compute_target_pool" "foo" {
|
2014-11-20 17:40:17 +00:00
|
|
|
description = "Resource created for Terraform acceptance testing"
|
2017-11-04 00:02:02 +00:00
|
|
|
instances = ["${google_compute_instance.foobar.self_link}", "us-central1-b/bar"]
|
2016-01-05 21:47:10 +00:00
|
|
|
name = "tpool-test-%s"
|
2014-11-20 17:40:17 +00:00
|
|
|
session_affinity = "CLIENT_IP_PROTO"
|
2016-08-26 17:45:59 +00:00
|
|
|
health_checks = [
|
|
|
|
"${google_compute_http_health_check.foobar.name}"
|
|
|
|
]
|
2017-11-08 17:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_target_pool" "bar" {
|
|
|
|
description = "Resource created for Terraform acceptance testing"
|
|
|
|
name = "tpool-test-%s"
|
|
|
|
health_checks = [
|
|
|
|
"${google_compute_http_health_check.foobar.self_link}"
|
|
|
|
]
|
|
|
|
}`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))
|
2017-11-28 00:32:00 +00:00
|
|
|
}
|