Add the other HealthCheck types

This commit is contained in:
Dana Hoffman 2016-11-29 16:08:44 -08:00
parent adafa722e7
commit 20ee56be07
2 changed files with 400 additions and 33 deletions

View File

@ -59,7 +59,6 @@ func resourceComputeHealthCheck() *schema.Resource {
"port": &schema.Schema{ "port": &schema.Schema{
Type: schema.TypeInt, Type: schema.TypeInt,
Optional: true, Optional: true,
Default: 80,
}, },
"port_name": &schema.Schema{ "port_name": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
@ -81,6 +80,102 @@ func resourceComputeHealthCheck() *schema.Resource {
}, },
}, },
}, },
"ssl_health_check": &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"port": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"port_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"proxy_header": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "NONE",
},
"request": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"response": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
},
"http_health_check": &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"host": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"port": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"port_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"proxy_header": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "NONE",
},
"request_path": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "/",
},
},
},
},
"https_health_check": &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"host": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"port": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"port_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"proxy_header": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "NONE",
},
"request_path": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "/",
},
},
},
},
"project": &schema.Schema{ "project": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
@ -140,7 +235,9 @@ func resourceComputeHealthCheckCreate(d *schema.ResourceData, meta interface{})
hchk.Type = v.(string) hchk.Type = v.(string)
} }
if v, ok := d.GetOk("tcp_health_check"); ok { if v, ok := d.GetOk("tcp_health_check"); ok {
// check that type is tcp? if 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 {
@ -161,6 +258,78 @@ func resourceComputeHealthCheckCreate(d *schema.ResourceData, meta interface{})
hchk.TcpHealthCheck = tcpHealthCheck 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)
}
sslcheck := v.([]interface{})[0].(map[string]interface{})
sslHealthCheck := &compute.SSLHealthCheck{}
if val, ok := sslcheck["port"]; ok {
sslHealthCheck.Port = int64(val.(int))
}
if val, ok := sslcheck["port_name"]; ok {
sslHealthCheck.PortName = val.(string)
}
if val, ok := sslcheck["proxy_header"]; ok {
sslHealthCheck.ProxyHeader = val.(string)
}
if val, ok := sslcheck["request"]; ok {
sslHealthCheck.Request = val.(string)
}
if val, ok := sslcheck["response"]; ok {
sslHealthCheck.Response = val.(string)
}
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)
}
httpcheck := v.([]interface{})[0].(map[string]interface{})
httpHealthCheck := &compute.HTTPHealthCheck{}
if val, ok := httpcheck["host"]; ok {
httpHealthCheck.Host = val.(string)
}
if val, ok := httpcheck["port"]; ok {
httpHealthCheck.Port = int64(val.(int))
}
if val, ok := httpcheck["port_name"]; ok {
httpHealthCheck.PortName = val.(string)
}
if val, ok := httpcheck["proxy_header"]; ok {
httpHealthCheck.ProxyHeader = val.(string)
}
if val, ok := httpcheck["request_path"]; ok {
httpHealthCheck.RequestPath = val.(string)
}
hchk.HttpHealthCheck = httpHealthCheck
}
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)
}
httpscheck := v.([]interface{})[0].(map[string]interface{})
httpsHealthCheck := &compute.HTTPSHealthCheck{}
if val, ok := httpscheck["host"]; ok {
httpsHealthCheck.Host = val.(string)
}
if val, ok := httpscheck["port"]; ok {
httpsHealthCheck.Port = int64(val.(int))
}
if val, ok := httpscheck["port_name"]; ok {
httpsHealthCheck.PortName = val.(string)
}
if val, ok := httpscheck["proxy_header"]; ok {
httpsHealthCheck.ProxyHeader = val.(string)
}
if val, ok := httpscheck["request_path"]; ok {
httpsHealthCheck.RequestPath = val.(string)
}
hchk.HttpsHealthCheck = httpsHealthCheck
}
log.Printf("[DEBUG] HealthCheck insert request: %#v", hchk) log.Printf("[DEBUG] HealthCheck insert request: %#v", hchk)
op, err := config.clientCompute.HealthChecks.Insert( op, err := config.clientCompute.HealthChecks.Insert(
project, hchk).Do() project, hchk).Do()
@ -211,9 +380,11 @@ func resourceComputeHealthCheckUpdate(d *schema.ResourceData, meta interface{})
hchk.Type = v.(string) hchk.Type = v.(string)
} }
if v, ok := d.GetOk("tcp_health_check"); ok { if v, ok := d.GetOk("tcp_health_check"); ok {
// check that type is tcp? if 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{})
var tcpHealthCheck *compute.TCPHealthCheck tcpHealthCheck := &compute.TCPHealthCheck{}
if val, ok := tcpcheck["port"]; ok { if val, ok := tcpcheck["port"]; ok {
tcpHealthCheck.Port = int64(val.(int)) tcpHealthCheck.Port = int64(val.(int))
} }
@ -231,6 +402,76 @@ func resourceComputeHealthCheckUpdate(d *schema.ResourceData, meta interface{})
} }
hchk.TcpHealthCheck = tcpHealthCheck 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)
}
sslcheck := v.([]interface{})[0].(map[string]interface{})
sslHealthCheck := &compute.SSLHealthCheck{}
if val, ok := sslcheck["port"]; ok {
sslHealthCheck.Port = int64(val.(int))
}
if val, ok := sslcheck["port_name"]; ok {
sslHealthCheck.PortName = val.(string)
}
if val, ok := sslcheck["proxy_header"]; ok {
sslHealthCheck.ProxyHeader = val.(string)
}
if val, ok := sslcheck["request"]; ok {
sslHealthCheck.Request = val.(string)
}
if val, ok := sslcheck["response"]; ok {
sslHealthCheck.Response = val.(string)
}
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)
}
httpcheck := v.([]interface{})[0].(map[string]interface{})
httpHealthCheck := &compute.HTTPHealthCheck{}
if val, ok := httpcheck["host"]; ok {
httpHealthCheck.Host = val.(string)
}
if val, ok := httpcheck["port"]; ok {
httpHealthCheck.Port = int64(val.(int))
}
if val, ok := httpcheck["port_name"]; ok {
httpHealthCheck.PortName = val.(string)
}
if val, ok := httpcheck["proxy_header"]; ok {
httpHealthCheck.ProxyHeader = val.(string)
}
if val, ok := httpcheck["request_path"]; ok {
httpHealthCheck.RequestPath = val.(string)
}
hchk.HttpHealthCheck = httpHealthCheck
}
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)
}
httpscheck := v.([]interface{})[0].(map[string]interface{})
httpsHealthCheck := &compute.HTTPSHealthCheck{}
if val, ok := httpscheck["host"]; ok {
httpsHealthCheck.Host = val.(string)
}
if val, ok := httpscheck["port"]; ok {
httpsHealthCheck.Port = int64(val.(int))
}
if val, ok := httpscheck["port_name"]; ok {
httpsHealthCheck.PortName = val.(string)
}
if val, ok := httpscheck["proxy_header"]; ok {
httpsHealthCheck.ProxyHeader = val.(string)
}
if val, ok := httpscheck["request_path"]; ok {
httpsHealthCheck.RequestPath = val.(string)
}
hchk.HttpsHealthCheck = httpsHealthCheck
}
log.Printf("[DEBUG] HealthCheck patch request: %#v", hchk) log.Printf("[DEBUG] HealthCheck patch request: %#v", hchk)
op, err := config.clientCompute.HealthChecks.Patch( op, err := config.clientCompute.HealthChecks.Patch(
@ -278,6 +519,9 @@ func resourceComputeHealthCheckRead(d *schema.ResourceData, meta interface{}) er
d.Set("unhealthy_threshold", hchk.UnhealthyThreshold) d.Set("unhealthy_threshold", hchk.UnhealthyThreshold)
d.Set("type", hchk.Type) 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.TcpHealthCheck)
d.Set("http_health_check", hchk.TcpHealthCheck)
d.Set("https_health_check", hchk.TcpHealthCheck)
d.Set("self_link", hchk.SelfLink) d.Set("self_link", hchk.SelfLink)
d.Set("name", hchk.Name) d.Set("name", hchk.Name)
d.Set("description", hchk.Description) d.Set("description", hchk.Description)

View File

@ -10,7 +10,7 @@ import (
"google.golang.org/api/compute/v1" "google.golang.org/api/compute/v1"
) )
func TestAccComputeHealthCheck_basic(t *testing.T) { func TestAccComputeHealthCheck_tcp(t *testing.T) {
var healthCheck compute.HealthCheck var healthCheck compute.HealthCheck
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
@ -19,7 +19,53 @@ func TestAccComputeHealthCheck_basic(t *testing.T) {
CheckDestroy: testAccCheckComputeHealthCheckDestroy, CheckDestroy: testAccCheckComputeHealthCheckDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccComputeHealthCheck_basic, Config: testAccComputeHealthCheck_tcp,
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeHealthCheckExists(
"google_compute_health_check.foobar", &healthCheck),
testAccCheckComputeHealthCheckThresholds(
3, 3, &healthCheck),
testAccCheckComputeHealthCheckTcpPort(80, &healthCheck),
),
},
},
})
}
func TestAccComputeHealthCheck_tcp_withPortName(t *testing.T) {
var healthCheck compute.HealthCheck
portName := "dummy-port"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeHealthCheckDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeHealthCheck_tcp_withPortName(portName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeHealthCheckExists(
"google_compute_health_check.foobar", &healthCheck),
testAccCheckComputeHealthCheckTcpPortName(portName, &healthCheck),
// 80 is the default port, so even though we did not set one,
// it should still have a value of 80.
testAccCheckComputeHealthCheckTcpPort(80, &healthCheck),
),
},
},
})
}
func TestAccComputeHealthCheck_ssl(t *testing.T) {
var healthCheck compute.HealthCheck
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeHealthCheckDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeHealthCheck_ssl,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckComputeHealthCheckExists( testAccCheckComputeHealthCheckExists(
"google_compute_health_check.foobar", &healthCheck), "google_compute_health_check.foobar", &healthCheck),
@ -31,7 +77,7 @@ func TestAccComputeHealthCheck_basic(t *testing.T) {
}) })
} }
func TestAccComputeHealthCheck_update(t *testing.T) { func TestAccComputeHealthCheck_http(t *testing.T) {
var healthCheck compute.HealthCheck var healthCheck compute.HealthCheck
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
@ -40,27 +86,41 @@ func TestAccComputeHealthCheck_update(t *testing.T) {
CheckDestroy: testAccCheckComputeHealthCheckDestroy, CheckDestroy: testAccCheckComputeHealthCheckDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccComputeHealthCheck_update1, Config: testAccComputeHealthCheck_http,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckComputeHealthCheckExists( testAccCheckComputeHealthCheckExists(
"google_compute_health_check.foobar", &healthCheck), "google_compute_health_check.foobar", &healthCheck),
testAccCheckComputeHealthCheckThresholds( testAccCheckComputeHealthCheckThresholds(
2, 2, &healthCheck), 3, 3, &healthCheck),
),
},
resource.TestStep{
Config: testAccComputeHealthCheck_update2,
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeHealthCheckExists(
"google_compute_health_check.foobar", &healthCheck),
testAccCheckComputeHealthCheckThresholds(
10, 10, &healthCheck),
), ),
}, },
}, },
}) })
} }
func TestAccComputeHealthCheck_https(t *testing.T) {
var healthCheck compute.HealthCheck
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeHealthCheckDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeHealthCheck_https,
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeHealthCheckExists(
"google_compute_health_check.foobar", &healthCheck),
testAccCheckComputeHealthCheckThresholds(
3, 3, &healthCheck),
),
},
},
})
}
// add in update test?
func testAccCheckComputeHealthCheckDestroy(s *terraform.State) error { func testAccCheckComputeHealthCheckDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config) config := testAccProvider.Meta().(*Config)
@ -122,7 +182,29 @@ func testAccCheckComputeHealthCheckThresholds(healthy, unhealthy int64, healthCh
} }
} }
var testAccComputeHealthCheck_basic = fmt.Sprintf(` func testAccCheckComputeHealthCheckTcpPort(port int64, healthCheck *compute.HealthCheck) resource.TestCheckFunc {
return func(s *terraform.State) error {
if healthCheck.TcpHealthCheck.Port != port {
return fmt.Errorf("Port doesn't match: expected %v, got %v", port, healthCheck.TcpHealthCheck.Port)
}
return nil
}
}
func testAccCheckComputeHealthCheckTcpPortName(portName string, healthCheck *compute.HealthCheck) resource.TestCheckFunc {
return func(s *terraform.State) error {
if healthCheck.TcpHealthCheck.PortName != portName {
return fmt.Errorf("PortName doesn't match: expected %s, got %s", portName, healthCheck.TcpHealthCheck.PortName)
}
if healthCheck.TcpHealthCheck.Port != 0 {
return fmt.Errorf("Port doesn't match: expected nil, got %v", healthCheck.TcpHealthCheck.Port)
}
return nil
}
}
var testAccComputeHealthCheck_tcp = fmt.Sprintf(`
resource "google_compute_health_check" "foobar" { resource "google_compute_health_check" "foobar" {
check_interval_sec = 3 check_interval_sec = 3
description = "Resource created for Terraform acceptance testing" description = "Resource created for Terraform acceptance testing"
@ -131,26 +213,67 @@ resource "google_compute_health_check" "foobar" {
timeout_sec = 2 timeout_sec = 2
unhealthy_threshold = 3 unhealthy_threshold = 3
tcp_health_check { tcp_health_check {
}
}
`, acctest.RandString(10))
func testAccComputeHealthCheck_tcp_withPortName(portName string) string {
return 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 {
port_name = "%s"
}
}
`, acctest.RandString(10), portName)
}
var testAccComputeHealthCheck_ssl = 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
type = "SSL"
ssl_health_check {
port = "443"
}
}
`, acctest.RandString(10))
var testAccComputeHealthCheck_http = 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
type = "HTTP"
http_health_check {
port = "80" port = "80"
} }
} }
`, acctest.RandString(10)) `, acctest.RandString(10))
var testAccComputeHealthCheck_update1 = fmt.Sprintf(` var testAccComputeHealthCheck_https = fmt.Sprintf(`
resource "google_compute_health_check" "foobar" { resource "google_compute_health_check" "foobar" {
name = "Health-test-%s" check_interval_sec = 3
description = "Resource created for Terraform acceptance testing" description = "Resource created for Terraform acceptance testing"
request_path = "/not_default" healthy_threshold = 3
} name = "health-test-%s"
`, acctest.RandString(10)) timeout_sec = 2
unhealthy_threshold = 3
/* Change description, restore request_path to default, and change type = "HTTPS"
* thresholds from defaults */ https_health_check {
var testAccComputeHealthCheck_update2 = fmt.Sprintf(` port = "443"
resource "google_compute_health_check" "foobar" { }
name = "Health-test-%s"
description = "Resource updated for Terraform acceptance testing"
healthy_threshold = 10
unhealthy_threshold = 10
} }
`, acctest.RandString(10)) `, acctest.RandString(10))