From 340543bb037fa94b58782766ff4bb28c6b10e410 Mon Sep 17 00:00:00 2001 From: George Arama <50641385+gearama@users.noreply.github.com> Date: Mon, 29 Apr 2024 09:14:10 -0700 Subject: [PATCH] samples update for nullable (#5574) * samples update for nullable * weqe * maybe * key sample --- .../samples/certificate_basic_operations.md | 5 +- .../certificate_basic_operations.cpp | 5 +- .../certificate_get_certificates.cpp | 3 +- .../azure-security-keyvault-keys/README.md | 2 - .../azure-security-keyvault-secrets/README.md | 100 +++++++++--------- .../samples/sample1_basic_operations.md | 18 ++-- .../samples/sample2_backup_restore.md | 10 +- .../samples/sample3_delete_recover.md | 10 +- .../samples/sample4_get_secrets_deleted.md | 10 +- .../sample1_basic_operations.cpp | 10 +- .../sample2_backup_restore.cpp | 5 +- .../sample3_delete_recover.cpp | 6 +- 12 files changed, 97 insertions(+), 87 deletions(-) diff --git a/sdk/keyvault/azure-security-keyvault-certificates/samples/certificate_basic_operations.md b/sdk/keyvault/azure-security-keyvault-certificates/samples/certificate_basic_operations.md index 38e809f12..c5646005f 100644 --- a/sdk/keyvault/azure-security-keyvault-certificates/samples/certificate_basic_operations.md +++ b/sdk/keyvault/azure-security-keyvault-certificates/samples/certificate_basic_operations.md @@ -71,7 +71,10 @@ auto updatedCertificate .Value; std::cout << "After update certificate is enabled : " - << (updatedCertificate.Properties.Enabled.Value() ? "true" : "false"); + << (updatedCertificate.Properties.Enabled.HasValue() + && updatedCertificate.Properties.Enabled.Value() + ? "true" + : "false"); ``` ## Deleting a Certificate diff --git a/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-basic-operations/certificate_basic_operations.cpp b/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-basic-operations/certificate_basic_operations.cpp index 89f0ac650..a5b50a783 100644 --- a/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-basic-operations/certificate_basic_operations.cpp +++ b/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-basic-operations/certificate_basic_operations.cpp @@ -93,7 +93,10 @@ int main() .Value; std::cout << "After update certificate is enabled : " - << (updatedCertificate.Properties.Enabled.Value() ? "true" : "false"); + << (updatedCertificate.Properties.Enabled.HasValue() + && updatedCertificate.Properties.Enabled.Value() + ? "true" + : "false"); } // delete the certificate { diff --git a/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-get-certificates/certificate_get_certificates.cpp b/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-get-certificates/certificate_get_certificates.cpp index b29659b60..6028d999a 100644 --- a/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-get-certificates/certificate_get_certificates.cpp +++ b/sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-get-certificates/certificate_get_certificates.cpp @@ -158,7 +158,8 @@ KeyVaultCertificateWithPolicy CreateCertificate( auto pollResponse = response.PollUntilDone(defaultWait).Value; // check the status of the poll response - if (!pollResponse.Error && pollResponse.Status.Value() == "completed") + if (!pollResponse.Error && pollResponse.Status.HasValue() + && pollResponse.Status.Value() == "completed") { // get the certificate auto certificate = certificateClient.GetCertificate(certificateName).Value; diff --git a/sdk/keyvault/azure-security-keyvault-keys/README.md b/sdk/keyvault/azure-security-keyvault-keys/README.md index 3af929e62..92e15c219 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/README.md +++ b/sdk/keyvault/azure-security-keyvault-keys/README.md @@ -313,8 +313,6 @@ client.PurgeDeletedKey(key.Name()); This example lists all the keys in the specified Azure Key Vault. ```cpp -Pageable allKeys = client.GetPropertiesOfKeys(); - for (auto keys = client.GetPropertiesOfKeys(); keys.HasPage(); keys.MoveToNextPage()) { for (auto const& key : keys.Items) diff --git a/sdk/keyvault/azure-security-keyvault-secrets/README.md b/sdk/keyvault/azure-security-keyvault-secrets/README.md index a826a6056..6eec2031a 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/README.md +++ b/sdk/keyvault/azure-security-keyvault-secrets/README.md @@ -51,108 +51,104 @@ For detailed samples please review the samples provided. First step is to create a SecretClient. - ```cpp - auto credential = std::make_shared(); +auto credential = std::make_shared(); - // create client - SecretClient secretClient(std::getenv("AZURE_KEYVAULT_URL"), credential); +// create client +SecretClient secretClient(std::getenv("AZURE_KEYVAULT_URL"), credential); ``` ### Create a secret We call the secret client to create a secret. - ```cpp - std::string secretName("MySampleSecret"); - std::string secretValue("my secret value"); +std::string secretName("MySampleSecret"); +std::string secretValue("my secret value"); - secretClient.SetSecret(secretName, secretValue); +secretClient.SetSecret(secretName, secretValue); ``` ### Get a secret We retrieve a secret by name. - ```cpp - // get secret - KeyVaultSecret secret = secretClient.GetSecret(secretName).Value; +// get secret +KeyVaultSecret secret = secretClient.GetSecret(secretName).Value; - std::cout << "Secret is returned with name " << secret.Name << " and value " - << secret.Value.Value() << std::endl; +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; ``` ### Update a secret Updating an existing secret - ```cpp - // change one of the properties - secret.Properties.ContentType = "my content"; - // update the secret - KeyVaultSecret updatedSecret = secretClient.UpdateSecretProperties(secret.Properties).Value; - std::cout << "Secret's content type is now " << updatedSecret.Properties.ContentType.Value() - << std::endl; +// 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; ``` ### Delete a secret Delete an existing secret. - ```cpp - // start deleting the secret - DeleteSecretOperation operation = secretClient.StartDeleteSecret(secret.Name); +// start deleting the secret +DeleteSecretOperation operation = secretClient.StartDeleteSecret(secret.Name); - // You only need to wait for completion if you want to purge or recover the secret. - // The duration of the delete operation might vary - // in case returns too fast increase the timeout value - operation.PollUntilDone(20s); +// You only need to wait for completion if you want to purge or recover the secret. +// The duration of the delete operation might vary +// in case returns too fast increase the timeout value +operation.PollUntilDone(20s); - // purge the deleted secret - secretClient.PurgeDeletedSecret(secret.Name); +// purge the deleted secret +secretClient.PurgeDeletedSecret(secret.Name); ``` ### Delete and purge a secret Delete and Purge a secret. - ```cpp - // start deleting the secret - DeleteSecretOperation operation = secretClient.StartDeleteSecret(secret.Name); +// start deleting the secret +DeleteSecretOperation operation = secretClient.StartDeleteSecret(secret.Name); - // You only need to wait for completion if you want to purge or recover the secret. - // The duration of the delete operation might vary - // in case returns too fast increase the timeout value - operation.PollUntilDone(20s); +// You only need to wait for completion if you want to purge or recover the secret. +// The duration of the delete operation might vary +// in case returns too fast increase the timeout value +operation.PollUntilDone(20s); - // purge the deleted secret - secretClient.PurgeDeletedSecret(secret.Name); +// purge the deleted secret +secretClient.PurgeDeletedSecret(secret.Name); ``` ### List Secrets List all the secrets in keyvault. - ```cpp - // get all the versions of a secret - for (auto secretsVersion = secretClient.GetPropertiesOfSecretsVersions(secret1.Name); - secretsVersion.HasPage(); - secretsVersion.MoveToNextPage()) - { // go through each version of the secret - // the number of results returned for in a page is not guaranteed - // it can be anywhere from 0 to 25 - for (auto const& secret : secretsVersion.Items) - { - std::cout << "Found Secret with name: " << secret.Name - << " and with version: " << secret.Version << std::endl; - } - } +// get all the versions of a secret +for (auto secretsVersion = secretClient.GetPropertiesOfSecretsVersions(secret1.Name); + secretsVersion.HasPage(); + secretsVersion.MoveToNextPage()) +{ // go through each version of the secret + // the number of results returned for in a page is not guaranteed + // it can be anywhere from 0 to 25 + for (auto const& secret : secretsVersion.Items) + { + std::cout << "Found Secret with name: " << secret.Name + << " and with version: " << secret.Version << std::endl; + } +} ``` ## Troubleshooting diff --git a/sdk/keyvault/azure-security-keyvault-secrets/samples/sample1_basic_operations.md b/sdk/keyvault/azure-security-keyvault-secrets/samples/sample1_basic_operations.md index ffca30820..945db5630 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/samples/sample1_basic_operations.md +++ b/sdk/keyvault/azure-security-keyvault-secrets/samples/sample1_basic_operations.md @@ -12,7 +12,7 @@ Key Vault Secrets client for C++ currently supports any `TokenCredential` for au In the sample below, you can create a credential by setting the Tenant ID, Client ID and Client Secret as environment variables. ```cpp Snippet:SecretSample1CreateCredential - auto credential = std::make_shared(); +auto credential = std::make_shared(); ``` Then, in the sample below, you can set `keyVaultUrl` based on an environment variable, configuration setting, or any way that works for your application. @@ -38,23 +38,25 @@ Call GetSecret to retrieve a secret from Key Vault. ```cpp Snippet:SecretSample1GetSecret // get secret -Secret secret = secretClient.GetSecret(secretName).Value; -std::cout << "Secret is returned with name " << secret.Name << " and value " << secret.Value - << std::endl; +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; ``` ## Updating secret properties Call UpdateSecretProperties to change on of the secret properties. - ```cpp Snippet:SecretSample1UpdateSecretProperties // change one of the properties secret.Properties.ContentType = "my content"; // update the secret -Secret updatedSecret = secretClient.UpdateSecretProperties(secret.Name, secret.Properties.Version, secret.Properties) - .Value; -std::cout << "Secret's content type is now " << updatedSecret.Properties.ContentType.Value() +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; ``` diff --git a/sdk/keyvault/azure-security-keyvault-secrets/samples/sample2_backup_restore.md b/sdk/keyvault/azure-security-keyvault-secrets/samples/sample2_backup_restore.md index b8c3939d6..902737181 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/samples/sample2_backup_restore.md +++ b/sdk/keyvault/azure-security-keyvault-secrets/samples/sample2_backup_restore.md @@ -12,7 +12,7 @@ Key Vault Secrets client for C++ currently supports any `TokenCredential` for au In the sample below, you can create a credential by setting the Tenant ID, Client ID and Client Secret as environment variables. ```cpp Snippet:SecretSample2CreateCredential - auto credential = std::make_shared(); +auto credential = std::make_shared(); ``` Then, in the sample below, you can set `keyVaultUrl` based on an environment variable, configuration setting, or any way that works for your application. @@ -38,9 +38,11 @@ Call GetSecret to retrieve a secret from Key Vault. ```cpp Snippet:SecretSample2GetSecret // get secret -Secret secret = secretClient.GetSecret(secretName).Value; -std::cout << "Secret is returned with name " << secret.Name << " and value " << secret.Value - << std::endl; +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; ``` ## Creating a Backup for the secret properties diff --git a/sdk/keyvault/azure-security-keyvault-secrets/samples/sample3_delete_recover.md b/sdk/keyvault/azure-security-keyvault-secrets/samples/sample3_delete_recover.md index f290f3c55..bb2cd700d 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/samples/sample3_delete_recover.md +++ b/sdk/keyvault/azure-security-keyvault-secrets/samples/sample3_delete_recover.md @@ -11,7 +11,7 @@ Key Vault Secrets client for C++ currently supports any `TokenCredential` for au In the sample below, you can create a credential by setting the Tenant ID, Client ID and Client Secret as environment variables. ```cpp Snippet:SecretSample3CreateCredential - auto credential = std::make_shared(); +auto credential = std::make_shared(); ``` Then, in the sample below, you can set `keyVaultUrl` based on an environment variable, configuration setting, or any way that works for your application. @@ -37,9 +37,11 @@ Call GetSecret to retrieve a secret from Key Vault. ```cpp Snippet:SecretSample3GetSecret // get secret -Secret secret = secretClient.GetSecret(secretName).Value; -std::cout << "Secret is returned with name " << secret.Name << " and value " << secret.Value - << std::endl; +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; ``` ## Deleting a secret diff --git a/sdk/keyvault/azure-security-keyvault-secrets/samples/sample4_get_secrets_deleted.md b/sdk/keyvault/azure-security-keyvault-secrets/samples/sample4_get_secrets_deleted.md index 4c0abd498..316814ea9 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/samples/sample4_get_secrets_deleted.md +++ b/sdk/keyvault/azure-security-keyvault-secrets/samples/sample4_get_secrets_deleted.md @@ -10,13 +10,13 @@ Key Vault Secrets client for C++ currently supports any `TokenCredential` for au In the sample below, you can create a credential by setting the Tenant ID, Client ID and Client Secret as environment variables. -```cpp Snippet:SecretSample4CreateCredential - auto credential = std::make_shared(); +```cpp +auto credential = std::make_shared(); ``` Then, in the sample below, you can set `keyVaultUrl` based on an environment variable, configuration setting, or any way that works for your application. -```cpp Snippet:SecretSample4SecretClient +```cpp SecretClient secretClient(std::getenv("AZURE_KEYVAULT_URL"), credential); ``` @@ -24,7 +24,7 @@ SecretClient secretClient(std::getenv("AZURE_KEYVAULT_URL"), credential); Call SetSecret to create a couple of new secret with names and secret values. -```cpp Snippet:SecretSample4SetSecret +```cpp std::string secretName("MySampleSecret"); std::string secretName2("MySampleSecret2"); std::string secretValue("my secret value"); @@ -37,7 +37,7 @@ Secret secret2 = secretClient.SetSecret(secretName2, secretValue).Value; Call GetPropertiesOfSecrets to get the properties of all the secrets in the key vault. The results of this call are paged to a maximum of 25 SecretProperties per page. -```cpp Snippet:SecretSample4ListAllSecrets +```cpp // get properties of secrets for (auto secrets = secretClient.GetPropertiesOfSecrets(); secrets.HasPage(); secrets.MoveToNextPage()) { // go through every secret of each page returned diff --git a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample1-basic-operations/sample1_basic_operations.cpp b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample1-basic-operations/sample1_basic_operations.cpp index 98e76f4fe..86446e33f 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample1-basic-operations/sample1_basic_operations.cpp +++ b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample1-basic-operations/sample1_basic_operations.cpp @@ -42,8 +42,9 @@ int main() // get secret KeyVaultSecret secret = secretClient.GetSecret(secretName).Value; - std::cout << "Secret is returned with name " << secret.Name << " and value " - << secret.Value.Value() << std::endl; + 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; // @end_snippet // @begin_snippet: SecretSample1UpdateSecretProperties @@ -51,8 +52,9 @@ int main() secret.Properties.ContentType = "my content"; // update the secret KeyVaultSecret updatedSecret = secretClient.UpdateSecretProperties(secret.Properties).Value; - std::cout << "Secret's content type is now " << updatedSecret.Properties.ContentType.Value() - << std::endl; + std::string updatedValueString + = updatedSecret.Value.HasValue() ? updatedSecret.Value.Value() : "NONE RETURNED"; + std::cout << "Secret's content type is now " << updatedValueString << std::endl; // @end_snippet // @begin_snippet: SecretSample1DeleteSecret diff --git a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample2-backup-restore/sample2_backup_restore.cpp b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample2-backup-restore/sample2_backup_restore.cpp index 3c127e8d4..b766a3280 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample2-backup-restore/sample2_backup_restore.cpp +++ b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample2-backup-restore/sample2_backup_restore.cpp @@ -40,8 +40,9 @@ int main() // get secret KeyVaultSecret secret = secretClient.GetSecret(secretName).Value; - std::cout << "Secret is returned with name " << secret.Name << " and value " - << secret.Value.Value() << std::endl; + 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; size_t backUpSize = 0; { diff --git a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample3-delete-recover/sample3_delete_recover.cpp b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample3-delete-recover/sample3_delete_recover.cpp index ea497bedc..9a851bbe5 100644 --- a/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample3-delete-recover/sample3_delete_recover.cpp +++ b/sdk/keyvault/azure-security-keyvault-secrets/test/samples/sample3-delete-recover/sample3_delete_recover.cpp @@ -40,9 +40,9 @@ int main() // get secret KeyVaultSecret secret = secretClient.GetSecret(secretName).Value; - std::cout << "Secret is returned with name " << secret.Name << " and value " - << secret.Value.Value() << std::endl; - + 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; // start deleting the secret DeleteSecretOperation operation = secretClient.StartDeleteSecret(secret.Name);