2015-04-28 18:11:38 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2015-11-04 00:28:47 +01:00
|
|
|
"fmt"
|
2015-04-28 18:11:38 +02:00
|
|
|
"log"
|
2015-11-04 00:29:53 +01:00
|
|
|
"strings"
|
2015-04-28 18:11:38 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/route53"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsRoute53HealthCheck() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsRoute53HealthCheckCreate,
|
|
|
|
Read: resourceAwsRoute53HealthCheckRead,
|
|
|
|
Update: resourceAwsRoute53HealthCheckUpdate,
|
|
|
|
Delete: resourceAwsRoute53HealthCheckDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
2015-11-04 00:29:53 +01:00
|
|
|
StateFunc: func(val interface{}) string {
|
|
|
|
return strings.ToUpper(val.(string))
|
|
|
|
},
|
2015-04-28 18:11:38 +02:00
|
|
|
},
|
|
|
|
"failure_threshold": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
2015-11-04 00:28:47 +01:00
|
|
|
Optional: true,
|
2015-04-28 18:11:38 +02:00
|
|
|
},
|
|
|
|
"request_interval": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
2015-11-04 00:28:47 +01:00
|
|
|
Optional: true,
|
2015-04-28 18:11:38 +02:00
|
|
|
ForceNew: true, // todo this should be updateable but the awslabs route53 service doesnt have the ability
|
|
|
|
},
|
|
|
|
"ip_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"fqdn": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"port": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2015-11-03 23:34:06 +01:00
|
|
|
"invert_healthcheck": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2015-04-28 18:11:38 +02:00
|
|
|
"resource_path": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2015-11-03 23:34:06 +01:00
|
|
|
|
2015-04-28 18:11:38 +02:00
|
|
|
"search_string": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2015-10-29 18:27:50 +01:00
|
|
|
"measure_latency": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
2015-10-29 20:50:02 +01:00
|
|
|
ForceNew: true,
|
2015-10-29 18:27:50 +01:00
|
|
|
},
|
2015-11-03 23:34:06 +01:00
|
|
|
|
2015-11-04 00:28:47 +01:00
|
|
|
"child_healthchecks": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Optional: true,
|
|
|
|
Set: schema.HashString,
|
|
|
|
},
|
|
|
|
"child_health_threshold": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
|
|
|
|
value := v.(int)
|
|
|
|
if value > 256 {
|
|
|
|
es = append(es, fmt.Errorf(
|
|
|
|
"Child HealthThreshold cannot be more than 256"))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2015-04-28 18:11:38 +02:00
|
|
|
"tags": tagsSchema(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsRoute53HealthCheckUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).r53conn
|
|
|
|
|
|
|
|
updateHealthCheck := &route53.UpdateHealthCheckInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
HealthCheckId: aws.String(d.Id()),
|
2015-04-28 18:11:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("failure_threshold") {
|
2015-07-28 22:29:46 +02:00
|
|
|
updateHealthCheck.FailureThreshold = aws.Int64(int64(d.Get("failure_threshold").(int)))
|
2015-04-28 18:11:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("fqdn") {
|
|
|
|
updateHealthCheck.FullyQualifiedDomainName = aws.String(d.Get("fqdn").(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("port") {
|
2015-07-28 22:29:46 +02:00
|
|
|
updateHealthCheck.Port = aws.Int64(int64(d.Get("port").(int)))
|
2015-04-28 18:11:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("resource_path") {
|
|
|
|
updateHealthCheck.ResourcePath = aws.String(d.Get("resource_path").(string))
|
|
|
|
}
|
|
|
|
|
2015-11-03 23:34:06 +01:00
|
|
|
if d.HasChange("invert_healthcheck") {
|
|
|
|
updateHealthCheck.Inverted = aws.Bool(d.Get("invert_healthcheck").(bool))
|
2015-04-28 18:11:38 +02:00
|
|
|
}
|
|
|
|
|
2015-11-04 00:28:47 +01:00
|
|
|
if d.HasChange("child_healthchecks") {
|
|
|
|
updateHealthCheck.ChildHealthChecks = expandStringList(d.Get("child_healthchecks").(*schema.Set).List())
|
|
|
|
|
|
|
|
}
|
|
|
|
if d.HasChange("child_health_threshold") {
|
|
|
|
updateHealthCheck.HealthThreshold = aws.Int64(int64(d.Get("child_health_threshold").(int)))
|
|
|
|
}
|
|
|
|
|
2015-04-28 18:11:38 +02:00
|
|
|
_, err := conn.UpdateHealthCheck(updateHealthCheck)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := setTagsR53(conn, d, "healthcheck"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceAwsRoute53HealthCheckRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsRoute53HealthCheckCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).r53conn
|
|
|
|
|
|
|
|
healthConfig := &route53.HealthCheckConfig{
|
2015-11-04 00:28:47 +01:00
|
|
|
Type: aws.String(d.Get("type").(string)),
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("request_interval"); ok {
|
|
|
|
healthConfig.RequestInterval = aws.Int64(int64(v.(int)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("failure_threshold"); ok {
|
|
|
|
healthConfig.FailureThreshold = aws.Int64(int64(v.(int)))
|
2015-04-28 18:11:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("fqdn"); ok {
|
|
|
|
healthConfig.FullyQualifiedDomainName = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("search_string"); ok {
|
|
|
|
healthConfig.SearchString = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("ip_address"); ok {
|
|
|
|
healthConfig.IPAddress = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("port"); ok {
|
2015-07-28 22:29:46 +02:00
|
|
|
healthConfig.Port = aws.Int64(int64(v.(int)))
|
2015-04-28 18:11:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("resource_path"); ok {
|
|
|
|
healthConfig.ResourcePath = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
|
2015-10-29 20:50:02 +01:00
|
|
|
if *healthConfig.Type != route53.HealthCheckTypeCalculated {
|
|
|
|
if v, ok := d.GetOk("measure_latency"); ok {
|
|
|
|
healthConfig.MeasureLatency = aws.Bool(v.(bool))
|
|
|
|
}
|
2015-10-29 18:27:50 +01:00
|
|
|
}
|
|
|
|
|
2015-11-03 23:34:06 +01:00
|
|
|
if v, ok := d.GetOk("invert_healthcheck"); ok {
|
|
|
|
healthConfig.Inverted = aws.Bool(v.(bool))
|
|
|
|
}
|
|
|
|
|
|
|
|
if *healthConfig.Type == route53.HealthCheckTypeCalculated {
|
|
|
|
if v, ok := d.GetOk("child_healthchecks"); ok {
|
|
|
|
healthConfig.ChildHealthChecks = expandStringList(v.(*schema.Set).List())
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("child_health_threshold"); ok {
|
|
|
|
healthConfig.HealthThreshold = aws.Int64(int64(v.(int)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-28 18:11:38 +02:00
|
|
|
input := &route53.CreateHealthCheckInput{
|
|
|
|
CallerReference: aws.String(time.Now().Format(time.RFC3339Nano)),
|
|
|
|
HealthCheckConfig: healthConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := conn.CreateHealthCheck(input)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
d.SetId(*resp.HealthCheck.Id)
|
2015-04-28 18:11:38 +02:00
|
|
|
|
|
|
|
if err := setTagsR53(conn, d, "healthcheck"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceAwsRoute53HealthCheckRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsRoute53HealthCheckRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).r53conn
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
read, err := conn.GetHealthCheck(&route53.GetHealthCheckInput{HealthCheckId: aws.String(d.Id())})
|
2015-04-28 18:11:38 +02:00
|
|
|
if err != nil {
|
|
|
|
if r53err, ok := err.(awserr.Error); ok && r53err.Code() == "NoSuchHealthCheck" {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if read == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
updated := read.HealthCheck.HealthCheckConfig
|
|
|
|
d.Set("type", updated.Type)
|
|
|
|
d.Set("failure_threshold", updated.FailureThreshold)
|
|
|
|
d.Set("request_interval", updated.RequestInterval)
|
|
|
|
d.Set("fqdn", updated.FullyQualifiedDomainName)
|
|
|
|
d.Set("search_string", updated.SearchString)
|
|
|
|
d.Set("ip_address", updated.IPAddress)
|
|
|
|
d.Set("port", updated.Port)
|
|
|
|
d.Set("resource_path", updated.ResourcePath)
|
2015-10-29 18:27:50 +01:00
|
|
|
d.Set("measure_latency", updated.MeasureLatency)
|
2016-04-21 22:48:48 +02:00
|
|
|
d.Set("invert_healthcheck", updated.Inverted)
|
2015-11-04 00:28:47 +01:00
|
|
|
d.Set("child_healthchecks", updated.ChildHealthChecks)
|
|
|
|
d.Set("child_health_threshold", updated.HealthThreshold)
|
2015-04-28 18:11:38 +02:00
|
|
|
|
|
|
|
// read the tags
|
|
|
|
req := &route53.ListTagsForResourceInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
ResourceId: aws.String(d.Id()),
|
2015-04-28 18:11:38 +02:00
|
|
|
ResourceType: aws.String("healthcheck"),
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := conn.ListTagsForResource(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var tags []*route53.Tag
|
|
|
|
if resp.ResourceTagSet != nil {
|
|
|
|
tags = resp.ResourceTagSet.Tags
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.Set("tags", tagsToMapR53(tags)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsRoute53HealthCheckDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).r53conn
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Deleteing Route53 health check: %s", d.Id())
|
2015-08-17 20:27:16 +02:00
|
|
|
_, err := conn.DeleteHealthCheck(&route53.DeleteHealthCheckInput{HealthCheckId: aws.String(d.Id())})
|
2015-04-28 18:11:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-03 23:34:06 +01:00
|
|
|
|
|
|
|
func createChildHealthCheckList(s *schema.Set) (nl []*string) {
|
|
|
|
l := s.List()
|
|
|
|
for _, n := range l {
|
|
|
|
nl = append(nl, aws.String(n.(string)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nl
|
|
|
|
}
|