2015-08-20 21:52:30 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceComputeBackendService() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceComputeBackendServiceCreate,
|
|
|
|
Read: resourceComputeBackendServiceRead,
|
|
|
|
Update: resourceComputeBackendServiceUpdate,
|
|
|
|
Delete: resourceComputeBackendServiceDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-04-10 23:34:15 +02:00
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(string)
|
|
|
|
re := `^(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)$`
|
|
|
|
if !regexp.MustCompile(re).MatchString(value) {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q (%q) doesn't match regexp %q", k, value, re))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"health_checks": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Required: true,
|
|
|
|
Set: schema.HashString,
|
|
|
|
},
|
|
|
|
|
2015-08-20 21:52:30 +02:00
|
|
|
"backend": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-04-10 23:34:15 +02:00
|
|
|
"group": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2015-08-20 21:52:30 +02:00
|
|
|
"balancing_mode": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: "UTILIZATION",
|
|
|
|
},
|
|
|
|
"capacity_scaler": &schema.Schema{
|
|
|
|
Type: schema.TypeFloat,
|
|
|
|
Optional: true,
|
|
|
|
Default: 1,
|
|
|
|
},
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"max_rate": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"max_rate_per_instance": &schema.Schema{
|
|
|
|
Type: schema.TypeFloat,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"max_utilization": &schema.Schema{
|
|
|
|
Type: schema.TypeFloat,
|
|
|
|
Optional: true,
|
|
|
|
Default: 0.8,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Optional: true,
|
|
|
|
Set: resourceGoogleComputeBackendServiceBackendHash,
|
|
|
|
},
|
|
|
|
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2016-07-28 22:38:09 +02:00
|
|
|
"enable_cdn": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
2016-07-28 23:57:33 +02:00
|
|
|
Default: false,
|
2016-07-28 22:38:09 +02:00
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"fingerprint": &schema.Schema{
|
2015-08-20 21:52:30 +02:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Computed: true,
|
2015-08-20 21:52:30 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"port_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"project": &schema.Schema{
|
2015-08-20 21:52:30 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2016-04-10 23:34:15 +02:00
|
|
|
ForceNew: true,
|
2015-08-20 21:52:30 +02:00
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"protocol": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2015-08-20 21:52:30 +02:00
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"region": &schema.Schema{
|
2017-03-14 01:25:32 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2017-05-09 12:10:14 +02:00
|
|
|
Removed: "region has been removed as it was never used. For internal load balancing, use google_compute_region_backend_service",
|
2015-08-20 21:52:30 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"self_link": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-04-10 18:59:57 +02:00
|
|
|
|
2016-11-27 20:12:56 +01:00
|
|
|
"session_affinity": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"timeout_sec": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
2016-04-10 18:59:57 +02:00
|
|
|
Optional: true,
|
2016-04-10 23:34:15 +02:00
|
|
|
Computed: true,
|
2016-04-10 18:59:57 +02:00
|
|
|
},
|
2017-06-08 00:17:49 +02:00
|
|
|
|
|
|
|
"connection_draining_timeout_sec": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 0,
|
|
|
|
},
|
2015-08-20 21:52:30 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeBackendServiceCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
hc := d.Get("health_checks").(*schema.Set).List()
|
|
|
|
healthChecks := make([]string, 0, len(hc))
|
|
|
|
for _, v := range hc {
|
|
|
|
healthChecks = append(healthChecks, v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
service := compute.BackendService{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
HealthChecks: healthChecks,
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("backend"); ok {
|
|
|
|
service.Backends = expandBackends(v.(*schema.Set).List())
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
|
|
service.Description = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("port_name"); ok {
|
|
|
|
service.PortName = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("protocol"); ok {
|
|
|
|
service.Protocol = v.(string)
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:12:56 +01:00
|
|
|
if v, ok := d.GetOk("session_affinity"); ok {
|
|
|
|
service.SessionAffinity = v.(string)
|
|
|
|
}
|
|
|
|
|
2015-08-20 21:52:30 +02:00
|
|
|
if v, ok := d.GetOk("timeout_sec"); ok {
|
|
|
|
service.TimeoutSec = int64(v.(int))
|
|
|
|
}
|
|
|
|
|
2016-07-28 22:38:09 +02:00
|
|
|
if v, ok := d.GetOk("enable_cdn"); ok {
|
|
|
|
service.EnableCDN = v.(bool)
|
|
|
|
}
|
|
|
|
|
2017-06-08 00:17:49 +02:00
|
|
|
if v, ok := d.GetOk("connection_draining_timeout_sec"); ok {
|
|
|
|
connectionDraining := &compute.ConnectionDraining{
|
|
|
|
DrainingTimeoutSec: int64(v.(int)),
|
|
|
|
}
|
|
|
|
|
|
|
|
service.ConnectionDraining = connectionDraining
|
|
|
|
}
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-20 21:52:30 +02:00
|
|
|
log.Printf("[DEBUG] Creating new Backend Service: %#v", service)
|
|
|
|
op, err := config.clientCompute.BackendServices.Insert(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, &service).Do()
|
2015-08-20 21:52:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating backend service: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Waiting for new backend service, operation: %#v", op)
|
|
|
|
|
2017-05-02 23:01:05 +02:00
|
|
|
// Store the ID now
|
2015-08-20 21:52:30 +02:00
|
|
|
d.SetId(service.Name)
|
|
|
|
|
2017-05-02 23:01:05 +02:00
|
|
|
// Wait for the operation to complete
|
|
|
|
waitErr := computeOperationWaitGlobal(config, op, project, "Creating Backend Service")
|
|
|
|
if waitErr != nil {
|
|
|
|
// The resource didn't actually create
|
|
|
|
d.SetId("")
|
|
|
|
return waitErr
|
2015-08-20 21:52:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return resourceComputeBackendServiceRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-20 21:52:30 +02:00
|
|
|
service, err := config.clientCompute.BackendServices.Get(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Id()).Do()
|
2015-08-20 21:52:30 +02:00
|
|
|
if err != nil {
|
2017-05-10 01:00:47 +02:00
|
|
|
return handleNotFoundError(err, d, fmt.Sprintf("Backend Service %q", d.Get("name").(string)))
|
2015-08-20 21:52:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("description", service.Description)
|
2016-07-28 22:38:09 +02:00
|
|
|
d.Set("enable_cdn", service.EnableCDN)
|
2015-08-20 21:52:30 +02:00
|
|
|
d.Set("port_name", service.PortName)
|
|
|
|
d.Set("protocol", service.Protocol)
|
2016-11-27 20:12:56 +01:00
|
|
|
d.Set("session_affinity", service.SessionAffinity)
|
2015-08-20 21:52:30 +02:00
|
|
|
d.Set("timeout_sec", service.TimeoutSec)
|
|
|
|
d.Set("fingerprint", service.Fingerprint)
|
|
|
|
d.Set("self_link", service.SelfLink)
|
|
|
|
d.Set("backend", flattenBackends(service.Backends))
|
2017-06-08 00:17:49 +02:00
|
|
|
d.Set("connection_draining_timeout_sec", service.ConnectionDraining.DrainingTimeoutSec)
|
|
|
|
|
2015-08-20 21:52:30 +02:00
|
|
|
d.Set("health_checks", service.HealthChecks)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeBackendServiceUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-20 21:52:30 +02:00
|
|
|
hc := d.Get("health_checks").(*schema.Set).List()
|
|
|
|
healthChecks := make([]string, 0, len(hc))
|
|
|
|
for _, v := range hc {
|
|
|
|
healthChecks = append(healthChecks, v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
service := compute.BackendService{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Fingerprint: d.Get("fingerprint").(string),
|
|
|
|
HealthChecks: healthChecks,
|
|
|
|
}
|
|
|
|
|
2016-08-08 02:28:43 +02:00
|
|
|
// Optional things
|
|
|
|
if v, ok := d.GetOk("backend"); ok {
|
|
|
|
service.Backends = expandBackends(v.(*schema.Set).List())
|
2015-08-20 21:52:30 +02:00
|
|
|
}
|
2016-08-08 02:28:43 +02:00
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
|
|
service.Description = v.(string)
|
2015-08-20 21:52:30 +02:00
|
|
|
}
|
2016-08-08 02:28:43 +02:00
|
|
|
if v, ok := d.GetOk("port_name"); ok {
|
|
|
|
service.PortName = v.(string)
|
2015-08-20 21:52:30 +02:00
|
|
|
}
|
2016-08-08 02:28:43 +02:00
|
|
|
if v, ok := d.GetOk("protocol"); ok {
|
|
|
|
service.Protocol = v.(string)
|
2015-08-20 21:52:30 +02:00
|
|
|
}
|
2016-08-08 02:28:43 +02:00
|
|
|
if v, ok := d.GetOk("timeout_sec"); ok {
|
|
|
|
service.TimeoutSec = int64(v.(int))
|
2015-08-20 21:52:30 +02:00
|
|
|
}
|
|
|
|
|
2017-06-08 00:17:49 +02:00
|
|
|
if d.HasChange("connection_draining_timeout_sec") {
|
|
|
|
connectionDraining := &compute.ConnectionDraining{
|
|
|
|
DrainingTimeoutSec: int64(d.Get("connection_draining_timeout_sec").(int)),
|
|
|
|
}
|
|
|
|
|
|
|
|
service.ConnectionDraining = connectionDraining
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:12:56 +01:00
|
|
|
if d.HasChange("session_affinity") {
|
|
|
|
service.SessionAffinity = d.Get("session_affinity").(string)
|
|
|
|
}
|
|
|
|
|
2016-07-28 22:38:09 +02:00
|
|
|
if d.HasChange("enable_cdn") {
|
|
|
|
service.EnableCDN = d.Get("enable_cdn").(bool)
|
|
|
|
}
|
|
|
|
|
2015-08-20 21:52:30 +02:00
|
|
|
log.Printf("[DEBUG] Updating existing Backend Service %q: %#v", d.Id(), service)
|
|
|
|
op, err := config.clientCompute.BackendServices.Update(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Id(), &service).Do()
|
2015-08-20 21:52:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating backend service: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(service.Name)
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, project, "Updating Backend Service")
|
2015-08-20 21:52:30 +02:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2015-08-20 21:52:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return resourceComputeBackendServiceRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeBackendServiceDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-20 21:52:30 +02:00
|
|
|
log.Printf("[DEBUG] Deleting backend service %s", d.Id())
|
|
|
|
op, err := config.clientCompute.BackendServices.Delete(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Id()).Do()
|
2015-08-20 21:52:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting backend service: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, project, "Deleting Backend Service")
|
2015-08-20 21:52:30 +02:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2015-08-20 21:52:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandBackends(configured []interface{}) []*compute.Backend {
|
|
|
|
backends := make([]*compute.Backend, 0, len(configured))
|
|
|
|
|
|
|
|
for _, raw := range configured {
|
|
|
|
data := raw.(map[string]interface{})
|
|
|
|
|
|
|
|
b := compute.Backend{
|
|
|
|
Group: data["group"].(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := data["balancing_mode"]; ok {
|
|
|
|
b.BalancingMode = v.(string)
|
|
|
|
}
|
|
|
|
if v, ok := data["capacity_scaler"]; ok {
|
|
|
|
b.CapacityScaler = v.(float64)
|
|
|
|
}
|
|
|
|
if v, ok := data["description"]; ok {
|
|
|
|
b.Description = v.(string)
|
|
|
|
}
|
|
|
|
if v, ok := data["max_rate"]; ok {
|
|
|
|
b.MaxRate = int64(v.(int))
|
|
|
|
}
|
|
|
|
if v, ok := data["max_rate_per_instance"]; ok {
|
|
|
|
b.MaxRatePerInstance = v.(float64)
|
|
|
|
}
|
2016-02-04 04:06:32 +01:00
|
|
|
if v, ok := data["max_utilization"]; ok {
|
2015-08-20 21:52:30 +02:00
|
|
|
b.MaxUtilization = v.(float64)
|
|
|
|
}
|
|
|
|
|
|
|
|
backends = append(backends, &b)
|
|
|
|
}
|
|
|
|
|
|
|
|
return backends
|
|
|
|
}
|
|
|
|
|
|
|
|
func flattenBackends(backends []*compute.Backend) []map[string]interface{} {
|
|
|
|
result := make([]map[string]interface{}, 0, len(backends))
|
|
|
|
|
|
|
|
for _, b := range backends {
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
|
|
|
|
data["balancing_mode"] = b.BalancingMode
|
|
|
|
data["capacity_scaler"] = b.CapacityScaler
|
|
|
|
data["description"] = b.Description
|
|
|
|
data["group"] = b.Group
|
|
|
|
data["max_rate"] = b.MaxRate
|
|
|
|
data["max_rate_per_instance"] = b.MaxRatePerInstance
|
|
|
|
data["max_utilization"] = b.MaxUtilization
|
|
|
|
|
|
|
|
result = append(result, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceGoogleComputeBackendServiceBackendHash(v interface{}) int {
|
|
|
|
if v == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["group"].(string)))
|
|
|
|
|
|
|
|
if v, ok := m["balancing_mode"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
|
|
|
|
}
|
|
|
|
if v, ok := m["capacity_scaler"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%f-", v.(float64)))
|
|
|
|
}
|
|
|
|
if v, ok := m["description"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
|
|
|
|
}
|
|
|
|
if v, ok := m["max_rate"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", int64(v.(int))))
|
|
|
|
}
|
|
|
|
if v, ok := m["max_rate_per_instance"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%f-", v.(float64)))
|
|
|
|
}
|
|
|
|
if v, ok := m["max_rate_per_instance"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%f-", v.(float64)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|