@@ -440,6 +440,96 @@ func testAccCheckGoogleKmsCryptoKeyRotationDisabled(t *testing.T, projectId, loc
440
440
}
441
441
}
442
442
443
+ func TestAccKmsCryptoKeyVersion_basic (t * testing.T ) {
444
+ t .Parallel ()
445
+
446
+ projectId := fmt .Sprintf ("tf-test-%d" , randInt (t ))
447
+ projectOrg := getTestOrgFromEnv (t )
448
+ projectBillingAccount := getTestBillingAccountFromEnv (t )
449
+ keyRingName := fmt .Sprintf ("tf-test-%s" , randString (t , 10 ))
450
+ cryptoKeyName := fmt .Sprintf ("tf-test-%s" , randString (t , 10 ))
451
+
452
+ vcrTest (t , resource.TestCase {
453
+ PreCheck : func () { testAccPreCheck (t ) },
454
+ Providers : testAccProviders ,
455
+ Steps : []resource.TestStep {
456
+ {
457
+ Config : testGoogleKmsCryptoKeyVersion_basic (projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName ),
458
+ },
459
+ {
460
+ ResourceName : "google_kms_crypto_key_version.crypto_key_version" ,
461
+ ImportState : true ,
462
+ ImportStateVerify : true ,
463
+ },
464
+ {
465
+ Config : testGoogleKmsCryptoKeyVersion_removed (projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName ),
466
+ },
467
+ },
468
+ })
469
+ }
470
+
471
+ func TestAccKmsCryptoKeyVersion_skipInitialVersion (t * testing.T ) {
472
+ t .Parallel ()
473
+
474
+ projectId := fmt .Sprintf ("tf-test-%d" , randInt (t ))
475
+ projectOrg := getTestOrgFromEnv (t )
476
+ projectBillingAccount := getTestBillingAccountFromEnv (t )
477
+ keyRingName := fmt .Sprintf ("tf-test-%s" , randString (t , 10 ))
478
+ cryptoKeyName := fmt .Sprintf ("tf-test-%s" , randString (t , 10 ))
479
+
480
+ vcrTest (t , resource.TestCase {
481
+ PreCheck : func () { testAccPreCheck (t ) },
482
+ Providers : testAccProviders ,
483
+ Steps : []resource.TestStep {
484
+ {
485
+ Config : testGoogleKmsCryptoKeyVersion_skipInitialVersion (projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName ),
486
+ },
487
+ {
488
+ ResourceName : "google_kms_crypto_key_version.crypto_key_version" ,
489
+ ImportState : true ,
490
+ ImportStateVerify : true ,
491
+ },
492
+ },
493
+ })
494
+ }
495
+
496
+ func TestAccKmsCryptoKeyVersion_patch (t * testing.T ) {
497
+ t .Parallel ()
498
+
499
+ projectId := fmt .Sprintf ("tf-test-%d" , randInt (t ))
500
+ projectOrg := getTestOrgFromEnv (t )
501
+ projectBillingAccount := getTestBillingAccountFromEnv (t )
502
+ keyRingName := fmt .Sprintf ("tf-test-%s" , randString (t , 10 ))
503
+ cryptoKeyName := fmt .Sprintf ("tf-test-%s" , randString (t , 10 ))
504
+ state := "DISABLED"
505
+
506
+ vcrTest (t , resource.TestCase {
507
+ PreCheck : func () { testAccPreCheck (t ) },
508
+ Providers : testAccProviders ,
509
+ Steps : []resource.TestStep {
510
+ {
511
+ Config : testGoogleKmsCryptoKeyVersion_patchInitialize (projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName ),
512
+ },
513
+ {
514
+ ResourceName : "google_kms_crypto_key_version.crypto_key_version" ,
515
+ ImportState : true ,
516
+ ImportStateVerify : true ,
517
+ },
518
+ {
519
+ Config : testGoogleKmsCryptoKeyVersion_patch ("true" , projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName , state ),
520
+ },
521
+ {
522
+ ResourceName : "google_kms_crypto_key_version.crypto_key_version" ,
523
+ ImportState : true ,
524
+ ImportStateVerify : true ,
525
+ },
526
+ {
527
+ Config : testGoogleKmsCryptoKeyVersion_patch ("false" , projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName , state ),
528
+ },
529
+ },
530
+ })
531
+ }
532
+
443
533
// This test runs in its own project, otherwise the test project would start to get filled
444
534
// with undeletable resources
445
535
func testGoogleKmsCryptoKey_basic (projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName string ) string {
@@ -643,3 +733,177 @@ resource "google_kms_crypto_key" "crypto_key" {
643
733
}
644
734
` , projectId , projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName )
645
735
}
736
+
737
+ func testGoogleKmsCryptoKeyVersion_basic (projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName string ) string {
738
+ return fmt .Sprintf (`
739
+ resource "google_project" "acceptance" {
740
+ name = "%s"
741
+ project_id = "%s"
742
+ org_id = "%s"
743
+ billing_account = "%s"
744
+ }
745
+
746
+ resource "google_project_service" "acceptance" {
747
+ project = google_project.acceptance.project_id
748
+ service = "cloudkms.googleapis.com"
749
+ }
750
+
751
+ resource "google_kms_key_ring" "key_ring" {
752
+ project = google_project_service.acceptance.project
753
+ name = "%s"
754
+ location = "us-central1"
755
+ }
756
+
757
+ resource "google_kms_crypto_key" "crypto_key" {
758
+ name = "%s"
759
+ key_ring = google_kms_key_ring.key_ring.id
760
+ labels = {
761
+ key = "value"
762
+ }
763
+ }
764
+
765
+ resource "google_kms_crypto_key_version" "crypto_key_version" {
766
+ crypto_key = google_kms_crypto_key.crypto_key.id
767
+ }
768
+ ` , projectId , projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName )
769
+ }
770
+
771
+ func testGoogleKmsCryptoKeyVersion_removed (projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName string ) string {
772
+ return fmt .Sprintf (`
773
+ resource "google_project" "acceptance" {
774
+ name = "%s"
775
+ project_id = "%s"
776
+ org_id = "%s"
777
+ billing_account = "%s"
778
+ }
779
+
780
+ resource "google_project_service" "acceptance" {
781
+ project = google_project.acceptance.project_id
782
+ service = "cloudkms.googleapis.com"
783
+ }
784
+
785
+ resource "google_kms_key_ring" "key_ring" {
786
+ project = google_project_service.acceptance.project
787
+ name = "%s"
788
+ location = "us-central1"
789
+ }
790
+
791
+ resource "google_kms_crypto_key" "crypto_key" {
792
+ name = "%s"
793
+ key_ring = google_kms_key_ring.key_ring.id
794
+ labels = {
795
+ key = "value"
796
+ }
797
+ }
798
+ ` , projectId , projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName )
799
+ }
800
+
801
+ func testGoogleKmsCryptoKeyVersion_skipInitialVersion (projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName string ) string {
802
+ return fmt .Sprintf (`
803
+ resource "google_project" "acceptance" {
804
+ name = "%s"
805
+ project_id = "%s"
806
+ org_id = "%s"
807
+ billing_account = "%s"
808
+ }
809
+
810
+ resource "google_project_service" "acceptance" {
811
+ project = google_project.acceptance.project_id
812
+ service = "cloudkms.googleapis.com"
813
+ }
814
+
815
+ resource "google_kms_key_ring" "key_ring" {
816
+ project = google_project_service.acceptance.project
817
+ name = "%s"
818
+ location = "us-central1"
819
+ }
820
+
821
+ resource "google_kms_crypto_key" "crypto_key" {
822
+ name = "%s"
823
+ key_ring = google_kms_key_ring.key_ring.id
824
+ labels = {
825
+ key = "value"
826
+ }
827
+ skip_initial_version_creation = true
828
+ }
829
+
830
+ resource "google_kms_crypto_key_version" "crypto_key_version" {
831
+ crypto_key = google_kms_crypto_key.crypto_key.id
832
+ }
833
+ ` , projectId , projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName )
834
+ }
835
+ func testGoogleKmsCryptoKeyVersion_patchInitialize (projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName string ) string {
836
+ return fmt .Sprintf (`
837
+ resource "google_project" "acceptance" {
838
+ name = "%s"
839
+ project_id = "%s"
840
+ org_id = "%s"
841
+ billing_account = "%s"
842
+ }
843
+
844
+ resource "google_project_service" "acceptance" {
845
+ project = google_project.acceptance.project_id
846
+ service = "cloudkms.googleapis.com"
847
+ }
848
+
849
+ resource "google_kms_key_ring" "key_ring" {
850
+ project = google_project_service.acceptance.project
851
+ name = "%s"
852
+ location = "us-central1"
853
+ }
854
+
855
+ resource "google_kms_crypto_key" "crypto_key" {
856
+ name = "%s"
857
+ key_ring = google_kms_key_ring.key_ring.id
858
+ labels = {
859
+ key = "value"
860
+ }
861
+ }
862
+
863
+ resource "google_kms_crypto_key_version" "crypto_key_version" {
864
+ crypto_key = google_kms_crypto_key.crypto_key.id
865
+ lifecycle {
866
+ prevent_destroy = true
867
+ }
868
+ state = "ENABLED"
869
+ }
870
+ ` , projectId , projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName )
871
+ }
872
+
873
+ func testGoogleKmsCryptoKeyVersion_patch (preventDestroy , projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName , state string ) string {
874
+ return fmt .Sprintf (`
875
+ resource "google_project" "acceptance" {
876
+ name = "%s"
877
+ project_id = "%s"
878
+ org_id = "%s"
879
+ billing_account = "%s"
880
+ }
881
+
882
+ resource "google_project_service" "acceptance" {
883
+ project = google_project.acceptance.project_id
884
+ service = "cloudkms.googleapis.com"
885
+ }
886
+
887
+ resource "google_kms_key_ring" "key_ring" {
888
+ project = google_project_service.acceptance.project
889
+ name = "%s"
890
+ location = "us-central1"
891
+ }
892
+
893
+ resource "google_kms_crypto_key" "crypto_key" {
894
+ name = "%s"
895
+ key_ring = google_kms_key_ring.key_ring.id
896
+ labels = {
897
+ key = "value"
898
+ }
899
+ }
900
+
901
+ resource "google_kms_crypto_key_version" "crypto_key_version" {
902
+ crypto_key = google_kms_crypto_key.crypto_key.id
903
+ lifecycle {
904
+ prevent_destroy = %s
905
+ }
906
+ state = "%s"
907
+ }
908
+ ` , projectId , projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName , preventDestroy , state )
909
+ }
0 commit comments