@@ -155,6 +155,33 @@ def disable_crypto_key_version(project_id, location_id, key_ring_id,
155
155
# [END kms_disable_cryptokey_version]
156
156
157
157
158
+ # [START kms_enable_cryptokey_version]
159
+ def enable_crypto_key_version (project_id , location_id , key_ring_id ,
160
+ crypto_key_id , version_id ):
161
+ """Enables a CryptoKeyVersion associated with a given CryptoKey and
162
+ KeyRing."""
163
+
164
+ # Creates an API client for the KMS API.
165
+ kms_client = googleapiclient .discovery .build ('cloudkms' , 'v1' )
166
+
167
+ # Construct the resource name of the CryptoKeyVersion.
168
+ name = (
169
+ 'projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}/'
170
+ 'cryptoKeyVersions/{}'
171
+ .format (
172
+ project_id , location_id , key_ring_id , crypto_key_id , version_id ))
173
+
174
+ # Use the KMS API to enable the CryptoKeyVersion.
175
+ crypto_keys = kms_client .projects ().locations ().keyRings ().cryptoKeys ()
176
+ request = crypto_keys .cryptoKeyVersions ().patch (
177
+ name = name , body = {'state' : 'ENABLED' }, updateMask = 'state' )
178
+ response = request .execute ()
179
+
180
+ print ('CryptoKeyVersion {}\' s state has been set to {}.' .format (
181
+ name , response ['state' ]))
182
+ # [END kms_enable_cryptokey_version]
183
+
184
+
158
185
# [START kms_destroy_cryptokey_version]
159
186
def destroy_crypto_key_version (
160
187
project_id , location_id , key_ring_id , crypto_key_id , version_id ):
@@ -181,6 +208,31 @@ def destroy_crypto_key_version(
181
208
# [END kms_destroy_cryptokey_version]
182
209
183
210
211
+ # [START kms_restore_cryptokey_version]
212
+ def restore_crypto_key_version (
213
+ project_id , location_id , key_ring_id , crypto_key_id , version_id ):
214
+ """Restores a CryptoKeyVersion that is scheduled for destruction."""
215
+
216
+ # Creates an API client for the KMS API.
217
+ kms_client = googleapiclient .discovery .build ('cloudkms' , 'v1' )
218
+
219
+ # Construct the resource name of the CryptoKeyVersion.
220
+ name = (
221
+ 'projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}/'
222
+ 'cryptoKeyVersions/{}'
223
+ .format (
224
+ project_id , location_id , key_ring_id , crypto_key_id , version_id ))
225
+
226
+ # Use the KMS API to restore the CryptoKeyVersion.
227
+ crypto_keys = kms_client .projects ().locations ().keyRings ().cryptoKeys ()
228
+ request = crypto_keys .cryptoKeyVersions ().restore (name = name , body = {})
229
+ response = request .execute ()
230
+
231
+ print ('CryptoKeyVersion {}\' s state has been set to {}.' .format (
232
+ name , response ['state' ]))
233
+ # [END kms_restore_cryptokey_version]
234
+
235
+
184
236
# [START kms_add_member_to_cryptokey_policy]
185
237
def add_member_to_crypto_key_policy (
186
238
project_id , location_id , key_ring_id , crypto_key_id , member , role ):
@@ -294,6 +346,14 @@ def get_key_ring_policy(project_id, location_id, key_ring_id):
294
346
disable_crypto_key_version_parser .add_argument ('crypto_key' )
295
347
disable_crypto_key_version_parser .add_argument ('version' )
296
348
349
+ enable_crypto_key_version_parser = subparsers .add_parser (
350
+ 'enable_crypto_key_version' )
351
+ enable_crypto_key_version_parser .add_argument ('project' )
352
+ enable_crypto_key_version_parser .add_argument ('location' )
353
+ enable_crypto_key_version_parser .add_argument ('key_ring' )
354
+ enable_crypto_key_version_parser .add_argument ('crypto_key' )
355
+ enable_crypto_key_version_parser .add_argument ('version' )
356
+
297
357
destroy_crypto_key_version_parser = subparsers .add_parser (
298
358
'destroy_crypto_key_version' )
299
359
destroy_crypto_key_version_parser .add_argument ('project' )
@@ -302,6 +362,14 @@ def get_key_ring_policy(project_id, location_id, key_ring_id):
302
362
destroy_crypto_key_version_parser .add_argument ('crypto_key' )
303
363
destroy_crypto_key_version_parser .add_argument ('version' )
304
364
365
+ restore_crypto_key_version_parser = subparsers .add_parser (
366
+ 'restore_crypto_key_version' )
367
+ restore_crypto_key_version_parser .add_argument ('project' )
368
+ restore_crypto_key_version_parser .add_argument ('location' )
369
+ restore_crypto_key_version_parser .add_argument ('key_ring' )
370
+ restore_crypto_key_version_parser .add_argument ('crypto_key' )
371
+ restore_crypto_key_version_parser .add_argument ('version' )
372
+
305
373
add_member_to_crypto_key_policy_parser = subparsers .add_parser (
306
374
'add_member_to_crypto_key_policy' )
307
375
add_member_to_crypto_key_policy_parser .add_argument ('project' )
@@ -352,13 +420,27 @@ def get_key_ring_policy(project_id, location_id, key_ring_id):
352
420
args .key_ring ,
353
421
args .crypto_key ,
354
422
args .version )
423
+ elif args .command == 'enable_crypto_key_version' :
424
+ enable_crypto_key_version (
425
+ args .project ,
426
+ args .location ,
427
+ args .key_ring ,
428
+ args .crypto_key ,
429
+ args .version )
355
430
elif args .command == 'destroy_crypto_key_version' :
356
431
destroy_crypto_key_version (
357
432
args .project ,
358
433
args .location ,
359
434
args .key_ring ,
360
435
args .crypto_key ,
361
436
args .version )
437
+ elif args .command == 'restore_crypto_key_version' :
438
+ restore_crypto_key_version (
439
+ args .project ,
440
+ args .location ,
441
+ args .key_ring ,
442
+ args .crypto_key ,
443
+ args .version )
362
444
elif args .command == 'add_member_to_crypto_key_policy' :
363
445
add_member_to_crypto_key_policy (
364
446
args .project ,
0 commit comments