Skip to content

Commit a722c03

Browse files
committed
Add disk related fields to google_compute_instance
1 parent d89901f commit a722c03

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

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

+28
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,34 @@ func expandNetworkPerformanceConfig(d tpgresource.TerraformResourceData, config
10241024
}, nil
10251025
}
10261026

1027+
func flattenComputeInstanceGuestOsFeatures(v interface{}) []interface{} {
1028+
if v == nil {
1029+
return nil
1030+
}
1031+
features, ok := v.([]*compute.GuestOsFeature)
1032+
if !ok {
1033+
return nil
1034+
}
1035+
var result []interface{}
1036+
for _, feature := range features {
1037+
if feature != nil && feature.Type != "" {
1038+
result = append(result, feature.Type)
1039+
}
1040+
}
1041+
return result
1042+
}
1043+
1044+
func expandComputeInstanceGuestOsFeatures(v interface{}) []*compute.GuestOsFeature {
1045+
if v == nil {
1046+
return nil
1047+
}
1048+
var result []*compute.GuestOsFeature
1049+
for _, feature := range v.([]interface{}) {
1050+
result = append(result, &compute.GuestOsFeature{Type: feature.(string)})
1051+
}
1052+
return result
1053+
}
1054+
10271055
func flattenNetworkPerformanceConfig(c *compute.NetworkPerformanceConfig) []map[string]interface{} {
10281056
if c == nil {
10291057
return nil

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

+39
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{
@@ -266,6 +268,18 @@ func ResourceComputeInstance() *schema.Resource {
266268
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.`,
267269
},
268270

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

3226+
if v, ok := d.GetOk("boot_disk.0.guest_os_features"); ok {
3227+
disk.GuestOsFeatures = expandComputeInstanceGuestOsFeatures(v)
3228+
}
3229+
32023230
if v, ok := d.GetOk("boot_disk.0.disk_encryption_key_raw"); ok {
32033231
if v != "" {
32043232
disk.DiskEncryptionKey = &compute.CustomerEncryptionKey{
@@ -3288,6 +3316,10 @@ func expandBootDisk(d *schema.ResourceData, config *transport_tpg.Config, projec
32883316
}
32893317
disk.InitializeParams.StoragePool = storagePoolUrl.(string)
32903318
}
3319+
3320+
if v, ok := d.GetOk("boot_disk.0.initialize_params.0.architecture"); ok {
3321+
disk.InitializeParams.Architecture = v.(string)
3322+
}
32913323
}
32923324

32933325
if v, ok := d.GetOk("boot_disk.0.mode"); ok {
@@ -3303,6 +3335,7 @@ func flattenBootDisk(d *schema.ResourceData, disk *compute.AttachedDisk, config
33033335
"device_name": disk.DeviceName,
33043336
"mode": disk.Mode,
33053337
"source": tpgresource.ConvertSelfLinkToV1(disk.Source),
3338+
"guest_os_features": flattenComputeInstanceGuestOsFeatures(disk.GuestOsFeatures),
33063339
// disk_encryption_key_raw is not returned from the API, so copy it from what the user
33073340
// originally specified to avoid diffs.
33083341
"disk_encryption_key_raw": d.Get("boot_disk.0.disk_encryption_key_raw"),
@@ -3334,6 +3367,7 @@ func flattenBootDisk(d *schema.ResourceData, disk *compute.AttachedDisk, config
33343367
// If the config specifies a family name that doesn't match the image name, then
33353368
// the diff won't be properly suppressed. See DiffSuppressFunc for this field.
33363369
"image": diskDetails.SourceImage,
3370+
"architecture": diskDetails.Architecture,
33373371
"size": diskDetails.SizeGb,
33383372
"labels": diskDetails.Labels,
33393373
"resource_manager_tags": d.Get("boot_disk.0.initialize_params.0.resource_manager_tags"),
@@ -3345,6 +3379,11 @@ func flattenBootDisk(d *schema.ResourceData, disk *compute.AttachedDisk, config
33453379
{{"}}"}}
33463380
}
33473381

3382+
//nil until set by the user not to cause any diffs and force a new VM
3383+
if d.Get("boot_disk.0.guest_os_features") == "" || d.Get("boot_disk.0.guest_os_features") == nil {
3384+
diskDetails.GuestOsFeatures = nil
3385+
}
3386+
33483387
if disk.DiskEncryptionKey != nil {
33493388
if disk.DiskEncryptionKey.Sha256 != "" {
33503389
result["disk_encryption_key_sha256"] = disk.DiskEncryptionKey.Sha256

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

+53
Original file line numberDiff line numberDiff line change
@@ -4530,6 +4530,33 @@ func TestAccComputeInstance_NicStackType_IPV6(t *testing.T) {
45304530
})
45314531
}
45324532

4533+
func TestAccComputeInstance_guestOsFeatures(t *testing.T) {
4534+
t.Parallel()
4535+
4536+
var instance compute.Instance
4537+
context_1 := map[string]interface{}{
4538+
"instance_name": fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10)),
4539+
"guest_os_features": `["UEFI_COMPATIBLE", "VIRTIO_SCSI_MULTIQUEUE", "GVNIC", "IDPF"]`,
4540+
}
4541+
4542+
acctest.VcrTest(t, resource.TestCase{
4543+
PreCheck: func() { acctest.AccTestPreCheck(t) },
4544+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
4545+
Steps: []resource.TestStep{
4546+
{
4547+
Config: testAccComputeInstance_guestOsFeatures(context_1),
4548+
Check: resource.ComposeTestCheckFunc(
4549+
testAccCheckComputeInstanceExists(t, "google_compute_instance.foobar", &instance),
4550+
resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.guest_os_features.0", "UEFI_COMPATIBLE"),
4551+
resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.guest_os_features.1", "VIRTIO_SCSI_MULTIQUEUE"),
4552+
resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.guest_os_features.2", "GVNIC"),
4553+
resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.guest_os_features.3", "IDPF"),
4554+
),
4555+
},
4556+
},
4557+
})
4558+
}
4559+
45334560

45344561
func testAccCheckComputeInstanceDestroyProducer(t *testing.T) func(s *terraform.State) error {
45354562
return func(s *terraform.State) error {
@@ -11619,6 +11646,32 @@ resource "google_compute_instance" "foobar" {
1161911646
`, context)
1162011647
}
1162111648

11649+
func testAccComputeInstance_guestOsFeatures(context map[string]interface{}) string {
11650+
return acctest.Nprintf(`
11651+
data "google_compute_image" "my_image" {
11652+
family = "debian-11"
11653+
project = "debian-cloud"
11654+
}
11655+
11656+
resource "google_compute_instance" "foobar" {
11657+
name = "%{instance_name}"
11658+
machine_type = "e2-medium"
11659+
zone = "us-central1-a"
11660+
11661+
boot_disk {
11662+
guest_os_features = %{guest_os_features}
11663+
initialize_params {
11664+
architecture = "X86_64"
11665+
image = data.google_compute_image.my_image.self_link
11666+
}
11667+
}
11668+
11669+
network_interface {
11670+
network = "default"
11671+
}
11672+
}`, context)
11673+
}
11674+
1162211675
func testAccComputeInstance_nicStackTypeUpdate_ipv6(context map[string]interface{}) string {
1162311676
return acctest.Nprintf(`
1162411677
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)