use conflictswith for health check instead of separate type field

This commit is contained in:
Dana Hoffman 2016-12-08 13:35:57 -08:00
parent 2960856105
commit 985b4e2b66
2 changed files with 66 additions and 53 deletions

View File

@ -43,17 +43,11 @@ func resourceComputeHealthCheck() *schema.Resource {
Default: 2, Default: 2,
}, },
"type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "TCP",
ForceNew: true,
},
"tcp_health_check": &schema.Schema{ "tcp_health_check": &schema.Schema{
Type: schema.TypeList, Type: schema.TypeList,
Optional: true, Optional: true,
MaxItems: 1, MaxItems: 1,
ConflictsWith: []string{"ssl_health_check", "http_health_check", "https_health_check"},
Elem: &schema.Resource{ Elem: &schema.Resource{
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"port": &schema.Schema{ "port": &schema.Schema{
@ -79,9 +73,10 @@ func resourceComputeHealthCheck() *schema.Resource {
}, },
"ssl_health_check": &schema.Schema{ "ssl_health_check": &schema.Schema{
Type: schema.TypeList, Type: schema.TypeList,
Optional: true, Optional: true,
MaxItems: 1, MaxItems: 1,
ConflictsWith: []string{"tcp_health_check", "http_health_check", "https_health_check"},
Elem: &schema.Resource{ Elem: &schema.Resource{
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"port": &schema.Schema{ "port": &schema.Schema{
@ -107,9 +102,10 @@ func resourceComputeHealthCheck() *schema.Resource {
}, },
"http_health_check": &schema.Schema{ "http_health_check": &schema.Schema{
Type: schema.TypeList, Type: schema.TypeList,
Optional: true, Optional: true,
MaxItems: 1, MaxItems: 1,
ConflictsWith: []string{"tcp_health_check", "ssl_health_check", "https_health_check"},
Elem: &schema.Resource{ Elem: &schema.Resource{
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"host": &schema.Schema{ "host": &schema.Schema{
@ -136,9 +132,10 @@ func resourceComputeHealthCheck() *schema.Resource {
}, },
"https_health_check": &schema.Schema{ "https_health_check": &schema.Schema{
Type: schema.TypeList, Type: schema.TypeList,
Optional: true, Optional: true,
MaxItems: 1, MaxItems: 1,
ConflictsWith: []string{"tcp_health_check", "ssl_health_check", "http_health_check"},
Elem: &schema.Resource{ Elem: &schema.Resource{
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"host": &schema.Schema{ "host": &schema.Schema{
@ -219,13 +216,9 @@ func resourceComputeHealthCheckCreate(d *schema.ResourceData, meta interface{})
if v, ok := d.GetOk("unhealthy_threshold"); ok { if v, ok := d.GetOk("unhealthy_threshold"); ok {
hchk.UnhealthyThreshold = int64(v.(int)) 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 v, ok := d.GetOk("tcp_health_check"); ok {
if hchk.Type != "TCP" { hchk.Type = "TCP"
return fmt.Errorf("TCP health check declared but type is listed as %s", hchk.Type)
}
tcpcheck := v.([]interface{})[0].(map[string]interface{}) tcpcheck := v.([]interface{})[0].(map[string]interface{})
tcpHealthCheck := &compute.TCPHealthCheck{} tcpHealthCheck := &compute.TCPHealthCheck{}
if val, ok := tcpcheck["port"]; ok { 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 v, ok := d.GetOk("ssl_health_check"); ok {
if hchk.Type != "SSL" { hchk.Type = "SSL"
return fmt.Errorf("SSL health check declared but type is listed as %s", hchk.Type)
}
sslcheck := v.([]interface{})[0].(map[string]interface{}) sslcheck := v.([]interface{})[0].(map[string]interface{})
sslHealthCheck := &compute.SSLHealthCheck{} sslHealthCheck := &compute.SSLHealthCheck{}
if val, ok := sslcheck["port"]; ok { 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 v, ok := d.GetOk("http_health_check"); ok {
if hchk.Type != "HTTP" { hchk.Type = "HTTP"
return fmt.Errorf("HTTP health check declared but type is listed as %s", hchk.Type)
}
httpcheck := v.([]interface{})[0].(map[string]interface{}) httpcheck := v.([]interface{})[0].(map[string]interface{})
httpHealthCheck := &compute.HTTPHealthCheck{} httpHealthCheck := &compute.HTTPHealthCheck{}
if val, ok := httpcheck["host"]; ok { 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 v, ok := d.GetOk("https_health_check"); ok {
if hchk.Type != "HTTPS" { hchk.Type = "HTTPS"
return fmt.Errorf("HTTPS health check declared but type is listed as %s", hchk.Type)
}
httpscheck := v.([]interface{})[0].(map[string]interface{}) httpscheck := v.([]interface{})[0].(map[string]interface{})
httpsHealthCheck := &compute.HTTPSHealthCheck{} httpsHealthCheck := &compute.HTTPSHealthCheck{}
if val, ok := httpscheck["host"]; ok { 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 { if v, ok := d.GetOk("unhealthy_threshold"); ok {
hchk.UnhealthyThreshold = int64(v.(int)) 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 v, ok := d.GetOk("tcp_health_check"); ok {
if hchk.Type != "TCP" { hchk.Type = "TCP"
return fmt.Errorf("TCP health check declared but type is listed as %s", hchk.Type)
}
tcpcheck := v.([]interface{})[0].(map[string]interface{}) tcpcheck := v.([]interface{})[0].(map[string]interface{})
tcpHealthCheck := &compute.TCPHealthCheck{} tcpHealthCheck := &compute.TCPHealthCheck{}
if val, ok := tcpcheck["port"]; ok { if val, ok := tcpcheck["port"]; ok {
@ -376,9 +358,7 @@ func resourceComputeHealthCheckUpdate(d *schema.ResourceData, meta interface{})
hchk.TcpHealthCheck = tcpHealthCheck hchk.TcpHealthCheck = tcpHealthCheck
} }
if v, ok := d.GetOk("ssl_health_check"); ok { if v, ok := d.GetOk("ssl_health_check"); ok {
if hchk.Type != "SSL" { hchk.Type = "SSL"
return fmt.Errorf("SSL health check declared but type is listed as %s", hchk.Type)
}
sslcheck := v.([]interface{})[0].(map[string]interface{}) sslcheck := v.([]interface{})[0].(map[string]interface{})
sslHealthCheck := &compute.SSLHealthCheck{} sslHealthCheck := &compute.SSLHealthCheck{}
if val, ok := sslcheck["port"]; ok { if val, ok := sslcheck["port"]; ok {
@ -396,9 +376,7 @@ func resourceComputeHealthCheckUpdate(d *schema.ResourceData, meta interface{})
hchk.SslHealthCheck = sslHealthCheck hchk.SslHealthCheck = sslHealthCheck
} }
if v, ok := d.GetOk("http_health_check"); ok { if v, ok := d.GetOk("http_health_check"); ok {
if hchk.Type != "HTTP" { hchk.Type = "HTTP"
return fmt.Errorf("HTTP health check declared but type is listed as %s", hchk.Type)
}
httpcheck := v.([]interface{})[0].(map[string]interface{}) httpcheck := v.([]interface{})[0].(map[string]interface{})
httpHealthCheck := &compute.HTTPHealthCheck{} httpHealthCheck := &compute.HTTPHealthCheck{}
if val, ok := httpcheck["host"]; ok { 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 v, ok := d.GetOk("https_health_check"); ok {
if hchk.Type != "HTTPS" { hchk.Type = "HTTPS"
return fmt.Errorf("HTTPS health check declared but type is listed as %s", hchk.Type)
}
httpscheck := v.([]interface{})[0].(map[string]interface{}) httpscheck := v.([]interface{})[0].(map[string]interface{})
httpsHealthCheck := &compute.HTTPSHealthCheck{} httpsHealthCheck := &compute.HTTPSHealthCheck{}
if val, ok := httpscheck["host"]; ok { 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("healthy_threshold", hchk.HealthyThreshold)
d.Set("timeout_sec", hchk.TimeoutSec) d.Set("timeout_sec", hchk.TimeoutSec)
d.Set("unhealthy_threshold", hchk.UnhealthyThreshold) d.Set("unhealthy_threshold", hchk.UnhealthyThreshold)
d.Set("type", hchk.Type)
d.Set("tcp_health_check", hchk.TcpHealthCheck) d.Set("tcp_health_check", hchk.TcpHealthCheck)
d.Set("ssl_health_check", hchk.SslHealthCheck) d.Set("ssl_health_check", hchk.SslHealthCheck)
d.Set("http_health_check", hchk.HttpHealthCheck) d.Set("http_health_check", hchk.HttpHealthCheck)

View File

@ -2,6 +2,7 @@ package google
import ( import (
"fmt" "fmt"
"regexp"
"testing" "testing"
"github.com/hashicorp/terraform/helper/acctest" "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 { func testAccCheckComputeHealthCheckDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config) 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 { func testAccCheckComputeHealthCheckThresholds(healthy, unhealthy int64, healthCheck *compute.HealthCheck) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
if healthCheck.HealthyThreshold != healthy { if healthCheck.HealthyThreshold != healthy {
@ -232,7 +257,6 @@ resource "google_compute_health_check" "foobar" {
name = "health-test-%s" name = "health-test-%s"
timeout_sec = 2 timeout_sec = 2
unhealthy_threshold = 3 unhealthy_threshold = 3
type = "SSL"
ssl_health_check { ssl_health_check {
port = "443" port = "443"
} }
@ -247,7 +271,6 @@ resource "google_compute_health_check" "foobar" {
name = "health-test-%s" name = "health-test-%s"
timeout_sec = 2 timeout_sec = 2
unhealthy_threshold = 3 unhealthy_threshold = 3
type = "HTTP"
http_health_check { http_health_check {
port = "80" port = "80"
} }
@ -262,9 +285,24 @@ resource "google_compute_health_check" "foobar" {
name = "health-test-%s" name = "health-test-%s"
timeout_sec = 2 timeout_sec = 2
unhealthy_threshold = 3 unhealthy_threshold = 3
type = "HTTPS"
https_health_check { https_health_check {
port = "443" port = "443"
} }
} }
`, acctest.RandString(10)) `, 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))