diff --git a/.changelog/4014.txt b/.changelog/4014.txt new file mode 100644 index 0000000000..dc7e2bf8a0 --- /dev/null +++ b/.changelog/4014.txt @@ -0,0 +1,3 @@ +```release-note:bug +compute: fixed an issue where `google_compute_instance_template` would throw an error for unspecified `disk_size_gb` values while upgrading the provider. +``` diff --git a/google-beta/resource_compute_instance_template.go b/google-beta/resource_compute_instance_template.go index 5d285cdef0..ffbf119b90 100644 --- a/google-beta/resource_compute_instance_template.go +++ b/google-beta/resource_compute_instance_template.go @@ -33,6 +33,8 @@ var ( } ) +var REQUIRED_SCRATCH_DISK_SIZE_GB = 375 + func resourceComputeInstanceTemplate() *schema.Resource { return &schema.Resource{ Create: resourceComputeInstanceTemplateCreate, @@ -128,6 +130,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.`, }, @@ -664,8 +667,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) } } @@ -944,8 +947,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 {