Skip to content

Commit

Permalink
provider/google: Make google_compute_autoscaler use Update instead of…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
rileykarson authored and stack72 committed Jun 6, 2017
1 parent 0523ccf commit d4dd0fc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 32 deletions.
13 changes: 10 additions & 3 deletions builtin/providers/google/import_compute_autoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
5 changes: 2 additions & 3 deletions builtin/providers/google/resource_compute_autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
68 changes: 42 additions & 26 deletions builtin/providers/google/resource_compute_autoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,64 @@ 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),
),
},
},
})
}

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),
),
},
},
})
}

func testAccCheckAutoscalerDestroy(s *terraform.State) error {
func testAccCheckComputeAutoscalerDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)

for _, rs := range s.RootModule().Resources {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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"]
Expand All @@ -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"
Expand All @@ -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 = {
Expand All @@ -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"]
Expand All @@ -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"
Expand All @@ -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 = {
Expand All @@ -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)
}

0 comments on commit d4dd0fc

Please sign in to comment.