|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | | -use azure_identity::DeveloperToolsCredential; |
| 4 | +use azure_core::{ |
| 5 | + error::{ErrorKind, Result}, |
| 6 | + http::StatusCode, |
| 7 | + time::Duration, |
| 8 | +}; |
| 9 | +use azure_core_test::{recorded, TestContext}; |
5 | 10 | use azure_security_keyvault_secrets::SecretClient; |
6 | 11 | use include_file::include_markdown; |
7 | 12 |
|
8 | | -#[ignore = "only ensures README code snippets compile"] |
9 | | -#[tokio::test] |
10 | | -async fn readme() -> Result<(), Box<dyn std::error::Error>> { |
11 | | - let credential = DeveloperToolsCredential::new(None)?; |
12 | | - let client = SecretClient::new("https://my-vault.vault.azure.net", credential.clone(), None)?; |
| 13 | +#[recorded::test] |
| 14 | +async fn readme(ctx: TestContext) -> Result<()> { |
| 15 | + use azure_security_keyvault_secrets::SecretClientOptions; |
| 16 | + |
| 17 | + let recording = ctx.recording(); |
| 18 | + |
| 19 | + let mut options = SecretClientOptions::default(); |
| 20 | + recording.instrument(&mut options.client_options); |
| 21 | + |
| 22 | + let client = SecretClient::new( |
| 23 | + recording.var("AZURE_KEYVAULT_URL", None).as_str(), |
| 24 | + recording.credential(), |
| 25 | + Some(options), |
| 26 | + )?; |
13 | 27 |
|
14 | 28 | // Each macro invocation is in its own block to prevent errors with duplicate imports. |
15 | 29 | { |
| 30 | + println!("Create a secret"); |
16 | 31 | include_markdown!("README.md", "create_secret"); |
17 | 32 | } |
18 | 33 |
|
19 | 34 | { |
| 35 | + println!("Get a secret"); |
20 | 36 | include_markdown!("README.md", "get_secret"); |
21 | 37 | } |
22 | 38 |
|
23 | 39 | { |
| 40 | + println!("Update a secret"); |
24 | 41 | include_markdown!("README.md", "update_secret"); |
25 | 42 | } |
26 | 43 |
|
27 | 44 | { |
28 | | - include_markdown!("README.md", "delete_secret"); |
| 45 | + println!("List secrets"); |
| 46 | + include_markdown!("README.md", "list_secrets"); |
29 | 47 | } |
30 | 48 |
|
31 | 49 | { |
32 | | - include_markdown!("README.md", "list_secrets"); |
| 50 | + println!("Handle errors"); |
| 51 | + include_markdown!("README.md", "errors"); |
33 | 52 | } |
34 | 53 |
|
35 | 54 | { |
36 | | - include_markdown!("README.md", "errors"); |
| 55 | + println!("Delete a secret"); |
| 56 | + include_markdown!("README.md", "delete_secret"); |
| 57 | + |
| 58 | + // Make sure the secret gets purged (may not take immediate effect). |
| 59 | + println!("Purge a secret"); |
| 60 | + for _ in 0..5 { |
| 61 | + match client.purge_deleted_secret("secret-name", None).await { |
| 62 | + Ok(_) => break, |
| 63 | + Err(err) if matches!(err.kind(), ErrorKind::HttpResponse { status, .. } if *status == StatusCode::Conflict) => |
| 64 | + { |
| 65 | + azure_core::sleep(Duration::seconds(1)).await; |
| 66 | + } |
| 67 | + Err(err) => return Err(err), |
| 68 | + } |
| 69 | + } |
37 | 70 | } |
38 | 71 |
|
39 | 72 | Ok(()) |
|
0 commit comments