From 64f9d8cef4d5a16846b3a3fe99e96a3d513ddee8 Mon Sep 17 00:00:00 2001 From: Martin Lehmann Date: Tue, 12 Nov 2019 21:56:03 +0100 Subject: [PATCH] [@google-cloud/kms] Add missing types for creating KeyRings and CryptoKeys (#40124) * Add createKeyRing declarations * Add createCryptoKey declarations that don't quite work with all options * apparently it worked with rotationPeriod * add nanos field to make it happy * function order * run lint * update package version from 1.3 to 1.5.1 in header * remove patch version --- .../google-cloud__kms-tests.ts | 62 +++++++++++++++++++ types/google-cloud__kms/index.d.ts | 33 +++++++++- 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/types/google-cloud__kms/google-cloud__kms-tests.ts b/types/google-cloud__kms/google-cloud__kms-tests.ts index d05e881ccc..7131ab45ba 100644 --- a/types/google-cloud__kms/google-cloud__kms-tests.ts +++ b/types/google-cloud__kms/google-cloud__kms-tests.ts @@ -19,6 +19,31 @@ const credentials = { let kmsClient: kms.KeyManagementServiceClient; kmsClient = new kms.KeyManagementServiceClient({ credentials }); +async function exampleCreateKeyRing() { + const formattedParent = kmsClient.locationPath('[PROJECT]', '[LOCATION]'); + const keyRingId = ''; + const keyRing = {}; + const request = { + parent: formattedParent, + keyRingId, + keyRing, + }; + + const [asyncCreatedKeyRing] = await kmsClient.createKeyRing(request); + console.log(`Created KeyRing: ${asyncCreatedKeyRing.name}`); + + const [asyncCreatedKeyRingWithGaxOpts] = await kmsClient.createKeyRing(request, { timeout: 1000 }); + console.log(`Created KeyRing with GAX opts: ${asyncCreatedKeyRingWithGaxOpts.name}`); + + kmsClient.createKeyRing(request, (err, [response]) => { + console.log(`Created KeyRing: ${response.name}`); + }); + + kmsClient.createKeyRing(request, { timeout: 1000 }, (err, [response]) => { + console.log(`Created KeyRing: ${response.name}`); + }); +} + async function exampleListKeyRings() { const locationPath = kmsClient.locationPath('[PROJECT_ID]', '[LOCATION]'); const [ asyncKeyRings ] = await kmsClient.listKeyRings({parent: locationPath}); @@ -42,6 +67,43 @@ async function exampleListKeyRings() { }); } +async function exampleCreateCryptoKey() { + const formattedParent = kmsClient.keyRingPath('[PROJECT]', '[LOCATION]', '[KEY_RING]'); + const cryptoKeyId = 'my-app-key'; + const purpose = 'ENCRYPT_DECRYPT' as const; + const nextRotationTime = { + seconds: 2147483647, + nanos: 0, + }; + const rotationPeriod = { + seconds: 604800, + }; + const cryptoKey = { + purpose, + nextRotationTime, + rotationPeriod, + }; + const request = { + parent: formattedParent, + cryptoKeyId, + cryptoKey, + }; + + const [asyncCreatedCryptoKey] = await kmsClient.createCryptoKey(request); + console.log(`Created CryptoKey: ${asyncCreatedCryptoKey.name}`); + + const [asyncCreatedCryptoKeyWithGaxOpts] = await kmsClient.createCryptoKey(request, { timeout: 1000 }); + console.log(`Created CryptoKey with GAX opts: ${asyncCreatedCryptoKeyWithGaxOpts.name}`); + + kmsClient.createCryptoKey(request, (err, [response]) => { + console.log(`Created CryptoKey: ${response.name}`); + }); + + kmsClient.createCryptoKey(request, { timeout: 1000 }, (err, [response]) => { + console.log(`Created CryptoKey: ${response.name}`); + }); +} + async function exampleListCryptoKeys() { const keyRingPath = kmsClient.keyRingPath('[PROJECT_ID]', '[LOCATION]', '[KEYRING_ID]'); const [ asyncKeys ] = await kmsClient.listCryptoKeys({parent: keyRingPath}); diff --git a/types/google-cloud__kms/index.d.ts b/types/google-cloud__kms/index.d.ts index eb34ffca09..96e3cee303 100644 --- a/types/google-cloud__kms/index.d.ts +++ b/types/google-cloud__kms/index.d.ts @@ -1,15 +1,17 @@ -// Type definitions for @google-cloud/kms 1.3 +// Type definitions for @google-cloud/kms 1.5 // Project: https://github.com/googleapis/nodejs-kms // Definitions by: Ben Talbot // Caian Ertl // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.2 +// TypeScript Version: 3.4 /// import * as google_protobuf_timestamp_pb from "google-protobuf/google/protobuf/timestamp_pb"; export namespace v1 { + type Omit = Pick>; + enum CryptoKeyVersionAlgorithm { // Not specified. CRYPTO_KEY_VERSION_ALGORITHM_UNSPECIFIED = 0, @@ -198,6 +200,13 @@ export namespace v1 { } type DecryptCallback = (err: Error | null, apiResponse: [DecryptResponse, any, any]) => void; + interface CreateKeyRingRequest { + parent: string; + keyRingId: string; + keyRing?: Partial; + } + type CreateKeyRingCallback = (err: Error | null, apiResponse: [KeyRing, any, any]) => void; + interface ListKeyRingsRequest { parent: string; page_size?: number; @@ -205,6 +214,16 @@ export namespace v1 { } type ListKeyRingsCallback = (err: Error | null, apiResponse: [KeyRing[], any, any]) => void; + interface CreateCryptoKeyRequest { + parent: string; + cryptoKeyId: string; + cryptoKey: Partial> & { + purpose: keyof typeof CryptoKeyPurpose; + }; + skipInitialVersionCreation?: boolean; + } + type CreateCryptoKeyCallback = (err: Error | null, apiResponse: [CryptoKey, any, any]) => void; + interface ListCryptoKeysRequest { parent: string; page_size?: number; @@ -230,10 +249,20 @@ export namespace v1 { decrypt(request: KeyManagementServiceClient.DecryptRequest, callback: KeyManagementServiceClient.DecryptCallback): void; decrypt(request: KeyManagementServiceClient.DecryptRequest, gaxOpts: GAX.CallOptions, callback: KeyManagementServiceClient.DecryptCallback): void; + createKeyRing(request: KeyManagementServiceClient.CreateKeyRingRequest, callback: KeyManagementServiceClient.CreateKeyRingCallback): void; + createKeyRing(request: KeyManagementServiceClient.CreateKeyRingRequest, gaxOpts: GAX.CallOptions, callback: KeyManagementServiceClient.CreateKeyRingCallback): void; + // This needs to be after the declaration that has callback but not options. + createKeyRing(request: KeyManagementServiceClient.CreateKeyRingRequest, gaxOpts?: GAX.CallOptions): Promise<[KeyRing, any, any]>; + listKeyRings(request: KeyManagementServiceClient.ListKeyRingsRequest, gaxOpts?: GAX.CallOptions): Promise<[KeyRing[], any, any]>; listKeyRings(request: KeyManagementServiceClient.ListKeyRingsRequest, callback: KeyManagementServiceClient.ListKeyRingsCallback): void; listKeyRings(request: KeyManagementServiceClient.ListKeyRingsRequest, gaxOpts: GAX.CallOptions, callback: KeyManagementServiceClient.ListKeyRingsCallback): void; + createCryptoKey(request: KeyManagementServiceClient.CreateCryptoKeyRequest, callback: KeyManagementServiceClient.CreateCryptoKeyCallback): void; + createCryptoKey(request: KeyManagementServiceClient.CreateCryptoKeyRequest, gaxOpts: GAX.CallOptions, callback: KeyManagementServiceClient.CreateCryptoKeyCallback): void; + // This needs to be after the declaration that has callback but not options. + createCryptoKey(request: KeyManagementServiceClient.CreateCryptoKeyRequest, gaxOpts?: GAX.CallOptions): Promise<[CryptoKey, any, any]>; + listCryptoKeys(request: KeyManagementServiceClient.ListCryptoKeysRequest, gaxOpts?: GAX.CallOptions): Promise<[CryptoKey[], any, any]>; listCryptoKeys(request: KeyManagementServiceClient.ListCryptoKeysRequest, callback: KeyManagementServiceClient.ListCryptoKeysCallback): void; listCryptoKeys(request: KeyManagementServiceClient.ListCryptoKeysRequest, gaxOpts: GAX.CallOptions, callback: KeyManagementServiceClient.ListCryptoKeysCallback): void;