From 579c37cd740b7b1027bf8a69f9a0417faca499dd Mon Sep 17 00:00:00 2001 From: Dan Carley Date: Thu, 7 May 2015 10:21:21 +0100 Subject: [PATCH] provider/gce: Set defaults for http_health_check In order to fix the failing test in the preceding commit when optional params are changed from their default "computed" values. These weren't working well with `HttpHealthCheck.Patch()` because it was attempting to set all unspecified params to Go's type defaults (eg. 0 for int64) which the API rejected. Changing the call to `HttpHealthCheck.Update()` seemed to fix this but it still didn't allow you to reset a param back to it's default by no longer specifying it. Settings defaults like this, which match the Terraform docs, seems like the best all round solution. Includes two additional tests for the acceptance tests which verify the params are really getting set correctly. --- .../resource_compute_http_health_check.go | 12 ++--- ...resource_compute_http_health_check_test.go | 45 ++++++++++++++++++- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/builtin/providers/google/resource_compute_http_health_check.go b/builtin/providers/google/resource_compute_http_health_check.go index 752302326..4dfe3a03d 100644 --- a/builtin/providers/google/resource_compute_http_health_check.go +++ b/builtin/providers/google/resource_compute_http_health_check.go @@ -21,7 +21,7 @@ func resourceComputeHttpHealthCheck() *schema.Resource { "check_interval_sec": &schema.Schema{ Type: schema.TypeInt, Optional: true, - Computed: true, + Default: 5, }, "description": &schema.Schema{ @@ -32,7 +32,7 @@ func resourceComputeHttpHealthCheck() *schema.Resource { "healthy_threshold": &schema.Schema{ Type: schema.TypeInt, Optional: true, - Computed: true, + Default: 2, }, "host": &schema.Schema{ @@ -49,13 +49,13 @@ func resourceComputeHttpHealthCheck() *schema.Resource { "port": &schema.Schema{ Type: schema.TypeInt, Optional: true, - Computed: true, + Default: 80, }, "request_path": &schema.Schema{ Type: schema.TypeString, Optional: true, - Computed: true, + Default: "/", }, "self_link": &schema.Schema{ @@ -66,13 +66,13 @@ func resourceComputeHttpHealthCheck() *schema.Resource { "timeout_sec": &schema.Schema{ Type: schema.TypeInt, Optional: true, - Computed: true, + Default: 5, }, "unhealthy_threshold": &schema.Schema{ Type: schema.TypeInt, Optional: true, - Computed: true, + Default: 2, }, }, } diff --git a/builtin/providers/google/resource_compute_http_health_check_test.go b/builtin/providers/google/resource_compute_http_health_check_test.go index bc987af19..d071b5ae3 100644 --- a/builtin/providers/google/resource_compute_http_health_check_test.go +++ b/builtin/providers/google/resource_compute_http_health_check_test.go @@ -6,9 +6,12 @@ import ( "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" + "google.golang.org/api/compute/v1" ) func TestAccComputeHttpHealthCheck_basic(t *testing.T) { + var healthCheck compute.HttpHealthCheck + resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -18,7 +21,11 @@ func TestAccComputeHttpHealthCheck_basic(t *testing.T) { Config: testAccComputeHttpHealthCheck_basic, Check: resource.ComposeTestCheckFunc( testAccCheckComputeHttpHealthCheckExists( - "google_compute_http_health_check.foobar"), + "google_compute_http_health_check.foobar", &healthCheck), + testAccCheckComputeHttpHealthCheckRequestPath( + "/health_check", &healthCheck), + testAccCheckComputeHttpHealthCheckThresholds( + 3, 3, &healthCheck), ), }, }, @@ -38,6 +45,10 @@ func TestAccComputeHttpHealthCheck_update(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckComputeHttpHealthCheckExists( "google_compute_http_health_check.foobar", &healthCheck), + testAccCheckComputeHttpHealthCheckRequestPath( + "/not_default", &healthCheck), + testAccCheckComputeHttpHealthCheckThresholds( + 2, 2, &healthCheck), ), }, resource.TestStep{ @@ -45,6 +56,10 @@ func TestAccComputeHttpHealthCheck_update(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckComputeHttpHealthCheckExists( "google_compute_http_health_check.foobar", &healthCheck), + testAccCheckComputeHttpHealthCheckRequestPath( + "/", &healthCheck), + testAccCheckComputeHttpHealthCheckThresholds( + 10, 10, &healthCheck), ), }, }, @@ -69,7 +84,7 @@ func testAccCheckComputeHttpHealthCheckDestroy(s *terraform.State) error { return nil } -func testAccCheckComputeHttpHealthCheckExists(n string) resource.TestCheckFunc { +func testAccCheckComputeHttpHealthCheckExists(n string, healthCheck *compute.HttpHealthCheck) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -92,6 +107,32 @@ func testAccCheckComputeHttpHealthCheckExists(n string) resource.TestCheckFunc { return fmt.Errorf("HttpHealthCheck not found") } + *healthCheck = *found + + return nil + } +} + +func testAccCheckComputeHttpHealthCheckRequestPath(path string, healthCheck *compute.HttpHealthCheck) resource.TestCheckFunc { + return func(s *terraform.State) error { + if healthCheck.RequestPath != path { + return fmt.Errorf("RequestPath doesn't match: expected %d, got %d", path, healthCheck.RequestPath) + } + + return nil + } +} + +func testAccCheckComputeHttpHealthCheckThresholds(healthy, unhealthy int64, healthCheck *compute.HttpHealthCheck) resource.TestCheckFunc { + return func(s *terraform.State) error { + if healthCheck.HealthyThreshold != healthy { + return fmt.Errorf("HealthyThreshold doesn't match: expected %d, got %d", healthy, healthCheck.HealthyThreshold) + } + + if healthCheck.UnhealthyThreshold != unhealthy { + return fmt.Errorf("UnhealthyThreshold doesn't match: expected %d, got %d", unhealthy, healthCheck.UnhealthyThreshold) + } + return nil } }