mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-07-09 03:28:29 +00:00
provider/google: Support Import of 'google_compute_autoscaler'
This commit is contained in:
parent
5254774342
commit
27ffab3a75
28
import_compute_autoscaler_test.go
Normal file
28
import_compute_autoscaler_test.go
Normal 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,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
27
provider.go
27
provider.go
|
@ -3,10 +3,13 @@ package google
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/pathorcontents"
|
"github.com/hashicorp/terraform/helper/pathorcontents"
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
|
"google.golang.org/api/compute/v1"
|
||||||
|
"google.golang.org/api/googleapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Provider returns a terraform.ResourceProvider.
|
// Provider returns a terraform.ResourceProvider.
|
||||||
|
@ -195,3 +198,27 @@ func getProject(d *schema.ResourceData, config *Config) (string, error) {
|
||||||
}
|
}
|
||||||
return res.(string), nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -3,10 +3,10 @@ package google
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
"google.golang.org/api/compute/v1"
|
"google.golang.org/api/compute/v1"
|
||||||
"google.golang.org/api/googleapi"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func resourceComputeAutoscaler() *schema.Resource {
|
func resourceComputeAutoscaler() *schema.Resource {
|
||||||
|
@ -15,6 +15,9 @@ func resourceComputeAutoscaler() *schema.Resource {
|
||||||
Read: resourceComputeAutoscalerRead,
|
Read: resourceComputeAutoscalerRead,
|
||||||
Update: resourceComputeAutoscalerUpdate,
|
Update: resourceComputeAutoscalerUpdate,
|
||||||
Delete: resourceComputeAutoscalerDelete,
|
Delete: resourceComputeAutoscalerDelete,
|
||||||
|
Importer: &schema.ResourceImporter{
|
||||||
|
State: schema.ImportStatePassthrough,
|
||||||
|
},
|
||||||
|
|
||||||
Schema: map[string]*schema.Schema{
|
Schema: map[string]*schema.Schema{
|
||||||
"name": &schema.Schema{
|
"name": &schema.Schema{
|
||||||
|
@ -241,6 +244,40 @@ func resourceComputeAutoscalerCreate(d *schema.ResourceData, meta interface{}) e
|
||||||
return resourceComputeAutoscalerRead(d, meta)
|
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 {
|
func resourceComputeAutoscalerRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
config := meta.(*Config)
|
config := meta.(*Config)
|
||||||
|
|
||||||
|
@ -249,22 +286,33 @@ func resourceComputeAutoscalerRead(d *schema.ResourceData, meta interface{}) err
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
zone := d.Get("zone").(string)
|
region, err := getRegion(d, config)
|
||||||
scaler, err := config.clientCompute.Autoscalers.Get(
|
|
||||||
project, zone, d.Id()).Do()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
return err
|
||||||
// The resource doesn't exist anymore
|
}
|
||||||
|
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))
|
log.Printf("[WARN] Removing Autoscalar %q because it's gone", d.Get("name").(string))
|
||||||
d.SetId("")
|
d.SetId("")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
scaler := resource.(*compute.Autoscaler)
|
||||||
return fmt.Errorf("Error reading Autoscaler: %s", err)
|
zoneUrl := strings.Split(scaler.Zone, "/")
|
||||||
}
|
|
||||||
|
|
||||||
d.Set("self_link", scaler.SelfLink)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -179,7 +179,7 @@ resource "google_compute_autoscaler" "foobar" {
|
||||||
target = "${google_compute_instance_group_manager.foobar.self_link}"
|
target = "${google_compute_instance_group_manager.foobar.self_link}"
|
||||||
autoscaling_policy = {
|
autoscaling_policy = {
|
||||||
max_replicas = 5
|
max_replicas = 5
|
||||||
min_replicas = 0
|
min_replicas = 1
|
||||||
cooldown_period = 60
|
cooldown_period = 60
|
||||||
cpu_utilization = {
|
cpu_utilization = {
|
||||||
target = 0.5
|
target = 0.5
|
||||||
|
@ -236,7 +236,7 @@ resource "google_compute_autoscaler" "foobar" {
|
||||||
target = "${google_compute_instance_group_manager.foobar.self_link}"
|
target = "${google_compute_instance_group_manager.foobar.self_link}"
|
||||||
autoscaling_policy = {
|
autoscaling_policy = {
|
||||||
max_replicas = 10
|
max_replicas = 10
|
||||||
min_replicas = 0
|
min_replicas = 1
|
||||||
cooldown_period = 60
|
cooldown_period = 60
|
||||||
cpu_utilization = {
|
cpu_utilization = {
|
||||||
target = 0.5
|
target = 0.5
|
||||||
|
|
Loading…
Reference in New Issue
Block a user