Skip to content

Add Optional Log Fields to google_cloud_region_backend_service #9484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/13094.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added `log_config.optional_mode` and `log_config.optional_fields` fields to `google_compute_region_backend_service` resource
```
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,25 @@ If logging is enabled, logs will be exported to Stackdriver.`,
Type: schema.TypeBool,
Optional: true,
Description: `Whether to enable logging for the load balancer traffic served by this backend service.`,
AtLeastOneOf: []string{"log_config.0.enable", "log_config.0.sample_rate"},
AtLeastOneOf: []string{"log_config.0.enable", "log_config.0.sample_rate", "log_config.0.optional_mode"},
},
"optional_fields": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: `Specifies the fields to include in logging. This field can only be specified if logging is enabled for this backend service.`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"optional_mode": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ValidateFunc: verify.ValidateEnum([]string{"INCLUDE_ALL_OPTIONAL", "EXCLUDE_ALL_OPTIONAL", "CUSTOM", ""}),
Description: `Specifies the optional logging mode for the load balancer traffic.
Supported values: INCLUDE_ALL_OPTIONAL, EXCLUDE_ALL_OPTIONAL, CUSTOM. Possible values: ["INCLUDE_ALL_OPTIONAL", "EXCLUDE_ALL_OPTIONAL", "CUSTOM"]`,
AtLeastOneOf: []string{"log_config.0.enable", "log_config.0.sample_rate", "log_config.0.optional_mode"},
},
"sample_rate": {
Type: schema.TypeFloat,
Expand All @@ -822,7 +840,7 @@ the field must be in [0, 1]. This configures the sampling rate of requests to th
where 1.0 means all logged requests are reported and 0.0 means no logged requests are reported.
The default value is 1.0.`,
Default: 1.0,
AtLeastOneOf: []string{"log_config.0.enable", "log_config.0.sample_rate"},
AtLeastOneOf: []string{"log_config.0.enable", "log_config.0.sample_rate", "log_config.0.optional_mode"},
},
},
},
Expand Down Expand Up @@ -3408,6 +3426,10 @@ func flattenComputeRegionBackendServiceLogConfig(v interface{}, d *schema.Resour
flattenComputeRegionBackendServiceLogConfigEnable(original["enable"], d, config)
transformed["sample_rate"] =
flattenComputeRegionBackendServiceLogConfigSampleRate(original["sampleRate"], d, config)
transformed["optional_mode"] =
flattenComputeRegionBackendServiceLogConfigOptionalMode(original["optionalMode"], d, config)
transformed["optional_fields"] =
flattenComputeRegionBackendServiceLogConfigOptionalFields(original["optionalFields"], d, config)
return []interface{}{transformed}
}
func flattenComputeRegionBackendServiceLogConfigEnable(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
Expand All @@ -3418,6 +3440,14 @@ func flattenComputeRegionBackendServiceLogConfigSampleRate(v interface{}, d *sch
return v
}

func flattenComputeRegionBackendServiceLogConfigOptionalMode(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenComputeRegionBackendServiceLogConfigOptionalFields(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenComputeRegionBackendServiceNetwork(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return v
Expand Down Expand Up @@ -4654,6 +4684,20 @@ func expandComputeRegionBackendServiceLogConfig(v interface{}, d tpgresource.Ter
transformed["sampleRate"] = transformedSampleRate
}

transformedOptionalMode, err := expandComputeRegionBackendServiceLogConfigOptionalMode(original["optional_mode"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedOptionalMode); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["optionalMode"] = transformedOptionalMode
}

transformedOptionalFields, err := expandComputeRegionBackendServiceLogConfigOptionalFields(original["optional_fields"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedOptionalFields); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["optionalFields"] = transformedOptionalFields
}

return transformed, nil
}

Expand All @@ -4665,6 +4709,14 @@ func expandComputeRegionBackendServiceLogConfigSampleRate(v interface{}, d tpgre
return v, nil
}

func expandComputeRegionBackendServiceLogConfigOptionalMode(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandComputeRegionBackendServiceLogConfigOptionalFields(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandComputeRegionBackendServiceNetwork(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
f, err := tpgresource.ParseGlobalFieldValue("networks", v.(string), "project", d, config, true)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ fields:
- field: 'load_balancing_scheme'
- field: 'locality_lb_policy'
- field: 'log_config.enable'
- field: 'log_config.optional_fields'
- field: 'log_config.optional_mode'
- field: 'log_config.sample_rate'
- field: 'name'
- field: 'network'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,37 @@ func TestAccComputeRegionBackendService_subsettingUpdate(t *testing.T) {
})
}

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

serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeRegionBackendServiceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeRegionBackendService_withLogConfig(serviceName, checkName),
},
{
ResourceName: "google_compute_region_backend_service.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeRegionBackendService_withLogConfigDisabled(serviceName, checkName),
},
{
ResourceName: "google_compute_region_backend_service.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccComputeRegionBackendService_ilbBasic_withUnspecifiedProtocol(serviceName, checkName string) string {
return fmt.Sprintf(`
resource "google_compute_region_backend_service" "foobar" {
Expand Down Expand Up @@ -1209,3 +1240,61 @@ resource "google_compute_region_security_policy" "policy" {
}
`, serviceName, polLink, polName)
}

func testAccComputeRegionBackendService_withLogConfig(serviceName, checkName string) string {
return fmt.Sprintf(`
resource "google_compute_region_backend_service" "foobar" {
name = "%s"
region = "us-central1"
health_checks = [google_compute_region_health_check.health_check.self_link]
protocol = "HTTP"
load_balancing_scheme = "INTERNAL_MANAGED"
locality_lb_policy = "ROUND_ROBIN"

log_config {
enable = true
sample_rate = 1.0
optional_mode = "INCLUDE_ALL_OPTIONAL"
}
}

resource "google_compute_region_health_check" "health_check" {
name = "%s"
region = "us-central1"
check_interval_sec = 1
timeout_sec = 1

http_health_check {
port = 80
}
}
`, serviceName, checkName)
}

func testAccComputeRegionBackendService_withLogConfigDisabled(serviceName, checkName string) string {
return fmt.Sprintf(`
resource "google_compute_region_backend_service" "foobar" {
name = "%s"
region = "us-central1"
health_checks = [google_compute_region_health_check.health_check.self_link]
protocol = "HTTP"
load_balancing_scheme = "INTERNAL_MANAGED"
locality_lb_policy = "ROUND_ROBIN"

log_config {
enable = false
}
}

resource "google_compute_region_health_check" "health_check" {
name = "%s"
region = "us-central1"
check_interval_sec = 1
timeout_sec = 1

http_health_check {
port = 80
}
}
`, serviceName, checkName)
}
10 changes: 10 additions & 0 deletions website/docs/r/compute_region_backend_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,16 @@ The following arguments are supported:
where 1.0 means all logged requests are reported and 0.0 means no logged requests are reported.
The default value is 1.0.

* `optional_mode` -
(Optional)
Specifies the optional logging mode for the load balancer traffic.
Supported values: INCLUDE_ALL_OPTIONAL, EXCLUDE_ALL_OPTIONAL, CUSTOM.
Possible values are: `INCLUDE_ALL_OPTIONAL`, `EXCLUDE_ALL_OPTIONAL`, `CUSTOM`.

* `optional_fields` -
(Optional)
Specifies the fields to include in logging. This field can only be specified if logging is enabled for this backend service.

<a name="nested_subsetting"></a>The `subsetting` block supports:

* `policy` -
Expand Down