@@ -305,6 +305,36 @@ func TestAccComputeInstanceFromMachineImage_confidentialInstanceConfigMain(t *te
305
305
})
306
306
}
307
307
308
+ func TestAccComputeInstanceFromMachineImage_withSourceMachineImageEncryptionKey (t * testing.T ) {
309
+ t .Parallel ()
310
+
311
+ var instance compute.Instance
312
+ var instanceName = fmt .Sprintf ("tf-test-%s" , acctest .RandString (t , 10 ))
313
+ var resourceName = "google_compute_instance_from_machine_image.foobar"
314
+ var machineImageName = fmt .Sprintf ("tf-test-%s" , acctest .RandString (t , 10 ))
315
+ bootDiskID := "tf-instance-from-mi-test-disk"
316
+ serviceAccountEmail := fmt .Sprintf ("%s@%s.iam.gserviceaccount.com" , "tf-test-sa" , envvar .GetTestProjectFromEnv ())
317
+ keyRingSuffix := acctest .RandString (t , 10 )
318
+ keyNameSuffix := acctest .RandString (t , 10 )
319
+
320
+ acctest .VcrTest (t , resource.TestCase {
321
+ PreCheck : func () { acctest .AccTestPreCheck (t ) },
322
+ ProtoV5ProviderFactories : acctest .ProtoV5ProviderFactories (t ),
323
+ CheckDestroy : testAccCheckComputeInstanceDestroyProducer (t ),
324
+ Steps : []resource.TestStep {
325
+ {
326
+ Config : testAccComputeInstanceFromMachineImage_withSourceMachineImageEncryptionKey (instanceName , machineImageName , bootDiskID , serviceAccountEmail , keyRingSuffix , keyNameSuffix ),
327
+ Check : resource .ComposeTestCheckFunc (
328
+ testAccCheckComputeInstanceExists (t , resourceName , & instance ),
329
+ resource .TestCheckResourceAttr (resourceName ,
330
+ "source_machine_image_encryption_key.0.kms_key_name" ,
331
+ fmt .Sprintf ("projects/%s/locations/global/keyRings/tf-test-keyring-%s/cryptoKeys/tf-test-key-%s" , envvar .GetTestProjectFromEnv (), keyRingSuffix , keyNameSuffix )),
332
+ ),
333
+ },
334
+ },
335
+ })
336
+ }
337
+
308
338
func testAccCheckComputeInstanceFromMachineImageDestroyProducer (t * testing.T ) func (s * terraform.State ) error {
309
339
return func (s * terraform.State ) error {
310
340
config := acctest .GoogleProviderConfig (t )
@@ -1104,3 +1134,88 @@ resource "google_compute_instance_from_machine_image" "foobar" {
1104
1134
}
1105
1135
` , projectID , projectID , org , billingId , instance , instance , newInstance )
1106
1136
}
1137
+
1138
+ func testAccComputeInstanceFromMachineImage_withSourceMachineImageEncryptionKey (instanceName , machineImageName , bootDiskID , serviceAccountEmail , keyRingSuffix , keyNameSuffix string ) string {
1139
+ return fmt .Sprintf (`
1140
+ data "google_compute_image" "my_image" {
1141
+ family = "debian-11"
1142
+ project = "debian-cloud"
1143
+ }
1144
+
1145
+ resource "google_service_account" "test_service_account" {
1146
+ account_id = "tf-test-sa"
1147
+ display_name = "Test Service Account"
1148
+ }
1149
+
1150
+ resource "google_kms_key_ring" "keyring" {
1151
+ name = "tf-test-keyring-%s"
1152
+ location = "global"
1153
+ }
1154
+
1155
+ resource "google_kms_crypto_key" "key" {
1156
+ name = "tf-test-key-%s"
1157
+ key_ring = google_kms_key_ring.keyring.id
1158
+
1159
+ lifecycle {
1160
+ prevent_destroy = false
1161
+ }
1162
+ }
1163
+
1164
+ resource "google_kms_crypto_key_iam_member" "crypto_key" {
1165
+ crypto_key_id = google_kms_crypto_key.key.id
1166
+ role = "roles/cloudkms.cryptoKeyEncrypterDecrypter"
1167
+ member = "serviceAccount:${google_service_account.test_service_account.email}"
1168
+ }
1169
+
1170
+ resource "google_compute_machine_image" "foobar" {
1171
+ name = "%s"
1172
+ source_instance = google_compute_instance.mi-source.id
1173
+ machine_image_encryption_key {
1174
+ kms_key_name = google_kms_crypto_key.key.id
1175
+ kms_key_service_account = google_service_account.test_service_account.email
1176
+ }
1177
+ }
1178
+
1179
+ resource "google_compute_instance" "mi-source" {
1180
+ name = "%s-source"
1181
+ machine_type = "e2-medium"
1182
+ zone = "us-central1-a"
1183
+
1184
+ boot_disk {
1185
+ initialize_params {
1186
+ image = data.google_compute_image.my_image.self_link
1187
+ }
1188
+ }
1189
+
1190
+ network_interface {
1191
+ network = "default"
1192
+ }
1193
+
1194
+ service_account {
1195
+ email = google_service_account.test_service_account.email
1196
+ scopes = ["cloud-platform"]
1197
+ }
1198
+
1199
+ scheduling {
1200
+ automatic_restart = true
1201
+ }
1202
+ }
1203
+
1204
+ resource "google_compute_instance_from_machine_image" "foobar" {
1205
+ name = "%s"
1206
+ zone = "us-central1-a"
1207
+
1208
+ source_machine_image = google_compute_machine_image.foobar.self_link
1209
+
1210
+ source_machine_image_encryption_key {
1211
+ kms_key_name = google_kms_crypto_key.key.id
1212
+ kms_key_service_account = google_service_account.test_service_account.email
1213
+ }
1214
+
1215
+ service_account {
1216
+ email = google_service_account.test_service_account.email
1217
+ scopes = ["cloud-platform"]
1218
+ }
1219
+ }
1220
+ ` , keyRingSuffix , keyNameSuffix , machineImageName , instanceName , instanceName )
1221
+ }
0 commit comments