Skip to content

Commit

Permalink
Add validation for scratch disks in Instance Template
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
rileykarson authored and modular-magician committed Sep 3, 2019
1 parent 37dc50f commit 56c941d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
22 changes: 20 additions & 2 deletions google/resource_compute_instance_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/customdiff"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
Expand All @@ -20,8 +21,11 @@ func resourceComputeInstanceTemplate() *schema.Resource {
State: schema.ImportStatePassthrough,
},
SchemaVersion: 1,
CustomizeDiff: resourceComputeInstanceTemplateSourceImageCustomizeDiff,
MigrateState: resourceComputeInstanceTemplateMigrateState,
CustomizeDiff: customdiff.All(
resourceComputeInstanceTemplateSourceImageCustomizeDiff,
resourceComputeInstanceTemplateScratchDiskCustomizeDiff,
),
MigrateState: resourceComputeInstanceTemplateMigrateState,

// A compute instance template is more or less a subset of a compute
// instance. Please attempt to maintain consistency with the
Expand Down Expand Up @@ -541,6 +545,20 @@ func resourceComputeInstanceTemplateSourceImageCustomizeDiff(diff *schema.Resour
return nil
}

func resourceComputeInstanceTemplateScratchDiskCustomizeDiff(diff *schema.ResourceDiff, meta interface{}) error {
numDisks := diff.Get("disk.#").(int)
for i := 0; i < numDisks; i++ {
// misspelled on purpose, type is a special symbol
typee := diff.Get(fmt.Sprintf("disk.%d.type", i)).(string)
diskType := diff.Get(fmt.Sprintf("disk.%d.disk_type", i)).(string)
if typee == "SCRATCH" && diskType != "local-ssd" {
return fmt.Errorf("SCRATCH disks must have a disk_type of local-ssd. disk %d has disk_type %s", i, diskType)
}
}

return nil
}

func buildDisks(d *schema.ResourceData, config *Config) ([]*computeBeta.AttachedDisk, error) {
project, err := getProject(d, config)
if err != nil {
Expand Down
52 changes: 52 additions & 0 deletions google/resource_compute_instance_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,21 @@ func TestAccComputeInstanceTemplate_shieldedVmConfig2(t *testing.T) {
})
}

func TestAccComputeInstanceTemplate_invalidDiskType(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccComputeInstanceTemplate_invalidDiskType(),
ExpectError: regexp.MustCompile("SCRATCH disks must have a disk_type of local-ssd"),
},
},
})
}

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

Expand Down Expand Up @@ -1868,3 +1883,40 @@ resource "google_compute_instance_template" "foobar" {
}
}`, acctest.RandString(10), enableSecureBoot, enableVtpm, enableIntegrityMonitoring)
}

func testAccComputeInstanceTemplate_invalidDiskType() string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "centos-7"
project = "gce-uefi-images"
}
resource "google_compute_instance_template" "foobar" {
name = "instancet-test-%s"
machine_type = "n1-standard-1"
can_ip_forward = false
disk {
source_image = "${data.google_compute_image.my_image.self_link}"
auto_delete = true
boot = true
}
disk {
source_image = "${data.google_compute_image.my_image.self_link}"
auto_delete = true
type = "SCRATCH"
disk_type = "local-ssd"
}
disk {
source_image = "${data.google_compute_image.my_image.self_link}"
auto_delete = true
type = "SCRATCH"
}
network_interface {
network = "default"
}
}`, acctest.RandString(10))
}

0 comments on commit 56c941d

Please sign in to comment.