Skip to content

Commit

Permalink
Add validation for scratch disks in Instance Template (#2282)
Browse files Browse the repository at this point in the history
* Add validation for scratch disks

* Remove source from scratch disk

* Use hardcoded image

* Add reverse logic
  • Loading branch information
rileykarson authored and slevenick committed Nov 7, 2019
1 parent 2634cbf commit bb5f05a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"reflect"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform-plugin-sdk/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"

computeBeta "google.golang.org/api/compute/v0.beta"
)

Expand All @@ -20,8 +22,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 @@ -522,6 +527,24 @@ 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)
}

if diskType == "local-ssd" && typee != "SCRATCH" {
return fmt.Errorf("disks with a disk_type of local-ssd must be SCRATCH disks. disk %d is a %s disk", i, typee)
}
}

return nil
}

func buildDisks(d *schema.ResourceData, config *Config) ([]*computeBeta.AttachedDisk, error) {
project, err := getProject(d, config)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,21 @@ func TestAccComputeInstanceTemplate_enableDisplay(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 @@ -1895,22 +1910,51 @@ 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
}
network_interface {
network = "default"
}
enable_display = true
}`, acctest.RandString(10))
}

func testAccComputeInstanceTemplate_invalidDiskType() string {
return fmt.Sprintf(`
# Use this datasource insead of hardcoded values when https://github.com/hashicorp/terraform/issues/22679
# is resolved.
# 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 = "https://www.googleapis.com/compute/v1/projects/gce-uefi-images/global/images/centos-7-v20190729"
auto_delete = true
boot = true
}
disk {
auto_delete = true
type = "SCRATCH"
disk_type = "local-ssd"
}
disk {
source_image = "https://www.googleapis.com/compute/v1/projects/gce-uefi-images/global/images/centos-7-v20190729"
auto_delete = true
type = "SCRATCH"
}
network_interface {
network = "default"
}
}`, acctest.RandString(10))
}

0 comments on commit bb5f05a

Please sign in to comment.