@@ -107,10 +107,27 @@ func resourceComputeInstance() *schema.Resource {
107
107
},
108
108
},
109
109
110
- "disk " : & schema.Schema {
110
+ "scratch_disk " : & schema.Schema {
111
111
Type : schema .TypeList ,
112
112
Optional : true ,
113
113
ForceNew : true ,
114
+ Elem : & schema.Resource {
115
+ Schema : map [string ]* schema.Schema {
116
+ "interface" : & schema.Schema {
117
+ Type : schema .TypeString ,
118
+ Optional : true ,
119
+ Default : "SCSI" ,
120
+ ValidateFunc : validation .StringInSlice ([]string {"SCSI" , "NVME" }, false ),
121
+ },
122
+ },
123
+ },
124
+ },
125
+
126
+ "disk" : & schema.Schema {
127
+ Type : schema .TypeList ,
128
+ Optional : true ,
129
+ ForceNew : true ,
130
+ Deprecated : "Use boot_disk, scratch_disk, and attached_disk instead" ,
114
131
Elem : & schema.Resource {
115
132
Schema : map [string ]* schema.Schema {
116
133
// TODO(mitchellh): one of image or disk is required
@@ -499,6 +516,15 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
499
516
disks = append (disks , bootDisk )
500
517
}
501
518
519
+ var hasScratchDisk bool
520
+ if _ , hasScratchDisk := d .GetOk ("scratch_disk" ); hasScratchDisk {
521
+ scratchDisks , err := expandScratchDisks (d , config , zone )
522
+ if err != nil {
523
+ return err
524
+ }
525
+ disks = append (disks , scratchDisks ... )
526
+ }
527
+
502
528
disksCount := d .Get ("disk.#" ).(int )
503
529
attachedDisksCount := d .Get ("attached_disk.#" ).(int )
504
530
@@ -545,6 +571,9 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err
545
571
546
572
if v , ok := d .GetOk (prefix + ".scratch" ); ok {
547
573
if v .(bool ) {
574
+ if hasScratchDisk {
575
+ return fmt .Errorf ("Cannot set scratch disks using both `scratch_disk` and `disk` properties" )
576
+ }
548
577
disk .Type = "SCRATCH"
549
578
}
550
579
}
@@ -960,11 +989,12 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
960
989
961
990
disksCount := d .Get ("disk.#" ).(int )
962
991
attachedDisksCount := d .Get ("attached_disk.#" ).(int )
992
+ scratchDisksCount := d .Get ("scratch_disk.#" ).(int )
963
993
964
994
if _ , ok := d .GetOk ("boot_disk" ); ok {
965
995
disksCount ++
966
996
}
967
- if expectedDisks := disksCount + attachedDisksCount ; len (instance .Disks ) != expectedDisks {
997
+ if expectedDisks := disksCount + attachedDisksCount + scratchDisksCount ; len (instance .Disks ) != expectedDisks {
968
998
return fmt .Errorf ("Expected %d disks, API returned %d" , expectedDisks , len (instance .Disks ))
969
999
}
970
1000
@@ -975,13 +1005,20 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
975
1005
976
1006
dIndex := 0
977
1007
adIndex := 0
1008
+ sIndex := 0
978
1009
disks := make ([]map [string ]interface {}, 0 , disksCount )
979
1010
attachedDisks := make ([]map [string ]interface {}, 0 , attachedDisksCount )
1011
+ scratchDisks := make ([]map [string ]interface {}, 0 , scratchDisksCount )
980
1012
for _ , disk := range instance .Disks {
981
1013
if _ , ok := d .GetOk ("boot_disk" ); ok && disk .Boot {
982
1014
// This disk is a boot disk and there is a boot disk set in the config, therefore
983
1015
// this is the boot disk set in the config.
984
1016
d .Set ("boot_disk" , flattenBootDisk (d , disk ))
1017
+ } else if _ , ok := d .GetOk ("scratch_disk" ); ok && disk .Type == "SCRATCH" {
1018
+ // This disk is a scratch disk and there are scratch disks set in the config, therefore
1019
+ // this is a scratch disk set in the config.
1020
+ scratchDisks = append (scratchDisks , flattenScratchDisk (disk ))
1021
+ sIndex ++
985
1022
} else if _ , ok := attachedDiskSources [disk .Source ]; ! ok {
986
1023
di := map [string ]interface {}{
987
1024
"disk" : d .Get (fmt .Sprintf ("disk.%d.disk" , dIndex )),
@@ -1013,6 +1050,7 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
1013
1050
}
1014
1051
d .Set ("disk" , disks )
1015
1052
d .Set ("attached_disk" , attachedDisks )
1053
+ d .Set ("scratch_disk" , scratchDisks )
1016
1054
1017
1055
d .Set ("self_link" , instance .SelfLink )
1018
1056
d .SetId (instance .Name )
@@ -1371,3 +1409,32 @@ func flattenBootDisk(d *schema.ResourceData, disk *compute.AttachedDisk) []map[s
1371
1409
1372
1410
return []map [string ]interface {}{result }
1373
1411
}
1412
+
1413
+ func expandScratchDisks (d * schema.ResourceData , config * Config , zone * compute.Zone ) ([]* compute.AttachedDisk , error ) {
1414
+ diskType , err := readDiskType (config , zone , "local-ssd" )
1415
+ if err != nil {
1416
+ return nil , fmt .Errorf ("Error loading disk type 'local-ssd': %s" , err )
1417
+ }
1418
+
1419
+ n := d .Get ("scratch_disk.#" ).(int )
1420
+ scratchDisks := make ([]* compute.AttachedDisk , 0 , n )
1421
+ for i := 0 ; i < n ; i ++ {
1422
+ scratchDisks = append (scratchDisks , & compute.AttachedDisk {
1423
+ AutoDelete : true ,
1424
+ Type : "SCRATCH" ,
1425
+ Interface : d .Get (fmt .Sprintf ("scratch_disk.%d.interface" , i )).(string ),
1426
+ InitializeParams : & compute.AttachedDiskInitializeParams {
1427
+ DiskType : diskType .SelfLink ,
1428
+ },
1429
+ })
1430
+ }
1431
+
1432
+ return scratchDisks , nil
1433
+ }
1434
+
1435
+ func flattenScratchDisk (disk * compute.AttachedDisk ) map [string ]interface {} {
1436
+ result := map [string ]interface {}{
1437
+ "interface" : disk .Interface ,
1438
+ }
1439
+ return result
1440
+ }
0 commit comments