Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KeyVault] - Update KeyVault Admin samples #15613

Merged
merged 1 commit into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
maorleger marked this conversation as resolved.
Show resolved Hide resolved
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