github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/aws/resource_aws_route53_health_check.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 "time" 8 9 "github.com/hashicorp/terraform/helper/schema" 10 11 "github.com/aws/aws-sdk-go/aws" 12 "github.com/aws/aws-sdk-go/aws/awserr" 13 "github.com/aws/aws-sdk-go/service/route53" 14 ) 15 16 func resourceAwsRoute53HealthCheck() *schema.Resource { 17 return &schema.Resource{ 18 Create: resourceAwsRoute53HealthCheckCreate, 19 Read: resourceAwsRoute53HealthCheckRead, 20 Update: resourceAwsRoute53HealthCheckUpdate, 21 Delete: resourceAwsRoute53HealthCheckDelete, 22 23 Schema: map[string]*schema.Schema{ 24 "type": &schema.Schema{ 25 Type: schema.TypeString, 26 Required: true, 27 ForceNew: true, 28 StateFunc: func(val interface{}) string { 29 return strings.ToUpper(val.(string)) 30 }, 31 }, 32 "failure_threshold": &schema.Schema{ 33 Type: schema.TypeInt, 34 Optional: true, 35 }, 36 "request_interval": &schema.Schema{ 37 Type: schema.TypeInt, 38 Optional: true, 39 ForceNew: true, // todo this should be updateable but the awslabs route53 service doesnt have the ability 40 }, 41 "ip_address": &schema.Schema{ 42 Type: schema.TypeString, 43 Optional: true, 44 ForceNew: true, 45 }, 46 "fqdn": &schema.Schema{ 47 Type: schema.TypeString, 48 Optional: true, 49 }, 50 "port": &schema.Schema{ 51 Type: schema.TypeInt, 52 Optional: true, 53 }, 54 55 "invert_healthcheck": &schema.Schema{ 56 Type: schema.TypeBool, 57 Optional: true, 58 }, 59 60 "resource_path": &schema.Schema{ 61 Type: schema.TypeString, 62 Optional: true, 63 }, 64 65 "search_string": &schema.Schema{ 66 Type: schema.TypeString, 67 Optional: true, 68 }, 69 "measure_latency": &schema.Schema{ 70 Type: schema.TypeBool, 71 Optional: true, 72 Default: false, 73 ForceNew: true, 74 }, 75 76 "child_healthchecks": &schema.Schema{ 77 Type: schema.TypeSet, 78 Elem: &schema.Schema{Type: schema.TypeString}, 79 Optional: true, 80 Set: schema.HashString, 81 }, 82 "child_health_threshold": &schema.Schema{ 83 Type: schema.TypeInt, 84 Optional: true, 85 ValidateFunc: func(v interface{}, k string) (ws []string, es []error) { 86 value := v.(int) 87 if value > 256 { 88 es = append(es, fmt.Errorf( 89 "Child HealthThreshold cannot be more than 256")) 90 } 91 return 92 }, 93 }, 94 95 "tags": tagsSchema(), 96 }, 97 } 98 } 99 100 func resourceAwsRoute53HealthCheckUpdate(d *schema.ResourceData, meta interface{}) error { 101 conn := meta.(*AWSClient).r53conn 102 103 updateHealthCheck := &route53.UpdateHealthCheckInput{ 104 HealthCheckId: aws.String(d.Id()), 105 } 106 107 if d.HasChange("failure_threshold") { 108 updateHealthCheck.FailureThreshold = aws.Int64(int64(d.Get("failure_threshold").(int))) 109 } 110 111 if d.HasChange("fqdn") { 112 updateHealthCheck.FullyQualifiedDomainName = aws.String(d.Get("fqdn").(string)) 113 } 114 115 if d.HasChange("port") { 116 updateHealthCheck.Port = aws.Int64(int64(d.Get("port").(int))) 117 } 118 119 if d.HasChange("resource_path") { 120 updateHealthCheck.ResourcePath = aws.String(d.Get("resource_path").(string)) 121 } 122 123 if d.HasChange("invert_healthcheck") { 124 updateHealthCheck.Inverted = aws.Bool(d.Get("invert_healthcheck").(bool)) 125 } 126 127 if d.HasChange("child_healthchecks") { 128 updateHealthCheck.ChildHealthChecks = expandStringList(d.Get("child_healthchecks").(*schema.Set).List()) 129 130 } 131 if d.HasChange("child_health_threshold") { 132 updateHealthCheck.HealthThreshold = aws.Int64(int64(d.Get("child_health_threshold").(int))) 133 } 134 135 _, err := conn.UpdateHealthCheck(updateHealthCheck) 136 if err != nil { 137 return err 138 } 139 140 if err := setTagsR53(conn, d, "healthcheck"); err != nil { 141 return err 142 } 143 144 return resourceAwsRoute53HealthCheckRead(d, meta) 145 } 146 147 func resourceAwsRoute53HealthCheckCreate(d *schema.ResourceData, meta interface{}) error { 148 conn := meta.(*AWSClient).r53conn 149 150 healthConfig := &route53.HealthCheckConfig{ 151 Type: aws.String(d.Get("type").(string)), 152 } 153 154 if v, ok := d.GetOk("request_interval"); ok { 155 healthConfig.RequestInterval = aws.Int64(int64(v.(int))) 156 } 157 158 if v, ok := d.GetOk("failure_threshold"); ok { 159 healthConfig.FailureThreshold = aws.Int64(int64(v.(int))) 160 } 161 162 if v, ok := d.GetOk("fqdn"); ok { 163 healthConfig.FullyQualifiedDomainName = aws.String(v.(string)) 164 } 165 166 if v, ok := d.GetOk("search_string"); ok { 167 healthConfig.SearchString = aws.String(v.(string)) 168 } 169 170 if v, ok := d.GetOk("ip_address"); ok { 171 healthConfig.IPAddress = aws.String(v.(string)) 172 } 173 174 if v, ok := d.GetOk("port"); ok { 175 healthConfig.Port = aws.Int64(int64(v.(int))) 176 } 177 178 if v, ok := d.GetOk("resource_path"); ok { 179 healthConfig.ResourcePath = aws.String(v.(string)) 180 } 181 182 if *healthConfig.Type != route53.HealthCheckTypeCalculated { 183 if v, ok := d.GetOk("measure_latency"); ok { 184 healthConfig.MeasureLatency = aws.Bool(v.(bool)) 185 } 186 } 187 188 if v, ok := d.GetOk("invert_healthcheck"); ok { 189 healthConfig.Inverted = aws.Bool(v.(bool)) 190 } 191 192 if *healthConfig.Type == route53.HealthCheckTypeCalculated { 193 if v, ok := d.GetOk("child_healthchecks"); ok { 194 healthConfig.ChildHealthChecks = expandStringList(v.(*schema.Set).List()) 195 } 196 197 if v, ok := d.GetOk("child_health_threshold"); ok { 198 healthConfig.HealthThreshold = aws.Int64(int64(v.(int))) 199 } 200 } 201 202 input := &route53.CreateHealthCheckInput{ 203 CallerReference: aws.String(time.Now().Format(time.RFC3339Nano)), 204 HealthCheckConfig: healthConfig, 205 } 206 207 resp, err := conn.CreateHealthCheck(input) 208 209 if err != nil { 210 return err 211 } 212 213 d.SetId(*resp.HealthCheck.Id) 214 215 if err := setTagsR53(conn, d, "healthcheck"); err != nil { 216 return err 217 } 218 219 return resourceAwsRoute53HealthCheckRead(d, meta) 220 } 221 222 func resourceAwsRoute53HealthCheckRead(d *schema.ResourceData, meta interface{}) error { 223 conn := meta.(*AWSClient).r53conn 224 225 read, err := conn.GetHealthCheck(&route53.GetHealthCheckInput{HealthCheckId: aws.String(d.Id())}) 226 if err != nil { 227 if r53err, ok := err.(awserr.Error); ok && r53err.Code() == "NoSuchHealthCheck" { 228 d.SetId("") 229 return nil 230 231 } 232 return err 233 } 234 235 if read == nil { 236 return nil 237 } 238 239 updated := read.HealthCheck.HealthCheckConfig 240 d.Set("type", updated.Type) 241 d.Set("failure_threshold", updated.FailureThreshold) 242 d.Set("request_interval", updated.RequestInterval) 243 d.Set("fqdn", updated.FullyQualifiedDomainName) 244 d.Set("search_string", updated.SearchString) 245 d.Set("ip_address", updated.IPAddress) 246 d.Set("port", updated.Port) 247 d.Set("resource_path", updated.ResourcePath) 248 d.Set("measure_latency", updated.MeasureLatency) 249 d.Set("invent_healthcheck", updated.Inverted) 250 d.Set("child_healthchecks", updated.ChildHealthChecks) 251 d.Set("child_health_threshold", updated.HealthThreshold) 252 253 // read the tags 254 req := &route53.ListTagsForResourceInput{ 255 ResourceId: aws.String(d.Id()), 256 ResourceType: aws.String("healthcheck"), 257 } 258 259 resp, err := conn.ListTagsForResource(req) 260 if err != nil { 261 return err 262 } 263 264 var tags []*route53.Tag 265 if resp.ResourceTagSet != nil { 266 tags = resp.ResourceTagSet.Tags 267 } 268 269 if err := d.Set("tags", tagsToMapR53(tags)); err != nil { 270 return err 271 } 272 273 return nil 274 } 275 276 func resourceAwsRoute53HealthCheckDelete(d *schema.ResourceData, meta interface{}) error { 277 conn := meta.(*AWSClient).r53conn 278 279 log.Printf("[DEBUG] Deleteing Route53 health check: %s", d.Id()) 280 _, err := conn.DeleteHealthCheck(&route53.DeleteHealthCheckInput{HealthCheckId: aws.String(d.Id())}) 281 if err != nil { 282 return err 283 } 284 285 return nil 286 } 287 288 func createChildHealthCheckList(s *schema.Set) (nl []*string) { 289 l := s.List() 290 for _, n := range l { 291 nl = append(nl, aws.String(n.(string))) 292 } 293 294 return nl 295 }