github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/resource_compute_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 resourceComputeHealthCheck() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceComputeHealthCheckCreate,
    15  		Read:   resourceComputeHealthCheckRead,
    16  		Delete: resourceComputeHealthCheckDelete,
    17  		Update: resourceComputeHealthCheckUpdate,
    18  		Importer: &schema.ResourceImporter{
    19  			State: schema.ImportStatePassthrough,
    20  		},
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"name": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  
    29  			"check_interval_sec": &schema.Schema{
    30  				Type:     schema.TypeInt,
    31  				Optional: true,
    32  				Default:  5,
    33  			},
    34  
    35  			"description": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Optional: true,
    38  			},
    39  
    40  			"healthy_threshold": &schema.Schema{
    41  				Type:     schema.TypeInt,
    42  				Optional: true,
    43  				Default:  2,
    44  			},
    45  
    46  			"tcp_health_check": &schema.Schema{
    47  				Type:          schema.TypeList,
    48  				Optional:      true,
    49  				MaxItems:      1,
    50  				ConflictsWith: []string{"ssl_health_check", "http_health_check", "https_health_check"},
    51  				Elem: &schema.Resource{
    52  					Schema: map[string]*schema.Schema{
    53  						"port": &schema.Schema{
    54  							Type:     schema.TypeInt,
    55  							Optional: true,
    56  							Default:  80,
    57  						},
    58  						"proxy_header": &schema.Schema{
    59  							Type:     schema.TypeString,
    60  							Optional: true,
    61  							Default:  "NONE",
    62  						},
    63  						"request": &schema.Schema{
    64  							Type:     schema.TypeString,
    65  							Optional: true,
    66  						},
    67  						"response": &schema.Schema{
    68  							Type:     schema.TypeString,
    69  							Optional: true,
    70  						},
    71  					},
    72  				},
    73  			},
    74  
    75  			"ssl_health_check": &schema.Schema{
    76  				Type:          schema.TypeList,
    77  				Optional:      true,
    78  				MaxItems:      1,
    79  				ConflictsWith: []string{"tcp_health_check", "http_health_check", "https_health_check"},
    80  				Elem: &schema.Resource{
    81  					Schema: map[string]*schema.Schema{
    82  						"port": &schema.Schema{
    83  							Type:     schema.TypeInt,
    84  							Optional: true,
    85  							Default:  443,
    86  						},
    87  						"proxy_header": &schema.Schema{
    88  							Type:     schema.TypeString,
    89  							Optional: true,
    90  							Default:  "NONE",
    91  						},
    92  						"request": &schema.Schema{
    93  							Type:     schema.TypeString,
    94  							Optional: true,
    95  						},
    96  						"response": &schema.Schema{
    97  							Type:     schema.TypeString,
    98  							Optional: true,
    99  						},
   100  					},
   101  				},
   102  			},
   103  
   104  			"http_health_check": &schema.Schema{
   105  				Type:          schema.TypeList,
   106  				Optional:      true,
   107  				MaxItems:      1,
   108  				ConflictsWith: []string{"tcp_health_check", "ssl_health_check", "https_health_check"},
   109  				Elem: &schema.Resource{
   110  					Schema: map[string]*schema.Schema{
   111  						"host": &schema.Schema{
   112  							Type:     schema.TypeString,
   113  							Optional: true,
   114  							Default:  80,
   115  						},
   116  						"port": &schema.Schema{
   117  							Type:     schema.TypeInt,
   118  							Optional: true,
   119  						},
   120  						"proxy_header": &schema.Schema{
   121  							Type:     schema.TypeString,
   122  							Optional: true,
   123  							Default:  "NONE",
   124  						},
   125  						"request_path": &schema.Schema{
   126  							Type:     schema.TypeString,
   127  							Optional: true,
   128  							Default:  "/",
   129  						},
   130  					},
   131  				},
   132  			},
   133  
   134  			"https_health_check": &schema.Schema{
   135  				Type:          schema.TypeList,
   136  				Optional:      true,
   137  				MaxItems:      1,
   138  				ConflictsWith: []string{"tcp_health_check", "ssl_health_check", "http_health_check"},
   139  				Elem: &schema.Resource{
   140  					Schema: map[string]*schema.Schema{
   141  						"host": &schema.Schema{
   142  							Type:     schema.TypeString,
   143  							Optional: true,
   144  							Default:  443,
   145  						},
   146  						"port": &schema.Schema{
   147  							Type:     schema.TypeInt,
   148  							Optional: true,
   149  						},
   150  						"proxy_header": &schema.Schema{
   151  							Type:     schema.TypeString,
   152  							Optional: true,
   153  							Default:  "NONE",
   154  						},
   155  						"request_path": &schema.Schema{
   156  							Type:     schema.TypeString,
   157  							Optional: true,
   158  							Default:  "/",
   159  						},
   160  					},
   161  				},
   162  			},
   163  
   164  			"project": &schema.Schema{
   165  				Type:     schema.TypeString,
   166  				Optional: true,
   167  				ForceNew: true,
   168  				Computed: true,
   169  			},
   170  
   171  			"self_link": &schema.Schema{
   172  				Type:     schema.TypeString,
   173  				Computed: true,
   174  			},
   175  
   176  			"timeout_sec": &schema.Schema{
   177  				Type:     schema.TypeInt,
   178  				Optional: true,
   179  				Default:  5,
   180  			},
   181  
   182  			"unhealthy_threshold": &schema.Schema{
   183  				Type:     schema.TypeInt,
   184  				Optional: true,
   185  				Default:  2,
   186  			},
   187  		},
   188  	}
   189  }
   190  
   191  func resourceComputeHealthCheckCreate(d *schema.ResourceData, meta interface{}) error {
   192  	config := meta.(*Config)
   193  
   194  	project, err := getProject(d, config)
   195  	if err != nil {
   196  		return err
   197  	}
   198  
   199  	// Build the parameter
   200  	hchk := &compute.HealthCheck{
   201  		Name: d.Get("name").(string),
   202  	}
   203  	// Optional things
   204  	if v, ok := d.GetOk("description"); ok {
   205  		hchk.Description = v.(string)
   206  	}
   207  	if v, ok := d.GetOk("check_interval_sec"); ok {
   208  		hchk.CheckIntervalSec = int64(v.(int))
   209  	}
   210  	if v, ok := d.GetOk("healthy_threshold"); ok {
   211  		hchk.HealthyThreshold = int64(v.(int))
   212  	}
   213  	if v, ok := d.GetOk("timeout_sec"); ok {
   214  		hchk.TimeoutSec = int64(v.(int))
   215  	}
   216  	if v, ok := d.GetOk("unhealthy_threshold"); ok {
   217  		hchk.UnhealthyThreshold = int64(v.(int))
   218  	}
   219  
   220  	if v, ok := d.GetOk("tcp_health_check"); ok {
   221  		hchk.Type = "TCP"
   222  		tcpcheck := v.([]interface{})[0].(map[string]interface{})
   223  		tcpHealthCheck := &compute.TCPHealthCheck{}
   224  		if val, ok := tcpcheck["port"]; ok {
   225  			tcpHealthCheck.Port = int64(val.(int))
   226  		}
   227  		if val, ok := tcpcheck["proxy_header"]; ok {
   228  			tcpHealthCheck.ProxyHeader = val.(string)
   229  		}
   230  		if val, ok := tcpcheck["request"]; ok {
   231  			tcpHealthCheck.Request = val.(string)
   232  		}
   233  		if val, ok := tcpcheck["response"]; ok {
   234  			tcpHealthCheck.Response = val.(string)
   235  		}
   236  		hchk.TcpHealthCheck = tcpHealthCheck
   237  	}
   238  
   239  	if v, ok := d.GetOk("ssl_health_check"); ok {
   240  		hchk.Type = "SSL"
   241  		sslcheck := v.([]interface{})[0].(map[string]interface{})
   242  		sslHealthCheck := &compute.SSLHealthCheck{}
   243  		if val, ok := sslcheck["port"]; ok {
   244  			sslHealthCheck.Port = int64(val.(int))
   245  		}
   246  		if val, ok := sslcheck["proxy_header"]; ok {
   247  			sslHealthCheck.ProxyHeader = val.(string)
   248  		}
   249  		if val, ok := sslcheck["request"]; ok {
   250  			sslHealthCheck.Request = val.(string)
   251  		}
   252  		if val, ok := sslcheck["response"]; ok {
   253  			sslHealthCheck.Response = val.(string)
   254  		}
   255  		hchk.SslHealthCheck = sslHealthCheck
   256  	}
   257  
   258  	if v, ok := d.GetOk("http_health_check"); ok {
   259  		hchk.Type = "HTTP"
   260  		httpcheck := v.([]interface{})[0].(map[string]interface{})
   261  		httpHealthCheck := &compute.HTTPHealthCheck{}
   262  		if val, ok := httpcheck["host"]; ok {
   263  			httpHealthCheck.Host = val.(string)
   264  		}
   265  		if val, ok := httpcheck["port"]; ok {
   266  			httpHealthCheck.Port = int64(val.(int))
   267  		}
   268  		if val, ok := httpcheck["proxy_header"]; ok {
   269  			httpHealthCheck.ProxyHeader = val.(string)
   270  		}
   271  		if val, ok := httpcheck["request_path"]; ok {
   272  			httpHealthCheck.RequestPath = val.(string)
   273  		}
   274  		hchk.HttpHealthCheck = httpHealthCheck
   275  	}
   276  
   277  	if v, ok := d.GetOk("https_health_check"); ok {
   278  		hchk.Type = "HTTPS"
   279  		httpscheck := v.([]interface{})[0].(map[string]interface{})
   280  		httpsHealthCheck := &compute.HTTPSHealthCheck{}
   281  		if val, ok := httpscheck["host"]; ok {
   282  			httpsHealthCheck.Host = val.(string)
   283  		}
   284  		if val, ok := httpscheck["port"]; ok {
   285  			httpsHealthCheck.Port = int64(val.(int))
   286  		}
   287  		if val, ok := httpscheck["proxy_header"]; ok {
   288  			httpsHealthCheck.ProxyHeader = val.(string)
   289  		}
   290  		if val, ok := httpscheck["request_path"]; ok {
   291  			httpsHealthCheck.RequestPath = val.(string)
   292  		}
   293  		hchk.HttpsHealthCheck = httpsHealthCheck
   294  	}
   295  
   296  	log.Printf("[DEBUG] HealthCheck insert request: %#v", hchk)
   297  	op, err := config.clientCompute.HealthChecks.Insert(
   298  		project, hchk).Do()
   299  	if err != nil {
   300  		return fmt.Errorf("Error creating HealthCheck: %s", err)
   301  	}
   302  
   303  	// It probably maybe worked, so store the ID now
   304  	d.SetId(hchk.Name)
   305  
   306  	err = computeOperationWaitGlobal(config, op, project, "Creating Health Check")
   307  	if err != nil {
   308  		return err
   309  	}
   310  
   311  	return resourceComputeHealthCheckRead(d, meta)
   312  }
   313  
   314  func resourceComputeHealthCheckUpdate(d *schema.ResourceData, meta interface{}) error {
   315  	config := meta.(*Config)
   316  
   317  	project, err := getProject(d, config)
   318  	if err != nil {
   319  		return err
   320  	}
   321  
   322  	// Build the parameter
   323  	hchk := &compute.HealthCheck{
   324  		Name: d.Get("name").(string),
   325  	}
   326  	// Optional things
   327  	if v, ok := d.GetOk("description"); ok {
   328  		hchk.Description = v.(string)
   329  	}
   330  	if v, ok := d.GetOk("check_interval_sec"); ok {
   331  		hchk.CheckIntervalSec = int64(v.(int))
   332  	}
   333  	if v, ok := d.GetOk("healthy_threshold"); ok {
   334  		hchk.HealthyThreshold = int64(v.(int))
   335  	}
   336  	if v, ok := d.GetOk("timeout_sec"); ok {
   337  		hchk.TimeoutSec = int64(v.(int))
   338  	}
   339  	if v, ok := d.GetOk("unhealthy_threshold"); ok {
   340  		hchk.UnhealthyThreshold = int64(v.(int))
   341  	}
   342  	if v, ok := d.GetOk("tcp_health_check"); ok {
   343  		hchk.Type = "TCP"
   344  		tcpcheck := v.([]interface{})[0].(map[string]interface{})
   345  		tcpHealthCheck := &compute.TCPHealthCheck{}
   346  		if val, ok := tcpcheck["port"]; ok {
   347  			tcpHealthCheck.Port = int64(val.(int))
   348  		}
   349  		if val, ok := tcpcheck["proxy_header"]; ok {
   350  			tcpHealthCheck.ProxyHeader = val.(string)
   351  		}
   352  		if val, ok := tcpcheck["request"]; ok {
   353  			tcpHealthCheck.Request = val.(string)
   354  		}
   355  		if val, ok := tcpcheck["response"]; ok {
   356  			tcpHealthCheck.Response = val.(string)
   357  		}
   358  		hchk.TcpHealthCheck = tcpHealthCheck
   359  	}
   360  	if v, ok := d.GetOk("ssl_health_check"); ok {
   361  		hchk.Type = "SSL"
   362  		sslcheck := v.([]interface{})[0].(map[string]interface{})
   363  		sslHealthCheck := &compute.SSLHealthCheck{}
   364  		if val, ok := sslcheck["port"]; ok {
   365  			sslHealthCheck.Port = int64(val.(int))
   366  		}
   367  		if val, ok := sslcheck["proxy_header"]; ok {
   368  			sslHealthCheck.ProxyHeader = val.(string)
   369  		}
   370  		if val, ok := sslcheck["request"]; ok {
   371  			sslHealthCheck.Request = val.(string)
   372  		}
   373  		if val, ok := sslcheck["response"]; ok {
   374  			sslHealthCheck.Response = val.(string)
   375  		}
   376  		hchk.SslHealthCheck = sslHealthCheck
   377  	}
   378  	if v, ok := d.GetOk("http_health_check"); ok {
   379  		hchk.Type = "HTTP"
   380  		httpcheck := v.([]interface{})[0].(map[string]interface{})
   381  		httpHealthCheck := &compute.HTTPHealthCheck{}
   382  		if val, ok := httpcheck["host"]; ok {
   383  			httpHealthCheck.Host = val.(string)
   384  		}
   385  		if val, ok := httpcheck["port"]; ok {
   386  			httpHealthCheck.Port = int64(val.(int))
   387  		}
   388  		if val, ok := httpcheck["proxy_header"]; ok {
   389  			httpHealthCheck.ProxyHeader = val.(string)
   390  		}
   391  		if val, ok := httpcheck["request_path"]; ok {
   392  			httpHealthCheck.RequestPath = val.(string)
   393  		}
   394  		hchk.HttpHealthCheck = httpHealthCheck
   395  	}
   396  
   397  	if v, ok := d.GetOk("https_health_check"); ok {
   398  		hchk.Type = "HTTPS"
   399  		httpscheck := v.([]interface{})[0].(map[string]interface{})
   400  		httpsHealthCheck := &compute.HTTPSHealthCheck{}
   401  		if val, ok := httpscheck["host"]; ok {
   402  			httpsHealthCheck.Host = val.(string)
   403  		}
   404  		if val, ok := httpscheck["port"]; ok {
   405  			httpsHealthCheck.Port = int64(val.(int))
   406  		}
   407  		if val, ok := httpscheck["proxy_header"]; ok {
   408  			httpsHealthCheck.ProxyHeader = val.(string)
   409  		}
   410  		if val, ok := httpscheck["request_path"]; ok {
   411  			httpsHealthCheck.RequestPath = val.(string)
   412  		}
   413  		hchk.HttpsHealthCheck = httpsHealthCheck
   414  	}
   415  
   416  	log.Printf("[DEBUG] HealthCheck patch request: %#v", hchk)
   417  	op, err := config.clientCompute.HealthChecks.Patch(
   418  		project, hchk.Name, hchk).Do()
   419  	if err != nil {
   420  		return fmt.Errorf("Error patching HealthCheck: %s", err)
   421  	}
   422  
   423  	// It probably maybe worked, so store the ID now
   424  	d.SetId(hchk.Name)
   425  
   426  	err = computeOperationWaitGlobal(config, op, project, "Updating Health Check")
   427  	if err != nil {
   428  		return err
   429  	}
   430  
   431  	return resourceComputeHealthCheckRead(d, meta)
   432  }
   433  
   434  func resourceComputeHealthCheckRead(d *schema.ResourceData, meta interface{}) error {
   435  	config := meta.(*Config)
   436  
   437  	project, err := getProject(d, config)
   438  	if err != nil {
   439  		return err
   440  	}
   441  
   442  	hchk, err := config.clientCompute.HealthChecks.Get(
   443  		project, d.Id()).Do()
   444  	if err != nil {
   445  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   446  			// The resource doesn't exist anymore
   447  			log.Printf("[WARN] Removing Health Check %q because it's gone", d.Get("name").(string))
   448  			d.SetId("")
   449  
   450  			return nil
   451  		}
   452  
   453  		return fmt.Errorf("Error reading HealthCheck: %s", err)
   454  	}
   455  
   456  	d.Set("check_interval_sec", hchk.CheckIntervalSec)
   457  	d.Set("healthy_threshold", hchk.HealthyThreshold)
   458  	d.Set("timeout_sec", hchk.TimeoutSec)
   459  	d.Set("unhealthy_threshold", hchk.UnhealthyThreshold)
   460  	d.Set("tcp_health_check", hchk.TcpHealthCheck)
   461  	d.Set("ssl_health_check", hchk.SslHealthCheck)
   462  	d.Set("http_health_check", hchk.HttpHealthCheck)
   463  	d.Set("https_health_check", hchk.HttpsHealthCheck)
   464  	d.Set("self_link", hchk.SelfLink)
   465  	d.Set("name", hchk.Name)
   466  	d.Set("description", hchk.Description)
   467  	d.Set("project", project)
   468  
   469  	return nil
   470  }
   471  
   472  func resourceComputeHealthCheckDelete(d *schema.ResourceData, meta interface{}) error {
   473  	config := meta.(*Config)
   474  
   475  	project, err := getProject(d, config)
   476  	if err != nil {
   477  		return err
   478  	}
   479  
   480  	// Delete the HealthCheck
   481  	op, err := config.clientCompute.HealthChecks.Delete(
   482  		project, d.Id()).Do()
   483  	if err != nil {
   484  		return fmt.Errorf("Error deleting HealthCheck: %s", err)
   485  	}
   486  
   487  	err = computeOperationWaitGlobal(config, op, project, "Deleting Health Check")
   488  	if err != nil {
   489  		return err
   490  	}
   491  
   492  	d.SetId("")
   493  	return nil
   494  }