diff --git a/google/resource_compute_instance.go b/google/resource_compute_instance.go index f55fd741695..9b52f0c8f25 100644 --- a/google/resource_compute_instance.go +++ b/google/resource_compute_instance.go @@ -1198,6 +1198,9 @@ func expandInstanceGuestAccelerators(d TerraformResourceData, config *Config) ([ guestAccelerators := make([]*computeBeta.AcceleratorConfig, len(accels)) for i, raw := range accels { data := raw.(map[string]interface{}) + if data["count"].(int) == 0 { + continue + } at, err := ParseAcceleratorFieldValue(data["type"].(string), d, config) if err != nil { return nil, fmt.Errorf("cannot parse accelerator type: %v", err) diff --git a/google/resource_compute_instance_template.go b/google/resource_compute_instance_template.go index 7ac6e2e6a0a..28a25e8420e 100644 --- a/google/resource_compute_instance_template.go +++ b/google/resource_compute_instance_template.go @@ -506,6 +506,9 @@ func expandInstanceTemplateGuestAccelerators(d TerraformResourceData, config *Co guestAccelerators := make([]*computeBeta.AcceleratorConfig, len(accels)) for i, raw := range accels { data := raw.(map[string]interface{}) + if data["count"].(int) == 0 { + continue + } guestAccelerators[i] = &computeBeta.AcceleratorConfig{ AcceleratorCount: int64(data["count"].(int)), // We can't use ParseAcceleratorFieldValue here because an instance diff --git a/google/resource_compute_instance_template_test.go b/google/resource_compute_instance_template_test.go index ac57ec5701d..51d1e9eaa7b 100644 --- a/google/resource_compute_instance_template_test.go +++ b/google/resource_compute_instance_template_test.go @@ -301,7 +301,7 @@ func TestAccComputeInstanceTemplate_guestAccelerator(t *testing.T) { CheckDestroy: testAccCheckComputeInstanceTemplateDestroy, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccComputeInstanceTemplate_guestAccelerator(acctest.RandString(10)), + Config: testAccComputeInstanceTemplate_guestAccelerator(acctest.RandString(10), 1), Check: resource.ComposeTestCheckFunc( testAccCheckComputeInstanceTemplateExists("google_compute_instance_template.foobar", &instanceTemplate), testAccCheckComputeInstanceTemplateHasGuestAccelerator(&instanceTemplate, "nvidia-tesla-k80", 1), @@ -312,6 +312,28 @@ func TestAccComputeInstanceTemplate_guestAccelerator(t *testing.T) { } +func TestAccComputeInstanceTemplate_guestAcceleratorSkip(t *testing.T) { + t.Parallel() + + var instanceTemplate compute.InstanceTemplate + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeInstanceTemplateDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccComputeInstanceTemplate_guestAccelerator(acctest.RandString(10), 0), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeInstanceTemplateExists("google_compute_instance_template.foobar", &instanceTemplate), + testAccCheckComputeInstanceTemplateLacksGuestAccelerator(&instanceTemplate), + ), + }, + }, + }) + +} + func TestAccComputeInstanceTemplate_minCpuPlatform(t *testing.T) { t.Parallel() @@ -604,6 +626,16 @@ func testAccCheckComputeInstanceTemplateHasGuestAccelerator(instanceTemplate *co } } +func testAccCheckComputeInstanceTemplateLacksGuestAccelerator(instanceTemplate *compute.InstanceTemplate) resource.TestCheckFunc { + return func(s *terraform.State) error { + if len(instanceTemplate.Properties.GuestAccelerators) > 0 { + return fmt.Errorf("Expected no guest accelerators") + } + + return nil + } +} + func testAccCheckComputeInstanceTemplateHasMinCpuPlatform(instanceTemplate *compute.InstanceTemplate, minCpuPlatform string) resource.TestCheckFunc { return func(s *terraform.State) error { if instanceTemplate.Properties.MinCpuPlatform != minCpuPlatform { @@ -1027,7 +1059,7 @@ resource "google_compute_instance_template" "foobar" { }`, i, i, i) } -func testAccComputeInstanceTemplate_guestAccelerator(i string) string { +func testAccComputeInstanceTemplate_guestAccelerator(i string, count int) string { return fmt.Sprintf(` resource "google_compute_instance_template" "foobar" { name = "instance-test-%s" @@ -1050,10 +1082,10 @@ resource "google_compute_instance_template" "foobar" { } guest_accelerator { - count = 1 + count = %d type = "nvidia-tesla-k80" } -}`, i) +}`, i, count) } func testAccComputeInstanceTemplate_minCpuPlatform(i string) string {