Skip to content

Commit ff184b8

Browse files
authored
Add support to google_compute_target_pool for health checks self_link (#702)
- Accepts self_link in addition of health check name - Removes the need for an API call to generate the self link - Improves the documentation to mention that only the legacy google_compute_http_health_check is supported. This will prevent our user from being stuck like mentioned here: #300. - Adds a MaxItems:1 in the schema. You can't have more than one. The API will fail. The official docs also says so. - Adds a check to the acceptance test to ensure the health checks are properly setup.
1 parent 288aed7 commit ff184b8

4 files changed

+68
-36
lines changed

google/field_helpers.go

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ func ParseSslCertificateFieldValue(sslCertificate string, d TerraformResourceDat
2525
return parseGlobalFieldValue("sslCertificates", sslCertificate, "project", d, config, false)
2626
}
2727

28+
func ParseHttpHealthCheckFieldValue(healthCheck string, d TerraformResourceData, config *Config) (*GlobalFieldValue, error) {
29+
return parseGlobalFieldValue("httpHealthChecks", healthCheck, "project", d, config, false)
30+
}
31+
2832
func ParseDiskFieldValue(disk string, d TerraformResourceData, config *Config) (*ZonalFieldValue, error) {
2933
return parseZonalFieldValue("disks", disk, "project", "zone", d, config, false)
3034
}

google/resource_compute_target_pool.go

+19-32
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ func resourceComputeTargetPool() *schema.Resource {
5151
Type: schema.TypeList,
5252
Optional: true,
5353
ForceNew: false,
54-
Elem: &schema.Schema{Type: schema.TypeString},
54+
MaxItems: 1,
55+
Elem: &schema.Schema{
56+
Type: schema.TypeString,
57+
DiffSuppressFunc: compareSelfLinkOrResourceName,
58+
},
5559
},
5660

5761
"instances": {
@@ -111,17 +115,17 @@ func resourceComputeTargetPool() *schema.Resource {
111115
}
112116

113117
// Healthchecks need to exist before being referred to from the target pool.
114-
func convertHealthChecks(config *Config, project string, names []string) ([]string, error) {
115-
urls := make([]string, len(names))
116-
for i, name := range names {
117-
// Look up the healthcheck
118-
res, err := config.clientCompute.HttpHealthChecks.Get(project, name).Do()
119-
if err != nil {
120-
return nil, fmt.Errorf("Error reading HealthCheck: %s", err)
121-
}
122-
urls[i] = res.SelfLink
118+
func convertHealthChecks(healthChecks []interface{}, d *schema.ResourceData, config *Config) ([]string, error) {
119+
if healthChecks == nil || len(healthChecks) == 0 {
120+
return []string{}, nil
123121
}
124-
return urls, nil
122+
123+
hc, err := ParseHttpHealthCheckFieldValue(healthChecks[0].(string), d, config)
124+
if err != nil {
125+
return nil, err
126+
}
127+
128+
return []string{hc.RelativeLink()}, nil
125129
}
126130

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

161-
hchkUrls, err := convertHealthChecks(
162-
config, project, convertStringArr(d.Get("health_checks").([]interface{})))
165+
hchkUrls, err := convertHealthChecks(d.Get("health_checks").([]interface{}), d, config)
163166
if err != nil {
164167
return err
165168
}
@@ -247,13 +250,11 @@ func resourceComputeTargetPoolUpdate(d *schema.ResourceData, meta interface{}) e
247250
if d.HasChange("health_checks") {
248251

249252
from_, to_ := d.GetChange("health_checks")
250-
from := convertStringArr(from_.([]interface{}))
251-
to := convertStringArr(to_.([]interface{}))
252-
fromUrls, err := convertHealthChecks(config, project, from)
253+
fromUrls, err := convertHealthChecks(from_.([]interface{}), d, config)
253254
if err != nil {
254255
return err
255256
}
256-
toUrls, err := convertHealthChecks(config, project, to)
257+
toUrls, err := convertHealthChecks(to_.([]interface{}), d, config)
257258
if err != nil {
258259
return err
259260
}
@@ -376,16 +377,6 @@ func convertInstancesFromUrls(urls []string) []string {
376377
return result
377378
}
378379

379-
func convertHealthChecksFromUrls(urls []string) []string {
380-
result := make([]string, 0, len(urls))
381-
for _, url := range urls {
382-
urlArray := strings.Split(url, "/")
383-
healthCheck := fmt.Sprintf("%s", urlArray[len(urlArray)-1])
384-
result = append(result, healthCheck)
385-
}
386-
return result
387-
}
388-
389380
func resourceComputeTargetPoolRead(d *schema.ResourceData, meta interface{}) error {
390381
config := meta.(*Config)
391382

@@ -410,11 +401,7 @@ func resourceComputeTargetPoolRead(d *schema.ResourceData, meta interface{}) err
410401
d.Set("backup_pool", tpool.BackupPool)
411402
d.Set("description", tpool.Description)
412403
d.Set("failover_ratio", tpool.FailoverRatio)
413-
if tpool.HealthChecks != nil {
414-
d.Set("health_checks", convertHealthChecksFromUrls(tpool.HealthChecks))
415-
} else {
416-
d.Set("health_checks", nil)
417-
}
404+
d.Set("health_checks", tpool.HealthChecks)
418405
if tpool.Instances != nil {
419406
d.Set("instances", convertInstancesFromUrls(tpool.Instances))
420407
} else {

google/resource_compute_target_pool_test.go

+36-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ func TestAccComputeTargetPool_basic(t *testing.T) {
2121
Config: testAccComputeTargetPool_basic,
2222
Check: resource.ComposeTestCheckFunc(
2323
testAccCheckComputeTargetPoolExists(
24-
"google_compute_target_pool.foobar"),
24+
"google_compute_target_pool.foo"),
25+
testAccCheckComputeTargetPoolHealthCheck("google_compute_target_pool.foo", "google_compute_http_health_check.foobar"),
26+
testAccCheckComputeTargetPoolExists(
27+
"google_compute_target_pool.bar"),
28+
testAccCheckComputeTargetPoolHealthCheck("google_compute_target_pool.bar", "google_compute_http_health_check.foobar"),
2529
),
2630
},
2731
},
@@ -73,6 +77,27 @@ func testAccCheckComputeTargetPoolExists(n string) resource.TestCheckFunc {
7377
}
7478
}
7579

80+
func testAccCheckComputeTargetPoolHealthCheck(targetPool, healthCheck string) resource.TestCheckFunc {
81+
return func(s *terraform.State) error {
82+
targetPoolRes, ok := s.RootModule().Resources[targetPool]
83+
if !ok {
84+
return fmt.Errorf("Not found: %s", targetPool)
85+
}
86+
87+
healthCheckRes, ok := s.RootModule().Resources[healthCheck]
88+
if !ok {
89+
return fmt.Errorf("Not found: %s", healthCheck)
90+
}
91+
92+
hcLink := healthCheckRes.Primary.Attributes["self_link"]
93+
if targetPoolRes.Primary.Attributes["health_checks.0"] != hcLink {
94+
return fmt.Errorf("Health check not set up. Expected %q", hcLink)
95+
}
96+
97+
return nil
98+
}
99+
}
100+
76101
var testAccComputeTargetPool_basic = fmt.Sprintf(`
77102
resource "google_compute_http_health_check" "foobar" {
78103
name = "healthcheck-test-%s"
@@ -95,12 +120,20 @@ resource "google_compute_instance" "foobar" {
95120
}
96121
}
97122
98-
resource "google_compute_target_pool" "foobar" {
123+
resource "google_compute_target_pool" "foo" {
99124
description = "Resource created for Terraform acceptance testing"
100125
instances = ["${google_compute_instance.foobar.self_link}", "us-central1-b/bar"]
101126
name = "tpool-test-%s"
102127
session_affinity = "CLIENT_IP_PROTO"
103128
health_checks = [
104129
"${google_compute_http_health_check.foobar.name}"
105130
]
106-
}`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))
131+
}
132+
133+
resource "google_compute_target_pool" "bar" {
134+
description = "Resource created for Terraform acceptance testing"
135+
name = "tpool-test-%s"
136+
health_checks = [
137+
"${google_compute_http_health_check.foobar.self_link}"
138+
]
139+
}`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))

website/docs/r/compute_target_pool.html.markdown

+9-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ resource "google_compute_target_pool" "default" {
3030
"${google_compute_http_health_check.default.name}",
3131
]
3232
}
33+
34+
resource "google_compute_http_health_check" "default" {
35+
name = "default"
36+
request_path = "/"
37+
check_interval_sec = 1
38+
timeout_sec = 1
39+
}
3340
```
3441

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

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

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

0 commit comments

Comments
 (0)