@@ -137,6 +137,7 @@ func TestAccKmsCryptoKey_basic(t *testing.T) {
137
137
Check : resource .ComposeTestCheckFunc (
138
138
testAccCheckGoogleKmsCryptoKeyWasRemovedFromState ("google_kms_crypto_key.crypto_key" ),
139
139
testAccCheckGoogleKmsCryptoKeyVersionsDestroyed (projectId , location , keyRingName , cryptoKeyName ),
140
+ testAccCheckGoogleKmsCryptoKeyRotationDisabled (projectId , location , keyRingName , cryptoKeyName ),
140
141
),
141
142
},
142
143
},
@@ -189,16 +190,15 @@ func TestAccKmsCryptoKey_rotation(t *testing.T) {
189
190
Check : resource .ComposeTestCheckFunc (
190
191
testAccCheckGoogleKmsCryptoKeyWasRemovedFromState ("google_kms_crypto_key.crypto_key" ),
191
192
testAccCheckGoogleKmsCryptoKeyVersionsDestroyed (projectId , location , keyRingName , cryptoKeyName ),
193
+ testAccCheckGoogleKmsCryptoKeyRotationDisabled (projectId , location , keyRingName , cryptoKeyName ),
192
194
),
193
195
},
194
196
},
195
197
})
196
198
}
197
199
198
- /*
199
- KMS KeyRings cannot be deleted. This ensures that the CryptoKey resource was removed from state,
200
- even though the server-side resource was not removed.
201
- */
200
+ // KMS KeyRings cannot be deleted. This ensures that the CryptoKey resource was removed from state,
201
+ // even though the server-side resource was not removed.
202
202
func testAccCheckGoogleKmsCryptoKeyWasRemovedFromState (resourceName string ) resource.TestCheckFunc {
203
203
return func (s * terraform.State ) error {
204
204
_ , ok := s .RootModule ().Resources [resourceName ]
@@ -211,11 +211,8 @@ func testAccCheckGoogleKmsCryptoKeyWasRemovedFromState(resourceName string) reso
211
211
}
212
212
}
213
213
214
- /*
215
- KMS KeyRings cannot be deleted. This ensures that the CryptoKey resource's CryptoKeyVersion
216
- sub-resources were scheduled to be destroyed, rendering the key itself inoperable.
217
- */
218
-
214
+ // KMS KeyRings cannot be deleted. This ensures that the CryptoKey resource's CryptoKeyVersion
215
+ // sub-resources were scheduled to be destroyed, rendering the key itself inoperable.
219
216
func testAccCheckGoogleKmsCryptoKeyVersionsDestroyed (projectId , location , keyRingName , cryptoKeyName string ) resource.TestCheckFunc {
220
217
return func (_ * terraform.State ) error {
221
218
config := testAccProvider .Meta ().(* Config )
@@ -239,10 +236,31 @@ func testAccCheckGoogleKmsCryptoKeyVersionsDestroyed(projectId, location, keyRin
239
236
}
240
237
}
241
238
242
- /*
243
- This test runs in its own project, otherwise the test project would start to get filled
244
- with undeletable resources
245
- */
239
+ // KMS KeyRings cannot be deleted. This ensures that the CryptoKey autorotation
240
+ // was disabled to prevent more versions of the key from being created.
241
+ func testAccCheckGoogleKmsCryptoKeyRotationDisabled (projectId , location , keyRingName , cryptoKeyName string ) resource.TestCheckFunc {
242
+ return func (_ * terraform.State ) error {
243
+ config := testAccProvider .Meta ().(* Config )
244
+ gcpResourceUri := fmt .Sprintf ("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s" , projectId , location , keyRingName , cryptoKeyName )
245
+
246
+ response , err := config .clientKms .Projects .Locations .KeyRings .CryptoKeys .Get (gcpResourceUri ).Do ()
247
+ if err != nil {
248
+ return fmt .Errorf ("Unexpected failure while verifying 'deleted' crypto key: %s" , err )
249
+ }
250
+
251
+ if response .NextRotationTime != "" {
252
+ return fmt .Errorf ("Expected empty nextRotationTime for 'deleted' crypto key, got %s" , response .NextRotationTime )
253
+ }
254
+ if response .RotationPeriod != "" {
255
+ return fmt .Errorf ("Expected empty RotationPeriod for 'deleted' crypto key, got %s" , response .RotationPeriod )
256
+ }
257
+
258
+ return nil
259
+ }
260
+ }
261
+
262
+ // This test runs in its own project, otherwise the test project would start to get filled
263
+ // with undeletable resources
246
264
func testGoogleKmsCryptoKey_basic (projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName string ) string {
247
265
return fmt .Sprintf (`
248
266
resource "google_project" "acceptance" {
0 commit comments