Skip to content

Commit

Permalink
Reorder backup and restore operation ctor args and add sample (#15205)
Browse files Browse the repository at this point in the history
fixes #15065
  • Loading branch information
christothes authored Sep 16, 2020
1 parent 389200d commit 28a6bc6
Show file tree
Hide file tree
Showing 9 changed files with 1,962 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Azure.Security.KeyVault.Administration
{
public partial class BackupOperation : Azure.Operation<System.Uri>
{
public BackupOperation(string id, Azure.Security.KeyVault.Administration.KeyVaultBackupClient client) { }
public BackupOperation(Azure.Security.KeyVault.Administration.KeyVaultBackupClient client, string id) { }
public System.DateTimeOffset? EndTime { get { throw null; } }
public override bool HasCompleted { get { throw null; } }
public override bool HasValue { get { throw null; } }
Expand Down Expand Up @@ -84,7 +84,7 @@ public enum ServiceVersion
}
public partial class RestoreOperation : Azure.Operation<Azure.Response>
{
public RestoreOperation(string id, Azure.Security.KeyVault.Administration.KeyVaultBackupClient client) { }
public RestoreOperation(Azure.Security.KeyVault.Administration.KeyVaultBackupClient client, string id) { }
public System.DateTimeOffset? EndTime { get { throw null; } }
public override bool HasCompleted { get { throw null; } }
public override bool HasValue { get { throw null; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ description: Samples for the Azure.Security.KeyVault.Administration client libra
- Creating, getting, and deleting role assignments [synchronously](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/keyvault/Azure.Security.KeyVault.Administration/samples/Sample1_RbacHelloWorldSync.md) or [asynchronously](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/keyvault/Azure.Security.KeyVault.Administration/samples/Sample1_RbacHelloWorldAsync.md)
- [Assigning roles for specific scopes](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/keyvault/Azure.Security.KeyVault.Administration/samples/Sample2_RbacScopeAssignment.md)
- Performing a full key backup and restore [synchronously](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/keyvault/Azure.Security.KeyVault.Administration/samples/Sample1_BackupHelloWorldSync.md) and [asynchronously](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/keyvault/Azure.Security.KeyVault.Administration/samples/Sample1_BackupHelloWorldAsync.md)
- [Checking the status of a previously started backup or restore](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/keyvault/Azure.Security.KeyVault.Administration/samples/Sample3_BackRestoreResume.md)
- [Performing selective key restore]
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Checking the status of a previously started full key backup and restore

This sample demonstrates how to a check the status and get the result of previously started full key backup and restore operations in Azure Key Vault.
To get started, you'll need a URI to an Azure Key Vault. See the [README](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/keyvault/Azure.Security.KeyVault.Administration/README.md) for links and instructions.

## Checking status of a full key backup operation

Using the `KeyVaultBackupClient` and a `BackupOperation`, you can check the status and retrieve the result of a previously started `BackupOperation`.
For example the `Id` from a started operation on one client can be saved to persistent storage instead of waiting for completion immediately.
At some later time, another client can retrieve the persisted operation Id and construct a `BackupOperation` using a `KeyVaultBackupClient` and the Id
and check for status or wait for completion.

```C# Snippet:ResumeBackupAsync
// Construct a new KeyVaultBackupClient or use an existing one.
KeyVaultBackupClient Client = new KeyVaultBackupClient(new Uri(keyVaultUrl), new DefaultAzureCredential());

// Construct a BackupOperation using a KeyVaultBackupClient and the Id from a previously started operation.
BackupOperation backupOperation = new BackupOperation(client, backupOperationId);

// Wait for completion of the BackupOperation.
Response<Uri> backupResult = await backupOperation.WaitForCompletionAsync();

// Get the Uri for the location of you backup blob.
Uri backupBlobUri = backupResult.Value;
```

## Checking status of a full key restore operation

Using the `KeyVaultBackupClient` and a `RestoreOperation`, you can check the status and retrieve the result of a previously started `RestoreOperation`.
For example the `Id` from a started operation on one client can be saved to persistent storage instead of waiting for completion immediately.
At some later time, another client can retrieve the persisted operation Id and construct a `RestoreOperation` using a `KeyVaultBackupClient` and the Id
and check for status or wait for completion.

```C# Snippet:ResumeRestoreAsync
// Construct a new KeyVaultBackupClient or use an existing one.
KeyVaultBackupClient Client = new KeyVaultBackupClient(new Uri(keyVaultUrl), new DefaultAzureCredential());

// Construct a RestoreOperation using a KeyVaultBackupClient and the Id from a previously started operation.
RestoreOperation restoreOperation = new RestoreOperation(client, restoreOperationId);

// Wait for completion of the RestoreOperation.
Response restoreResult = await restoreOperation.WaitForCompletionAsync();
```

<!-- LINKS -->
[DefaultAzureCredential]: ../../../identity/Azure.Identity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public class BackupOperation : Operation<Uri>
/// <see cref="WaitForCompletionAsync(CancellationToken)"/>, or <see cref="WaitForCompletionAsync(TimeSpan, CancellationToken)"/> must be called
/// to re-populate the details of this operation.
/// </summary>
/// <param name="id">The <see cref="Id" /> from a previous <see cref="BackupOperation" />.</param>
/// <param name="client">An instance of <see cref="KeyVaultBackupClient" />.</param>
/// <param name="id">The <see cref="Id" /> from a previous <see cref="BackupOperation" />.</param>
/// <exception cref="ArgumentNullException"><paramref name="id"/> or <paramref name="client"/> is null.</exception>
public BackupOperation(string id, KeyVaultBackupClient client)
public BackupOperation(KeyVaultBackupClient client, string id)
{
Argument.AssertNotNull(id, nameof(id));
Argument.AssertNotNull(client, nameof(client));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public class RestoreOperation : Operation<Response>
/// <see cref="WaitForCompletionAsync(CancellationToken)"/>, or <see cref="WaitForCompletionAsync(TimeSpan, CancellationToken)"/> must be called
/// to re-populate the details of this operation.
/// </summary>
/// <param name="id">The <see cref="Id" /> from a previous <see cref="BackupOperation" />.</param>
/// <param name="client">An instance of <see cref="KeyVaultBackupClient" />.</param>
/// <param name="id">The <see cref="Id" /> from a previous <see cref="BackupOperation" />.</param>
/// <exception cref="ArgumentNullException"><paramref name="id"/> or <paramref name="client"/> is null.</exception>
public RestoreOperation(string id, KeyVaultBackupClient client)
public RestoreOperation(KeyVaultBackupClient client, string id)
{
Argument.AssertNotNull(id, nameof(id));
Argument.AssertNotNull(client, nameof(client));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@ public BackupRestoreTestBase(bool isAsync) : base(isAsync)
Sanitizer = new BackupRestoreRecordedTestSanitizer();
}

private KeyVaultBackupClient GetClient(TestRecording recording = null)
internal KeyVaultBackupClient GetClient(TestRecording recording = null, bool isInstrumented = true)
{
recording ??= Recording;

return InstrumentClient
(new KeyVaultBackupClient(
new Uri(TestEnvironment.KeyVaultUrl),
TestEnvironment.Credential,
recording.InstrumentClientOptions(new KeyVaultBackupClientOptions())));

var client = new KeyVaultBackupClient(
new Uri(TestEnvironment.KeyVaultUrl),
TestEnvironment.Credential,
recording.InstrumentClientOptions(new KeyVaultBackupClientOptions()));
return isInstrumented ? InstrumentClient(client) : client;
}


Expand Down
Loading

0 comments on commit 28a6bc6

Please sign in to comment.