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