github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_route53_health_check.go (about)

     1  package aws
     2  
     3  import (
     4  	"log"
     5  	"time"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/aws/awserr"
    11  	"github.com/aws/aws-sdk-go/service/route53"
    12  )
    13  
    14  func resourceAwsRoute53HealthCheck() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceAwsRoute53HealthCheckCreate,
    17  		Read:   resourceAwsRoute53HealthCheckRead,
    18  		Update: resourceAwsRoute53HealthCheckUpdate,
    19  		Delete: resourceAwsRoute53HealthCheckDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"type": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  			"failure_threshold": &schema.Schema{
    28  				Type:     schema.TypeInt,
    29  				Required: true,
    30  			},
    31  			"request_interval": &schema.Schema{
    32  				Type:     schema.TypeInt,
    33  				Required: true,
    34  				ForceNew: true, // todo this should be updateable but the awslabs route53 service doesnt have the ability
    35  			},
    36  			"ip_address": &schema.Schema{
    37  				Type:     schema.TypeString,
    38  				Optional: true,
    39  				ForceNew: true,
    40  			},
    41  			"fqdn": &schema.Schema{
    42  				Type:     schema.TypeString,
    43  				Optional: true,
    44  			},
    45  			"port": &schema.Schema{
    46  				Type:     schema.TypeInt,
    47  				Optional: true,
    48  			},
    49  
    50  			"resource_path": &schema.Schema{
    51  				Type:     schema.TypeString,
    52  				Optional: true,
    53  			},
    54  			"search_string": &schema.Schema{
    55  				Type:     schema.TypeString,
    56  				Optional: true,
    57  			},
    58  			"tags": tagsSchema(),
    59  		},
    60  	}
    61  }
    62  
    63  func resourceAwsRoute53HealthCheckUpdate(d *schema.ResourceData, meta interface{}) error {
    64  	conn := meta.(*AWSClient).r53conn
    65  
    66  	updateHealthCheck := &route53.UpdateHealthCheckInput{
    67  		HealthCheckId: aws.String(d.Id()),
    68  	}
    69  
    70  	if d.HasChange("failure_threshold") {
    71  		updateHealthCheck.FailureThreshold = aws.Int64(int64(d.Get("failure_threshold").(int)))
    72  	}
    73  
    74  	if d.HasChange("fqdn") {
    75  		updateHealthCheck.FullyQualifiedDomainName = aws.String(d.Get("fqdn").(string))
    76  	}
    77  
    78  	if d.HasChange("port") {
    79  		updateHealthCheck.Port = aws.Int64(int64(d.Get("port").(int)))
    80  	}
    81  
    82  	if d.HasChange("resource_path") {
    83  		updateHealthCheck.ResourcePath = aws.String(d.Get("resource_path").(string))
    84  	}
    85  
    86  	if d.HasChange("search_string") {
    87  		updateHealthCheck.SearchString = aws.String(d.Get("search_string").(string))
    88  	}
    89  
    90  	_, err := conn.UpdateHealthCheck(updateHealthCheck)
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	if err := setTagsR53(conn, d, "healthcheck"); err != nil {
    96  		return err
    97  	}
    98  
    99  	return resourceAwsRoute53HealthCheckRead(d, meta)
   100  }
   101  
   102  func resourceAwsRoute53HealthCheckCreate(d *schema.ResourceData, meta interface{}) error {
   103  	conn := meta.(*AWSClient).r53conn
   104  
   105  	healthConfig := &route53.HealthCheckConfig{
   106  		Type:             aws.String(d.Get("type").(string)),
   107  		FailureThreshold: aws.Int64(int64(d.Get("failure_threshold").(int))),
   108  		RequestInterval:  aws.Int64(int64(d.Get("request_interval").(int))),
   109  	}
   110  
   111  	if v, ok := d.GetOk("fqdn"); ok {
   112  		healthConfig.FullyQualifiedDomainName = aws.String(v.(string))
   113  	}
   114  
   115  	if v, ok := d.GetOk("search_string"); ok {
   116  		healthConfig.SearchString = aws.String(v.(string))
   117  	}
   118  
   119  	if v, ok := d.GetOk("ip_address"); ok {
   120  		healthConfig.IPAddress = aws.String(v.(string))
   121  	}
   122  
   123  	if v, ok := d.GetOk("port"); ok {
   124  		healthConfig.Port = aws.Int64(int64(v.(int)))
   125  	}
   126  
   127  	if v, ok := d.GetOk("resource_path"); ok {
   128  		healthConfig.ResourcePath = aws.String(v.(string))
   129  	}
   130  
   131  	input := &route53.CreateHealthCheckInput{
   132  		CallerReference:   aws.String(time.Now().Format(time.RFC3339Nano)),
   133  		HealthCheckConfig: healthConfig,
   134  	}
   135  
   136  	resp, err := conn.CreateHealthCheck(input)
   137  
   138  	if err != nil {
   139  		return err
   140  	}
   141  
   142  	d.SetId(*resp.HealthCheck.Id)
   143  
   144  	if err := setTagsR53(conn, d, "healthcheck"); err != nil {
   145  		return err
   146  	}
   147  
   148  	return resourceAwsRoute53HealthCheckRead(d, meta)
   149  }
   150  
   151  func resourceAwsRoute53HealthCheckRead(d *schema.ResourceData, meta interface{}) error {
   152  	conn := meta.(*AWSClient).r53conn
   153  
   154  	read, err := conn.GetHealthCheck(&route53.GetHealthCheckInput{HealthCheckId: aws.String(d.Id())})
   155  	if err != nil {
   156  		if r53err, ok := err.(awserr.Error); ok && r53err.Code() == "NoSuchHealthCheck" {
   157  			d.SetId("")
   158  			return nil
   159  
   160  		}
   161  		return err
   162  	}
   163  
   164  	if read == nil {
   165  		return nil
   166  	}
   167  
   168  	updated := read.HealthCheck.HealthCheckConfig
   169  	d.Set("type", updated.Type)
   170  	d.Set("failure_threshold", updated.FailureThreshold)
   171  	d.Set("request_interval", updated.RequestInterval)
   172  	d.Set("fqdn", updated.FullyQualifiedDomainName)
   173  	d.Set("search_string", updated.SearchString)
   174  	d.Set("ip_address", updated.IPAddress)
   175  	d.Set("port", updated.Port)
   176  	d.Set("resource_path", updated.ResourcePath)
   177  
   178  	// read the tags
   179  	req := &route53.ListTagsForResourceInput{
   180  		ResourceId:   aws.String(d.Id()),
   181  		ResourceType: aws.String("healthcheck"),
   182  	}
   183  
   184  	resp, err := conn.ListTagsForResource(req)
   185  	if err != nil {
   186  		return err
   187  	}
   188  
   189  	var tags []*route53.Tag
   190  	if resp.ResourceTagSet != nil {
   191  		tags = resp.ResourceTagSet.Tags
   192  	}
   193  
   194  	if err := d.Set("tags", tagsToMapR53(tags)); err != nil {
   195  		return err
   196  	}
   197  
   198  	return nil
   199  }
   200  
   201  func resourceAwsRoute53HealthCheckDelete(d *schema.ResourceData, meta interface{}) error {
   202  	conn := meta.(*AWSClient).r53conn
   203  
   204  	log.Printf("[DEBUG] Deleteing Route53 health check: %s", d.Id())
   205  	_, err := conn.DeleteHealthCheck(&route53.DeleteHealthCheckInput{HealthCheckId: aws.String(d.Id())})
   206  	if err != nil {
   207  		return err
   208  	}
   209  
   210  	return nil
   211  }