Skip to content
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

fixed perma-diff on log_config.enable #10378

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/5324.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
compute: fixed perma-diff bug on `log_config.enable` of both `google_compute_backend_service` and `google_compute_region_backend_service`
```
16 changes: 13 additions & 3 deletions google/resource_compute_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ import (
"google.golang.org/api/googleapi"
)

// suppress changes on sample_rate if log_config is set to disabled.
func suppressWhenDisabled(k, old, new string, d *schema.ResourceData) bool {
_, n := d.GetChange("log_config.0.enable")
if isEmptyValue(reflect.ValueOf(n)) {
return true
}
return false
}

// Whether the backend is a global or regional NEG
func isNegBackend(backend map[string]interface{}) bool {
backendGroup, ok := backend["group"]
Expand Down Expand Up @@ -652,8 +661,9 @@ If logging is enabled, logs will be exported to Stackdriver.`,
AtLeastOneOf: []string{"log_config.0.enable", "log_config.0.sample_rate"},
},
"sample_rate": {
Type: schema.TypeFloat,
Optional: true,
Type: schema.TypeFloat,
Optional: true,
DiffSuppressFunc: suppressWhenDisabled,
Description: `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.
Expand Down Expand Up @@ -3525,7 +3535,7 @@ func expandComputeBackendServiceLogConfig(v interface{}, d TerraformResourceData
transformedEnable, err := expandComputeBackendServiceLogConfigEnable(original["enable"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedEnable); val.IsValid() && !isEmptyValue(val) {
} else {
transformed["enable"] = transformedEnable
}

Expand Down
54 changes: 49 additions & 5 deletions google/resource_compute_backend_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,15 +583,39 @@ func TestAccComputeBackendService_withLogConfig(t *testing.T) {
CheckDestroy: testAccCheckComputeBackendServiceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeBackendService_withLogConfig(serviceName, checkName, 0.7),
Config: testAccComputeBackendService_withLogConfig(serviceName, checkName, 0.7, true),
},
{
ResourceName: "google_compute_backend_service.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeBackendService_withLogConfig(serviceName, checkName, 0.4),
Config: testAccComputeBackendService_withLogConfig(serviceName, checkName, 0.4, true),
},
{
ResourceName: "google_compute_backend_service.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeBackendService_withLogConfig(serviceName, checkName, 0.4, false),
},
{
ResourceName: "google_compute_backend_service.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeBackendService_withLogConfig2(serviceName, checkName, false),
},
{
ResourceName: "google_compute_backend_service.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeBackendService_withLogConfig(serviceName, checkName, 0.7, false),
},
{
ResourceName: "google_compute_backend_service.foobar",
Expand Down Expand Up @@ -1407,14 +1431,14 @@ resource "google_compute_instance_template" "foobar" {
`, fr, proxy, backend, hc, urlmap)
}

func testAccComputeBackendService_withLogConfig(serviceName, checkName string, sampleRate float64) string {
func testAccComputeBackendService_withLogConfig(serviceName, checkName string, sampleRate float64, enabled bool) 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
enable = %t
sample_rate = %v
}
}
Expand All @@ -1425,5 +1449,25 @@ resource "google_compute_http_health_check" "zero" {
check_interval_sec = 1
timeout_sec = 1
}
`, serviceName, sampleRate, checkName)
`, serviceName, enabled, sampleRate, checkName)
}

func testAccComputeBackendService_withLogConfig2(serviceName, checkName string, enabled bool) 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 = %t
}
}

resource "google_compute_http_health_check" "zero" {
name = "%s"
request_path = "/"
check_interval_sec = 1
timeout_sec = 1
}
`, serviceName, enabled, checkName)
}
7 changes: 4 additions & 3 deletions google/resource_compute_region_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,9 @@ If logging is enabled, logs will be exported to Stackdriver.`,
AtLeastOneOf: []string{"log_config.0.enable", "log_config.0.sample_rate"},
},
"sample_rate": {
Type: schema.TypeFloat,
Optional: true,
Type: schema.TypeFloat,
Optional: true,
DiffSuppressFunc: suppressWhenDisabled,
Description: `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.
Expand Down Expand Up @@ -3519,7 +3520,7 @@ func expandComputeRegionBackendServiceLogConfig(v interface{}, d TerraformResour
transformedEnable, err := expandComputeRegionBackendServiceLogConfigEnable(original["enable"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedEnable); val.IsValid() && !isEmptyValue(val) {
} else {
transformed["enable"] = transformedEnable
}

Expand Down