2014-11-20 18:40:17 +01:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-03-18 18:10:39 +01:00
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
"google.golang.org/api/googleapi"
|
2014-11-20 18:40:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceComputeHttpHealthCheck() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceComputeHttpHealthCheckCreate,
|
|
|
|
Read: resourceComputeHttpHealthCheckRead,
|
|
|
|
Delete: resourceComputeHttpHealthCheckDelete,
|
|
|
|
Update: resourceComputeHttpHealthCheckUpdate,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-04-10 23:34:15 +02:00
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2014-11-20 18:40:17 +01:00
|
|
|
"check_interval_sec": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2015-05-07 11:21:21 +02:00
|
|
|
Default: 5,
|
2014-11-20 18:40:17 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"healthy_threshold": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2015-05-07 11:21:21 +02:00
|
|
|
Default: 2,
|
2014-11-20 18:40:17 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"host": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"port": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2015-05-07 11:21:21 +02:00
|
|
|
Default: 80,
|
2014-11-20 18:40:17 +01:00
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"project": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2014-11-20 18:40:17 +01:00
|
|
|
"request_path": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-05-07 11:21:21 +02:00
|
|
|
Default: "/",
|
2014-11-20 18:40:17 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"self_link": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"timeout_sec": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2015-05-07 11:21:21 +02:00
|
|
|
Default: 5,
|
2014-11-20 18:40:17 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"unhealthy_threshold": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2015-05-07 11:21:21 +02:00
|
|
|
Default: 2,
|
2014-11-20 18:40:17 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeHttpHealthCheckCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-20 18:40:17 +01:00
|
|
|
// Build the parameter
|
|
|
|
hchk := &compute.HttpHealthCheck{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
}
|
2015-02-08 01:03:18 +01:00
|
|
|
// Optional things
|
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
|
|
hchk.Description = v.(string)
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
2015-02-08 01:03:18 +01:00
|
|
|
if v, ok := d.GetOk("host"); ok {
|
|
|
|
hchk.Host = v.(string)
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
2015-02-08 01:03:18 +01:00
|
|
|
if v, ok := d.GetOk("request_path"); ok {
|
|
|
|
hchk.RequestPath = v.(string)
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
2015-02-08 01:03:18 +01:00
|
|
|
if v, ok := d.GetOk("check_interval_sec"); ok {
|
|
|
|
hchk.CheckIntervalSec = int64(v.(int))
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
2015-04-27 10:42:50 +02:00
|
|
|
if v, ok := d.GetOk("healthy_threshold"); ok {
|
2015-02-08 01:03:18 +01:00
|
|
|
hchk.HealthyThreshold = int64(v.(int))
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("port"); ok {
|
|
|
|
hchk.Port = int64(v.(int))
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("timeout_sec"); ok {
|
|
|
|
hchk.TimeoutSec = int64(v.(int))
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("unhealthy_threshold"); ok {
|
|
|
|
hchk.UnhealthyThreshold = int64(v.(int))
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] HttpHealthCheck insert request: %#v", hchk)
|
|
|
|
op, err := config.clientCompute.HttpHealthChecks.Insert(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, hchk).Do()
|
2014-11-20 18:40:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating HttpHealthCheck: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It probably maybe worked, so store the ID now
|
|
|
|
d.SetId(hchk.Name)
|
|
|
|
|
2015-09-24 22:30:12 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, "Creating Http Health Check")
|
2014-11-20 18:40:17 +01:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return resourceComputeHttpHealthCheckRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeHttpHealthCheckUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-20 18:40:17 +01:00
|
|
|
// Build the parameter
|
|
|
|
hchk := &compute.HttpHealthCheck{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
}
|
2015-02-08 01:03:18 +01:00
|
|
|
// Optional things
|
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
|
|
hchk.Description = v.(string)
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("host"); ok {
|
|
|
|
hchk.Host = v.(string)
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("request_path"); ok {
|
|
|
|
hchk.RequestPath = v.(string)
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("check_interval_sec"); ok {
|
|
|
|
hchk.CheckIntervalSec = int64(v.(int))
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
2015-04-27 10:42:50 +02:00
|
|
|
if v, ok := d.GetOk("healthy_threshold"); ok {
|
2015-02-08 01:03:18 +01:00
|
|
|
hchk.HealthyThreshold = int64(v.(int))
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
2015-02-08 01:03:18 +01:00
|
|
|
if v, ok := d.GetOk("port"); ok {
|
|
|
|
hchk.Port = int64(v.(int))
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
2015-02-08 01:03:18 +01:00
|
|
|
if v, ok := d.GetOk("timeout_sec"); ok {
|
|
|
|
hchk.TimeoutSec = int64(v.(int))
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
2015-02-08 01:03:18 +01:00
|
|
|
if v, ok := d.GetOk("unhealthy_threshold"); ok {
|
|
|
|
hchk.UnhealthyThreshold = int64(v.(int))
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] HttpHealthCheck patch request: %#v", hchk)
|
|
|
|
op, err := config.clientCompute.HttpHealthChecks.Patch(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, hchk.Name, hchk).Do()
|
2014-11-20 18:40:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error patching HttpHealthCheck: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It probably maybe worked, so store the ID now
|
|
|
|
d.SetId(hchk.Name)
|
|
|
|
|
2015-09-24 22:30:12 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, "Updating Http Health Check")
|
2014-11-20 18:40:17 +01:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return resourceComputeHttpHealthCheckRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeHttpHealthCheckRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-20 18:40:17 +01:00
|
|
|
hchk, err := config.clientCompute.HttpHealthChecks.Get(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Id()).Do()
|
2014-11-20 18:40:17 +01:00
|
|
|
if err != nil {
|
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
|
|
|
// The resource doesn't exist anymore
|
2015-11-13 21:36:03 +01:00
|
|
|
log.Printf("[WARN] Removing HTTP Health Check %q because it's gone", d.Get("name").(string))
|
2014-11-20 18:40:17 +01:00
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error reading HttpHealthCheck: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-02-08 01:05:19 +01:00
|
|
|
d.Set("host", hchk.Host)
|
|
|
|
d.Set("request_path", hchk.RequestPath)
|
|
|
|
d.Set("check_interval_sec", hchk.CheckIntervalSec)
|
|
|
|
d.Set("health_threshold", hchk.HealthyThreshold)
|
|
|
|
d.Set("port", hchk.Port)
|
|
|
|
d.Set("timeout_sec", hchk.TimeoutSec)
|
|
|
|
d.Set("unhealthy_threshold", hchk.UnhealthyThreshold)
|
2014-11-20 18:40:17 +01:00
|
|
|
d.Set("self_link", hchk.SelfLink)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeHttpHealthCheckDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-20 18:40:17 +01:00
|
|
|
// Delete the HttpHealthCheck
|
|
|
|
op, err := config.clientCompute.HttpHealthChecks.Delete(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Id()).Do()
|
2014-11-20 18:40:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting HttpHealthCheck: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:30:12 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, "Deleting Http Health Check")
|
2014-11-20 18:40:17 +01:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2014-11-20 18:40:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|