Skip to content

Commit

Permalink
Add log_config to backend service
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
slevenick authored and modular-magician committed Sep 13, 2019
1 parent 18a18c5 commit f65a4a6
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
89 changes: 89 additions & 0 deletions google-beta/resource_compute_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,23 @@ func resourceComputeBackendService() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{"EXTERNAL", "INTERNAL_SELF_MANAGED", ""}, false),
Default: "EXTERNAL",
},
"log_config": {
Type: schema.TypeList,
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,
Expand Down Expand Up @@ -493,6 +510,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 {
Expand Down Expand Up @@ -655,6 +678,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)
}
Expand Down Expand Up @@ -773,6 +799,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 {
Expand Down Expand Up @@ -1182,6 +1214,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
}
Expand Down Expand Up @@ -1533,6 +1588,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
Expand Down
52 changes: 52 additions & 0 deletions google-beta/resource_compute_backend_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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)
}
18 changes: 18 additions & 0 deletions website/docs/r/compute_backend_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit f65a4a6

Please sign in to comment.