github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_http_health_check.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"google.golang.org/api/compute/v1"
     9  	"google.golang.org/api/googleapi"
    10  )
    11  
    12  func resourceComputeHttpHealthCheck() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceComputeHttpHealthCheckCreate,
    15  		Read:   resourceComputeHttpHealthCheckRead,
    16  		Delete: resourceComputeHttpHealthCheckDelete,
    17  		Update: resourceComputeHttpHealthCheckUpdate,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"check_interval_sec": &schema.Schema{
    21  				Type:     schema.TypeInt,
    22  				Optional: true,
    23  				Default:  5,
    24  			},
    25  
    26  			"description": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Optional: true,
    29  			},
    30  
    31  			"healthy_threshold": &schema.Schema{
    32  				Type:     schema.TypeInt,
    33  				Optional: true,
    34  				Default:  2,
    35  			},
    36  
    37  			"host": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Optional: true,
    40  			},
    41  
    42  			"name": &schema.Schema{
    43  				Type:     schema.TypeString,
    44  				Required: true,
    45  				ForceNew: true,
    46  			},
    47  
    48  			"port": &schema.Schema{
    49  				Type:     schema.TypeInt,
    50  				Optional: true,
    51  				Default:  80,
    52  			},
    53  
    54  			"request_path": &schema.Schema{
    55  				Type:     schema.TypeString,
    56  				Optional: true,
    57  				Default:  "/",
    58  			},
    59  
    60  			"self_link": &schema.Schema{
    61  				Type:     schema.TypeString,
    62  				Computed: true,
    63  			},
    64  
    65  			"timeout_sec": &schema.Schema{
    66  				Type:     schema.TypeInt,
    67  				Optional: true,
    68  				Default:  5,
    69  			},
    70  
    71  			"unhealthy_threshold": &schema.Schema{
    72  				Type:     schema.TypeInt,
    73  				Optional: true,
    74  				Default:  2,
    75  			},
    76  		},
    77  	}
    78  }
    79  
    80  func resourceComputeHttpHealthCheckCreate(d *schema.ResourceData, meta interface{}) error {
    81  	config := meta.(*Config)
    82  
    83  	// Build the parameter
    84  	hchk := &compute.HttpHealthCheck{
    85  		Name: d.Get("name").(string),
    86  	}
    87  	// Optional things
    88  	if v, ok := d.GetOk("description"); ok {
    89  		hchk.Description = v.(string)
    90  	}
    91  	if v, ok := d.GetOk("host"); ok {
    92  		hchk.Host = v.(string)
    93  	}
    94  	if v, ok := d.GetOk("request_path"); ok {
    95  		hchk.RequestPath = v.(string)
    96  	}
    97  	if v, ok := d.GetOk("check_interval_sec"); ok {
    98  		hchk.CheckIntervalSec = int64(v.(int))
    99  	}
   100  	if v, ok := d.GetOk("healthy_threshold"); ok {
   101  		hchk.HealthyThreshold = int64(v.(int))
   102  	}
   103  	if v, ok := d.GetOk("port"); ok {
   104  		hchk.Port = int64(v.(int))
   105  	}
   106  	if v, ok := d.GetOk("timeout_sec"); ok {
   107  		hchk.TimeoutSec = int64(v.(int))
   108  	}
   109  	if v, ok := d.GetOk("unhealthy_threshold"); ok {
   110  		hchk.UnhealthyThreshold = int64(v.(int))
   111  	}
   112  
   113  	log.Printf("[DEBUG] HttpHealthCheck insert request: %#v", hchk)
   114  	op, err := config.clientCompute.HttpHealthChecks.Insert(
   115  		config.Project, hchk).Do()
   116  	if err != nil {
   117  		return fmt.Errorf("Error creating HttpHealthCheck: %s", err)
   118  	}
   119  
   120  	// It probably maybe worked, so store the ID now
   121  	d.SetId(hchk.Name)
   122  
   123  	err = computeOperationWaitGlobal(config, op, "Creating Http Health Check")
   124  	if err != nil {
   125  		return err
   126  	}
   127  
   128  	return resourceComputeHttpHealthCheckRead(d, meta)
   129  }
   130  
   131  func resourceComputeHttpHealthCheckUpdate(d *schema.ResourceData, meta interface{}) error {
   132  	config := meta.(*Config)
   133  
   134  	// Build the parameter
   135  	hchk := &compute.HttpHealthCheck{
   136  		Name: d.Get("name").(string),
   137  	}
   138  	// Optional things
   139  	if v, ok := d.GetOk("description"); ok {
   140  		hchk.Description = v.(string)
   141  	}
   142  	if v, ok := d.GetOk("host"); ok {
   143  		hchk.Host = v.(string)
   144  	}
   145  	if v, ok := d.GetOk("request_path"); ok {
   146  		hchk.RequestPath = v.(string)
   147  	}
   148  	if v, ok := d.GetOk("check_interval_sec"); ok {
   149  		hchk.CheckIntervalSec = int64(v.(int))
   150  	}
   151  	if v, ok := d.GetOk("healthy_threshold"); ok {
   152  		hchk.HealthyThreshold = int64(v.(int))
   153  	}
   154  	if v, ok := d.GetOk("port"); ok {
   155  		hchk.Port = int64(v.(int))
   156  	}
   157  	if v, ok := d.GetOk("timeout_sec"); ok {
   158  		hchk.TimeoutSec = int64(v.(int))
   159  	}
   160  	if v, ok := d.GetOk("unhealthy_threshold"); ok {
   161  		hchk.UnhealthyThreshold = int64(v.(int))
   162  	}
   163  
   164  	log.Printf("[DEBUG] HttpHealthCheck patch request: %#v", hchk)
   165  	op, err := config.clientCompute.HttpHealthChecks.Patch(
   166  		config.Project, hchk.Name, hchk).Do()
   167  	if err != nil {
   168  		return fmt.Errorf("Error patching HttpHealthCheck: %s", err)
   169  	}
   170  
   171  	// It probably maybe worked, so store the ID now
   172  	d.SetId(hchk.Name)
   173  
   174  	err = computeOperationWaitGlobal(config, op, "Updating Http Health Check")
   175  	if err != nil {
   176  		return err
   177  	}
   178  
   179  	return resourceComputeHttpHealthCheckRead(d, meta)
   180  }
   181  
   182  func resourceComputeHttpHealthCheckRead(d *schema.ResourceData, meta interface{}) error {
   183  	config := meta.(*Config)
   184  
   185  	hchk, err := config.clientCompute.HttpHealthChecks.Get(
   186  		config.Project, d.Id()).Do()
   187  	if err != nil {
   188  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   189  			// The resource doesn't exist anymore
   190  			d.SetId("")
   191  
   192  			return nil
   193  		}
   194  
   195  		return fmt.Errorf("Error reading HttpHealthCheck: %s", err)
   196  	}
   197  
   198  	d.Set("host", hchk.Host)
   199  	d.Set("request_path", hchk.RequestPath)
   200  	d.Set("check_interval_sec", hchk.CheckIntervalSec)
   201  	d.Set("health_threshold", hchk.HealthyThreshold)
   202  	d.Set("port", hchk.Port)
   203  	d.Set("timeout_sec", hchk.TimeoutSec)
   204  	d.Set("unhealthy_threshold", hchk.UnhealthyThreshold)
   205  	d.Set("self_link", hchk.SelfLink)
   206  
   207  	return nil
   208  }
   209  
   210  func resourceComputeHttpHealthCheckDelete(d *schema.ResourceData, meta interface{}) error {
   211  	config := meta.(*Config)
   212  
   213  	// Delete the HttpHealthCheck
   214  	op, err := config.clientCompute.HttpHealthChecks.Delete(
   215  		config.Project, d.Id()).Do()
   216  	if err != nil {
   217  		return fmt.Errorf("Error deleting HttpHealthCheck: %s", err)
   218  	}
   219  
   220  	err = computeOperationWaitGlobal(config, op, "Deleting Http Health Check")
   221  	if err != nil {
   222  		return err
   223  	}
   224  
   225  	d.SetId("")
   226  	return nil
   227  }