Merge pull request #8115 from nwwebb/import-autoscaler

provider/google: Support Import of 'google_compute_autoscaler'
This commit is contained in:
Lars Wander 2016-08-11 18:07:22 -04:00 committed by GitHub
commit ef9591952a
4 changed files with 118 additions and 15 deletions

View File

@ -0,0 +1,28 @@
package google
import (
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccAutoscaler_importBasic(t *testing.T) {
resourceName := "google_compute_autoscaler.foobar"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAutoscalerDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAutoscaler_basic,
},
resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

View File

@ -3,10 +3,13 @@ package google
import (
"encoding/json"
"fmt"
"strings"
"github.com/hashicorp/terraform/helper/pathorcontents"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
)
// Provider returns a terraform.ResourceProvider.
@ -195,3 +198,27 @@ func getProject(d *schema.ResourceData, config *Config) (string, error) {
}
return res.(string), nil
}
func getZonalResourceFromRegion(getResource func(string) (interface{}, error), region string, compute *compute.Service, project string) (interface{}, error) {
zoneList, err := compute.Zones.List(project).Do()
if err != nil {
return nil, err
}
var resource interface{}
for _, zone := range zoneList.Items {
if strings.Contains(zone.Name, region) {
resource, err = getResource(zone.Name)
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
// Resource was not found in this zone
continue
}
return nil, fmt.Errorf("Error reading Resource: %s", err)
}
// Resource was found
return resource, nil
}
}
// Resource does not exist in this region
return nil, nil
}

View File

@ -3,10 +3,10 @@ package google
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
)
func resourceComputeAutoscaler() *schema.Resource {
@ -15,6 +15,9 @@ func resourceComputeAutoscaler() *schema.Resource {
Read: resourceComputeAutoscalerRead,
Update: resourceComputeAutoscalerUpdate,
Delete: resourceComputeAutoscalerDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
@ -241,6 +244,40 @@ func resourceComputeAutoscalerCreate(d *schema.ResourceData, meta interface{}) e
return resourceComputeAutoscalerRead(d, meta)
}
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
}
func resourceComputeAutoscalerRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
@ -249,22 +286,33 @@ func resourceComputeAutoscalerRead(d *schema.ResourceData, meta interface{}) err
return err
}
zone := d.Get("zone").(string)
scaler, err := config.clientCompute.Autoscalers.Get(
project, zone, d.Id()).Do()
region, err := getRegion(d, config)
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
// The resource doesn't exist anymore
log.Printf("[WARN] Removing Autoscalar %q because it's gone", d.Get("name").(string))
d.SetId("")
return nil
}
return fmt.Errorf("Error reading Autoscaler: %s", err)
return err
}
var getAutoscaler = func(zone string) (interface{}, error) {
return config.clientCompute.Autoscalers.Get(project, zone, d.Id()).Do()
}
resource, err := getZonalResourceFromRegion(getAutoscaler, region, config.clientCompute, project)
if err != nil {
return err
}
if resource == nil {
log.Printf("[WARN] Removing Autoscalar %q because it's gone", d.Get("name").(string))
d.SetId("")
return nil
}
scaler := resource.(*compute.Autoscaler)
zoneUrl := strings.Split(scaler.Zone, "/")
d.Set("self_link", scaler.SelfLink)
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))
}
return nil
}

View File

@ -179,7 +179,7 @@ resource "google_compute_autoscaler" "foobar" {
target = "${google_compute_instance_group_manager.foobar.self_link}"
autoscaling_policy = {
max_replicas = 5
min_replicas = 0
min_replicas = 1
cooldown_period = 60
cpu_utilization = {
target = 0.5
@ -236,7 +236,7 @@ resource "google_compute_autoscaler" "foobar" {
target = "${google_compute_instance_group_manager.foobar.self_link}"
autoscaling_policy = {
max_replicas = 10
min_replicas = 0
min_replicas = 1
cooldown_period = 60
cpu_utilization = {
target = 0.5