Skip to content

Commit

Permalink
Fix KeyVault Secrets sample and use the snippets in its README
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsonkhan committed Sep 21, 2024
1 parent 70794a7 commit f91d34b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 13 additions & 6 deletions sdk/keyvault/azure-security-keyvault-secrets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ For detailed samples please review the samples provided.

First step is to create a SecretClient.

<!-- @insert_snippet: SecretSample1CreateCredential -->
```cpp
auto const keyVaultUrl = std::getenv("AZURE_KEYVAULT_URL");
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();

// create client
SecretClient secretClient(keyVaultUrl, credential);
```
Expand All @@ -91,6 +93,7 @@ SecretClient secretClient(keyVaultUrl, credential);
We call the secret client to create a secret.
<!-- @insert_snippet: SecretSample1CreateSecret -->
```cpp
std::string secretName("MySampleSecret");
std::string secretValue("my secret value");
Expand All @@ -102,34 +105,36 @@ secretClient.SetSecret(secretName, secretValue);

We retrieve a secret by name.

<!-- @insert_snippet: SecretSample1GetSecret -->
```cpp
// get secret
KeyVaultSecret secret = secretClient.GetSecret(secretName).Value;

std::string valueString = secret.Value.HasValue() ? secret.Value.Value() : "NONE RETURNED";
std::cout << "Secret is returned with name " << secret.Name << " and value "
<< valueString << std::endl;
std::cout << "Secret is returned with name " << secret.Name << " and value " << valueString
<< std::endl;
```

### Update a secret

Updating an existing secret

<!-- @insert_snippet: SecretSample1UpdateSecretProperties -->
```cpp
// change one of the properties
secret.Properties.ContentType = "my content";
// update the secret
KeyVaultSecret updatedSecret = secretClient.UpdateSecretProperties(secret.Properties).Value;
std::string updatedValueString = updatedSecret.Value.HasValue() ? updatedSecret.Value.Value()
: "NONE RETURNED";
std::cout << "Secret's content type is now " << updatedValueString
<< std::endl;
std::string updatedValueString
= updatedSecret.Value.HasValue() ? updatedSecret.Value.Value() : "NONE RETURNED";
std::cout << "Secret's content type is now " << updatedValueString << std::endl;
```

### Delete a secret

Delete an existing secret.

<!-- @insert_snippet: SecretSample1DeleteSecret -->
```cpp
// start deleting the secret
DeleteSecretOperation operation = secretClient.StartDeleteSecret(secret.Name);
Expand All @@ -147,6 +152,7 @@ secretClient.PurgeDeletedSecret(secret.Name);

Delete and Purge a secret.

<!-- @insert_snippet: SecretSample1DeleteSecret -->
```cpp
// start deleting the secret
DeleteSecretOperation operation = secretClient.StartDeleteSecret(secret.Name);
Expand All @@ -164,6 +170,7 @@ secretClient.PurgeDeletedSecret(secret.Name);

List all the secrets in keyvault.

<!-- @insert_snippet: SecretSample4ListAllSecrets -->
```cpp
// get all the versions of a secret
for (auto secretsVersion = secretClient.GetPropertiesOfSecretsVersions(secret1.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ using namespace std::chrono_literals;

int main()
{
auto const keyVaultUrl = std::getenv("AZURE_KEYVAULT_URL");
// @begin_snippet: SecretSample1CreateCredential
auto const keyVaultUrl = std::getenv("AZURE_KEYVAULT_URL");
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();

// create client
Expand Down

0 comments on commit f91d34b

Please sign in to comment.