Skip to content

Commit

Permalink
Fix google_compute_instance_template crash (#3194) (#1812)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Mar 2, 2020
1 parent 4e60d6a commit 3601b8a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .changelog/3194.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
Fixed a scenario where `google_compute_instance_template` would cause a crash.
```
10 changes: 1 addition & 9 deletions google-beta/resource_compute_instance_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func resourceComputeInstanceTemplate() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"interface": {
Expand Down Expand Up @@ -504,11 +505,6 @@ func resourceComputeInstanceTemplateSourceImageCustomizeDiff(diff *schema.Resour
var err error
old, new := diff.GetChange(key)
if old == "" || new == "" {
// no sense in resolving empty strings
err = diff.ForceNew(key)
if err != nil {
return err
}
continue
}
// project must be retrieved once we know there is a diff to resolve, otherwise it will
Expand All @@ -535,10 +531,6 @@ func resourceComputeInstanceTemplateSourceImageCustomizeDiff(diff *schema.Resour
return err
}
if oldResolved != newResolved {
err = diff.ForceNew(key)
if err != nil {
return err
}
continue
}
err = diff.Clear(key)
Expand Down
66 changes: 66 additions & 0 deletions google-beta/resource_compute_instance_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,40 @@ func TestAccComputeInstanceTemplate_invalidDiskType(t *testing.T) {
})
}

func TestAccComputeInstanceTemplate_imageResourceTest(t *testing.T) {
t.Parallel()
diskName := "tf-test-disk-" + acctest.RandString(10)
computeImage := "tf-test-image-" + acctest.RandString(10)
imageDesc1 := "Some description"
imageDesc2 := "Some other description"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeInstanceTemplateDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeInstanceTemplate_imageResourceTest(diskName, computeImage, imageDesc1),
},
{
ResourceName: "google_compute_instance_template.foobar",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name_prefix"},
},
{
Config: testAccComputeInstanceTemplate_imageResourceTest(diskName, computeImage, imageDesc2),
},
{
ResourceName: "google_compute_instance_template.foobar",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name_prefix"},
},
},
})
}

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

Expand Down Expand Up @@ -2039,3 +2073,35 @@ resource "google_compute_instance_template" "foobar" {
}
`, acctest.RandString(10))
}

func testAccComputeInstanceTemplate_imageResourceTest(diskName string, imageName string, imageDescription string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-9"
project = "debian-cloud"
}
resource "google_compute_disk" "my_disk" {
name = "%s"
zone = "us-central1-a"
image = data.google_compute_image.my_image.self_link
}
resource "google_compute_image" "diskimage" {
name = "%s"
description = "%s"
source_disk = google_compute_disk.my_disk.self_link
}
resource "google_compute_instance_template" "foobar" {
name_prefix = "tf-test-instance-"
machine_type = "n1-standard-1"
disk {
source_image = google_compute_image.diskimage.self_link
}
network_interface {
network = "default"
access_config {}
}
}
`, diskName, imageName, imageDescription)
}

0 comments on commit 3601b8a

Please sign in to comment.