From d4dd0fcdac35a93b92b9362d8568b3244d974eae Mon Sep 17 00:00:00 2001 From: Riley Karson Date: Tue, 6 Jun 2017 05:20:29 -0700 Subject: [PATCH] provider/google: Make google_compute_autoscaler use Update instead of Patch. (#15101) * Updated google_compute_autoscaler tests so that update fails as expected. * Changed google_compute_autoscaler's Update function from using Patch to Update. --- .../google/import_compute_autoscaler_test.go | 13 +++- .../google/resource_compute_autoscaler.go | 5 +- .../resource_compute_autoscaler_test.go | 68 ++++++++++++------- 3 files changed, 54 insertions(+), 32 deletions(-) diff --git a/builtin/providers/google/import_compute_autoscaler_test.go b/builtin/providers/google/import_compute_autoscaler_test.go index 4d5792c69..e358438ac 100644 --- a/builtin/providers/google/import_compute_autoscaler_test.go +++ b/builtin/providers/google/import_compute_autoscaler_test.go @@ -3,19 +3,26 @@ package google import ( "testing" + "fmt" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" ) -func TestAccAutoscaler_importBasic(t *testing.T) { +func TestAccComputeAutoscaler_importBasic(t *testing.T) { resourceName := "google_compute_autoscaler.foobar" + var it_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10)) + var tp_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10)) + var igm_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10)) + var autoscaler_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10)) + resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testAccCheckAutoscalerDestroy, + CheckDestroy: testAccCheckComputeAutoscalerDestroy, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccAutoscaler_basic, + Config: testAccComputeAutoscaler_basic(it_name, tp_name, igm_name, autoscaler_name), }, resource.TestStep{ diff --git a/builtin/providers/google/resource_compute_autoscaler.go b/builtin/providers/google/resource_compute_autoscaler.go index fc738b9c1..fb9f76f69 100644 --- a/builtin/providers/google/resource_compute_autoscaler.go +++ b/builtin/providers/google/resource_compute_autoscaler.go @@ -269,7 +269,6 @@ func flattenAutoscalingPolicy(policy *compute.AutoscalingPolicy) []map[string]in for _, customMetricUtilization := range policy.CustomMetricUtilizations { metricUtil := make(map[string]interface{}) metricUtil["target"] = customMetricUtilization.UtilizationTarget - metricUtils = append(metricUtils, metricUtil) } policyMap["metric"] = metricUtils @@ -299,7 +298,7 @@ func resourceComputeAutoscalerRead(d *schema.ResourceData, meta interface{}) err return err } if resource == nil { - log.Printf("[WARN] Removing Autoscalar %q because it's gone", d.Get("name").(string)) + log.Printf("[WARN] Removing Autoscaler %q because it's gone", d.Get("name").(string)) d.SetId("") return nil } @@ -332,7 +331,7 @@ func resourceComputeAutoscalerUpdate(d *schema.ResourceData, meta interface{}) e return err } - op, err := config.clientCompute.Autoscalers.Patch( + op, err := config.clientCompute.Autoscalers.Update( project, zone, scaler).Do() if err != nil { return fmt.Errorf("Error updating Autoscaler: %s", err) diff --git a/builtin/providers/google/resource_compute_autoscaler_test.go b/builtin/providers/google/resource_compute_autoscaler_test.go index 23ea207e6..3824c2509 100644 --- a/builtin/providers/google/resource_compute_autoscaler_test.go +++ b/builtin/providers/google/resource_compute_autoscaler_test.go @@ -10,18 +10,23 @@ import ( "google.golang.org/api/compute/v1" ) -func TestAccAutoscaler_basic(t *testing.T) { +func TestAccComputeAutoscaler_basic(t *testing.T) { var ascaler compute.Autoscaler + var it_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10)) + var tp_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10)) + var igm_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10)) + var autoscaler_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10)) + resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testAccCheckAutoscalerDestroy, + CheckDestroy: testAccCheckComputeAutoscalerDestroy, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccAutoscaler_basic, + Config: testAccComputeAutoscaler_basic(it_name, tp_name, igm_name, autoscaler_name), Check: resource.ComposeTestCheckFunc( - testAccCheckAutoscalerExists( + testAccCheckComputeAutoscalerExists( "google_compute_autoscaler.foobar", &ascaler), ), }, @@ -29,27 +34,32 @@ func TestAccAutoscaler_basic(t *testing.T) { }) } -func TestAccAutoscaler_update(t *testing.T) { +func TestAccComputeAutoscaler_update(t *testing.T) { var ascaler compute.Autoscaler + var it_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10)) + var tp_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10)) + var igm_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10)) + var autoscaler_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10)) + resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testAccCheckAutoscalerDestroy, + CheckDestroy: testAccCheckComputeAutoscalerDestroy, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccAutoscaler_basic, + Config: testAccComputeAutoscaler_basic(it_name, tp_name, igm_name, autoscaler_name), Check: resource.ComposeTestCheckFunc( - testAccCheckAutoscalerExists( + testAccCheckComputeAutoscalerExists( "google_compute_autoscaler.foobar", &ascaler), ), }, resource.TestStep{ - Config: testAccAutoscaler_update, + Config: testAccComputeAutoscaler_update(it_name, tp_name, igm_name, autoscaler_name), Check: resource.ComposeTestCheckFunc( - testAccCheckAutoscalerExists( + testAccCheckComputeAutoscalerExists( "google_compute_autoscaler.foobar", &ascaler), - testAccCheckAutoscalerUpdated( + testAccCheckComputeAutoscalerUpdated( "google_compute_autoscaler.foobar", 10), ), }, @@ -57,7 +67,7 @@ func TestAccAutoscaler_update(t *testing.T) { }) } -func testAccCheckAutoscalerDestroy(s *terraform.State) error { +func testAccCheckComputeAutoscalerDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) for _, rs := range s.RootModule().Resources { @@ -75,7 +85,7 @@ func testAccCheckAutoscalerDestroy(s *terraform.State) error { return nil } -func testAccCheckAutoscalerExists(n string, ascaler *compute.Autoscaler) resource.TestCheckFunc { +func testAccCheckComputeAutoscalerExists(n string, ascaler *compute.Autoscaler) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -104,7 +114,7 @@ func testAccCheckAutoscalerExists(n string, ascaler *compute.Autoscaler) resourc } } -func testAccCheckAutoscalerUpdated(n string, max int64) resource.TestCheckFunc { +func testAccCheckComputeAutoscalerUpdated(n string, max int64) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -131,9 +141,10 @@ func testAccCheckAutoscalerUpdated(n string, max int64) resource.TestCheckFunc { } } -var testAccAutoscaler_basic = fmt.Sprintf(` +func testAccComputeAutoscaler_basic(it_name, tp_name, igm_name, autoscaler_name string) string { + return fmt.Sprintf(` resource "google_compute_instance_template" "foobar" { - name = "ascaler-test-%s" + name = "%s" machine_type = "n1-standard-1" can_ip_forward = false tags = ["foo", "bar"] @@ -159,13 +170,13 @@ resource "google_compute_instance_template" "foobar" { resource "google_compute_target_pool" "foobar" { description = "Resource created for Terraform acceptance testing" - name = "ascaler-test-%s" + name = "%s" session_affinity = "CLIENT_IP_PROTO" } resource "google_compute_instance_group_manager" "foobar" { description = "Terraform test instance group manager" - name = "ascaler-test-%s" + name = "%s" instance_template = "${google_compute_instance_template.foobar.self_link}" target_pools = ["${google_compute_target_pool.foobar.self_link}"] base_instance_name = "foobar" @@ -174,7 +185,7 @@ resource "google_compute_instance_group_manager" "foobar" { resource "google_compute_autoscaler" "foobar" { description = "Resource created for Terraform acceptance testing" - name = "ascaler-test-%s" + name = "%s" zone = "us-central1-a" target = "${google_compute_instance_group_manager.foobar.self_link}" autoscaling_policy = { @@ -186,11 +197,14 @@ resource "google_compute_autoscaler" "foobar" { } } -}`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10), acctest.RandString(10)) +} +`, it_name, tp_name, igm_name, autoscaler_name) +} -var testAccAutoscaler_update = fmt.Sprintf(` +func testAccComputeAutoscaler_update(it_name, tp_name, igm_name, autoscaler_name string) string { + return fmt.Sprintf(` resource "google_compute_instance_template" "foobar" { - name = "ascaler-test-%s" + name = "%s" machine_type = "n1-standard-1" can_ip_forward = false tags = ["foo", "bar"] @@ -216,13 +230,13 @@ resource "google_compute_instance_template" "foobar" { resource "google_compute_target_pool" "foobar" { description = "Resource created for Terraform acceptance testing" - name = "ascaler-test-%s" + name = "%s" session_affinity = "CLIENT_IP_PROTO" } resource "google_compute_instance_group_manager" "foobar" { description = "Terraform test instance group manager" - name = "ascaler-test-%s" + name = "%s" instance_template = "${google_compute_instance_template.foobar.self_link}" target_pools = ["${google_compute_target_pool.foobar.self_link}"] base_instance_name = "foobar" @@ -231,7 +245,7 @@ resource "google_compute_instance_group_manager" "foobar" { resource "google_compute_autoscaler" "foobar" { description = "Resource created for Terraform acceptance testing" - name = "ascaler-test-%s" + name = "%s" zone = "us-central1-a" target = "${google_compute_instance_group_manager.foobar.self_link}" autoscaling_policy = { @@ -243,4 +257,6 @@ resource "google_compute_autoscaler" "foobar" { } } -}`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10), acctest.RandString(10)) +} +`, it_name, tp_name, igm_name, autoscaler_name) +}