github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/spotinst/resource_spotinst_healthcheck.go (about)

     1  package spotinst
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/spotinst/spotinst-sdk-go/spotinst"
     9  	"github.com/spotinst/spotinst-sdk-go/spotinst/util/stringutil"
    10  )
    11  
    12  func resourceSpotinstHealthCheck() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceSpotinstHealthCheckCreate,
    15  		Update: resourceSpotinstHealthCheckUpdate,
    16  		Read:   resourceSpotinstHealthCheckRead,
    17  		Delete: resourceSpotinstHealthCheckDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"name": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Optional: true,
    23  			},
    24  
    25  			"resource_id": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  			},
    29  
    30  			"check": &schema.Schema{
    31  				Type:     schema.TypeSet,
    32  				Required: true,
    33  				MaxItems: 1,
    34  				Elem: &schema.Resource{
    35  					Schema: map[string]*schema.Schema{
    36  						"protocol": &schema.Schema{
    37  							Type:     schema.TypeString,
    38  							Required: true,
    39  						},
    40  
    41  						"endpoint": &schema.Schema{
    42  							Type:     schema.TypeString,
    43  							Required: true,
    44  						},
    45  
    46  						"port": &schema.Schema{
    47  							Type:     schema.TypeInt,
    48  							Required: true,
    49  						},
    50  
    51  						"interval": &schema.Schema{
    52  							Type:     schema.TypeInt,
    53  							Required: true,
    54  						},
    55  
    56  						"timeout": &schema.Schema{
    57  							Type:     schema.TypeInt,
    58  							Required: true,
    59  						},
    60  					},
    61  				},
    62  			},
    63  
    64  			"threshold": &schema.Schema{
    65  				Type:     schema.TypeSet,
    66  				Required: true,
    67  				MaxItems: 1,
    68  				Elem: &schema.Resource{
    69  					Schema: map[string]*schema.Schema{
    70  						"healthy": &schema.Schema{
    71  							Type:     schema.TypeInt,
    72  							Required: true,
    73  						},
    74  
    75  						"unhealthy": &schema.Schema{
    76  							Type:     schema.TypeInt,
    77  							Required: true,
    78  						},
    79  					},
    80  				},
    81  			},
    82  
    83  			"proxy": &schema.Schema{
    84  				Type:     schema.TypeSet,
    85  				Required: true,
    86  				MaxItems: 1,
    87  				Elem: &schema.Resource{
    88  					Schema: map[string]*schema.Schema{
    89  						"addr": &schema.Schema{
    90  							Type:     schema.TypeString,
    91  							Required: true,
    92  						},
    93  
    94  						"port": &schema.Schema{
    95  							Type:     schema.TypeInt,
    96  							Required: true,
    97  						},
    98  					},
    99  				},
   100  			},
   101  		},
   102  	}
   103  }
   104  
   105  func resourceSpotinstHealthCheckCreate(d *schema.ResourceData, meta interface{}) error {
   106  	client := meta.(*spotinst.Client)
   107  	newHealthCheck, err := buildHealthCheckOpts(d, meta)
   108  	if err != nil {
   109  		return err
   110  	}
   111  	log.Printf("[DEBUG] HealthCheck create configuration: %#v\n", newHealthCheck)
   112  	input := &spotinst.CreateHealthCheckInput{HealthCheck: newHealthCheck}
   113  	resp, err := client.HealthCheckService.Create(input)
   114  	if err != nil {
   115  		return fmt.Errorf("Error creating health check: %s", err)
   116  	}
   117  	d.SetId(spotinst.StringValue(resp.HealthCheck.ID))
   118  	log.Printf("[INFO] HealthCheck created successfully: %s\n", d.Id())
   119  	return resourceSpotinstHealthCheckRead(d, meta)
   120  }
   121  
   122  func resourceSpotinstHealthCheckRead(d *schema.ResourceData, meta interface{}) error {
   123  	client := meta.(*spotinst.Client)
   124  	input := &spotinst.ReadHealthCheckInput{ID: spotinst.String(d.Id())}
   125  	resp, err := client.HealthCheckService.Read(input)
   126  	if err != nil {
   127  		return fmt.Errorf("Error retrieving health check: %s", err)
   128  	}
   129  	if hc := resp.HealthCheck; hc != nil {
   130  		d.Set("name", hc.Name)
   131  		d.Set("resource_id", hc.ResourceID)
   132  
   133  		// Set the check.
   134  		check := make([]map[string]interface{}, 0, 1)
   135  		check = append(check, map[string]interface{}{
   136  			"protocol": hc.Check.Protocol,
   137  			"endpoint": hc.Check.Endpoint,
   138  			"port":     hc.Check.Port,
   139  			"interval": hc.Check.Interval,
   140  			"timeout":  hc.Check.Timeout,
   141  		})
   142  		d.Set("check", check)
   143  
   144  		// Set the threshold.
   145  		threshold := make([]map[string]interface{}, 0, 1)
   146  		threshold = append(threshold, map[string]interface{}{
   147  			"healthy":   hc.Check.Healthy,
   148  			"unhealthy": hc.Check.Unhealthy,
   149  		})
   150  		d.Set("threshold", threshold)
   151  
   152  		// Set the proxy.
   153  		proxy := make([]map[string]interface{}, 0, 1)
   154  		proxy = append(proxy, map[string]interface{}{
   155  			"addr": hc.Addr,
   156  			"port": hc.Port,
   157  		})
   158  		d.Set("proxy", proxy)
   159  	} else {
   160  		d.SetId("")
   161  	}
   162  	return nil
   163  }
   164  
   165  func resourceSpotinstHealthCheckUpdate(d *schema.ResourceData, meta interface{}) error {
   166  	client := meta.(*spotinst.Client)
   167  	healthCheck := &spotinst.HealthCheck{ID: spotinst.String(d.Id())}
   168  	update := false
   169  
   170  	if d.HasChange("name") {
   171  		healthCheck.Name = spotinst.String(d.Get("name").(string))
   172  		update = true
   173  	}
   174  
   175  	if d.HasChange("resource_id") {
   176  		healthCheck.ResourceID = spotinst.String(d.Get("resource_id").(string))
   177  		update = true
   178  	}
   179  
   180  	if d.HasChange("check") {
   181  		if v, ok := d.GetOk("check"); ok {
   182  			if check, err := expandHealthCheckConfig(v); err != nil {
   183  				return err
   184  			} else {
   185  				healthCheck.Check = check
   186  				update = true
   187  			}
   188  		}
   189  	}
   190  
   191  	if d.HasChange("threshold") {
   192  		if v, ok := d.GetOk("threshold"); ok {
   193  			if threshold, err := expandHealthCheckThreshold(v); err != nil {
   194  				return err
   195  			} else {
   196  				healthCheck.Check.HealthCheckThreshold = threshold
   197  				update = true
   198  			}
   199  		}
   200  	}
   201  
   202  	if d.HasChange("proxy") {
   203  		if v, ok := d.GetOk("proxy"); ok {
   204  			if proxy, err := expandHealthCheckProxy(v); err != nil {
   205  				return err
   206  			} else {
   207  				healthCheck.HealthCheckProxy = proxy
   208  				update = true
   209  			}
   210  		}
   211  	}
   212  
   213  	if update {
   214  		log.Printf("[DEBUG] HealthCheck update configuration: %s\n", stringutil.Stringify(healthCheck))
   215  		input := &spotinst.UpdateHealthCheckInput{HealthCheck: healthCheck}
   216  		if _, err := client.HealthCheckService.Update(input); err != nil {
   217  			return fmt.Errorf("Error updating health check %s: %s", d.Id(), err)
   218  		}
   219  	}
   220  
   221  	return resourceSpotinstHealthCheckRead(d, meta)
   222  }
   223  
   224  func resourceSpotinstHealthCheckDelete(d *schema.ResourceData, meta interface{}) error {
   225  	client := meta.(*spotinst.Client)
   226  	log.Printf("[INFO] Deleting health check: %s\n", d.Id())
   227  	input := &spotinst.DeleteHealthCheckInput{ID: spotinst.String(d.Id())}
   228  	if _, err := client.HealthCheckService.Delete(input); err != nil {
   229  		return fmt.Errorf("Error deleting health check: %s", err)
   230  	}
   231  	d.SetId("")
   232  	return nil
   233  }
   234  
   235  // buildHealthCheckOpts builds the Spotinst HealthCheck options.
   236  func buildHealthCheckOpts(d *schema.ResourceData, meta interface{}) (*spotinst.HealthCheck, error) {
   237  	healthCheck := &spotinst.HealthCheck{
   238  		Name:       spotinst.String(d.Get("name").(string)),
   239  		ResourceID: spotinst.String(d.Get("resource_id").(string)),
   240  	}
   241  
   242  	if v, ok := d.GetOk("check"); ok {
   243  		if check, err := expandHealthCheckConfig(v); err != nil {
   244  			return nil, err
   245  		} else {
   246  			healthCheck.Check = check
   247  		}
   248  	}
   249  
   250  	if v, ok := d.GetOk("threshold"); ok {
   251  		if threshold, err := expandHealthCheckThreshold(v); err != nil {
   252  			return nil, err
   253  		} else {
   254  			healthCheck.Check.HealthCheckThreshold = threshold
   255  		}
   256  	}
   257  
   258  	if v, ok := d.GetOk("proxy"); ok {
   259  		if proxy, err := expandHealthCheckProxy(v); err != nil {
   260  			return nil, err
   261  		} else {
   262  			healthCheck.HealthCheckProxy = proxy
   263  		}
   264  	}
   265  
   266  	return healthCheck, nil
   267  }
   268  
   269  // expandHealthCheckConfig expands the Check block.
   270  func expandHealthCheckConfig(data interface{}) (*spotinst.HealthCheckConfig, error) {
   271  	list := data.(*schema.Set).List()
   272  	m := list[0].(map[string]interface{})
   273  	check := &spotinst.HealthCheckConfig{}
   274  
   275  	if v, ok := m["protocol"].(string); ok && v != "" {
   276  		check.Protocol = spotinst.String(v)
   277  	}
   278  
   279  	if v, ok := m["endpoint"].(string); ok && v != "" {
   280  		check.Endpoint = spotinst.String(v)
   281  	}
   282  
   283  	if v, ok := m["port"].(int); ok && v >= 0 {
   284  		check.Port = spotinst.Int(v)
   285  	}
   286  
   287  	if v, ok := m["interval"].(int); ok && v >= 0 {
   288  		check.Interval = spotinst.Int(v)
   289  	}
   290  
   291  	if v, ok := m["timeout"].(int); ok && v >= 0 {
   292  		check.Timeout = spotinst.Int(v)
   293  	}
   294  
   295  	log.Printf("[DEBUG] HealthCheck check configuration: %s\n", stringutil.Stringify(check))
   296  	return check, nil
   297  }
   298  
   299  // expandHealthCheckThreshold expands the Threshold block.
   300  func expandHealthCheckThreshold(data interface{}) (*spotinst.HealthCheckThreshold, error) {
   301  	list := data.(*schema.Set).List()
   302  	m := list[0].(map[string]interface{})
   303  	threshold := &spotinst.HealthCheckThreshold{}
   304  
   305  	if v, ok := m["healthy"].(int); ok && v >= 0 {
   306  		threshold.Healthy = spotinst.Int(v)
   307  	}
   308  
   309  	if v, ok := m["unhealthy"].(int); ok && v >= 0 {
   310  		threshold.Unhealthy = spotinst.Int(v)
   311  	}
   312  
   313  	log.Printf("[DEBUG] HealthCheck threshold configuration: %s\n", stringutil.Stringify(threshold))
   314  	return threshold, nil
   315  }
   316  
   317  // expandHealthCheckProxy expands the Proxy block.
   318  func expandHealthCheckProxy(data interface{}) (*spotinst.HealthCheckProxy, error) {
   319  	list := data.(*schema.Set).List()
   320  	m := list[0].(map[string]interface{})
   321  	proxy := &spotinst.HealthCheckProxy{}
   322  
   323  	if v, ok := m["addr"].(string); ok && v != "" {
   324  		proxy.Addr = spotinst.String(v)
   325  	}
   326  
   327  	if v, ok := m["port"].(int); ok && v > 0 {
   328  		proxy.Port = spotinst.Int(v)
   329  	}
   330  
   331  	log.Printf("[DEBUG] HealthCheck proxy configuration: %s\n", stringutil.Stringify(proxy))
   332  	return proxy, nil
   333  }