@@ -155,6 +155,33 @@ def disable_crypto_key_version(project_id, location_id, key_ring_id,
155155# [END kms_disable_cryptokey_version]
156156
157157
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+
158185# [START kms_destroy_cryptokey_version]
159186def destroy_crypto_key_version (
160187 project_id , location_id , key_ring_id , crypto_key_id , version_id ):
@@ -181,6 +208,31 @@ def destroy_crypto_key_version(
181208# [END kms_destroy_cryptokey_version]
182209
183210
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+
184236# [START kms_add_member_to_cryptokey_policy]
185237def add_member_to_crypto_key_policy (
186238 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):
294346 disable_crypto_key_version_parser .add_argument ('crypto_key' )
295347 disable_crypto_key_version_parser .add_argument ('version' )
296348
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+
297357 destroy_crypto_key_version_parser = subparsers .add_parser (
298358 'destroy_crypto_key_version' )
299359 destroy_crypto_key_version_parser .add_argument ('project' )
@@ -302,6 +362,14 @@ def get_key_ring_policy(project_id, location_id, key_ring_id):
302362 destroy_crypto_key_version_parser .add_argument ('crypto_key' )
303363 destroy_crypto_key_version_parser .add_argument ('version' )
304364
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+
305373 add_member_to_crypto_key_policy_parser = subparsers .add_parser (
306374 'add_member_to_crypto_key_policy' )
307375 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):
352420 args .key_ring ,
353421 args .crypto_key ,
354422 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 )
355430 elif args .command == 'destroy_crypto_key_version' :
356431 destroy_crypto_key_version (
357432 args .project ,
358433 args .location ,
359434 args .key_ring ,
360435 args .crypto_key ,
361436 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 )
362444 elif args .command == 'add_member_to_crypto_key_policy' :
363445 add_member_to_crypto_key_policy (
364446 args .project ,
0 commit comments