Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
Initial commit adding the aws route 53 health check.
Browse files Browse the repository at this point in the history
  • Loading branch information
Trent Johnson committed Apr 28, 2015
1 parent 1ef9731 commit a8f4bbe
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
1 change: 1 addition & 0 deletions builtin/providers/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func Provider() terraform.ResourceProvider {
"aws_network_interface": resourceAwsNetworkInterface(),
"aws_route53_record": resourceAwsRoute53Record(),
"aws_route53_zone": resourceAwsRoute53Zone(),
"aws_route53_health_check": resourceAwsRoute53HealthCheck(),
"aws_route_table": resourceAwsRouteTable(),
"aws_route_table_association": resourceAwsRouteTableAssociation(),
"aws_s3_bucket": resourceAwsS3Bucket(),
Expand Down
190 changes: 190 additions & 0 deletions builtin/providers/aws/resource_aws_route53_health_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package aws

import (
// "fmt"
"log"
// "strings"
"time"

// "github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"

"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/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, // can be {HTTP | HTTPS | HTTP_STR_MATCH | HTTPS_STR_MATCH | TCP}
Required: true,
ForceNew: true,
},
"failure_threshold": &schema.Schema{
Type: schema.TypeInt, // Valid Ints 1 - 10
Required: true,
},
"request_interval": &schema.Schema{
Type: schema.TypeInt, // valid values { 10 | 30 }
Required: true,
ForceNew: true, // todo this should be updateable but the awslabs route53 service doesnt have the ability
},
"ip_address": &schema.Schema{ // if not supplied it will send a health check to FullyQualifiedDomainName
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"fully_qualified_domain_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"port": &schema.Schema{ // required for any type other than TCP
Type: schema.TypeInt,
Optional: true,
},

"resource_path": &schema.Schema{ // must start with a '/'
Type: schema.TypeString, // required for everything except TCP
Optional: true,
},
"search_string": &schema.Schema{ // only used for *_STR_MATCH
Type: schema.TypeString,
Optional: true,
},
},
}
}

func resourceAwsRoute53HealthCheckUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).r53conn

updateHealthCheck := &route53.UpdateHealthCheckInput{
HealthCheckID: aws.String(d.Id()),
}

if d.HasChange("failure_threshold") {
updateHealthCheck.FailureThreshold = aws.Long(int64(d.Get("failure_threshold").(int)))
}

if d.HasChange("fully_qualified_domain_name") {
updateHealthCheck.FullyQualifiedDomainName = aws.String(d.Get("fully_qualified_domain_name").(string))
}

if d.HasChange("port") {
updateHealthCheck.Port = aws.Long(int64(d.Get("port").(int)))
}

if d.HasChange("resource_path") {
updateHealthCheck.ResourcePath = aws.String(d.Get("resource_path").(string))
}

if d.HasChange("search_string") {
updateHealthCheck.SearchString = aws.String(d.Get("search_string").(string))
}

_, err := conn.UpdateHealthCheck(updateHealthCheck)
if err != nil {
return err
}

return resourceAwsRoute53HealthCheckRead(d, meta)
}

func resourceAwsRoute53HealthCheckCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).r53conn

// do we need to check if the optional fields existf before adding them?
healthConfig := &route53.HealthCheckConfig{
Type: aws.String(d.Get("type").(string)),
FailureThreshold: aws.Long(int64(d.Get("failure_threshold").(int))),
RequestInterval: aws.Long(int64(d.Get("request_interval").(int))),
}

if v, ok := d.GetOk("fully_qualified_domain_name"); 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 {
healthConfig.Port = aws.Long(int64(v.(int)))
}

if v, ok := d.GetOk("resource_path"); ok {
healthConfig.ResourcePath = aws.String(v.(string))
}

input := &route53.CreateHealthCheckInput{
CallerReference: aws.String(time.Now().Format(time.RFC3339Nano)),
HealthCheckConfig: healthConfig,
}

wait := resource.StateChangeConf{
Pending: []string{"rejected"},
Target: "accepted",
Timeout: 5 * time.Minute,
MinTimeout: 1 * time.Second,
Refresh: func() (interface{}, string, error) {
resp, err := conn.CreateHealthCheck(input)
if err != nil {
return nil, "failure", err
}
return resp, "accepted", nil
},
}

// we get the health check itself and the url of the new health check back
respRaw, err := wait.WaitForState()
if err != nil {
return err
}

resp := respRaw.(*route53.CreateHealthCheckOutput)
d.SetId(*resp.HealthCheck.ID) // set the id of the health check before calling the read to confirm the rest of the

return resourceAwsRoute53HealthCheckRead(d, meta)
}

func resourceAwsRoute53HealthCheckRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).r53conn

check, err := conn.GetHealthCheck(&route53.GetHealthCheckInput{HealthCheckID: aws.String(d.Id())})
if err != nil {
if r53err, ok := err.(aws.APIError); ok && r53err.Code == "NoSuchHealthCheck" {
d.SetId("")
return nil

}
return err
}

// todo update the internal values based on what is coming back from the get

log.Printf("[INFO] Check coming back from amazon %s", check)
return nil
}

func resourceAwsRoute53HealthCheckDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).r53conn

log.Printf("[DEBUG] Deleteing Route53 health check: %s", d.Id())
_, err := conn.DeleteHealthCheck(&route53.DeleteHealthCheckInput{HealthCheckID: aws.String(d.Id())})
if err != nil {
return err
}

return nil
}

2 comments on commit a8f4bbe

@adamdecaf
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trentio have you seen hashicorp#28 (comment) ? It might be nice to let them know that it works / exists now!

@trentio
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamdecaf have some tests to write before that

Please sign in to comment.