github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/google/resource_compute_health_check.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 "google.golang.org/api/compute/v1" 9 ) 10 11 func resourceComputeHealthCheck() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceComputeHealthCheckCreate, 14 Read: resourceComputeHealthCheckRead, 15 Delete: resourceComputeHealthCheckDelete, 16 Update: resourceComputeHealthCheckUpdate, 17 Importer: &schema.ResourceImporter{ 18 State: schema.ImportStatePassthrough, 19 }, 20 21 Schema: map[string]*schema.Schema{ 22 "name": &schema.Schema{ 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: true, 26 }, 27 28 "check_interval_sec": &schema.Schema{ 29 Type: schema.TypeInt, 30 Optional: true, 31 Default: 5, 32 }, 33 34 "description": &schema.Schema{ 35 Type: schema.TypeString, 36 Optional: true, 37 }, 38 39 "healthy_threshold": &schema.Schema{ 40 Type: schema.TypeInt, 41 Optional: true, 42 Default: 2, 43 }, 44 45 "tcp_health_check": &schema.Schema{ 46 Type: schema.TypeList, 47 Optional: true, 48 MaxItems: 1, 49 ConflictsWith: []string{"ssl_health_check", "http_health_check", "https_health_check"}, 50 Elem: &schema.Resource{ 51 Schema: map[string]*schema.Schema{ 52 "port": &schema.Schema{ 53 Type: schema.TypeInt, 54 Optional: true, 55 Default: 80, 56 }, 57 "proxy_header": &schema.Schema{ 58 Type: schema.TypeString, 59 Optional: true, 60 Default: "NONE", 61 }, 62 "request": &schema.Schema{ 63 Type: schema.TypeString, 64 Optional: true, 65 }, 66 "response": &schema.Schema{ 67 Type: schema.TypeString, 68 Optional: true, 69 }, 70 }, 71 }, 72 }, 73 74 "ssl_health_check": &schema.Schema{ 75 Type: schema.TypeList, 76 Optional: true, 77 MaxItems: 1, 78 ConflictsWith: []string{"tcp_health_check", "http_health_check", "https_health_check"}, 79 Elem: &schema.Resource{ 80 Schema: map[string]*schema.Schema{ 81 "port": &schema.Schema{ 82 Type: schema.TypeInt, 83 Optional: true, 84 Default: 443, 85 }, 86 "proxy_header": &schema.Schema{ 87 Type: schema.TypeString, 88 Optional: true, 89 Default: "NONE", 90 }, 91 "request": &schema.Schema{ 92 Type: schema.TypeString, 93 Optional: true, 94 }, 95 "response": &schema.Schema{ 96 Type: schema.TypeString, 97 Optional: true, 98 }, 99 }, 100 }, 101 }, 102 103 "http_health_check": &schema.Schema{ 104 Type: schema.TypeList, 105 Optional: true, 106 MaxItems: 1, 107 ConflictsWith: []string{"tcp_health_check", "ssl_health_check", "https_health_check"}, 108 Elem: &schema.Resource{ 109 Schema: map[string]*schema.Schema{ 110 "host": &schema.Schema{ 111 Type: schema.TypeString, 112 Optional: true, 113 }, 114 "port": &schema.Schema{ 115 Type: schema.TypeInt, 116 Optional: true, 117 Default: 80, 118 }, 119 "proxy_header": &schema.Schema{ 120 Type: schema.TypeString, 121 Optional: true, 122 Default: "NONE", 123 }, 124 "request_path": &schema.Schema{ 125 Type: schema.TypeString, 126 Optional: true, 127 Default: "/", 128 }, 129 }, 130 }, 131 }, 132 133 "https_health_check": &schema.Schema{ 134 Type: schema.TypeList, 135 Optional: true, 136 MaxItems: 1, 137 ConflictsWith: []string{"tcp_health_check", "ssl_health_check", "http_health_check"}, 138 Elem: &schema.Resource{ 139 Schema: map[string]*schema.Schema{ 140 "host": &schema.Schema{ 141 Type: schema.TypeString, 142 Optional: true, 143 }, 144 "port": &schema.Schema{ 145 Type: schema.TypeInt, 146 Optional: true, 147 Default: 443, 148 }, 149 "proxy_header": &schema.Schema{ 150 Type: schema.TypeString, 151 Optional: true, 152 Default: "NONE", 153 }, 154 "request_path": &schema.Schema{ 155 Type: schema.TypeString, 156 Optional: true, 157 Default: "/", 158 }, 159 }, 160 }, 161 }, 162 163 "project": &schema.Schema{ 164 Type: schema.TypeString, 165 Optional: true, 166 ForceNew: true, 167 Computed: true, 168 }, 169 170 "self_link": &schema.Schema{ 171 Type: schema.TypeString, 172 Computed: true, 173 }, 174 175 "timeout_sec": &schema.Schema{ 176 Type: schema.TypeInt, 177 Optional: true, 178 Default: 5, 179 }, 180 181 "unhealthy_threshold": &schema.Schema{ 182 Type: schema.TypeInt, 183 Optional: true, 184 Default: 2, 185 }, 186 }, 187 } 188 } 189 190 func resourceComputeHealthCheckCreate(d *schema.ResourceData, meta interface{}) error { 191 config := meta.(*Config) 192 193 project, err := getProject(d, config) 194 if err != nil { 195 return err 196 } 197 198 // Build the parameter 199 hchk := &compute.HealthCheck{ 200 Name: d.Get("name").(string), 201 } 202 // Optional things 203 if v, ok := d.GetOk("description"); ok { 204 hchk.Description = v.(string) 205 } 206 if v, ok := d.GetOk("check_interval_sec"); ok { 207 hchk.CheckIntervalSec = int64(v.(int)) 208 } 209 if v, ok := d.GetOk("healthy_threshold"); ok { 210 hchk.HealthyThreshold = int64(v.(int)) 211 } 212 if v, ok := d.GetOk("timeout_sec"); ok { 213 hchk.TimeoutSec = int64(v.(int)) 214 } 215 if v, ok := d.GetOk("unhealthy_threshold"); ok { 216 hchk.UnhealthyThreshold = int64(v.(int)) 217 } 218 219 if v, ok := d.GetOk("tcp_health_check"); ok { 220 hchk.Type = "TCP" 221 tcpcheck := v.([]interface{})[0].(map[string]interface{}) 222 tcpHealthCheck := &compute.TCPHealthCheck{} 223 if val, ok := tcpcheck["port"]; ok { 224 tcpHealthCheck.Port = int64(val.(int)) 225 } 226 if val, ok := tcpcheck["proxy_header"]; ok { 227 tcpHealthCheck.ProxyHeader = val.(string) 228 } 229 if val, ok := tcpcheck["request"]; ok { 230 tcpHealthCheck.Request = val.(string) 231 } 232 if val, ok := tcpcheck["response"]; ok { 233 tcpHealthCheck.Response = val.(string) 234 } 235 hchk.TcpHealthCheck = tcpHealthCheck 236 } 237 238 if v, ok := d.GetOk("ssl_health_check"); ok { 239 hchk.Type = "SSL" 240 sslcheck := v.([]interface{})[0].(map[string]interface{}) 241 sslHealthCheck := &compute.SSLHealthCheck{} 242 if val, ok := sslcheck["port"]; ok { 243 sslHealthCheck.Port = int64(val.(int)) 244 } 245 if val, ok := sslcheck["proxy_header"]; ok { 246 sslHealthCheck.ProxyHeader = val.(string) 247 } 248 if val, ok := sslcheck["request"]; ok { 249 sslHealthCheck.Request = val.(string) 250 } 251 if val, ok := sslcheck["response"]; ok { 252 sslHealthCheck.Response = val.(string) 253 } 254 hchk.SslHealthCheck = sslHealthCheck 255 } 256 257 if v, ok := d.GetOk("http_health_check"); ok { 258 hchk.Type = "HTTP" 259 httpcheck := v.([]interface{})[0].(map[string]interface{}) 260 httpHealthCheck := &compute.HTTPHealthCheck{} 261 if val, ok := httpcheck["host"]; ok { 262 httpHealthCheck.Host = val.(string) 263 } 264 if val, ok := httpcheck["port"]; ok { 265 httpHealthCheck.Port = int64(val.(int)) 266 } 267 if val, ok := httpcheck["proxy_header"]; ok { 268 httpHealthCheck.ProxyHeader = val.(string) 269 } 270 if val, ok := httpcheck["request_path"]; ok { 271 httpHealthCheck.RequestPath = val.(string) 272 } 273 hchk.HttpHealthCheck = httpHealthCheck 274 } 275 276 if v, ok := d.GetOk("https_health_check"); ok { 277 hchk.Type = "HTTPS" 278 httpscheck := v.([]interface{})[0].(map[string]interface{}) 279 httpsHealthCheck := &compute.HTTPSHealthCheck{} 280 if val, ok := httpscheck["host"]; ok { 281 httpsHealthCheck.Host = val.(string) 282 } 283 if val, ok := httpscheck["port"]; ok { 284 httpsHealthCheck.Port = int64(val.(int)) 285 } 286 if val, ok := httpscheck["proxy_header"]; ok { 287 httpsHealthCheck.ProxyHeader = val.(string) 288 } 289 if val, ok := httpscheck["request_path"]; ok { 290 httpsHealthCheck.RequestPath = val.(string) 291 } 292 hchk.HttpsHealthCheck = httpsHealthCheck 293 } 294 295 log.Printf("[DEBUG] HealthCheck insert request: %#v", hchk) 296 op, err := config.clientCompute.HealthChecks.Insert( 297 project, hchk).Do() 298 if err != nil { 299 return fmt.Errorf("Error creating HealthCheck: %s", err) 300 } 301 302 // It probably maybe worked, so store the ID now 303 d.SetId(hchk.Name) 304 305 err = computeOperationWaitGlobal(config, op, project, "Creating Health Check") 306 if err != nil { 307 return err 308 } 309 310 return resourceComputeHealthCheckRead(d, meta) 311 } 312 313 func resourceComputeHealthCheckUpdate(d *schema.ResourceData, meta interface{}) error { 314 config := meta.(*Config) 315 316 project, err := getProject(d, config) 317 if err != nil { 318 return err 319 } 320 321 // Build the parameter 322 hchk := &compute.HealthCheck{ 323 Name: d.Get("name").(string), 324 } 325 // Optional things 326 if v, ok := d.GetOk("description"); ok { 327 hchk.Description = v.(string) 328 } 329 if v, ok := d.GetOk("check_interval_sec"); ok { 330 hchk.CheckIntervalSec = int64(v.(int)) 331 } 332 if v, ok := d.GetOk("healthy_threshold"); ok { 333 hchk.HealthyThreshold = int64(v.(int)) 334 } 335 if v, ok := d.GetOk("timeout_sec"); ok { 336 hchk.TimeoutSec = int64(v.(int)) 337 } 338 if v, ok := d.GetOk("unhealthy_threshold"); ok { 339 hchk.UnhealthyThreshold = int64(v.(int)) 340 } 341 if v, ok := d.GetOk("tcp_health_check"); ok { 342 hchk.Type = "TCP" 343 tcpcheck := v.([]interface{})[0].(map[string]interface{}) 344 tcpHealthCheck := &compute.TCPHealthCheck{} 345 if val, ok := tcpcheck["port"]; ok { 346 tcpHealthCheck.Port = int64(val.(int)) 347 } 348 if val, ok := tcpcheck["proxy_header"]; ok { 349 tcpHealthCheck.ProxyHeader = val.(string) 350 } 351 if val, ok := tcpcheck["request"]; ok { 352 tcpHealthCheck.Request = val.(string) 353 } 354 if val, ok := tcpcheck["response"]; ok { 355 tcpHealthCheck.Response = val.(string) 356 } 357 hchk.TcpHealthCheck = tcpHealthCheck 358 } 359 if v, ok := d.GetOk("ssl_health_check"); ok { 360 hchk.Type = "SSL" 361 sslcheck := v.([]interface{})[0].(map[string]interface{}) 362 sslHealthCheck := &compute.SSLHealthCheck{} 363 if val, ok := sslcheck["port"]; ok { 364 sslHealthCheck.Port = int64(val.(int)) 365 } 366 if val, ok := sslcheck["proxy_header"]; ok { 367 sslHealthCheck.ProxyHeader = val.(string) 368 } 369 if val, ok := sslcheck["request"]; ok { 370 sslHealthCheck.Request = val.(string) 371 } 372 if val, ok := sslcheck["response"]; ok { 373 sslHealthCheck.Response = val.(string) 374 } 375 hchk.SslHealthCheck = sslHealthCheck 376 } 377 if v, ok := d.GetOk("http_health_check"); ok { 378 hchk.Type = "HTTP" 379 httpcheck := v.([]interface{})[0].(map[string]interface{}) 380 httpHealthCheck := &compute.HTTPHealthCheck{} 381 if val, ok := httpcheck["host"]; ok { 382 httpHealthCheck.Host = val.(string) 383 } 384 if val, ok := httpcheck["port"]; ok { 385 httpHealthCheck.Port = int64(val.(int)) 386 } 387 if val, ok := httpcheck["proxy_header"]; ok { 388 httpHealthCheck.ProxyHeader = val.(string) 389 } 390 if val, ok := httpcheck["request_path"]; ok { 391 httpHealthCheck.RequestPath = val.(string) 392 } 393 hchk.HttpHealthCheck = httpHealthCheck 394 } 395 396 if v, ok := d.GetOk("https_health_check"); ok { 397 hchk.Type = "HTTPS" 398 httpscheck := v.([]interface{})[0].(map[string]interface{}) 399 httpsHealthCheck := &compute.HTTPSHealthCheck{} 400 if val, ok := httpscheck["host"]; ok { 401 httpsHealthCheck.Host = val.(string) 402 } 403 if val, ok := httpscheck["port"]; ok { 404 httpsHealthCheck.Port = int64(val.(int)) 405 } 406 if val, ok := httpscheck["proxy_header"]; ok { 407 httpsHealthCheck.ProxyHeader = val.(string) 408 } 409 if val, ok := httpscheck["request_path"]; ok { 410 httpsHealthCheck.RequestPath = val.(string) 411 } 412 hchk.HttpsHealthCheck = httpsHealthCheck 413 } 414 415 log.Printf("[DEBUG] HealthCheck patch request: %#v", hchk) 416 op, err := config.clientCompute.HealthChecks.Patch( 417 project, hchk.Name, hchk).Do() 418 if err != nil { 419 return fmt.Errorf("Error patching HealthCheck: %s", err) 420 } 421 422 // It probably maybe worked, so store the ID now 423 d.SetId(hchk.Name) 424 425 err = computeOperationWaitGlobal(config, op, project, "Updating Health Check") 426 if err != nil { 427 return err 428 } 429 430 return resourceComputeHealthCheckRead(d, meta) 431 } 432 433 func resourceComputeHealthCheckRead(d *schema.ResourceData, meta interface{}) error { 434 config := meta.(*Config) 435 436 project, err := getProject(d, config) 437 if err != nil { 438 return err 439 } 440 441 hchk, err := config.clientCompute.HealthChecks.Get( 442 project, d.Id()).Do() 443 if err != nil { 444 return handleNotFoundError(err, d, fmt.Sprintf("Health Check %q", d.Get("name").(string))) 445 } 446 447 d.Set("check_interval_sec", hchk.CheckIntervalSec) 448 d.Set("healthy_threshold", hchk.HealthyThreshold) 449 d.Set("timeout_sec", hchk.TimeoutSec) 450 d.Set("unhealthy_threshold", hchk.UnhealthyThreshold) 451 d.Set("tcp_health_check", hchk.TcpHealthCheck) 452 d.Set("ssl_health_check", hchk.SslHealthCheck) 453 d.Set("http_health_check", hchk.HttpHealthCheck) 454 d.Set("https_health_check", hchk.HttpsHealthCheck) 455 d.Set("self_link", hchk.SelfLink) 456 d.Set("name", hchk.Name) 457 d.Set("description", hchk.Description) 458 d.Set("project", project) 459 460 return nil 461 } 462 463 func resourceComputeHealthCheckDelete(d *schema.ResourceData, meta interface{}) error { 464 config := meta.(*Config) 465 466 project, err := getProject(d, config) 467 if err != nil { 468 return err 469 } 470 471 // Delete the HealthCheck 472 op, err := config.clientCompute.HealthChecks.Delete( 473 project, d.Id()).Do() 474 if err != nil { 475 return fmt.Errorf("Error deleting HealthCheck: %s", err) 476 } 477 478 err = computeOperationWaitGlobal(config, op, project, "Deleting Health Check") 479 if err != nil { 480 return err 481 } 482 483 d.SetId("") 484 return nil 485 }