@@ -282,6 +282,40 @@ func TestAccComputeInstance_diskEncryption(t *testing.T) {
282
282
})
283
283
}
284
284
285
+ func TestAccComputeInstance_kmsDiskEncryption (t * testing.T ) {
286
+ t .Parallel ()
287
+
288
+ var instance compute.Instance
289
+ var instanceName = fmt .Sprintf ("instance-test-%s" , acctest .RandString (10 ))
290
+ bootKmsKeyName := "projects/project/locations/us-central1/keyRings/testing-cloud-kms/cryptoKeys/key-0/cryptoKeyVersions/1"
291
+ diskNameToEncryptionKey := map [string ]* compute.CustomerEncryptionKey {
292
+ fmt .Sprintf ("instance-testd-%s" , acctest .RandString (10 )): {
293
+ KmsKeyName : "projects/project/locations/us-central1/keyRings/testing-cloud-kms/cryptoKeys/key-1/cryptoKeyVersions/1" ,
294
+ },
295
+ fmt .Sprintf ("instance-testd-%s" , acctest .RandString (10 )): {
296
+ KmsKeyName : "projects/project/locations/us-central1/keyRings/testing-cloud-kms/cryptoKeys/key-2/cryptoKeyVersions/1" ,
297
+ },
298
+ fmt .Sprintf ("instance-testd-%s" , acctest .RandString (10 )): {
299
+ KmsKeyName : "projects/project/locations/us-central1/keyRings/testing-cloud-kms/cryptoKeys/key-3/cryptoKeyVersions/1" ,
300
+ },
301
+ }
302
+
303
+ resource .Test (t , resource.TestCase {
304
+ PreCheck : func () { testAccPreCheck (t ) },
305
+ Providers : testAccProviders ,
306
+ CheckDestroy : testAccCheckComputeInstanceDestroy ,
307
+ Steps : []resource.TestStep {
308
+ {
309
+ Config : testAccComputeInstance_disks_encryption (bootKmsKeyName , diskNameToEncryptionKey , instanceName ),
310
+ Check : resource .ComposeTestCheckFunc (
311
+ testAccCheckComputeInstanceExists ("google_compute_instance.foobar" , & instance ),
312
+ testAccCheckComputeInstanceDiskKmsEncryptionKey ("google_compute_instance.foobar" , & instance , bootKmsKeyName , diskNameToEncryptionKey ),
313
+ ),
314
+ },
315
+ },
316
+ })
317
+ }
318
+
285
319
func TestAccComputeInstance_attachedDisk (t * testing.T ) {
286
320
t .Parallel ()
287
321
@@ -1363,6 +1397,50 @@ func testAccCheckComputeInstanceDiskEncryptionKey(n string, instance *compute.In
1363
1397
}
1364
1398
}
1365
1399
1400
+ func testAccCheckComputeInstanceDiskKmsEncryptionKey (n string , instance * compute.Instance , bootDiskEncryptionKey string , diskNameToEncryptionKey map [string ]* compute.CustomerEncryptionKey ) resource.TestCheckFunc {
1401
+ return func (s * terraform.State ) error {
1402
+ rs , ok := s .RootModule ().Resources [n ]
1403
+ if ! ok {
1404
+ return fmt .Errorf ("Not found: %s" , n )
1405
+ }
1406
+
1407
+ for i , disk := range instance .Disks {
1408
+ if disk .Boot {
1409
+ attr := rs .Primary .Attributes ["boot_disk.0.kms_key_self_link" ]
1410
+ if attr != bootDiskEncryptionKey {
1411
+ return fmt .Errorf ("Boot disk has wrong encryption key in state.\n Expected: %s\n Actual: %s" , bootDiskEncryptionKey , attr )
1412
+ }
1413
+ if disk .DiskEncryptionKey == nil && attr != "" {
1414
+ return fmt .Errorf ("Disk %d has mismatched encryption key.\n TF State: %+v\n GCP State: <empty>" , i , attr )
1415
+ }
1416
+ } else {
1417
+ if disk .DiskEncryptionKey != nil {
1418
+ expectedKey := diskNameToEncryptionKey [GetResourceNameFromSelfLink (disk .Source )].KmsKeyName
1419
+ if disk .DiskEncryptionKey .KmsKeyName != expectedKey {
1420
+ return fmt .Errorf ("Disk %d has unexpected encryption key in GCP.\n Expected: %s\n Actual: %s" , i , expectedKey , disk .DiskEncryptionKey .Sha256 )
1421
+ }
1422
+ }
1423
+ }
1424
+ }
1425
+
1426
+ numAttachedDisks , err := strconv .Atoi (rs .Primary .Attributes ["attached_disk.#" ])
1427
+ if err != nil {
1428
+ return fmt .Errorf ("Error converting value of attached_disk.#" )
1429
+ }
1430
+ for i := 0 ; i < numAttachedDisks ; i ++ {
1431
+ diskName := GetResourceNameFromSelfLink (rs .Primary .Attributes [fmt .Sprintf ("attached_disk.%d.source" , i )])
1432
+ kmsKeyName := rs .Primary .Attributes [fmt .Sprintf ("attached_disk.%d.kms_key_self_link" , i )]
1433
+ if key , ok := diskNameToEncryptionKey [diskName ]; ok {
1434
+ expectedEncryptionKey := key .KmsKeyName
1435
+ if kmsKeyName != expectedEncryptionKey {
1436
+ return fmt .Errorf ("Attached disk %d has unexpected encryption key in state.\n Expected: %s\n Actual: %s" , i , expectedEncryptionKey , kmsKeyName )
1437
+ }
1438
+ }
1439
+ }
1440
+ return nil
1441
+ }
1442
+ }
1443
+
1366
1444
func testAccCheckComputeInstanceTag (instance * compute.Instance , n string ) resource.TestCheckFunc {
1367
1445
return func (s * terraform.State ) error {
1368
1446
if instance .Tags == nil {
0 commit comments