Skip to content

Commit

Permalink
[KeyVault] - fix admin samples (#15613)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
maorleger authored Jun 9, 2021
1 parent 1127eec commit 36d7801
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 62 deletions.
35 changes: 27 additions & 8 deletions sdk/keyvault/keyvault-admin/samples-dev/backupRestoreHelloWorld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,41 @@ export async function main(): Promise<void> {
}
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://<blob-storage-endpoint>/<folder-name>
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,45 @@ export async function main(): Promise<void> {
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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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://<blob-storage-endpoint>/<folder-name>
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,41 @@ export async function main(): Promise<void> {
}
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://<blob-storage-endpoint>/<folder-name>
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,45 @@ export async function main(): Promise<void> {
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) => {
Expand Down

0 comments on commit 36d7801

Please sign in to comment.