terraform/builtin/providers/spotinst/resource_spotinst_healthche...

334 lines
8.5 KiB
Go
Raw Normal View History

New Provider: Spotinst (#5001) * providers/spotinst: Add support for Spotinst resources * providers/spotinst: Fix merge conflict - layouts/docs.erb * docs/providers/spotinst: Fix the resource description field * providers/spotinst: Fix the acceptance tests * providers/spotinst: Mark the device_index as a required field * providers/spotinst: Change the associate_public_ip_address field to TypeBool * docs/providers/spotinst: Update the description of the adjustment field * providers/spotinst: Rename IamRole to IamInstanceProfile to make it more compatible with the AWS provider * docs/providers/spotinst: Rename iam_role to iam_instance_profile * providers/spotinst: Deprecate the iam_role attribute * providers/spotinst: Fix a misspelled var (IamRole) * providers/spotinst: Fix possible null pointer exception related to "iam_instance_profile" * docs/providers/spotinst: Add "load_balancer_names" missing description * providers/spotinst: New resource "spotinst_subscription" added * providers/spotinst: Eliminate a possible null pointer exception in "spotinst_aws_group" * providers/spotinst: Eliminate a possible null pointer exception in "spotinst_subscription" * providers/spotinst: Mark spotinst_subscription as deleted in destroy * providers/spotinst: Add support for custom event format in spotinst_subscription * providers/spotinst: Disable the destroy step of spotinst_subscription * providers/spotinst: Add support for update subscriptions * providers/spotinst: Merge fixed conflict - layouts/docs.erb * providers/spotinst: Vendor dependencies * providers/spotinst: Return a detailed error message * provider/spotinst: Update the plugin list * providers/spotinst: Vendor dependencies using govendor * providers/spotinst: New resource "spotinst_healthcheck" added * providers/spotinst: Update the Spotinst SDK * providers/spotinst: Comment out unnecessary log.Printf * providers/spotinst: Fix the acceptance tests * providers/spotinst: Gofmt fixes * providers/spotinst: Use multiple functions to expand each block * providers/spotinst: Allow ondemand_count to be zero * providers/spotinst: Change security_group_ids from TypeSet to TypeList * providers/spotinst: Remove unnecessary `ForceNew` fields * providers/spotinst: Update the Spotinst SDK * providers/spotinst: Add support for capacity unit * providers/spotinst: Add support for EBS volume pool * providers/spotinst: Delete health check * providers/spotinst: Allow to set multiple availability zones * providers/spotinst: Gofmt * providers/spotinst: Omit empty strings from the load_balancer_names field * providers/spotinst: Update the Spotinst SDK to v1.1.9 * providers/spotinst: Add support for new strategy parameters * providers/spotinst: Update the Spotinst SDK to v1.2.0 * providers/spotinst: Add support for Kubernetes integration * providers/spotinst: Fix merge conflict - vendor/vendor.json * providers/spotinst: Update the Spotinst SDK to v1.2.1 * providers/spotinst: Add support for Application Load Balancers * providers/spotinst: Do not allow to set ondemand_count to 0 * providers/spotinst: Update the Spotinst SDK to v1.2.2 * providers/spotinst: Add support for scaling policy operators * providers/spotinst: Add dimensions to spotinst_aws_group tests * providers/spotinst: Allow both ARN and name for IAM instance profiles * providers/spotinst: Allow ondemand_count=0 * providers/spotinst: Split out the set funcs into flatten style funcs * providers/spotinst: Update the Spotinst SDK to v1.2.3 * providers/spotinst: Add support for EBS optimized flag * providers/spotinst: Update the Spotinst SDK to v2.0.0 * providers/spotinst: Use stringutil.Stringify for debugging * providers/spotinst: Update the Spotinst SDK to v2.0.1 * providers/spotinst: Key pair is now optional * providers/spotinst: Make sure we do not nullify signals on strategy update * providers/spotinst: Hash both Strategy and EBS Block Device * providers/spotinst: Hash AWS load balancer * providers/spotinst: Update the Spotinst SDK to v2.0.2 * providers/spotinst: Verify namespace exists before appending policy * providers/spotinst: Image ID will be in a separate block from now on, so as to allow ignoring changes only on the image ID. This change is backwards compatible. * providers/spotinst: user data decoded when returned from spotinst api, so that TF compares the two states properly, and does not update without cause.
2017-02-22 21:57:16 +01:00
package spotinst
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/spotinst-sdk-go/spotinst/util/stringutil"
)
func resourceSpotinstHealthCheck() *schema.Resource {
return &schema.Resource{
Create: resourceSpotinstHealthCheckCreate,
Update: resourceSpotinstHealthCheckUpdate,
Read: resourceSpotinstHealthCheckRead,
Delete: resourceSpotinstHealthCheckDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"resource_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"check": &schema.Schema{
Type: schema.TypeSet,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"protocol": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"endpoint": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"port": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"interval": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"timeout": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
},
},
},
"threshold": &schema.Schema{
Type: schema.TypeSet,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"healthy": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"unhealthy": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
},
},
},
"proxy": &schema.Schema{
Type: schema.TypeSet,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"addr": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"port": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
},
},
},
},
}
}
func resourceSpotinstHealthCheckCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*spotinst.Client)
newHealthCheck, err := buildHealthCheckOpts(d, meta)
if err != nil {
return err
}
log.Printf("[DEBUG] HealthCheck create configuration: %#v\n", newHealthCheck)
input := &spotinst.CreateHealthCheckInput{HealthCheck: newHealthCheck}
resp, err := client.HealthCheckService.Create(input)
if err != nil {
return fmt.Errorf("Error creating health check: %s", err)
}
d.SetId(spotinst.StringValue(resp.HealthCheck.ID))
log.Printf("[INFO] HealthCheck created successfully: %s\n", d.Id())
return resourceSpotinstHealthCheckRead(d, meta)
}
func resourceSpotinstHealthCheckRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*spotinst.Client)
input := &spotinst.ReadHealthCheckInput{ID: spotinst.String(d.Id())}
resp, err := client.HealthCheckService.Read(input)
if err != nil {
return fmt.Errorf("Error retrieving health check: %s", err)
}
if hc := resp.HealthCheck; hc != nil {
d.Set("name", hc.Name)
d.Set("resource_id", hc.ResourceID)
// Set the check.
check := make([]map[string]interface{}, 0, 1)
check = append(check, map[string]interface{}{
"protocol": hc.Check.Protocol,
"endpoint": hc.Check.Endpoint,
"port": hc.Check.Port,
"interval": hc.Check.Interval,
"timeout": hc.Check.Timeout,
})
d.Set("check", check)
// Set the threshold.
threshold := make([]map[string]interface{}, 0, 1)
threshold = append(threshold, map[string]interface{}{
"healthy": hc.Check.Healthy,
"unhealthy": hc.Check.Unhealthy,
})
d.Set("threshold", threshold)
// Set the proxy.
proxy := make([]map[string]interface{}, 0, 1)
proxy = append(proxy, map[string]interface{}{
"addr": hc.Addr,
"port": hc.Port,
})
d.Set("proxy", proxy)
} else {
d.SetId("")
}
return nil
}
func resourceSpotinstHealthCheckUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*spotinst.Client)
healthCheck := &spotinst.HealthCheck{ID: spotinst.String(d.Id())}
update := false
if d.HasChange("name") {
healthCheck.Name = spotinst.String(d.Get("name").(string))
update = true
}
if d.HasChange("resource_id") {
healthCheck.ResourceID = spotinst.String(d.Get("resource_id").(string))
update = true
}
if d.HasChange("check") {
if v, ok := d.GetOk("check"); ok {
if check, err := expandHealthCheckConfig(v); err != nil {
return err
} else {
healthCheck.Check = check
update = true
}
}
}
if d.HasChange("threshold") {
if v, ok := d.GetOk("threshold"); ok {
if threshold, err := expandHealthCheckThreshold(v); err != nil {
return err
} else {
healthCheck.Check.HealthCheckThreshold = threshold
update = true
}
}
}
if d.HasChange("proxy") {
if v, ok := d.GetOk("proxy"); ok {
if proxy, err := expandHealthCheckProxy(v); err != nil {
return err
} else {
healthCheck.HealthCheckProxy = proxy
update = true
}
}
}
if update {
log.Printf("[DEBUG] HealthCheck update configuration: %s\n", stringutil.Stringify(healthCheck))
input := &spotinst.UpdateHealthCheckInput{HealthCheck: healthCheck}
if _, err := client.HealthCheckService.Update(input); err != nil {
return fmt.Errorf("Error updating health check %s: %s", d.Id(), err)
}
}
return resourceSpotinstHealthCheckRead(d, meta)
}
func resourceSpotinstHealthCheckDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*spotinst.Client)
log.Printf("[INFO] Deleting health check: %s\n", d.Id())
input := &spotinst.DeleteHealthCheckInput{ID: spotinst.String(d.Id())}
if _, err := client.HealthCheckService.Delete(input); err != nil {
return fmt.Errorf("Error deleting health check: %s", err)
}
d.SetId("")
return nil
}
// buildHealthCheckOpts builds the Spotinst HealthCheck options.
func buildHealthCheckOpts(d *schema.ResourceData, meta interface{}) (*spotinst.HealthCheck, error) {
healthCheck := &spotinst.HealthCheck{
Name: spotinst.String(d.Get("name").(string)),
ResourceID: spotinst.String(d.Get("resource_id").(string)),
}
if v, ok := d.GetOk("check"); ok {
if check, err := expandHealthCheckConfig(v); err != nil {
return nil, err
} else {
healthCheck.Check = check
}
}
if v, ok := d.GetOk("threshold"); ok {
if threshold, err := expandHealthCheckThreshold(v); err != nil {
return nil, err
} else {
healthCheck.Check.HealthCheckThreshold = threshold
}
}
if v, ok := d.GetOk("proxy"); ok {
if proxy, err := expandHealthCheckProxy(v); err != nil {
return nil, err
} else {
healthCheck.HealthCheckProxy = proxy
}
}
return healthCheck, nil
}
// expandHealthCheckConfig expands the Check block.
func expandHealthCheckConfig(data interface{}) (*spotinst.HealthCheckConfig, error) {
list := data.(*schema.Set).List()
m := list[0].(map[string]interface{})
check := &spotinst.HealthCheckConfig{}
if v, ok := m["protocol"].(string); ok && v != "" {
check.Protocol = spotinst.String(v)
}
if v, ok := m["endpoint"].(string); ok && v != "" {
check.Endpoint = spotinst.String(v)
}
if v, ok := m["port"].(int); ok && v >= 0 {
check.Port = spotinst.Int(v)
}
if v, ok := m["interval"].(int); ok && v >= 0 {
check.Interval = spotinst.Int(v)
}
if v, ok := m["timeout"].(int); ok && v >= 0 {
check.Timeout = spotinst.Int(v)
}
log.Printf("[DEBUG] HealthCheck check configuration: %s\n", stringutil.Stringify(check))
return check, nil
}
// expandHealthCheckThreshold expands the Threshold block.
func expandHealthCheckThreshold(data interface{}) (*spotinst.HealthCheckThreshold, error) {
list := data.(*schema.Set).List()
m := list[0].(map[string]interface{})
threshold := &spotinst.HealthCheckThreshold{}
if v, ok := m["healthy"].(int); ok && v >= 0 {
threshold.Healthy = spotinst.Int(v)
}
if v, ok := m["unhealthy"].(int); ok && v >= 0 {
threshold.Unhealthy = spotinst.Int(v)
}
log.Printf("[DEBUG] HealthCheck threshold configuration: %s\n", stringutil.Stringify(threshold))
return threshold, nil
}
// expandHealthCheckProxy expands the Proxy block.
func expandHealthCheckProxy(data interface{}) (*spotinst.HealthCheckProxy, error) {
list := data.(*schema.Set).List()
m := list[0].(map[string]interface{})
proxy := &spotinst.HealthCheckProxy{}
if v, ok := m["addr"].(string); ok && v != "" {
proxy.Addr = spotinst.String(v)
}
if v, ok := m["port"].(int); ok && v > 0 {
proxy.Port = spotinst.Int(v)
}
log.Printf("[DEBUG] HealthCheck proxy configuration: %s\n", stringutil.Stringify(proxy))
return proxy, nil
}