From c4dcb97e3cf827cc2c171fc404d04b152affa022 Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Mon, 16 Sep 2019 20:30:18 +0000 Subject: [PATCH] Add log_config to backend service Signed-off-by: Modular Magician --- .../resource_compute_backend_service.go | 90 +++++++++++++++++++ .../resource_compute_backend_service_test.go | 52 +++++++++++ .../r/compute_backend_service.html.markdown | 18 ++++ 3 files changed, 160 insertions(+) diff --git a/google-beta/resource_compute_backend_service.go b/google-beta/resource_compute_backend_service.go index c5848738ee..5e957298d7 100644 --- a/google-beta/resource_compute_backend_service.go +++ b/google-beta/resource_compute_backend_service.go @@ -282,6 +282,24 @@ func resourceComputeBackendService() *schema.Resource { ValidateFunc: validation.StringInSlice([]string{"EXTERNAL", "INTERNAL_SELF_MANAGED", ""}, false), Default: "EXTERNAL", }, + "log_config": { + Type: schema.TypeList, + Computed: true, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "enable": { + Type: schema.TypeBool, + Optional: true, + }, + "sample_rate": { + Type: schema.TypeFloat, + Optional: true, + }, + }, + }, + }, "port_name": { Type: schema.TypeString, Computed: true, @@ -493,6 +511,12 @@ func resourceComputeBackendServiceCreate(d *schema.ResourceData, meta interface{ } else if v, ok := d.GetOkExists("timeout_sec"); !isEmptyValue(reflect.ValueOf(timeoutSecProp)) && (ok || !reflect.DeepEqual(v, timeoutSecProp)) { obj["timeoutSec"] = timeoutSecProp } + logConfigProp, err := expandComputeBackendServiceLogConfig(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 = resourceComputeBackendServiceEncoder(d, meta, obj) if err != nil { @@ -655,6 +679,9 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{}) if err := d.Set("timeout_sec", flattenComputeBackendServiceTimeoutSec(res["timeoutSec"], d)); err != nil { return fmt.Errorf("Error reading BackendService: %s", err) } + if err := d.Set("log_config", flattenComputeBackendServiceLogConfig(res["logConfig"], d)); err != nil { + return fmt.Errorf("Error reading BackendService: %s", err) + } if err := d.Set("self_link", ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil { return fmt.Errorf("Error reading BackendService: %s", err) } @@ -773,6 +800,12 @@ func resourceComputeBackendServiceUpdate(d *schema.ResourceData, meta interface{ } else if v, ok := d.GetOkExists("timeout_sec"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, timeoutSecProp)) { obj["timeoutSec"] = timeoutSecProp } + logConfigProp, err := expandComputeBackendServiceLogConfig(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 = resourceComputeBackendServiceEncoder(d, meta, obj) if err != nil { @@ -1182,6 +1215,29 @@ func flattenComputeBackendServiceTimeoutSec(v interface{}, d *schema.ResourceDat return v } +func flattenComputeBackendServiceLogConfig(v interface{}, d *schema.ResourceData) interface{} { + if v == nil { + return nil + } + original := v.(map[string]interface{}) + if len(original) == 0 { + return nil + } + transformed := make(map[string]interface{}) + transformed["enable"] = + flattenComputeBackendServiceLogConfigEnable(original["enable"], d) + transformed["sample_rate"] = + flattenComputeBackendServiceLogConfigSampleRate(original["sampleRate"], d) + return []interface{}{transformed} +} +func flattenComputeBackendServiceLogConfigEnable(v interface{}, d *schema.ResourceData) interface{} { + return v +} + +func flattenComputeBackendServiceLogConfigSampleRate(v interface{}, d *schema.ResourceData) interface{} { + return v +} + func expandComputeBackendServiceAffinityCookieTtlSec(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { return v, nil } @@ -1533,6 +1589,40 @@ func expandComputeBackendServiceTimeoutSec(v interface{}, d TerraformResourceDat return v, nil } +func expandComputeBackendServiceLogConfig(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 := expandComputeBackendServiceLogConfigEnable(original["enable"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedEnable); val.IsValid() && !isEmptyValue(val) { + transformed["enable"] = transformedEnable + } + + transformedSampleRate, err := expandComputeBackendServiceLogConfigSampleRate(original["sample_rate"], d, config) + if err != nil { + return nil, err + } else if val := reflect.ValueOf(transformedSampleRate); val.IsValid() && !isEmptyValue(val) { + transformed["sampleRate"] = transformedSampleRate + } + + return transformed, nil +} + +func expandComputeBackendServiceLogConfigEnable(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandComputeBackendServiceLogConfigSampleRate(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + func resourceComputeBackendServiceEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) { // The BackendService API's Update / PUT API is badly formed and behaves like // a PATCH field for at least IAP. When sent a `null` `iap` field, the API diff --git a/google-beta/resource_compute_backend_service_test.go b/google-beta/resource_compute_backend_service_test.go index 2baa86b1c6..80ca2e5731 100644 --- a/google-beta/resource_compute_backend_service_test.go +++ b/google-beta/resource_compute_backend_service_test.go @@ -747,6 +747,37 @@ func TestAccComputeBackendService_internalLoadBalancing(t *testing.T) { }) } +func TestAccComputeBackendService_withLogConfig(t *testing.T) { + t.Parallel() + + serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) + checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeBackendServiceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccComputeBackendService_withLogConfig(serviceName, checkName, 0.7), + }, + { + ResourceName: "google_compute_backend_service.foobar", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccComputeBackendService_withLogConfig(serviceName, checkName, 0.4), + }, + { + ResourceName: "google_compute_backend_service.foobar", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccComputeBackendService_basic(serviceName, checkName string) string { return fmt.Sprintf(` resource "google_compute_backend_service" "foobar" { @@ -1422,3 +1453,24 @@ resource "google_compute_instance_template" "foobar" { } }`, fr, proxy, backend, hc, urlmap) } + +func testAccComputeBackendService_withLogConfig(serviceName, checkName string, sampleRate float64) string { + return fmt.Sprintf(` +resource "google_compute_backend_service" "foobar" { + name = "%s" + health_checks = ["${google_compute_http_health_check.zero.self_link}"] + + log_config { + enable = true + sample_rate = %v + } +} + +resource "google_compute_http_health_check" "zero" { + name = "%s" + request_path = "/" + check_interval_sec = 1 + timeout_sec = 1 +} +`, serviceName, sampleRate, checkName) +} diff --git a/website/docs/r/compute_backend_service.html.markdown b/website/docs/r/compute_backend_service.html.markdown index b1e9c487a3..a6ca637d0d 100644 --- a/website/docs/r/compute_backend_service.html.markdown +++ b/website/docs/r/compute_backend_service.html.markdown @@ -158,6 +158,11 @@ The following arguments are supported: How many seconds to wait for the backend before considering it a failed request. Default is 30 seconds. Valid range is [1, 86400]. +* `log_config` - + (Optional, [Beta](https://terraform.io/docs/providers/google/provider_versions.html)) + This field denotes the logging options for the load balancer traffic served by this backend service. + If logging is enabled, logs will be exported to Stackdriver. 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. @@ -323,6 +328,19 @@ The `iap` block supports: * `oauth2_client_secret_sha256` - OAuth2 Client Secret SHA-256 for IAP +The `log_config` block supports: + +* `enable` - + (Optional) + Whether to enable logging for the load balancer traffic served by this backend service. + +* `sample_rate` - + (Optional) + This field can only be specified if logging is enabled for this backend service. The value of + the field must be in [0, 1]. This configures the sampling rate of requests to the load balancer + where 1.0 means all logged requests are reported and 0.0 means no logged requests are reported. + The default value is 1.0. + ## Attributes Reference In addition to the arguments listed above, the following computed attributes are exported: