From 36d78013937d1bf997dd296b701d1cd55b648874 Mon Sep 17 00:00:00 2001 From: Maor Leger Date: Wed, 9 Jun 2021 08:37:34 -0700 Subject: [PATCH] [KeyVault] - fix admin samples (#15613) This PR just updates the KeyVault admin samples to provide some context around what is happening, restore the console logging of backup / restore results, and provide a helper method to ensure URIs are well-formed. --- .../samples-dev/backupRestoreHelloWorld.ts | 35 ++++++++++++---- .../samples-dev/backupSelectiveKeyRestore.ts | 41 +++++++++++++------ .../v4/javascript/backupRestoreHelloWorld.js | 35 ++++++++++++---- .../javascript/backupSelectiveKeyRestore.js | 41 +++++++++++++------ .../samples/v4/javascript/package.json | 2 +- .../samples/v4/typescript/package.json | 2 +- .../typescript/src/backupRestoreHelloWorld.ts | 35 ++++++++++++---- .../src/backupSelectiveKeyRestore.ts | 41 +++++++++++++------ 8 files changed, 170 insertions(+), 62 deletions(-) diff --git a/sdk/keyvault/keyvault-admin/samples-dev/backupRestoreHelloWorld.ts b/sdk/keyvault/keyvault-admin/samples-dev/backupRestoreHelloWorld.ts index 4fd269b8c29c..926b9da7f27a 100644 --- a/sdk/keyvault/keyvault-admin/samples-dev/backupRestoreHelloWorld.ts +++ b/sdk/keyvault/keyvault-admin/samples-dev/backupRestoreHelloWorld.ts @@ -24,22 +24,41 @@ export async function main(): Promise { } const client = new KeyVaultBackupClient(url, credential); - const blobStorageUri = process.env["BLOB_STORAGE_URI"]; - if (!blobStorageUri) { - throw new Error("Missing environment variable BLOB_STORAGE_URI."); - } const sasToken = process.env["BLOB_STORAGE_SAS_TOKEN"]; if (!sasToken) { throw new Error("Missing environment variable BLOB_STORAGE_SAS_TOKEN."); } - const backupPoller = await client.beginBackup(blobStorageUri!, sasToken); - await backupPoller.pollUntilDone(); - // The folder name should be at the end of the backupFolderUri, as in: https:/// - const restorePoller = await client.beginRestore(blobStorageUri, sasToken); + // Create a Uri with the storage container path. + const blobContainerUri = buildBlobContainerUri(); + + // Start the backup and wait for its completion. + const backupPoller = await client.beginBackup(blobContainerUri, sasToken); + const backupResult = await backupPoller.pollUntilDone(); + + // Finally, start and wait for the restore operation using the folderUri returned from a previous backup operation. + const restorePoller = await client.beginRestore(backupResult.folderUri!, sasToken); await restorePoller.pollUntilDone(); } +/** + * Helper function to construct a valid blob container URI from its parts. + */ +function buildBlobContainerUri() { + const blobStorageUri = process.env["BLOB_STORAGE_URI"]; + if (!blobStorageUri) { + throw new Error("Missing environment variable BLOB_STORAGE_URI."); + } + + const blobContainerName = process.env["BLOB_CONTAINER_NAME"]; + if (!blobContainerName) { + throw new Error("Missing environment variable BLOB_CONTAINER_NAME."); + } + + // If there are trailing slashes, remove them before building the URI. + return `${blobStorageUri.replace(/\/$/, "")}/${blobContainerName}`; +} + main().catch((err) => { console.log("error code: ", err.code); console.log("error message: ", err.message); diff --git a/sdk/keyvault/keyvault-admin/samples-dev/backupSelectiveKeyRestore.ts b/sdk/keyvault/keyvault-admin/samples-dev/backupSelectiveKeyRestore.ts index 367f82f7975f..fa2e99fd5a8b 100644 --- a/sdk/keyvault/keyvault-admin/samples-dev/backupSelectiveKeyRestore.ts +++ b/sdk/keyvault/keyvault-admin/samples-dev/backupSelectiveKeyRestore.ts @@ -29,28 +29,45 @@ export async function main(): Promise { const keyName = "key-name"; const key = await keyClient.createRsaKey(keyName); - const blobStorageUri = process.env["BLOB_STORAGE_URI"]; - if (!blobStorageUri) { - throw new Error("Missing environment variable BLOB_STORAGE_URI."); - } const sasToken = process.env["BLOB_STORAGE_SAS_TOKEN"]; if (!sasToken) { throw new Error("Missing environment variable BLOB_STORAGE_SAS_TOKEN."); } - const backupPoller = await client.beginBackup(blobStorageUri, sasToken); - await backupPoller.pollUntilDone(); + // Create a Uri with the storage container path. + const blobContainerUri = buildBlobContainerUri(); + + // Start the backup and wait for its completion. + const backupPoller = await client.beginBackup(blobContainerUri, sasToken); + const backupResult = await backupPoller.pollUntilDone(); + console.log("backupResult", backupResult); + + // Finally, start and wait for the restore operation using the folderUri returned from a previous backup operation. const selectiveKeyRestorePoller = await client.beginSelectiveKeyRestore( key.name, - blobStorageUri, + backupResult.folderUri!, sasToken ); - await selectiveKeyRestorePoller.pollUntilDone(); + const restoreResult = await selectiveKeyRestorePoller.pollUntilDone(); + console.log("restoreResult", restoreResult); +} + +/** + * Helper function to construct a valid blob container URI from its parts. + */ +function buildBlobContainerUri() { + const blobStorageUri = process.env["BLOB_STORAGE_URI"]; + if (!blobStorageUri) { + throw new Error("Missing environment variable BLOB_STORAGE_URI."); + } + + const blobContainerName = process.env["BLOB_CONTAINER_NAME"]; + if (!blobContainerName) { + throw new Error("Missing environment variable BLOB_CONTAINER_NAME."); + } - // Deleting and purging the key, just in case we want to create the same key again. - const deleteKeyPoller = await keyClient.beginDeleteKey(keyName); - await deleteKeyPoller.pollUntilDone(); - await keyClient.purgeDeletedKey(keyName); + // If there are trailing slashes, remove them before building the URI. + return `${blobStorageUri.replace(/\/$/, "")}/${blobContainerName}`; } main().catch((err) => { diff --git a/sdk/keyvault/keyvault-admin/samples/v4/javascript/backupRestoreHelloWorld.js b/sdk/keyvault/keyvault-admin/samples/v4/javascript/backupRestoreHelloWorld.js index 87d9af32444f..1e15815cb81c 100644 --- a/sdk/keyvault/keyvault-admin/samples/v4/javascript/backupRestoreHelloWorld.js +++ b/sdk/keyvault/keyvault-admin/samples/v4/javascript/backupRestoreHelloWorld.js @@ -24,22 +24,41 @@ async function main() { } const client = new KeyVaultBackupClient(url, credential); - const blobStorageUri = process.env["BLOB_STORAGE_URI"]; - if (!blobStorageUri) { - throw new Error("Missing environment variable BLOB_STORAGE_URI."); - } const sasToken = process.env["BLOB_STORAGE_SAS_TOKEN"]; if (!sasToken) { throw new Error("Missing environment variable BLOB_STORAGE_SAS_TOKEN."); } - const backupPoller = await client.beginBackup(blobStorageUri, sasToken); - await backupPoller.pollUntilDone(); - // The folder name should be at the end of the backupFolderUri, as in: https:/// - const restorePoller = await client.beginRestore(blobStorageUri, sasToken); + // Create a Uri with the storage container path. + const blobContainerUri = buildBlobContainerUri(); + + // Start the backup and wait for its completion. + const backupPoller = await client.beginBackup(blobContainerUri, sasToken); + const backupResult = await backupPoller.pollUntilDone(); + + // Finally, start and wait for the restore operation using the folderUri returned from a previous backup operation. + const restorePoller = await client.beginRestore(backupResult.folderUri, sasToken); await restorePoller.pollUntilDone(); } +/** + * Helper function to construct a valid blob container URI from its parts. + */ +function buildBlobContainerUri() { + const blobStorageUri = process.env["BLOB_STORAGE_URI"]; + if (!blobStorageUri) { + throw new Error("Missing environment variable BLOB_STORAGE_URI."); + } + + const blobContainerName = process.env["BLOB_CONTAINER_NAME"]; + if (!blobContainerName) { + throw new Error("Missing environment variable BLOB_CONTAINER_NAME."); + } + + // If there are trailing slashes, remove them before building the URI. + return `${blobStorageUri.replace(/\/$/, "")}/${blobContainerName}`; +} + main().catch((err) => { console.log("error code: ", err.code); console.log("error message: ", err.message); diff --git a/sdk/keyvault/keyvault-admin/samples/v4/javascript/backupSelectiveKeyRestore.js b/sdk/keyvault/keyvault-admin/samples/v4/javascript/backupSelectiveKeyRestore.js index 4e94e8218f3a..8bfb8f3b2367 100644 --- a/sdk/keyvault/keyvault-admin/samples/v4/javascript/backupSelectiveKeyRestore.js +++ b/sdk/keyvault/keyvault-admin/samples/v4/javascript/backupSelectiveKeyRestore.js @@ -29,28 +29,45 @@ async function main() { const keyName = "key-name"; const key = await keyClient.createRsaKey(keyName); - const blobStorageUri = process.env["BLOB_STORAGE_URI"]; - if (!blobStorageUri) { - throw new Error("Missing environment variable BLOB_STORAGE_URI."); - } const sasToken = process.env["BLOB_STORAGE_SAS_TOKEN"]; if (!sasToken) { throw new Error("Missing environment variable BLOB_STORAGE_SAS_TOKEN."); } - const backupPoller = await client.beginBackup(blobStorageUri, sasToken); - await backupPoller.pollUntilDone(); + // Create a Uri with the storage container path. + const blobContainerUri = buildBlobContainerUri(); + + // Start the backup and wait for its completion. + const backupPoller = await client.beginBackup(blobContainerUri, sasToken); + const backupResult = await backupPoller.pollUntilDone(); + console.log("backupResult", backupResult); + + // Finally, start and wait for the restore operation using the folderUri returned from a previous backup operation. const selectiveKeyRestorePoller = await client.beginSelectiveKeyRestore( key.name, - blobStorageUri, + backupResult.folderUri, sasToken ); - await selectiveKeyRestorePoller.pollUntilDone(); + const restoreResult = await selectiveKeyRestorePoller.pollUntilDone(); + console.log("restoreResult", restoreResult); +} + +/** + * Helper function to construct a valid blob container URI from its parts. + */ +function buildBlobContainerUri() { + const blobStorageUri = process.env["BLOB_STORAGE_URI"]; + if (!blobStorageUri) { + throw new Error("Missing environment variable BLOB_STORAGE_URI."); + } + + const blobContainerName = process.env["BLOB_CONTAINER_NAME"]; + if (!blobContainerName) { + throw new Error("Missing environment variable BLOB_CONTAINER_NAME."); + } - // Deleting and purging the key, just in case we want to create the same key again. - const deleteKeyPoller = await keyClient.beginDeleteKey(keyName); - await deleteKeyPoller.pollUntilDone(); - await keyClient.purgeDeletedKey(keyName); + // If there are trailing slashes, remove them before building the URI. + return `${blobStorageUri.replace(/\/$/, "")}/${blobContainerName}`; } main().catch((err) => { diff --git a/sdk/keyvault/keyvault-admin/samples/v4/javascript/package.json b/sdk/keyvault/keyvault-admin/samples/v4/javascript/package.json index 37657a143c18..38dd004183b2 100644 --- a/sdk/keyvault/keyvault-admin/samples/v4/javascript/package.json +++ b/sdk/keyvault/keyvault-admin/samples/v4/javascript/package.json @@ -3,7 +3,7 @@ "private": true, "version": "1.0.0", "description": "Azure Key Vault Administration client library samples for JavaScript", - "engine": { + "engines": { "node": ">=12.0.0" }, "repository": { diff --git a/sdk/keyvault/keyvault-admin/samples/v4/typescript/package.json b/sdk/keyvault/keyvault-admin/samples/v4/typescript/package.json index ede426b51fc8..813a05274d14 100644 --- a/sdk/keyvault/keyvault-admin/samples/v4/typescript/package.json +++ b/sdk/keyvault/keyvault-admin/samples/v4/typescript/package.json @@ -3,7 +3,7 @@ "private": true, "version": "1.0.0", "description": "Azure Key Vault Administration client library samples for TypeScript", - "engine": { + "engines": { "node": ">=12.0.0" }, "scripts": { diff --git a/sdk/keyvault/keyvault-admin/samples/v4/typescript/src/backupRestoreHelloWorld.ts b/sdk/keyvault/keyvault-admin/samples/v4/typescript/src/backupRestoreHelloWorld.ts index 4fd269b8c29c..926b9da7f27a 100644 --- a/sdk/keyvault/keyvault-admin/samples/v4/typescript/src/backupRestoreHelloWorld.ts +++ b/sdk/keyvault/keyvault-admin/samples/v4/typescript/src/backupRestoreHelloWorld.ts @@ -24,22 +24,41 @@ export async function main(): Promise { } const client = new KeyVaultBackupClient(url, credential); - const blobStorageUri = process.env["BLOB_STORAGE_URI"]; - if (!blobStorageUri) { - throw new Error("Missing environment variable BLOB_STORAGE_URI."); - } const sasToken = process.env["BLOB_STORAGE_SAS_TOKEN"]; if (!sasToken) { throw new Error("Missing environment variable BLOB_STORAGE_SAS_TOKEN."); } - const backupPoller = await client.beginBackup(blobStorageUri!, sasToken); - await backupPoller.pollUntilDone(); - // The folder name should be at the end of the backupFolderUri, as in: https:/// - const restorePoller = await client.beginRestore(blobStorageUri, sasToken); + // Create a Uri with the storage container path. + const blobContainerUri = buildBlobContainerUri(); + + // Start the backup and wait for its completion. + const backupPoller = await client.beginBackup(blobContainerUri, sasToken); + const backupResult = await backupPoller.pollUntilDone(); + + // Finally, start and wait for the restore operation using the folderUri returned from a previous backup operation. + const restorePoller = await client.beginRestore(backupResult.folderUri!, sasToken); await restorePoller.pollUntilDone(); } +/** + * Helper function to construct a valid blob container URI from its parts. + */ +function buildBlobContainerUri() { + const blobStorageUri = process.env["BLOB_STORAGE_URI"]; + if (!blobStorageUri) { + throw new Error("Missing environment variable BLOB_STORAGE_URI."); + } + + const blobContainerName = process.env["BLOB_CONTAINER_NAME"]; + if (!blobContainerName) { + throw new Error("Missing environment variable BLOB_CONTAINER_NAME."); + } + + // If there are trailing slashes, remove them before building the URI. + return `${blobStorageUri.replace(/\/$/, "")}/${blobContainerName}`; +} + main().catch((err) => { console.log("error code: ", err.code); console.log("error message: ", err.message); diff --git a/sdk/keyvault/keyvault-admin/samples/v4/typescript/src/backupSelectiveKeyRestore.ts b/sdk/keyvault/keyvault-admin/samples/v4/typescript/src/backupSelectiveKeyRestore.ts index 367f82f7975f..fa2e99fd5a8b 100644 --- a/sdk/keyvault/keyvault-admin/samples/v4/typescript/src/backupSelectiveKeyRestore.ts +++ b/sdk/keyvault/keyvault-admin/samples/v4/typescript/src/backupSelectiveKeyRestore.ts @@ -29,28 +29,45 @@ export async function main(): Promise { const keyName = "key-name"; const key = await keyClient.createRsaKey(keyName); - const blobStorageUri = process.env["BLOB_STORAGE_URI"]; - if (!blobStorageUri) { - throw new Error("Missing environment variable BLOB_STORAGE_URI."); - } const sasToken = process.env["BLOB_STORAGE_SAS_TOKEN"]; if (!sasToken) { throw new Error("Missing environment variable BLOB_STORAGE_SAS_TOKEN."); } - const backupPoller = await client.beginBackup(blobStorageUri, sasToken); - await backupPoller.pollUntilDone(); + // Create a Uri with the storage container path. + const blobContainerUri = buildBlobContainerUri(); + + // Start the backup and wait for its completion. + const backupPoller = await client.beginBackup(blobContainerUri, sasToken); + const backupResult = await backupPoller.pollUntilDone(); + console.log("backupResult", backupResult); + + // Finally, start and wait for the restore operation using the folderUri returned from a previous backup operation. const selectiveKeyRestorePoller = await client.beginSelectiveKeyRestore( key.name, - blobStorageUri, + backupResult.folderUri!, sasToken ); - await selectiveKeyRestorePoller.pollUntilDone(); + const restoreResult = await selectiveKeyRestorePoller.pollUntilDone(); + console.log("restoreResult", restoreResult); +} + +/** + * Helper function to construct a valid blob container URI from its parts. + */ +function buildBlobContainerUri() { + const blobStorageUri = process.env["BLOB_STORAGE_URI"]; + if (!blobStorageUri) { + throw new Error("Missing environment variable BLOB_STORAGE_URI."); + } + + const blobContainerName = process.env["BLOB_CONTAINER_NAME"]; + if (!blobContainerName) { + throw new Error("Missing environment variable BLOB_CONTAINER_NAME."); + } - // Deleting and purging the key, just in case we want to create the same key again. - const deleteKeyPoller = await keyClient.beginDeleteKey(keyName); - await deleteKeyPoller.pollUntilDone(); - await keyClient.purgeDeletedKey(keyName); + // If there are trailing slashes, remove them before building the URI. + return `${blobStorageUri.replace(/\/$/, "")}/${blobContainerName}`; } main().catch((err) => {