Skip to content

Commit

Permalink
Health Check Logging (#3346) (#1934)
Browse files Browse the repository at this point in the history
* hc-logging

* update health checking example to be beta only

* RegionHealthCheck Logging test

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Apr 6, 2020
1 parent 4487914 commit cf58b17
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/3346.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: Added field `log_config` to `google_compute_health_check` and `google_compute_region_health_check` to enable health check logging.
```
71 changes: 71 additions & 0 deletions google-beta/resource_compute_health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,22 @@ can only be ASCII.`,
ConflictsWith: []string{"http_health_check", "tcp_health_check", "ssl_health_check", "http2_health_check"},
AtLeastOneOf: []string{"http_health_check", "https_health_check", "http2_health_check", "tcp_health_check", "ssl_health_check"},
},
"log_config": {
Type: schema.TypeList,
Optional: true,
Description: `Configure logging on this health check.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enable": {
Type: schema.TypeBool,
Optional: true,
Description: `Indicates whether or not to export logs. This is false by default,
which means no health check logging will be done.`,
},
},
},
},
"ssl_health_check": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -656,6 +672,12 @@ func resourceComputeHealthCheckCreate(d *schema.ResourceData, meta interface{})
} else if v, ok := d.GetOkExists("http2_health_check"); !isEmptyValue(reflect.ValueOf(http2HealthCheckProp)) && (ok || !reflect.DeepEqual(v, http2HealthCheckProp)) {
obj["http2HealthCheck"] = http2HealthCheckProp
}
logConfigProp, err := expandComputeHealthCheckLogConfig(d.Get("log_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("log_config"); !isEmptyValue(reflect.ValueOf(logConfigProp)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
obj["logConfig"] = logConfigProp
}

obj, err = resourceComputeHealthCheckEncoder(d, meta, obj)
if err != nil {
Expand Down Expand Up @@ -759,6 +781,9 @@ func resourceComputeHealthCheckRead(d *schema.ResourceData, meta interface{}) er
if err := d.Set("http2_health_check", flattenComputeHealthCheckHttp2HealthCheck(res["http2HealthCheck"], d, config)); err != nil {
return fmt.Errorf("Error reading HealthCheck: %s", err)
}
if err := d.Set("log_config", flattenComputeHealthCheckLogConfig(res["logConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading HealthCheck: %s", err)
}
if err := d.Set("self_link", ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil {
return fmt.Errorf("Error reading HealthCheck: %s", err)
}
Expand Down Expand Up @@ -841,6 +866,12 @@ func resourceComputeHealthCheckUpdate(d *schema.ResourceData, meta interface{})
} else if v, ok := d.GetOkExists("http2_health_check"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, http2HealthCheckProp)) {
obj["http2HealthCheck"] = http2HealthCheckProp
}
logConfigProp, err := expandComputeHealthCheckLogConfig(d.Get("log_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("log_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
obj["logConfig"] = logConfigProp
}

obj, err = resourceComputeHealthCheckEncoder(d, meta, obj)
if err != nil {
Expand Down Expand Up @@ -1325,6 +1356,23 @@ func flattenComputeHealthCheckHttp2HealthCheckPortSpecification(v interface{}, d
return v
}

func flattenComputeHealthCheckLogConfig(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["enable"] =
flattenComputeHealthCheckLogConfigEnable(original["enable"], d, config)
return []interface{}{transformed}
}
func flattenComputeHealthCheckLogConfigEnable(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func expandComputeHealthCheckCheckIntervalSec(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -1772,6 +1820,29 @@ func expandComputeHealthCheckHttp2HealthCheckPortSpecification(v interface{}, d
return v, nil
}

func expandComputeHealthCheckLogConfig(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedEnable, err := expandComputeHealthCheckLogConfigEnable(original["enable"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedEnable); val.IsValid() && !isEmptyValue(val) {
transformed["enable"] = transformedEnable
}

return transformed, nil
}

func expandComputeHealthCheckLogConfigEnable(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func resourceComputeHealthCheckEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {

if _, ok := d.GetOk("http_health_check"); ok {
Expand Down
40 changes: 40 additions & 0 deletions google-beta/resource_compute_health_check_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,46 @@ resource "google_compute_health_check" "http2-health-check" {
`, context)
}

func TestAccComputeHealthCheck_healthCheckWithLoggingExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(10),
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProvidersOiCS,
CheckDestroy: testAccCheckComputeHealthCheckDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeHealthCheck_healthCheckWithLoggingExample(context),
},
},
})
}

func testAccComputeHealthCheck_healthCheckWithLoggingExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_health_check" "health-check-with-logging" {
provider = google-beta
name = "tf-test-tcp-health-check%{random_suffix}"
timeout_sec = 1
check_interval_sec = 1
tcp_health_check {
port = "22"
}
log_config {
enable = true
}
}
`, context)
}

func testAccCheckComputeHealthCheckDestroy(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
if rs.Type != "google_compute_health_check" {
Expand Down
71 changes: 71 additions & 0 deletions google-beta/resource_compute_region_health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,22 @@ can only be ASCII.`,
ConflictsWith: []string{"http_health_check", "tcp_health_check", "ssl_health_check", "http2_health_check"},
AtLeastOneOf: []string{"http_health_check", "https_health_check", "http2_health_check", "tcp_health_check", "ssl_health_check"},
},
"log_config": {
Type: schema.TypeList,
Optional: true,
Description: `Configure logging on this health check.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enable": {
Type: schema.TypeBool,
Optional: true,
Description: `Indicates whether or not to export logs. This is false by default,
which means no health check logging will be done.`,
},
},
},
},
"region": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -581,6 +597,12 @@ func resourceComputeRegionHealthCheckCreate(d *schema.ResourceData, meta interfa
} else if v, ok := d.GetOkExists("http2_health_check"); !isEmptyValue(reflect.ValueOf(http2HealthCheckProp)) && (ok || !reflect.DeepEqual(v, http2HealthCheckProp)) {
obj["http2HealthCheck"] = http2HealthCheckProp
}
logConfigProp, err := expandComputeRegionHealthCheckLogConfig(d.Get("log_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("log_config"); !isEmptyValue(reflect.ValueOf(logConfigProp)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
obj["logConfig"] = logConfigProp
}
regionProp, err := expandComputeRegionHealthCheckRegion(d.Get("region"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -690,6 +712,9 @@ func resourceComputeRegionHealthCheckRead(d *schema.ResourceData, meta interface
if err := d.Set("http2_health_check", flattenComputeRegionHealthCheckHttp2HealthCheck(res["http2HealthCheck"], d, config)); err != nil {
return fmt.Errorf("Error reading RegionHealthCheck: %s", err)
}
if err := d.Set("log_config", flattenComputeRegionHealthCheckLogConfig(res["logConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading RegionHealthCheck: %s", err)
}
if err := d.Set("region", flattenComputeRegionHealthCheckRegion(res["region"], d, config)); err != nil {
return fmt.Errorf("Error reading RegionHealthCheck: %s", err)
}
Expand Down Expand Up @@ -775,6 +800,12 @@ func resourceComputeRegionHealthCheckUpdate(d *schema.ResourceData, meta interfa
} else if v, ok := d.GetOkExists("http2_health_check"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, http2HealthCheckProp)) {
obj["http2HealthCheck"] = http2HealthCheckProp
}
logConfigProp, err := expandComputeRegionHealthCheckLogConfig(d.Get("log_config"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("log_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
obj["logConfig"] = logConfigProp
}
regionProp, err := expandComputeRegionHealthCheckRegion(d.Get("region"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -1266,6 +1297,23 @@ func flattenComputeRegionHealthCheckHttp2HealthCheckPortSpecification(v interfac
return v
}

func flattenComputeRegionHealthCheckLogConfig(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["enable"] =
flattenComputeRegionHealthCheckLogConfigEnable(original["enable"], d, config)
return []interface{}{transformed}
}
func flattenComputeRegionHealthCheckLogConfigEnable(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenComputeRegionHealthCheckRegion(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return v
Expand Down Expand Up @@ -1720,6 +1768,29 @@ func expandComputeRegionHealthCheckHttp2HealthCheckPortSpecification(v interface
return v, nil
}

func expandComputeRegionHealthCheckLogConfig(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedEnable, err := expandComputeRegionHealthCheckLogConfigEnable(original["enable"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedEnable); val.IsValid() && !isEmptyValue(val) {
transformed["enable"] = transformedEnable
}

return transformed, nil
}

func expandComputeRegionHealthCheckLogConfigEnable(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeRegionHealthCheckRegion(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
f, err := parseGlobalFieldValue("regions", v.(string), "project", d, config, true)
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions google-beta/resource_compute_region_health_check_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,46 @@ resource "google_compute_region_health_check" "http-region-health-check" {
`, context)
}

func TestAccComputeRegionHealthCheck_regionHealthCheckHttpLogsExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(10),
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProvidersOiCS,
CheckDestroy: testAccCheckComputeRegionHealthCheckDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeRegionHealthCheck_regionHealthCheckHttpLogsExample(context),
},
},
})
}

func testAccComputeRegionHealthCheck_regionHealthCheckHttpLogsExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_region_health_check" "http-region-health-check" {
provider = google-beta
name = "tf-test-http-region-health-check%{random_suffix}"
timeout_sec = 1
check_interval_sec = 1
http_health_check {
port = "80"
}
log_config {
enable = true
}
}
`, context)
}

func TestAccComputeRegionHealthCheck_regionHealthCheckHttpFullExample(t *testing.T) {
t.Parallel()

Expand Down
37 changes: 37 additions & 0 deletions website/docs/r/compute_health_check.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,32 @@ resource "google_compute_health_check" "http2-health-check" {
}
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.jparrowsec.cn%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">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Health Check With Logging


```hcl
resource "google_compute_health_check" "health-check-with-logging" {
provider = google-beta
name = "tcp-health-check"
timeout_sec = 1
check_interval_sec = 1
tcp_health_check {
port = "22"
}
log_config {
enable = true
}
}
```

## Argument Reference

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

* `log_config` -
(Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
Configure logging on this health check. Structure is documented below.

* `project` - (Optional) The ID of the project in which the resource belongs.
If it is not provided, the provider project is used.

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

The `log_config` block supports:

* `enable` -
(Optional)
Indicates whether or not to export logs. This is false by default,
which means no health check logging will be done.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:
Expand Down
Loading

0 comments on commit cf58b17

Please sign in to comment.