1111# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212# See the License for the specific language governing permissions and
1313
14-
1514# [START kms_create_key_hsm]
16- def create_key_hsm (project_id , location_id , key_ring_id , key_id ):
15+ import datetime
16+
17+ from google .cloud import kms
18+ from google .protobuf import duration_pb2 # type: ignore
19+
20+
21+ def create_key_hsm (
22+ project_id : str , location_id : str , key_ring_id : str , key_id : str
23+ ) -> kms .CryptoKey :
1724 """
1825 Creates a new key in Cloud KMS backed by Cloud HSM.
1926
@@ -28,11 +35,6 @@ def create_key_hsm(project_id, location_id, key_ring_id, key_id):
2835
2936 """
3037
31- # Import the client library.
32- from google .cloud import kms
33- from google .protobuf import duration_pb2
34- import datetime
35-
3638 # Create the client.
3739 client = kms .KeyManagementServiceClient ()
3840
@@ -41,23 +43,29 @@ def create_key_hsm(project_id, location_id, key_ring_id, key_id):
4143
4244 # Build the key.
4345 purpose = kms .CryptoKey .CryptoKeyPurpose .ENCRYPT_DECRYPT
44- algorithm = kms .CryptoKeyVersion .CryptoKeyVersionAlgorithm .GOOGLE_SYMMETRIC_ENCRYPTION
46+ algorithm = (
47+ kms .CryptoKeyVersion .CryptoKeyVersionAlgorithm .GOOGLE_SYMMETRIC_ENCRYPTION
48+ )
4549 protection_level = kms .ProtectionLevel .HSM
4650 key = {
47- ' purpose' : purpose ,
48- ' version_template' : {
49- ' algorithm' : algorithm ,
50- ' protection_level' : protection_level
51+ " purpose" : purpose ,
52+ " version_template" : {
53+ " algorithm" : algorithm ,
54+ " protection_level" : protection_level ,
5155 },
52-
5356 # Optional: customize how long key versions should be kept before
5457 # destroying.
55- 'destroy_scheduled_duration' : duration_pb2 .Duration ().FromTimedelta (datetime .timedelta (days = 1 ))
58+ "destroy_scheduled_duration" : duration_pb2 .Duration ().FromTimedelta (
59+ datetime .timedelta (days = 1 )
60+ ),
5661 }
5762
5863 # Call the API.
5964 created_key = client .create_crypto_key (
60- request = {'parent' : key_ring_name , 'crypto_key_id' : key_id , 'crypto_key' : key })
61- print (f'Created hsm key: { created_key .name } ' )
65+ request = {"parent" : key_ring_name , "crypto_key_id" : key_id , "crypto_key" : key }
66+ )
67+ print (f"Created hsm key: { created_key .name } " )
6268 return created_key
69+
70+
6371# [END kms_create_key_hsm]
0 commit comments