From 985b4e2b6689c2f4809f89cf0972a3089861eae7 Mon Sep 17 00:00:00 2001 From: Dana Hoffman Date: Thu, 8 Dec 2016 13:35:57 -0800 Subject: [PATCH] use conflictswith for health check instead of separate type field --- .../google/resource_compute_health_check.go | 75 +++++++------------ .../resource_compute_health_check_test.go | 44 ++++++++++- 2 files changed, 66 insertions(+), 53 deletions(-) diff --git a/builtin/providers/google/resource_compute_health_check.go b/builtin/providers/google/resource_compute_health_check.go index 3665d0194..de8d7d428 100644 --- a/builtin/providers/google/resource_compute_health_check.go +++ b/builtin/providers/google/resource_compute_health_check.go @@ -43,17 +43,11 @@ func resourceComputeHealthCheck() *schema.Resource { Default: 2, }, - "type": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - Default: "TCP", - ForceNew: true, - }, - "tcp_health_check": &schema.Schema{ - Type: schema.TypeList, - Optional: true, - MaxItems: 1, + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + ConflictsWith: []string{"ssl_health_check", "http_health_check", "https_health_check"}, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "port": &schema.Schema{ @@ -79,9 +73,10 @@ func resourceComputeHealthCheck() *schema.Resource { }, "ssl_health_check": &schema.Schema{ - Type: schema.TypeList, - Optional: true, - MaxItems: 1, + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + ConflictsWith: []string{"tcp_health_check", "http_health_check", "https_health_check"}, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "port": &schema.Schema{ @@ -107,9 +102,10 @@ func resourceComputeHealthCheck() *schema.Resource { }, "http_health_check": &schema.Schema{ - Type: schema.TypeList, - Optional: true, - MaxItems: 1, + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + ConflictsWith: []string{"tcp_health_check", "ssl_health_check", "https_health_check"}, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "host": &schema.Schema{ @@ -136,9 +132,10 @@ func resourceComputeHealthCheck() *schema.Resource { }, "https_health_check": &schema.Schema{ - Type: schema.TypeList, - Optional: true, - MaxItems: 1, + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + ConflictsWith: []string{"tcp_health_check", "ssl_health_check", "http_health_check"}, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "host": &schema.Schema{ @@ -219,13 +216,9 @@ func resourceComputeHealthCheckCreate(d *schema.ResourceData, meta interface{}) if v, ok := d.GetOk("unhealthy_threshold"); ok { hchk.UnhealthyThreshold = int64(v.(int)) } - if v, ok := d.GetOk("type"); ok { - hchk.Type = v.(string) - } + if v, ok := d.GetOk("tcp_health_check"); ok { - if hchk.Type != "TCP" { - return fmt.Errorf("TCP health check declared but type is listed as %s", hchk.Type) - } + hchk.Type = "TCP" tcpcheck := v.([]interface{})[0].(map[string]interface{}) tcpHealthCheck := &compute.TCPHealthCheck{} if val, ok := tcpcheck["port"]; ok { @@ -244,9 +237,7 @@ func resourceComputeHealthCheckCreate(d *schema.ResourceData, meta interface{}) } if v, ok := d.GetOk("ssl_health_check"); ok { - if hchk.Type != "SSL" { - return fmt.Errorf("SSL health check declared but type is listed as %s", hchk.Type) - } + hchk.Type = "SSL" sslcheck := v.([]interface{})[0].(map[string]interface{}) sslHealthCheck := &compute.SSLHealthCheck{} if val, ok := sslcheck["port"]; ok { @@ -265,9 +256,7 @@ func resourceComputeHealthCheckCreate(d *schema.ResourceData, meta interface{}) } if v, ok := d.GetOk("http_health_check"); ok { - if hchk.Type != "HTTP" { - return fmt.Errorf("HTTP health check declared but type is listed as %s", hchk.Type) - } + hchk.Type = "HTTP" httpcheck := v.([]interface{})[0].(map[string]interface{}) httpHealthCheck := &compute.HTTPHealthCheck{} if val, ok := httpcheck["host"]; ok { @@ -286,9 +275,7 @@ func resourceComputeHealthCheckCreate(d *schema.ResourceData, meta interface{}) } if v, ok := d.GetOk("https_health_check"); ok { - if hchk.Type != "HTTPS" { - return fmt.Errorf("HTTPS health check declared but type is listed as %s", hchk.Type) - } + hchk.Type = "HTTPS" httpscheck := v.([]interface{})[0].(map[string]interface{}) httpsHealthCheck := &compute.HTTPSHealthCheck{} if val, ok := httpscheck["host"]; ok { @@ -352,13 +339,8 @@ func resourceComputeHealthCheckUpdate(d *schema.ResourceData, meta interface{}) if v, ok := d.GetOk("unhealthy_threshold"); ok { hchk.UnhealthyThreshold = int64(v.(int)) } - if v, ok := d.GetOk("type"); ok { - hchk.Type = v.(string) - } if v, ok := d.GetOk("tcp_health_check"); ok { - if hchk.Type != "TCP" { - return fmt.Errorf("TCP health check declared but type is listed as %s", hchk.Type) - } + hchk.Type = "TCP" tcpcheck := v.([]interface{})[0].(map[string]interface{}) tcpHealthCheck := &compute.TCPHealthCheck{} if val, ok := tcpcheck["port"]; ok { @@ -376,9 +358,7 @@ func resourceComputeHealthCheckUpdate(d *schema.ResourceData, meta interface{}) hchk.TcpHealthCheck = tcpHealthCheck } if v, ok := d.GetOk("ssl_health_check"); ok { - if hchk.Type != "SSL" { - return fmt.Errorf("SSL health check declared but type is listed as %s", hchk.Type) - } + hchk.Type = "SSL" sslcheck := v.([]interface{})[0].(map[string]interface{}) sslHealthCheck := &compute.SSLHealthCheck{} if val, ok := sslcheck["port"]; ok { @@ -396,9 +376,7 @@ func resourceComputeHealthCheckUpdate(d *schema.ResourceData, meta interface{}) hchk.SslHealthCheck = sslHealthCheck } if v, ok := d.GetOk("http_health_check"); ok { - if hchk.Type != "HTTP" { - return fmt.Errorf("HTTP health check declared but type is listed as %s", hchk.Type) - } + hchk.Type = "HTTP" httpcheck := v.([]interface{})[0].(map[string]interface{}) httpHealthCheck := &compute.HTTPHealthCheck{} if val, ok := httpcheck["host"]; ok { @@ -417,9 +395,7 @@ func resourceComputeHealthCheckUpdate(d *schema.ResourceData, meta interface{}) } if v, ok := d.GetOk("https_health_check"); ok { - if hchk.Type != "HTTPS" { - return fmt.Errorf("HTTPS health check declared but type is listed as %s", hchk.Type) - } + hchk.Type = "HTTPS" httpscheck := v.([]interface{})[0].(map[string]interface{}) httpsHealthCheck := &compute.HTTPSHealthCheck{} if val, ok := httpscheck["host"]; ok { @@ -481,7 +457,6 @@ func resourceComputeHealthCheckRead(d *schema.ResourceData, meta interface{}) er d.Set("healthy_threshold", hchk.HealthyThreshold) d.Set("timeout_sec", hchk.TimeoutSec) d.Set("unhealthy_threshold", hchk.UnhealthyThreshold) - d.Set("type", hchk.Type) d.Set("tcp_health_check", hchk.TcpHealthCheck) d.Set("ssl_health_check", hchk.SslHealthCheck) d.Set("http_health_check", hchk.HttpHealthCheck) diff --git a/builtin/providers/google/resource_compute_health_check_test.go b/builtin/providers/google/resource_compute_health_check_test.go index 1cb4e6ceb..d97c6c3fc 100644 --- a/builtin/providers/google/resource_compute_health_check_test.go +++ b/builtin/providers/google/resource_compute_health_check_test.go @@ -2,6 +2,7 @@ package google import ( "fmt" + "regexp" "testing" "github.com/hashicorp/terraform/helper/acctest" @@ -127,6 +128,20 @@ func TestAccComputeHealthCheck_https(t *testing.T) { }) } +func TestAccComputeHealthCheck_tcpAndSsl_shouldFail(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeHealthCheckDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccComputeHealthCheck_tcpAndSsl_shouldFail, + ExpectError: regexp.MustCompile("conflicts with tcp_health_check"), + }, + }, + }) +} + func testAccCheckComputeHealthCheckDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) @@ -174,6 +189,16 @@ func testAccCheckComputeHealthCheckExists(n string, healthCheck *compute.HealthC } } +func testAccCheckErrorCreating(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + _, ok := s.RootModule().Resources[n] + if ok { + return fmt.Errorf("HealthCheck %s created successfully with bad config", n) + } + return nil + } +} + func testAccCheckComputeHealthCheckThresholds(healthy, unhealthy int64, healthCheck *compute.HealthCheck) resource.TestCheckFunc { return func(s *terraform.State) error { if healthCheck.HealthyThreshold != healthy { @@ -232,7 +257,6 @@ resource "google_compute_health_check" "foobar" { name = "health-test-%s" timeout_sec = 2 unhealthy_threshold = 3 - type = "SSL" ssl_health_check { port = "443" } @@ -247,7 +271,6 @@ resource "google_compute_health_check" "foobar" { name = "health-test-%s" timeout_sec = 2 unhealthy_threshold = 3 - type = "HTTP" http_health_check { port = "80" } @@ -262,9 +285,24 @@ resource "google_compute_health_check" "foobar" { name = "health-test-%s" timeout_sec = 2 unhealthy_threshold = 3 - type = "HTTPS" https_health_check { port = "443" } } `, acctest.RandString(10)) + +var testAccComputeHealthCheck_tcpAndSsl_shouldFail = fmt.Sprintf(` +resource "google_compute_health_check" "foobar" { + check_interval_sec = 3 + description = "Resource created for Terraform acceptance testing" + healthy_threshold = 3 + name = "health-test-%s" + timeout_sec = 2 + unhealthy_threshold = 3 + + tcp_health_check { + } + ssl_health_check { + } +} +`, acctest.RandString(10))