[@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
This commit is contained in:
Martin Lehmann
2019-11-12 12:56:03 -08:00
committed by Pranav Senthilnathan
parent 1402489d76
commit 64f9d8cef4
2 changed files with 93 additions and 2 deletions
@@ -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});
+31 -2
View File
@@ -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 <https://github.com/ben-tbotlabs>
// Caian Ertl <https://github.com/caiertl>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
// TypeScript Version: 3.4
/// <reference types="node" />
import * as google_protobuf_timestamp_pb from "google-protobuf/google/protobuf/timestamp_pb";
export namespace v1 {
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
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<KeyRing>;
}
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<Omit<CryptoKey, 'purpose'>> & {
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;