Skip to content

Commit a880124

Browse files
Add disk related fields to google_compute_instance (#13193) (#21875)
[upstream:35941edf7d2de7b08fffcdd8623f8d89b50b1c73] Signed-off-by: Modular Magician <[email protected]>
1 parent 5636d4a commit a880124

File tree

5 files changed

+126
-4
lines changed

5 files changed

+126
-4
lines changed

.changelog/13193.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: added `architecture` and `guest_os_features` to `google_compute_instance`
3+
```

google/services/compute/compute_instance_helpers.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,34 @@ func expandNetworkPerformanceConfig(d tpgresource.TerraformResourceData, config
852852
}, nil
853853
}
854854

855+
func flattenComputeInstanceGuestOsFeatures(v interface{}) []interface{} {
856+
if v == nil {
857+
return nil
858+
}
859+
features, ok := v.([]*compute.GuestOsFeature)
860+
if !ok {
861+
return nil
862+
}
863+
var result []interface{}
864+
for _, feature := range features {
865+
if feature != nil && feature.Type != "" {
866+
result = append(result, feature.Type)
867+
}
868+
}
869+
return result
870+
}
871+
872+
func expandComputeInstanceGuestOsFeatures(v interface{}) []*compute.GuestOsFeature {
873+
if v == nil {
874+
return nil
875+
}
876+
var result []*compute.GuestOsFeature
877+
for _, feature := range v.([]interface{}) {
878+
result = append(result, &compute.GuestOsFeature{Type: feature.(string)})
879+
}
880+
return result
881+
}
882+
855883
func flattenNetworkPerformanceConfig(c *compute.NetworkPerformanceConfig) []map[string]interface{} {
856884
if c == nil {
857885
return nil

google/services/compute/resource_compute_instance.go

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ var (
6262
}
6363

6464
bootDiskKeys = []string{
65+
"boot_disk.0.guest_os_features",
6566
"boot_disk.0.auto_delete",
6667
"boot_disk.0.device_name",
6768
"boot_disk.0.disk_encryption_key_raw",
@@ -82,6 +83,7 @@ var (
8283
"boot_disk.0.initialize_params.0.enable_confidential_compute",
8384
"boot_disk.0.initialize_params.0.storage_pool",
8485
"boot_disk.0.initialize_params.0.resource_policies",
86+
"boot_disk.0.initialize_params.0.architecture",
8587
}
8688

8789
schedulingKeys = []string{
@@ -260,6 +262,18 @@ func ResourceComputeInstance() *schema.Resource {
260262
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.`,
261263
},
262264

265+
"guest_os_features": {
266+
Type: schema.TypeList,
267+
Optional: true,
268+
AtLeastOneOf: bootDiskKeys,
269+
ForceNew: true,
270+
Computed: true,
271+
Description: `A list of features to enable on the guest operating system. Applicable only for bootable images.`,
272+
Elem: &schema.Schema{
273+
Type: schema.TypeString,
274+
},
275+
},
276+
263277
"initialize_params": {
264278
Type: schema.TypeList,
265279
Optional: true,
@@ -362,6 +376,16 @@ func ResourceComputeInstance() *schema.Resource {
362376
DiffSuppressFunc: tpgresource.CompareResourceNames,
363377
Description: `The URL of the storage pool in which the new disk is created`,
364378
},
379+
380+
"architecture": {
381+
Type: schema.TypeString,
382+
Optional: true,
383+
Computed: true,
384+
ForceNew: true,
385+
AtLeastOneOf: initializeParamsKeys,
386+
ValidateFunc: validation.StringInSlice([]string{"X86_64", "ARM64"}, false),
387+
Description: `The architecture of the disk. One of "X86_64" or "ARM64".`,
388+
},
365389
},
366390
},
367391
},
@@ -2986,6 +3010,10 @@ func expandBootDisk(d *schema.ResourceData, config *transport_tpg.Config, projec
29863010
disk.Interface = v.(string)
29873011
}
29883012

3013+
if v, ok := d.GetOk("boot_disk.0.guest_os_features"); ok {
3014+
disk.GuestOsFeatures = expandComputeInstanceGuestOsFeatures(v)
3015+
}
3016+
29893017
if v, ok := d.GetOk("boot_disk.0.disk_encryption_key_raw"); ok {
29903018
if v != "" {
29913019
disk.DiskEncryptionKey = &compute.CustomerEncryptionKey{
@@ -3075,6 +3103,10 @@ func expandBootDisk(d *schema.ResourceData, config *transport_tpg.Config, projec
30753103
}
30763104
disk.InitializeParams.StoragePool = storagePoolUrl.(string)
30773105
}
3106+
3107+
if v, ok := d.GetOk("boot_disk.0.initialize_params.0.architecture"); ok {
3108+
disk.InitializeParams.Architecture = v.(string)
3109+
}
30783110
}
30793111

30803112
if v, ok := d.GetOk("boot_disk.0.mode"); ok {
@@ -3086,10 +3118,11 @@ func expandBootDisk(d *schema.ResourceData, config *transport_tpg.Config, projec
30863118

30873119
func flattenBootDisk(d *schema.ResourceData, disk *compute.AttachedDisk, config *transport_tpg.Config) []map[string]interface{} {
30883120
result := map[string]interface{}{
3089-
"auto_delete": disk.AutoDelete,
3090-
"device_name": disk.DeviceName,
3091-
"mode": disk.Mode,
3092-
"source": tpgresource.ConvertSelfLinkToV1(disk.Source),
3121+
"auto_delete": disk.AutoDelete,
3122+
"device_name": disk.DeviceName,
3123+
"mode": disk.Mode,
3124+
"source": tpgresource.ConvertSelfLinkToV1(disk.Source),
3125+
"guest_os_features": flattenComputeInstanceGuestOsFeatures(disk.GuestOsFeatures),
30933126
// disk_encryption_key_raw is not returned from the API, so copy it from what the user
30943127
// originally specified to avoid diffs.
30953128
"disk_encryption_key_raw": d.Get("boot_disk.0.disk_encryption_key_raw"),
@@ -3121,6 +3154,7 @@ func flattenBootDisk(d *schema.ResourceData, disk *compute.AttachedDisk, config
31213154
// If the config specifies a family name that doesn't match the image name, then
31223155
// the diff won't be properly suppressed. See DiffSuppressFunc for this field.
31233156
"image": diskDetails.SourceImage,
3157+
"architecture": diskDetails.Architecture,
31243158
"size": diskDetails.SizeGb,
31253159
"labels": diskDetails.Labels,
31263160
"resource_manager_tags": d.Get("boot_disk.0.initialize_params.0.resource_manager_tags"),

google/services/compute/resource_compute_instance_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3857,6 +3857,33 @@ func TestAccComputeInstance_NicStackType_IPV6(t *testing.T) {
38573857
})
38583858
}
38593859

3860+
func TestAccComputeInstance_guestOsFeatures(t *testing.T) {
3861+
t.Parallel()
3862+
3863+
var instance compute.Instance
3864+
context_1 := map[string]interface{}{
3865+
"instance_name": fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10)),
3866+
"guest_os_features": `["UEFI_COMPATIBLE", "VIRTIO_SCSI_MULTIQUEUE", "GVNIC", "IDPF"]`,
3867+
}
3868+
3869+
acctest.VcrTest(t, resource.TestCase{
3870+
PreCheck: func() { acctest.AccTestPreCheck(t) },
3871+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
3872+
Steps: []resource.TestStep{
3873+
{
3874+
Config: testAccComputeInstance_guestOsFeatures(context_1),
3875+
Check: resource.ComposeTestCheckFunc(
3876+
testAccCheckComputeInstanceExists(t, "google_compute_instance.foobar", &instance),
3877+
resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.guest_os_features.0", "UEFI_COMPATIBLE"),
3878+
resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.guest_os_features.1", "VIRTIO_SCSI_MULTIQUEUE"),
3879+
resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.guest_os_features.2", "GVNIC"),
3880+
resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.guest_os_features.3", "IDPF"),
3881+
),
3882+
},
3883+
},
3884+
})
3885+
}
3886+
38603887
func testAccCheckComputeInstanceDestroyProducer(t *testing.T) func(s *terraform.State) error {
38613888
return func(s *terraform.State) error {
38623889
config := acctest.GoogleProviderConfig(t)
@@ -10029,6 +10056,32 @@ resource "google_compute_instance" "foobar" {
1002910056
`, context)
1003010057
}
1003110058

10059+
func testAccComputeInstance_guestOsFeatures(context map[string]interface{}) string {
10060+
return acctest.Nprintf(`
10061+
data "google_compute_image" "my_image" {
10062+
family = "debian-11"
10063+
project = "debian-cloud"
10064+
}
10065+
10066+
resource "google_compute_instance" "foobar" {
10067+
name = "%{instance_name}"
10068+
machine_type = "e2-medium"
10069+
zone = "us-central1-a"
10070+
10071+
boot_disk {
10072+
guest_os_features = %{guest_os_features}
10073+
initialize_params {
10074+
architecture = "X86_64"
10075+
image = data.google_compute_image.my_image.self_link
10076+
}
10077+
}
10078+
10079+
network_interface {
10080+
network = "default"
10081+
}
10082+
}`, context)
10083+
}
10084+
1003210085
func testAccComputeInstance_nicStackTypeUpdate_ipv6(context map[string]interface{}) string {
1003310086
return acctest.Nprintf(`
1003410087
data "google_compute_image" "my_image" {

website/docs/r/compute_instance.html.markdown

Lines changed: 4 additions & 0 deletions
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)