azure-sdk-for-cpp/sdk/keyvault/azure-security-keyvault-secrets/samples/sample1-basic-operations/sample1_basic_operations.cpp
gearama 125c305961
Keyvault Secrets remake (#6463)
* first stab at a client tsp /tspconfig scripts

* space

* updates to clientTSP

* update for latest specs for KV

* gfd

* gfd

* hghf

* dfgdf

* jhg

* oopsie

* comments

* headers

* clang & param

* coverage

* more comments not gened

* spell

* more comments

* regen for testing

* gfd

* gdf

* tretre

* update to use tsp client

* vcxvx

* fdsfsd

* fds

* round 2

* api view

* dsa

* rte

* update to use code injection

* clang and comments

* gfd

* updates

* clang

* fsd

* spell

* comments

* some cleanup

* first test passing

* dsa

* second test

* startdeletesecret

* update secret ptoeprties

* recover secret

* cleanup clang

* docs

* update recordings and clean up tests

* ggfd

* ooops

* samples build

* gdf

* samples run

* perf tests

* CLANG

* fds

* CLAAAAAAAAANG

* update to latest version of the TSPs

* update to latest codegen

* gdfgdf

* replace method

* client and stuffs

* PR comments

* remove security from path

* clang

* qfe

* gdf

* update to models and to the lastest codegen

* forgot cmake file

* Update sdk/keyvault/azure-security-keyvault-secrets/samples/sample1-basic-operations/sample1_basic_operations.cpp

Co-authored-by: Anton Kolesnyk <41349689+antkmsft@users.noreply.github.com>

* test api view

* dfsfsd

* ccxcvx

* step1

* delete, and lor and paged

* all work

* works and serializers and test for them gone

* cleanup test files

* sample

* tests passing

* dfsfsd

* latest codegen

* after merge

* fsd

* gfd

* gfd

* ApiView

* move to _details namespace

* put back readme

* update readmes

* clang

* dfsd

* get rid of snippets, validation fails without explanation as to what the failure is

* update locations of methods tp cpp files

* format files

* cover

---------

Co-authored-by: Anton Kolesnyk <41349689+antkmsft@users.noreply.github.com>
2025-03-14 13:46:49 -07:00

87 lines
2.8 KiB
C++

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* @brief This sample provides the code implementation to use the Key Vault Secrets SDK client for
* C++ to create, get, update, delete and purge a secret.
*
* @remark The following environment variables must be set before running the sample.
* - AZURE_KEYVAULT_URL: To the Key Vault account URL.
*
*/
#include <azure/identity.hpp>
#include <azure/keyvault/secrets.hpp>
#include <chrono>
#include <iostream>
using namespace Azure::Security::KeyVault::Secrets;
using namespace std::chrono_literals;
int main()
{
// @begin_snippet: SecretSample1CreateCredential
auto const keyVaultUrl = std::getenv("AZURE_KEYVAULT_URL");
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();
// create client
SecretClient secretClient(keyVaultUrl, credential);
// @end_snippet
try
{
// create secret
// @begin_snippet: SecretSample1CreateSecret
std::string secretName("MySampleSecret");
std::string secretValue("my secret value");
secretClient.SetSecret(secretName, secretValue);
// @end_snippet
// @begin_snippet: SecretSample1GetSecret
// 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;
// @end_snippet
// @begin_snippet: SecretSample1UpdateSecretProperties
// change one of the properties
secret.Properties.ContentType = "my content";
// update the secret
KeyVaultSecret updatedSecret = secretClient.UpdateSecretProperties(secret.Properties).Value;
std::string updatedValueString = updatedSecret.Properties.ContentType.ValueOr("NONE RETURNED");
std::cout << "Secret's content type is now " << updatedValueString << std::endl;
// @end_snippet
// @begin_snippet: SecretSample1DeleteSecret
// 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);
// purge the deleted secret
secretClient.PurgeDeletedSecret(secret.Name);
// @end_snippet
}
catch (Azure::Core::Credentials::AuthenticationException const& e)
{
std::cout << "Authentication Exception happened:" << std::endl << e.what() << std::endl;
return 1;
}
catch (Azure::Core::RequestFailedException const& e)
{
std::cout << "Key Vault Secret Client Exception happened:" << std::endl
<< e.Message << std::endl;
return 1;
}
return 0;
}