Skip to content

Commit

Permalink
only allow instance templates with 375gb scratch disks (#2495)
Browse files Browse the repository at this point in the history
  • Loading branch information
danawillow authored and rileykarson committed Oct 24, 2019
1 parent e9e46bf commit ae81037
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,11 @@ func resourceComputeInstanceTemplateSourceImageCustomizeDiff(diff *schema.Resour
}

func resourceComputeInstanceTemplateScratchDiskCustomizeDiff(diff *schema.ResourceDiff, meta interface{}) error {
// separate func to allow unit testing
return resourceComputeInstanceTemplateScratchDiskCustomizeDiffFunc(diff)
}

func resourceComputeInstanceTemplateScratchDiskCustomizeDiffFunc(diff TerraformResourceDiff) error {
numDisks := diff.Get("disk.#").(int)
for i := 0; i < numDisks; i++ {
// misspelled on purpose, type is a special symbol
Expand All @@ -534,6 +539,11 @@ func resourceComputeInstanceTemplateScratchDiskCustomizeDiff(diff *schema.Resour
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)
}

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

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,54 @@ func TestComputeInstanceTemplate_reorderDisks(t *testing.T) {
}
}

func TestComputeInstanceTemplate_scratchDiskSizeCustomizeDiff(t *testing.T) {
t.Parallel()

cases := map[string]struct {
Typee string // misspelled on purpose, type is a special symbol
DiskType string
DiskSize int
ExpectError bool
}{
"scratch disk correct size": {
Typee: "SCRATCH",
DiskType: "local-ssd",
DiskSize: 375,
ExpectError: false,
},
"scratch disk incorrect size": {
Typee: "SCRATCH",
DiskType: "local-ssd",
DiskSize: 300,
ExpectError: true,
},
"non-scratch disk": {
Typee: "PERSISTENT",
DiskType: "",
DiskSize: 300,
ExpectError: false,
},
}

for tn, tc := range cases {
d := &ResourceDiffMock{
After: map[string]interface{}{
"disk.#": 1,
"disk.0.type": tc.Typee,
"disk.0.disk_type": tc.DiskType,
"disk.0.disk_size_gb": tc.DiskSize,
},
}
err := resourceComputeInstanceTemplateScratchDiskCustomizeDiffFunc(d)
if tc.ExpectError && err == nil {
t.Errorf("%s failed, expected error but was none", tn)
}
if !tc.ExpectError && err != nil {
t.Errorf("%s failed, found unexpected error: %s", tn, err)
}
}
}

func TestAccComputeInstanceTemplate_basic(t *testing.T) {
t.Parallel()

Expand Down
4 changes: 4 additions & 0 deletions third_party/terraform/utils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func (d *ResourceDiffMock) GetChange(key string) (interface{}, interface{}) {
return d.Before[key], d.After[key]
}

func (d *ResourceDiffMock) Get(key string) interface{} {
return d.After[key]
}

func (d *ResourceDiffMock) Clear(key string) error {
if d.Cleared == nil {
d.Cleared = map[string]struct{}{}
Expand Down
1 change: 1 addition & 0 deletions third_party/terraform/utils/utils.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type TerraformResourceData interface {

type TerraformResourceDiff interface {
GetChange(string) (interface{}, interface{})
Get(string) interface{}
Clear(string) error
}

Expand Down

0 comments on commit ae81037

Please sign in to comment.