Skip to content

Commit

Permalink
Assume scratch disk size when not returned by the API (#4014) (#2515)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Sep 25, 2020
1 parent aa7ef01 commit 4384851
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/4014.txt
Original file line number Diff line number Diff line change
@@ -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.
```
15 changes: 12 additions & 3 deletions google-beta/resource_compute_instance_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var (
}
)

var REQUIRED_SCRATCH_DISK_SIZE_GB = 375

func resourceComputeInstanceTemplate() *schema.Resource {
return &schema.Resource{
Create: resourceComputeInstanceTemplateCreate,
Expand Down Expand Up @@ -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.`,
},

Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4384851

Please sign in to comment.