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 #4395

Closed
Closed
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
5 changes: 2 additions & 3 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,9 @@ func Provider() terraform.ResourceProvider {
return provider
}

// Generated resources: 81
// Generated resources: 80
// Generated IAM resources: 21
// Total generated resources: 102
// Total generated resources: 101
func ResourceMap() map[string]*schema.Resource {
resourceMap, _ := ResourceMapWithErrors()
return resourceMap
Expand Down Expand Up @@ -477,7 +477,6 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_compute_router_nat": resourceComputeRouterNat(),
"google_compute_snapshot": resourceComputeSnapshot(),
"google_compute_ssl_certificate": resourceComputeSslCertificate(),
"google_compute_reservation": resourceComputeReservation(),
"google_compute_ssl_policy": resourceComputeSslPolicy(),
"google_compute_subnetwork": resourceComputeSubnetwork(),
"google_compute_target_http_proxy": resourceComputeTargetHttpProxy(),
Expand Down
26 changes: 24 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,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
53 changes: 53 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,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))
}
Loading