Skip to content

Commit 35941ed

Browse files
authored
Add disk related fields to google_compute_instance (#13193)
1 parent 3d42c99 commit 35941ed

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

mmv1/third_party/terraform/services/compute/compute_instance_helpers.go.tmpl

+28
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,34 @@ func expandNetworkPerformanceConfig(d tpgresource.TerraformResourceData, config
10511051
}, nil
10521052
}
10531053

1054+
func flattenComputeInstanceGuestOsFeatures(v interface{}) []interface{} {
1055+
if v == nil {
1056+
return nil
1057+
}
1058+
features, ok := v.([]*compute.GuestOsFeature)
1059+
if !ok {
1060+
return nil
1061+
}
1062+
var result []interface{}
1063+
for _, feature := range features {
1064+
if feature != nil && feature.Type != "" {
1065+
result = append(result, feature.Type)
1066+
}
1067+
}
1068+
return result
1069+
}
1070+
1071+
func expandComputeInstanceGuestOsFeatures(v interface{}) []*compute.GuestOsFeature {
1072+
if v == nil {
1073+
return nil
1074+
}
1075+
var result []*compute.GuestOsFeature
1076+
for _, feature := range v.([]interface{}) {
1077+
result = append(result, &compute.GuestOsFeature{Type: feature.(string)})
1078+
}
1079+
return result
1080+
}
1081+
10541082
func flattenNetworkPerformanceConfig(c *compute.NetworkPerformanceConfig) []map[string]interface{} {
10551083
if c == nil {
10561084
return nil

mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl

+34
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ var (
6464
}
6565

6666
bootDiskKeys = []string{
67+
"boot_disk.0.guest_os_features",
6768
"boot_disk.0.auto_delete",
6869
"boot_disk.0.device_name",
6970
"boot_disk.0.disk_encryption_key_raw",
@@ -84,6 +85,7 @@ var (
8485
"boot_disk.0.initialize_params.0.enable_confidential_compute",
8586
"boot_disk.0.initialize_params.0.storage_pool",
8687
"boot_disk.0.initialize_params.0.resource_policies",
88+
"boot_disk.0.initialize_params.0.architecture",
8789
}
8890

8991
schedulingKeys = []string{
@@ -267,6 +269,18 @@ func ResourceComputeInstance() *schema.Resource {
267269
Description: `The self_link of the encryption key that is stored in Google Cloud KMS to encrypt this disk. Only one of kms_key_self_link and disk_encryption_key_raw may be set.`,
268270
},
269271

272+
"guest_os_features": {
273+
Type: schema.TypeList,
274+
Optional: true,
275+
AtLeastOneOf: bootDiskKeys,
276+
ForceNew: true,
277+
Computed: true,
278+
Description: `A list of features to enable on the guest operating system. Applicable only for bootable images.`,
279+
Elem: &schema.Schema{
280+
Type: schema.TypeString,
281+
},
282+
},
283+
270284
"initialize_params": {
271285
Type: schema.TypeList,
272286
Optional: true,
@@ -369,6 +383,16 @@ func ResourceComputeInstance() *schema.Resource {
369383
DiffSuppressFunc: tpgresource.CompareResourceNames,
370384
Description: `The URL of the storage pool in which the new disk is created`,
371385
},
386+
387+
"architecture": {
388+
Type: schema.TypeString,
389+
Optional: true,
390+
Computed: true,
391+
ForceNew: true,
392+
AtLeastOneOf: initializeParamsKeys,
393+
ValidateFunc: validation.StringInSlice([]string{"X86_64", "ARM64"}, false),
394+
Description: `The architecture of the disk. One of "X86_64" or "ARM64".`,
395+
},
372396
},
373397
},
374398
},
@@ -3209,6 +3233,10 @@ func expandBootDisk(d *schema.ResourceData, config *transport_tpg.Config, projec
32093233
disk.Interface = v.(string)
32103234
}
32113235

3236+
if v, ok := d.GetOk("boot_disk.0.guest_os_features"); ok {
3237+
disk.GuestOsFeatures = expandComputeInstanceGuestOsFeatures(v)
3238+
}
3239+
32123240
if v, ok := d.GetOk("boot_disk.0.disk_encryption_key_raw"); ok {
32133241
if v != "" {
32143242
disk.DiskEncryptionKey = &compute.CustomerEncryptionKey{
@@ -3298,6 +3326,10 @@ func expandBootDisk(d *schema.ResourceData, config *transport_tpg.Config, projec
32983326
}
32993327
disk.InitializeParams.StoragePool = storagePoolUrl.(string)
33003328
}
3329+
3330+
if v, ok := d.GetOk("boot_disk.0.initialize_params.0.architecture"); ok {
3331+
disk.InitializeParams.Architecture = v.(string)
3332+
}
33013333
}
33023334

33033335
if v, ok := d.GetOk("boot_disk.0.mode"); ok {
@@ -3313,6 +3345,7 @@ func flattenBootDisk(d *schema.ResourceData, disk *compute.AttachedDisk, config
33133345
"device_name": disk.DeviceName,
33143346
"mode": disk.Mode,
33153347
"source": tpgresource.ConvertSelfLinkToV1(disk.Source),
3348+
"guest_os_features": flattenComputeInstanceGuestOsFeatures(disk.GuestOsFeatures),
33163349
// disk_encryption_key_raw is not returned from the API, so copy it from what the user
33173350
// originally specified to avoid diffs.
33183351
"disk_encryption_key_raw": d.Get("boot_disk.0.disk_encryption_key_raw"),
@@ -3344,6 +3377,7 @@ func flattenBootDisk(d *schema.ResourceData, disk *compute.AttachedDisk, config
33443377
// If the config specifies a family name that doesn't match the image name, then
33453378
// the diff won't be properly suppressed. See DiffSuppressFunc for this field.
33463379
"image": diskDetails.SourceImage,
3380+
"architecture": diskDetails.Architecture,
33473381
"size": diskDetails.SizeGb,
33483382
"labels": diskDetails.Labels,
33493383
"resource_manager_tags": d.Get("boot_disk.0.initialize_params.0.resource_manager_tags"),

mmv1/third_party/terraform/services/compute/resource_compute_instance_test.go.tmpl

+53
Original file line numberDiff line numberDiff line change
@@ -4608,6 +4608,33 @@ func TestAccComputeInstance_NicStackType_IPV6(t *testing.T) {
46084608
})
46094609
}
46104610

4611+
func TestAccComputeInstance_guestOsFeatures(t *testing.T) {
4612+
t.Parallel()
4613+
4614+
var instance compute.Instance
4615+
context_1 := map[string]interface{}{
4616+
"instance_name": fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10)),
4617+
"guest_os_features": `["UEFI_COMPATIBLE", "VIRTIO_SCSI_MULTIQUEUE", "GVNIC", "IDPF"]`,
4618+
}
4619+
4620+
acctest.VcrTest(t, resource.TestCase{
4621+
PreCheck: func() { acctest.AccTestPreCheck(t) },
4622+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
4623+
Steps: []resource.TestStep{
4624+
{
4625+
Config: testAccComputeInstance_guestOsFeatures(context_1),
4626+
Check: resource.ComposeTestCheckFunc(
4627+
testAccCheckComputeInstanceExists(t, "google_compute_instance.foobar", &instance),
4628+
resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.guest_os_features.0", "UEFI_COMPATIBLE"),
4629+
resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.guest_os_features.1", "VIRTIO_SCSI_MULTIQUEUE"),
4630+
resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.guest_os_features.2", "GVNIC"),
4631+
resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.guest_os_features.3", "IDPF"),
4632+
),
4633+
},
4634+
},
4635+
})
4636+
}
4637+
46114638

46124639
func testAccCheckComputeInstanceDestroyProducer(t *testing.T) func(s *terraform.State) error {
46134640
return func(s *terraform.State) error {
@@ -11804,6 +11831,32 @@ resource "google_compute_instance" "foobar" {
1180411831
`, context)
1180511832
}
1180611833

11834+
func testAccComputeInstance_guestOsFeatures(context map[string]interface{}) string {
11835+
return acctest.Nprintf(`
11836+
data "google_compute_image" "my_image" {
11837+
family = "debian-11"
11838+
project = "debian-cloud"
11839+
}
11840+
11841+
resource "google_compute_instance" "foobar" {
11842+
name = "%{instance_name}"
11843+
machine_type = "e2-medium"
11844+
zone = "us-central1-a"
11845+
11846+
boot_disk {
11847+
guest_os_features = %{guest_os_features}
11848+
initialize_params {
11849+
architecture = "X86_64"
11850+
image = data.google_compute_image.my_image.self_link
11851+
}
11852+
}
11853+
11854+
network_interface {
11855+
network = "default"
11856+
}
11857+
}`, context)
11858+
}
11859+
1180711860
func testAccComputeInstance_nicStackTypeUpdate_ipv6(context map[string]interface{}) string {
1180811861
return acctest.Nprintf(`
1180911862
data "google_compute_image" "my_image" {

mmv1/third_party/terraform/website/docs/r/compute_instance.html.markdown

+4
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ is desired, you will need to modify your state file manually using
265265
* `mode` - (Optional) The mode in which to attach this disk, either `READ_WRITE`
266266
or `READ_ONLY`. If not specified, the default is to attach the disk in `READ_WRITE` mode.
267267

268+
* `guest_os_features` - (optional) A list of features to enable on the guest operating system. Applicable only for bootable images. Read [Enabling guest operating system features](https://cloud.google.com/compute/docs/images/create-delete-deprecate-private-images#guest-os-features) to see a list of available options.
269+
268270
* `disk_encryption_key_raw` - (Optional) A 256-bit [customer-supplied encryption key]
269271
(https://cloud.google.com/compute/docs/disks/customer-supplied-encryption),
270272
encoded in [RFC 4648 base64](https://tools.ietf.org/html/rfc4648#section-4)
@@ -303,6 +305,8 @@ is desired, you will need to modify your state file manually using
303305
* `labels` - (Optional) A set of key/value label pairs assigned to the disk. This
304306
field is only applicable for persistent disks.
305307

308+
* `architecture` - (Optional) The architecture of the attached disk. Valid values are `ARM64` or `x86_64`.
309+
306310
* `resource_manager_tags` - (Optional) A tag is a key-value pair that can be attached to a Google Cloud resource. You can use tags to conditionally allow or deny policies based on whether a resource has a specific tag. This value is not returned by the API. In Terraform, this value cannot be updated and changing it will recreate the resource.
307311

308312
* `resource_policies` - (Optional) A list of self_links of resource policies to attach to the instance's boot disk. Modifying this list will cause the instance to recreate, so any external values are not set until the user specifies this field. Currently a max of 1 resource policy is supported.

0 commit comments

Comments
 (0)