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