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