Skip to content

Commit cf58b17

Browse files
Health Check Logging (#3346) (#1934)
* hc-logging * update health checking example to be beta only * RegionHealthCheck Logging test Signed-off-by: Modular Magician <[email protected]>
1 parent 4487914 commit cf58b17

7 files changed

+299
-0
lines changed

.changelog/3346.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: Added field `log_config` to `google_compute_health_check` and `google_compute_region_health_check` to enable health check logging.
3+
```

google-beta/resource_compute_health_check.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,22 @@ can only be ASCII.`,
401401
ConflictsWith: []string{"http_health_check", "tcp_health_check", "ssl_health_check", "http2_health_check"},
402402
AtLeastOneOf: []string{"http_health_check", "https_health_check", "http2_health_check", "tcp_health_check", "ssl_health_check"},
403403
},
404+
"log_config": {
405+
Type: schema.TypeList,
406+
Optional: true,
407+
Description: `Configure logging on this health check.`,
408+
MaxItems: 1,
409+
Elem: &schema.Resource{
410+
Schema: map[string]*schema.Schema{
411+
"enable": {
412+
Type: schema.TypeBool,
413+
Optional: true,
414+
Description: `Indicates whether or not to export logs. This is false by default,
415+
which means no health check logging will be done.`,
416+
},
417+
},
418+
},
419+
},
404420
"ssl_health_check": {
405421
Type: schema.TypeList,
406422
Optional: true,
@@ -656,6 +672,12 @@ func resourceComputeHealthCheckCreate(d *schema.ResourceData, meta interface{})
656672
} else if v, ok := d.GetOkExists("http2_health_check"); !isEmptyValue(reflect.ValueOf(http2HealthCheckProp)) && (ok || !reflect.DeepEqual(v, http2HealthCheckProp)) {
657673
obj["http2HealthCheck"] = http2HealthCheckProp
658674
}
675+
logConfigProp, err := expandComputeHealthCheckLogConfig(d.Get("log_config"), d, config)
676+
if err != nil {
677+
return err
678+
} else if v, ok := d.GetOkExists("log_config"); !isEmptyValue(reflect.ValueOf(logConfigProp)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
679+
obj["logConfig"] = logConfigProp
680+
}
659681

660682
obj, err = resourceComputeHealthCheckEncoder(d, meta, obj)
661683
if err != nil {
@@ -759,6 +781,9 @@ func resourceComputeHealthCheckRead(d *schema.ResourceData, meta interface{}) er
759781
if err := d.Set("http2_health_check", flattenComputeHealthCheckHttp2HealthCheck(res["http2HealthCheck"], d, config)); err != nil {
760782
return fmt.Errorf("Error reading HealthCheck: %s", err)
761783
}
784+
if err := d.Set("log_config", flattenComputeHealthCheckLogConfig(res["logConfig"], d, config)); err != nil {
785+
return fmt.Errorf("Error reading HealthCheck: %s", err)
786+
}
762787
if err := d.Set("self_link", ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil {
763788
return fmt.Errorf("Error reading HealthCheck: %s", err)
764789
}
@@ -841,6 +866,12 @@ func resourceComputeHealthCheckUpdate(d *schema.ResourceData, meta interface{})
841866
} else if v, ok := d.GetOkExists("http2_health_check"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, http2HealthCheckProp)) {
842867
obj["http2HealthCheck"] = http2HealthCheckProp
843868
}
869+
logConfigProp, err := expandComputeHealthCheckLogConfig(d.Get("log_config"), d, config)
870+
if err != nil {
871+
return err
872+
} else if v, ok := d.GetOkExists("log_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
873+
obj["logConfig"] = logConfigProp
874+
}
844875

845876
obj, err = resourceComputeHealthCheckEncoder(d, meta, obj)
846877
if err != nil {
@@ -1325,6 +1356,23 @@ func flattenComputeHealthCheckHttp2HealthCheckPortSpecification(v interface{}, d
13251356
return v
13261357
}
13271358

1359+
func flattenComputeHealthCheckLogConfig(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1360+
if v == nil {
1361+
return nil
1362+
}
1363+
original := v.(map[string]interface{})
1364+
if len(original) == 0 {
1365+
return nil
1366+
}
1367+
transformed := make(map[string]interface{})
1368+
transformed["enable"] =
1369+
flattenComputeHealthCheckLogConfigEnable(original["enable"], d, config)
1370+
return []interface{}{transformed}
1371+
}
1372+
func flattenComputeHealthCheckLogConfigEnable(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1373+
return v
1374+
}
1375+
13281376
func expandComputeHealthCheckCheckIntervalSec(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
13291377
return v, nil
13301378
}
@@ -1772,6 +1820,29 @@ func expandComputeHealthCheckHttp2HealthCheckPortSpecification(v interface{}, d
17721820
return v, nil
17731821
}
17741822

1823+
func expandComputeHealthCheckLogConfig(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1824+
l := v.([]interface{})
1825+
if len(l) == 0 || l[0] == nil {
1826+
return nil, nil
1827+
}
1828+
raw := l[0]
1829+
original := raw.(map[string]interface{})
1830+
transformed := make(map[string]interface{})
1831+
1832+
transformedEnable, err := expandComputeHealthCheckLogConfigEnable(original["enable"], d, config)
1833+
if err != nil {
1834+
return nil, err
1835+
} else if val := reflect.ValueOf(transformedEnable); val.IsValid() && !isEmptyValue(val) {
1836+
transformed["enable"] = transformedEnable
1837+
}
1838+
1839+
return transformed, nil
1840+
}
1841+
1842+
func expandComputeHealthCheckLogConfigEnable(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1843+
return v, nil
1844+
}
1845+
17751846
func resourceComputeHealthCheckEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
17761847

17771848
if _, ok := d.GetOk("http_health_check"); ok {

google-beta/resource_compute_health_check_generated_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,46 @@ resource "google_compute_health_check" "http2-health-check" {
452452
`, context)
453453
}
454454

455+
func TestAccComputeHealthCheck_healthCheckWithLoggingExample(t *testing.T) {
456+
t.Parallel()
457+
458+
context := map[string]interface{}{
459+
"random_suffix": acctest.RandString(10),
460+
}
461+
462+
resource.Test(t, resource.TestCase{
463+
PreCheck: func() { testAccPreCheck(t) },
464+
Providers: testAccProvidersOiCS,
465+
CheckDestroy: testAccCheckComputeHealthCheckDestroy,
466+
Steps: []resource.TestStep{
467+
{
468+
Config: testAccComputeHealthCheck_healthCheckWithLoggingExample(context),
469+
},
470+
},
471+
})
472+
}
473+
474+
func testAccComputeHealthCheck_healthCheckWithLoggingExample(context map[string]interface{}) string {
475+
return Nprintf(`
476+
resource "google_compute_health_check" "health-check-with-logging" {
477+
provider = google-beta
478+
479+
name = "tf-test-tcp-health-check%{random_suffix}"
480+
481+
timeout_sec = 1
482+
check_interval_sec = 1
483+
484+
tcp_health_check {
485+
port = "22"
486+
}
487+
488+
log_config {
489+
enable = true
490+
}
491+
}
492+
`, context)
493+
}
494+
455495
func testAccCheckComputeHealthCheckDestroy(s *terraform.State) error {
456496
for name, rs := range s.RootModule().Resources {
457497
if rs.Type != "google_compute_health_check" {

google-beta/resource_compute_region_health_check.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,22 @@ can only be ASCII.`,
317317
ConflictsWith: []string{"http_health_check", "tcp_health_check", "ssl_health_check", "http2_health_check"},
318318
AtLeastOneOf: []string{"http_health_check", "https_health_check", "http2_health_check", "tcp_health_check", "ssl_health_check"},
319319
},
320+
"log_config": {
321+
Type: schema.TypeList,
322+
Optional: true,
323+
Description: `Configure logging on this health check.`,
324+
MaxItems: 1,
325+
Elem: &schema.Resource{
326+
Schema: map[string]*schema.Schema{
327+
"enable": {
328+
Type: schema.TypeBool,
329+
Optional: true,
330+
Description: `Indicates whether or not to export logs. This is false by default,
331+
which means no health check logging will be done.`,
332+
},
333+
},
334+
},
335+
},
320336
"region": {
321337
Type: schema.TypeString,
322338
Computed: true,
@@ -581,6 +597,12 @@ func resourceComputeRegionHealthCheckCreate(d *schema.ResourceData, meta interfa
581597
} else if v, ok := d.GetOkExists("http2_health_check"); !isEmptyValue(reflect.ValueOf(http2HealthCheckProp)) && (ok || !reflect.DeepEqual(v, http2HealthCheckProp)) {
582598
obj["http2HealthCheck"] = http2HealthCheckProp
583599
}
600+
logConfigProp, err := expandComputeRegionHealthCheckLogConfig(d.Get("log_config"), d, config)
601+
if err != nil {
602+
return err
603+
} else if v, ok := d.GetOkExists("log_config"); !isEmptyValue(reflect.ValueOf(logConfigProp)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
604+
obj["logConfig"] = logConfigProp
605+
}
584606
regionProp, err := expandComputeRegionHealthCheckRegion(d.Get("region"), d, config)
585607
if err != nil {
586608
return err
@@ -690,6 +712,9 @@ func resourceComputeRegionHealthCheckRead(d *schema.ResourceData, meta interface
690712
if err := d.Set("http2_health_check", flattenComputeRegionHealthCheckHttp2HealthCheck(res["http2HealthCheck"], d, config)); err != nil {
691713
return fmt.Errorf("Error reading RegionHealthCheck: %s", err)
692714
}
715+
if err := d.Set("log_config", flattenComputeRegionHealthCheckLogConfig(res["logConfig"], d, config)); err != nil {
716+
return fmt.Errorf("Error reading RegionHealthCheck: %s", err)
717+
}
693718
if err := d.Set("region", flattenComputeRegionHealthCheckRegion(res["region"], d, config)); err != nil {
694719
return fmt.Errorf("Error reading RegionHealthCheck: %s", err)
695720
}
@@ -775,6 +800,12 @@ func resourceComputeRegionHealthCheckUpdate(d *schema.ResourceData, meta interfa
775800
} else if v, ok := d.GetOkExists("http2_health_check"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, http2HealthCheckProp)) {
776801
obj["http2HealthCheck"] = http2HealthCheckProp
777802
}
803+
logConfigProp, err := expandComputeRegionHealthCheckLogConfig(d.Get("log_config"), d, config)
804+
if err != nil {
805+
return err
806+
} else if v, ok := d.GetOkExists("log_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
807+
obj["logConfig"] = logConfigProp
808+
}
778809
regionProp, err := expandComputeRegionHealthCheckRegion(d.Get("region"), d, config)
779810
if err != nil {
780811
return err
@@ -1266,6 +1297,23 @@ func flattenComputeRegionHealthCheckHttp2HealthCheckPortSpecification(v interfac
12661297
return v
12671298
}
12681299

1300+
func flattenComputeRegionHealthCheckLogConfig(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1301+
if v == nil {
1302+
return nil
1303+
}
1304+
original := v.(map[string]interface{})
1305+
if len(original) == 0 {
1306+
return nil
1307+
}
1308+
transformed := make(map[string]interface{})
1309+
transformed["enable"] =
1310+
flattenComputeRegionHealthCheckLogConfigEnable(original["enable"], d, config)
1311+
return []interface{}{transformed}
1312+
}
1313+
func flattenComputeRegionHealthCheckLogConfigEnable(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1314+
return v
1315+
}
1316+
12691317
func flattenComputeRegionHealthCheckRegion(v interface{}, d *schema.ResourceData, config *Config) interface{} {
12701318
if v == nil {
12711319
return v
@@ -1720,6 +1768,29 @@ func expandComputeRegionHealthCheckHttp2HealthCheckPortSpecification(v interface
17201768
return v, nil
17211769
}
17221770

1771+
func expandComputeRegionHealthCheckLogConfig(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1772+
l := v.([]interface{})
1773+
if len(l) == 0 || l[0] == nil {
1774+
return nil, nil
1775+
}
1776+
raw := l[0]
1777+
original := raw.(map[string]interface{})
1778+
transformed := make(map[string]interface{})
1779+
1780+
transformedEnable, err := expandComputeRegionHealthCheckLogConfigEnable(original["enable"], d, config)
1781+
if err != nil {
1782+
return nil, err
1783+
} else if val := reflect.ValueOf(transformedEnable); val.IsValid() && !isEmptyValue(val) {
1784+
transformed["enable"] = transformedEnable
1785+
}
1786+
1787+
return transformed, nil
1788+
}
1789+
1790+
func expandComputeRegionHealthCheckLogConfigEnable(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1791+
return v, nil
1792+
}
1793+
17231794
func expandComputeRegionHealthCheckRegion(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
17241795
f, err := parseGlobalFieldValue("regions", v.(string), "project", d, config, true)
17251796
if err != nil {

google-beta/resource_compute_region_health_check_generated_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,46 @@ resource "google_compute_region_health_check" "http-region-health-check" {
233233
`, context)
234234
}
235235

236+
func TestAccComputeRegionHealthCheck_regionHealthCheckHttpLogsExample(t *testing.T) {
237+
t.Parallel()
238+
239+
context := map[string]interface{}{
240+
"random_suffix": acctest.RandString(10),
241+
}
242+
243+
resource.Test(t, resource.TestCase{
244+
PreCheck: func() { testAccPreCheck(t) },
245+
Providers: testAccProvidersOiCS,
246+
CheckDestroy: testAccCheckComputeRegionHealthCheckDestroy,
247+
Steps: []resource.TestStep{
248+
{
249+
Config: testAccComputeRegionHealthCheck_regionHealthCheckHttpLogsExample(context),
250+
},
251+
},
252+
})
253+
}
254+
255+
func testAccComputeRegionHealthCheck_regionHealthCheckHttpLogsExample(context map[string]interface{}) string {
256+
return Nprintf(`
257+
resource "google_compute_region_health_check" "http-region-health-check" {
258+
provider = google-beta
259+
260+
name = "tf-test-http-region-health-check%{random_suffix}"
261+
262+
timeout_sec = 1
263+
check_interval_sec = 1
264+
265+
http_health_check {
266+
port = "80"
267+
}
268+
269+
log_config {
270+
enable = true
271+
}
272+
}
273+
`, context)
274+
}
275+
236276
func TestAccComputeRegionHealthCheck_regionHealthCheckHttpFullExample(t *testing.T) {
237277
t.Parallel()
238278

website/docs/r/compute_health_check.html.markdown

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,32 @@ resource "google_compute_health_check" "http2-health-check" {
279279
}
280280
}
281281
```
282+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
283+
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=health_check_with_logging&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
284+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
285+
</a>
286+
</div>
287+
## Example Usage - Health Check With Logging
288+
289+
290+
```hcl
291+
resource "google_compute_health_check" "health-check-with-logging" {
292+
provider = google-beta
293+
294+
name = "tcp-health-check"
295+
296+
timeout_sec = 1
297+
check_interval_sec = 1
298+
299+
tcp_health_check {
300+
port = "22"
301+
}
302+
303+
log_config {
304+
enable = true
305+
}
306+
}
307+
```
282308

283309
## Argument Reference
284310

@@ -345,6 +371,10 @@ The following arguments are supported:
345371
(Optional)
346372
A nested object resource Structure is documented below.
347373

374+
* `log_config` -
375+
(Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
376+
Configure logging on this health check. Structure is documented below.
377+
348378
* `project` - (Optional) The ID of the project in which the resource belongs.
349379
If it is not provided, the provider project is used.
350380

@@ -576,6 +606,13 @@ The `http2_health_check` block supports:
576606
If not specified, HTTP2 health check follows behavior specified in `port` and
577607
`portName` fields.
578608

609+
The `log_config` block supports:
610+
611+
* `enable` -
612+
(Optional)
613+
Indicates whether or not to export logs. This is false by default,
614+
which means no health check logging will be done.
615+
579616
## Attributes Reference
580617

581618
In addition to the arguments listed above, the following computed attributes are exported:

0 commit comments

Comments
 (0)