Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation for scratch disks in Instance Template #2282

Merged
merged 4 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -515,6 +520,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 @@ -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,41 @@ resource "google_compute_instance_template" "foobar" {
}
}`, acctest.RandString(10), enableSecureBoot, enableVtpm, enableIntegrityMonitoring)
}

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