2015-03-04 11:14:59 +01:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-08-04 22:14:43 +02:00
|
|
|
"strings"
|
2015-03-04 11:14:59 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-08-14 13:06:06 +02:00
|
|
|
"google.golang.org/api/compute/v1"
|
2015-03-04 11:14:59 +01:00
|
|
|
)
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
func resourceComputeAutoscaler() *schema.Resource {
|
2015-03-04 11:14:59 +01:00
|
|
|
return &schema.Resource{
|
2015-07-28 02:47:10 +02:00
|
|
|
Create: resourceComputeAutoscalerCreate,
|
|
|
|
Read: resourceComputeAutoscalerRead,
|
|
|
|
Update: resourceComputeAutoscalerUpdate,
|
|
|
|
Delete: resourceComputeAutoscalerDelete,
|
2016-08-04 22:14:43 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2015-03-04 11:14:59 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
ForceNew: true,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"target": &schema.Schema{
|
2015-03-04 11:14:59 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Required: true,
|
2015-03-04 11:14:59 +01:00
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"zone": &schema.Schema{
|
2015-03-04 11:14:59 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2016-04-10 23:34:15 +02:00
|
|
|
ForceNew: true,
|
2015-03-04 11:14:59 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"autoscaling_policy": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"min_replicas": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"max_replicas": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"cooldown_period": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
2015-05-17 02:14:38 +02:00
|
|
|
Optional: true,
|
2015-08-14 13:06:06 +02:00
|
|
|
Default: 60,
|
2015-03-04 11:14:59 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"cpu_utilization": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"target": &schema.Schema{
|
|
|
|
Type: schema.TypeFloat,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"metric": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"target": &schema.Schema{
|
|
|
|
Type: schema.TypeFloat,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"load_balancing_utilization": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"target": &schema.Schema{
|
|
|
|
Type: schema.TypeFloat,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"description": &schema.Schema{
|
2015-03-04 11:14:59 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Optional: true,
|
2015-03-04 11:14:59 +01:00
|
|
|
},
|
2016-04-10 18:59:57 +02:00
|
|
|
|
|
|
|
"project": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-04-10 23:34:15 +02:00
|
|
|
|
|
|
|
"self_link": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-03-04 11:14:59 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
func buildAutoscaler(d *schema.ResourceData) (*compute.Autoscaler, error) {
|
2015-03-04 11:14:59 +01:00
|
|
|
// Build the parameter
|
2015-07-28 02:47:10 +02:00
|
|
|
scaler := &compute.Autoscaler{
|
2015-03-04 11:14:59 +01:00
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Target: d.Get("target").(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optional fields
|
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
|
|
scaler.Description = v.(string)
|
|
|
|
}
|
|
|
|
|
2015-05-17 02:14:38 +02:00
|
|
|
aspCount := d.Get("autoscaling_policy.#").(int)
|
|
|
|
if aspCount != 1 {
|
|
|
|
return nil, fmt.Errorf("The autoscaler must have exactly one autoscaling_policy, found %d.", aspCount)
|
|
|
|
}
|
|
|
|
|
2015-03-04 11:14:59 +01:00
|
|
|
prefix := "autoscaling_policy.0."
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
scaler.AutoscalingPolicy = &compute.AutoscalingPolicy{
|
2015-03-04 11:14:59 +01:00
|
|
|
MaxNumReplicas: int64(d.Get(prefix + "max_replicas").(int)),
|
|
|
|
MinNumReplicas: int64(d.Get(prefix + "min_replicas").(int)),
|
|
|
|
CoolDownPeriodSec: int64(d.Get(prefix + "cooldown_period").(int)),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that only one autoscaling policy is defined
|
|
|
|
|
|
|
|
policyCounter := 0
|
|
|
|
if _, ok := d.GetOk(prefix + "cpu_utilization"); ok {
|
2015-05-17 02:14:38 +02:00
|
|
|
if d.Get(prefix+"cpu_utilization.0.target").(float64) != 0 {
|
|
|
|
cpuUtilCount := d.Get(prefix + "cpu_utilization.#").(int)
|
|
|
|
if cpuUtilCount != 1 {
|
|
|
|
return nil, fmt.Errorf("The autoscaling_policy must have exactly one cpu_utilization, found %d.", cpuUtilCount)
|
|
|
|
}
|
|
|
|
policyCounter++
|
2015-07-28 02:47:10 +02:00
|
|
|
scaler.AutoscalingPolicy.CpuUtilization = &compute.AutoscalingPolicyCpuUtilization{
|
2015-05-17 02:14:38 +02:00
|
|
|
UtilizationTarget: d.Get(prefix + "cpu_utilization.0.target").(float64),
|
|
|
|
}
|
2015-03-04 11:14:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := d.GetOk("autoscaling_policy.0.metric"); ok {
|
2015-05-17 02:14:38 +02:00
|
|
|
if d.Get(prefix+"metric.0.name") != "" {
|
|
|
|
policyCounter++
|
|
|
|
metricCount := d.Get(prefix + "metric.#").(int)
|
|
|
|
if metricCount != 1 {
|
|
|
|
return nil, fmt.Errorf("The autoscaling_policy must have exactly one metric, found %d.", metricCount)
|
|
|
|
}
|
2015-07-28 02:47:10 +02:00
|
|
|
scaler.AutoscalingPolicy.CustomMetricUtilizations = []*compute.AutoscalingPolicyCustomMetricUtilization{
|
2015-05-17 02:14:38 +02:00
|
|
|
{
|
|
|
|
Metric: d.Get(prefix + "metric.0.name").(string),
|
|
|
|
UtilizationTarget: d.Get(prefix + "metric.0.target").(float64),
|
|
|
|
UtilizationTargetType: d.Get(prefix + "metric.0.type").(string),
|
|
|
|
},
|
|
|
|
}
|
2015-03-04 11:14:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if _, ok := d.GetOk("autoscaling_policy.0.load_balancing_utilization"); ok {
|
2015-05-17 02:14:38 +02:00
|
|
|
if d.Get(prefix+"load_balancing_utilization.0.target").(float64) != 0 {
|
|
|
|
policyCounter++
|
|
|
|
lbuCount := d.Get(prefix + "load_balancing_utilization.#").(int)
|
|
|
|
if lbuCount != 1 {
|
|
|
|
return nil, fmt.Errorf("The autoscaling_policy must have exactly one load_balancing_utilization, found %d.", lbuCount)
|
|
|
|
}
|
2015-07-28 02:47:10 +02:00
|
|
|
scaler.AutoscalingPolicy.LoadBalancingUtilization = &compute.AutoscalingPolicyLoadBalancingUtilization{
|
2015-05-17 02:14:38 +02:00
|
|
|
UtilizationTarget: d.Get(prefix + "load_balancing_utilization.0.target").(float64),
|
|
|
|
}
|
2015-03-04 11:14:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if policyCounter != 1 {
|
2015-05-17 02:14:38 +02:00
|
|
|
return nil, fmt.Errorf("One policy must be defined for an autoscaler.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return scaler, nil
|
|
|
|
}
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
func resourceComputeAutoscalerCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-05-17 02:14:38 +02:00
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-17 02:14:38 +02:00
|
|
|
// Get the zone
|
|
|
|
log.Printf("[DEBUG] Loading zone: %s", d.Get("zone").(string))
|
|
|
|
zone, err := config.clientCompute.Zones.Get(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("zone").(string)).Do()
|
2015-05-17 02:14:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error loading zone '%s': %s", d.Get("zone").(string), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
scaler, err := buildAutoscaler(d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-03-04 11:14:59 +01:00
|
|
|
}
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
op, err := config.clientCompute.Autoscalers.Insert(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, zone.Name, scaler).Do()
|
2015-03-04 11:14:59 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating Autoscaler: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It probably maybe worked, so store the ID now
|
|
|
|
d.SetId(scaler.Name)
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitZone(config, op, project, zone.Name, "Creating Autoscaler")
|
2015-03-04 11:14:59 +01:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2015-03-04 11:14:59 +01:00
|
|
|
}
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
return resourceComputeAutoscalerRead(d, meta)
|
2015-03-04 11:14:59 +01:00
|
|
|
}
|
|
|
|
|
2016-08-04 22:14:43 +02:00
|
|
|
func flattenAutoscalingPolicy(policy *compute.AutoscalingPolicy) []map[string]interface{} {
|
|
|
|
result := make([]map[string]interface{}, 0, 1)
|
|
|
|
policyMap := make(map[string]interface{})
|
|
|
|
policyMap["max_replicas"] = policy.MaxNumReplicas
|
|
|
|
policyMap["min_replicas"] = policy.MinNumReplicas
|
|
|
|
policyMap["cooldown_period"] = policy.CoolDownPeriodSec
|
|
|
|
if policy.CpuUtilization != nil {
|
|
|
|
cpuUtils := make([]map[string]interface{}, 0, 1)
|
|
|
|
cpuUtil := make(map[string]interface{})
|
|
|
|
cpuUtil["target"] = policy.CpuUtilization.UtilizationTarget
|
|
|
|
cpuUtils = append(cpuUtils, cpuUtil)
|
|
|
|
policyMap["cpu_utilization"] = cpuUtils
|
|
|
|
}
|
|
|
|
if policy.LoadBalancingUtilization != nil {
|
|
|
|
loadBalancingUtils := make([]map[string]interface{}, 0, 1)
|
|
|
|
loadBalancingUtil := make(map[string]interface{})
|
|
|
|
loadBalancingUtil["target"] = policy.LoadBalancingUtilization.UtilizationTarget
|
|
|
|
loadBalancingUtils = append(loadBalancingUtils, loadBalancingUtil)
|
|
|
|
policyMap["load_balancing_utilization"] = loadBalancingUtils
|
|
|
|
}
|
|
|
|
if policy.CustomMetricUtilizations != nil {
|
|
|
|
metricUtils := make([]map[string]interface{}, 0, len(policy.CustomMetricUtilizations))
|
|
|
|
for _, customMetricUtilization := range policy.CustomMetricUtilizations {
|
|
|
|
metricUtil := make(map[string]interface{})
|
|
|
|
metricUtil["target"] = customMetricUtilization.UtilizationTarget
|
|
|
|
metricUtils = append(metricUtils, metricUtil)
|
|
|
|
}
|
|
|
|
policyMap["metric"] = metricUtils
|
|
|
|
}
|
|
|
|
result = append(result, policyMap)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
func resourceComputeAutoscalerRead(d *schema.ResourceData, meta interface{}) error {
|
2015-03-04 11:14:59 +01:00
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-04 22:14:43 +02:00
|
|
|
region, err := getRegion(d, config)
|
2015-03-04 11:14:59 +01:00
|
|
|
if err != nil {
|
2016-08-04 22:14:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
var getAutoscaler = func(zone string) (interface{}, error) {
|
|
|
|
return config.clientCompute.Autoscalers.Get(project, zone, d.Id()).Do()
|
2015-03-04 11:14:59 +01:00
|
|
|
}
|
|
|
|
|
2016-08-04 22:14:43 +02:00
|
|
|
resource, err := getZonalResourceFromRegion(getAutoscaler, region, config.clientCompute, project)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resource == nil {
|
2017-06-06 14:20:29 +02:00
|
|
|
log.Printf("[WARN] Removing Autoscaler %q because it's gone", d.Get("name").(string))
|
2016-08-04 22:14:43 +02:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
scaler := resource.(*compute.Autoscaler)
|
|
|
|
zoneUrl := strings.Split(scaler.Zone, "/")
|
2015-03-04 11:14:59 +01:00
|
|
|
d.Set("self_link", scaler.SelfLink)
|
2016-08-04 22:14:43 +02:00
|
|
|
d.Set("name", scaler.Name)
|
|
|
|
d.Set("target", scaler.Target)
|
|
|
|
d.Set("zone", zoneUrl[len(zoneUrl)-1])
|
|
|
|
d.Set("description", scaler.Description)
|
|
|
|
if scaler.AutoscalingPolicy != nil {
|
|
|
|
d.Set("autoscaling_policy", flattenAutoscalingPolicy(scaler.AutoscalingPolicy))
|
|
|
|
}
|
2015-03-04 11:14:59 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
func resourceComputeAutoscalerUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-03-04 11:14:59 +01:00
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-04 11:14:59 +01:00
|
|
|
zone := d.Get("zone").(string)
|
|
|
|
|
2015-05-17 02:14:38 +02:00
|
|
|
scaler, err := buildAutoscaler(d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-03-04 11:14:59 +01:00
|
|
|
}
|
|
|
|
|
2017-06-06 14:20:29 +02:00
|
|
|
op, err := config.clientCompute.Autoscalers.Update(
|
2017-04-26 21:23:13 +02:00
|
|
|
project, zone, scaler).Do()
|
2015-03-04 11:14:59 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating Autoscaler: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It probably maybe worked, so store the ID now
|
|
|
|
d.SetId(scaler.Name)
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitZone(config, op, project, zone, "Updating Autoscaler")
|
2015-03-04 11:14:59 +01:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2015-03-04 11:14:59 +01:00
|
|
|
}
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
return resourceComputeAutoscalerRead(d, meta)
|
2015-03-04 11:14:59 +01:00
|
|
|
}
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
func resourceComputeAutoscalerDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-03-04 11:14:59 +01:00
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-04 11:14:59 +01:00
|
|
|
zone := d.Get("zone").(string)
|
2015-07-28 02:47:10 +02:00
|
|
|
op, err := config.clientCompute.Autoscalers.Delete(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, zone, d.Id()).Do()
|
2015-03-04 11:14:59 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting autoscaler: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitZone(config, op, project, zone, "Deleting Autoscaler")
|
2015-03-04 11:14:59 +01:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2015-03-04 11:14:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|