Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to google_compute_target_pool for health checks self_link #702

Merged
merged 2 commits into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions google/field_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func ParseSslCertificateFieldValue(sslCertificate string, d TerraformResourceDat
return parseGlobalFieldValue("sslCertificates", sslCertificate, "project", d, config, false)
}

func ParseHttpHealthCheckFieldValue(healthCheck string, d TerraformResourceData, config *Config) (*GlobalFieldValue, error) {
return parseGlobalFieldValue("httpHealthChecks", healthCheck, "project", d, config, false)
}

func ParseDiskFieldValue(disk string, d TerraformResourceData, config *Config) (*ZonalFieldValue, error) {
return parseZonalFieldValue("disks", disk, "project", "zone", d, config, false)
}
Expand Down
51 changes: 19 additions & 32 deletions google/resource_compute_target_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ func resourceComputeTargetPool() *schema.Resource {
Type: schema.TypeList,
Optional: true,
ForceNew: false,
Elem: &schema.Schema{Type: schema.TypeString},
MaxItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
},

"instances": {
Expand Down Expand Up @@ -111,17 +115,17 @@ func resourceComputeTargetPool() *schema.Resource {
}

// Healthchecks need to exist before being referred to from the target pool.
func convertHealthChecks(config *Config, project string, names []string) ([]string, error) {
urls := make([]string, len(names))
for i, name := range names {
// Look up the healthcheck
res, err := config.clientCompute.HttpHealthChecks.Get(project, name).Do()
if err != nil {
return nil, fmt.Errorf("Error reading HealthCheck: %s", err)
}
urls[i] = res.SelfLink
func convertHealthChecks(healthChecks []interface{}, d *schema.ResourceData, config *Config) ([]string, error) {
if healthChecks == nil || len(healthChecks) == 0 {
return []string{}, nil
}
return urls, nil

hc, err := ParseHttpHealthCheckFieldValue(healthChecks[0].(string), d, config)
if err != nil {
return nil, err
}

return []string{hc.RelativeLink()}, nil
}

// Instances do not need to exist yet, so we simply generate URLs.
Expand Down Expand Up @@ -158,8 +162,7 @@ func resourceComputeTargetPoolCreate(d *schema.ResourceData, meta interface{}) e
return err
}

hchkUrls, err := convertHealthChecks(
config, project, convertStringArr(d.Get("health_checks").([]interface{})))
hchkUrls, err := convertHealthChecks(d.Get("health_checks").([]interface{}), d, config)
if err != nil {
return err
}
Expand Down Expand Up @@ -247,13 +250,11 @@ func resourceComputeTargetPoolUpdate(d *schema.ResourceData, meta interface{}) e
if d.HasChange("health_checks") {

from_, to_ := d.GetChange("health_checks")
from := convertStringArr(from_.([]interface{}))
to := convertStringArr(to_.([]interface{}))
fromUrls, err := convertHealthChecks(config, project, from)
fromUrls, err := convertHealthChecks(from_.([]interface{}), d, config)
if err != nil {
return err
}
toUrls, err := convertHealthChecks(config, project, to)
toUrls, err := convertHealthChecks(to_.([]interface{}), d, config)
if err != nil {
return err
}
Expand Down Expand Up @@ -376,16 +377,6 @@ func convertInstancesFromUrls(urls []string) []string {
return result
}

func convertHealthChecksFromUrls(urls []string) []string {
result := make([]string, 0, len(urls))
for _, url := range urls {
urlArray := strings.Split(url, "/")
healthCheck := fmt.Sprintf("%s", urlArray[len(urlArray)-1])
result = append(result, healthCheck)
}
return result
}

func resourceComputeTargetPoolRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

Expand All @@ -410,11 +401,7 @@ func resourceComputeTargetPoolRead(d *schema.ResourceData, meta interface{}) err
d.Set("backup_pool", tpool.BackupPool)
d.Set("description", tpool.Description)
d.Set("failover_ratio", tpool.FailoverRatio)
if tpool.HealthChecks != nil {
d.Set("health_checks", convertHealthChecksFromUrls(tpool.HealthChecks))
} else {
d.Set("health_checks", nil)
}
d.Set("health_checks", tpool.HealthChecks)
if tpool.Instances != nil {
d.Set("instances", convertInstancesFromUrls(tpool.Instances))
} else {
Expand Down
39 changes: 36 additions & 3 deletions google/resource_compute_target_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ func TestAccComputeTargetPool_basic(t *testing.T) {
Config: testAccComputeTargetPool_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeTargetPoolExists(
"google_compute_target_pool.foobar"),
"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"),
),
},
},
Expand Down Expand Up @@ -73,6 +77,27 @@ func testAccCheckComputeTargetPoolExists(n string) resource.TestCheckFunc {
}
}

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
}
}

var testAccComputeTargetPool_basic = fmt.Sprintf(`
resource "google_compute_http_health_check" "foobar" {
name = "healthcheck-test-%s"
Expand All @@ -95,12 +120,20 @@ resource "google_compute_instance" "foobar" {
}
}

resource "google_compute_target_pool" "foobar" {
resource "google_compute_target_pool" "foo" {
description = "Resource created for Terraform acceptance testing"
instances = ["${google_compute_instance.foobar.self_link}", "us-central1-b/bar"]
name = "tpool-test-%s"
session_affinity = "CLIENT_IP_PROTO"
health_checks = [
"${google_compute_http_health_check.foobar.name}"
]
}`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))
}

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))
10 changes: 9 additions & 1 deletion website/docs/r/compute_target_pool.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ resource "google_compute_target_pool" "default" {
"${google_compute_http_health_check.default.name}",
]
}

resource "google_compute_http_health_check" "default" {
name = "default"
request_path = "/"
check_interval_sec = 1
timeout_sec = 1
}
```

## Argument Reference
Expand All @@ -49,7 +56,8 @@ The following arguments are supported:
* `failover_ratio` - (Optional) Ratio (0 to 1) of failed nodes before using the
backup pool (which must also be set).

* `health_checks` - (Optional) List of zero or one healthcheck names.
* `health_checks` - (Optional) List of zero or one health check name or self_link. Only
legacy `google_compute_http_health_check` is supported.

* `instances` - (Optional) List of instances in the pool. They can be given as
URLs, or in the form of "zone/name". Note that the instances need not exist
Expand Down