From 017fd3fe6ec576957c27ee909704260d9c4f4eb2 Mon Sep 17 00:00:00 2001 From: Cameron Thornton Date: Thu, 24 Sep 2020 16:13:05 -0500 Subject: [PATCH] Set scratch disk size when not returned by the API --- .../resource_compute_instance_template.go.erb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/third_party/terraform/resources/resource_compute_instance_template.go.erb b/third_party/terraform/resources/resource_compute_instance_template.go.erb index c0847a4b85c1..becda48c1a6c 100644 --- a/third_party/terraform/resources/resource_compute_instance_template.go.erb +++ b/third_party/terraform/resources/resource_compute_instance_template.go.erb @@ -36,6 +36,8 @@ var ( } ) +var REQUIRED_SCRATCH_DISK_SIZE_GB = 375 + func resourceComputeInstanceTemplate() *schema.Resource { return &schema.Resource{ Create: resourceComputeInstanceTemplateCreate, @@ -131,6 +133,7 @@ func resourceComputeInstanceTemplate() *schema.Resource { Type: schema.TypeInt, Optional: true, ForceNew: true, + Computed: true, Description: `The size of the image in gigabytes. If not specified, it will inherit the size of its base image. For SCRATCH disks, the size must be exactly 375GB.`, }, @@ -671,8 +674,8 @@ func resourceComputeInstanceTemplateScratchDiskCustomizeDiffFunc(diff TerraformR } diskSize := diff.Get(fmt.Sprintf("disk.%d.disk_size_gb", i)).(int) - if typee == "SCRATCH" && diskSize != 375 { - return fmt.Errorf("SCRATCH disks must be exactly 375GB, disk %d is %d", i, diskSize) + if typee == "SCRATCH" && diskSize != REQUIRED_SCRATCH_DISK_SIZE_GB { + return fmt.Errorf("SCRATCH disks must be exactly %dGB, disk %d is %d", REQUIRED_SCRATCH_DISK_SIZE_GB, i, diskSize) } } @@ -953,8 +956,14 @@ func flattenDisk(disk *computeBeta.AttachedDisk, defaultProject string) (map[str } diskMap["disk_type"] = disk.InitializeParams.DiskType diskMap["disk_name"] = disk.InitializeParams.DiskName - diskMap["disk_size_gb"] = disk.InitializeParams.DiskSizeGb diskMap["labels"] = disk.InitializeParams.Labels + // The API does not return a disk size value for scratch disks. They can only be one size, + // so we can assume that size here. + if disk.InitializeParams.DiskSizeGb == 0 && disk.Type == "SCRATCH" { + diskMap["disk_size_gb"] = REQUIRED_SCRATCH_DISK_SIZE_GB + } else { + diskMap["disk_size_gb"] = disk.InitializeParams.DiskSizeGb + } } if disk.DiskEncryptionKey != nil {