Skip to content

Commit 340543b

Browse files
authored
samples update for nullable (#5574)
* samples update for nullable * weqe * maybe * key sample
1 parent 6390582 commit 340543b

12 files changed

Lines changed: 97 additions & 87 deletions

File tree

sdk/keyvault/azure-security-keyvault-certificates/samples/certificate_basic_operations.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ auto updatedCertificate
7171
.Value;
7272

7373
std::cout << "After update certificate is enabled : "
74-
<< (updatedCertificate.Properties.Enabled.Value() ? "true" : "false");
74+
<< (updatedCertificate.Properties.Enabled.HasValue()
75+
&& updatedCertificate.Properties.Enabled.Value()
76+
? "true"
77+
: "false");
7578
```
7679

7780
## Deleting a Certificate

sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-basic-operations/certificate_basic_operations.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ int main()
9393
.Value;
9494

9595
std::cout << "After update certificate is enabled : "
96-
<< (updatedCertificate.Properties.Enabled.Value() ? "true" : "false");
96+
<< (updatedCertificate.Properties.Enabled.HasValue()
97+
&& updatedCertificate.Properties.Enabled.Value()
98+
? "true"
99+
: "false");
97100
}
98101
// delete the certificate
99102
{

sdk/keyvault/azure-security-keyvault-certificates/test/samples/certificate-get-certificates/certificate_get_certificates.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ KeyVaultCertificateWithPolicy CreateCertificate(
158158
auto pollResponse = response.PollUntilDone(defaultWait).Value;
159159

160160
// check the status of the poll response
161-
if (!pollResponse.Error && pollResponse.Status.Value() == "completed")
161+
if (!pollResponse.Error && pollResponse.Status.HasValue()
162+
&& pollResponse.Status.Value() == "completed")
162163
{
163164
// get the certificate
164165
auto certificate = certificateClient.GetCertificate(certificateName).Value;

sdk/keyvault/azure-security-keyvault-keys/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,6 @@ client.PurgeDeletedKey(key.Name());
313313
This example lists all the keys in the specified Azure Key Vault.
314314

315315
```cpp
316-
Pageable<KeyProperties> allKeys = client.GetPropertiesOfKeys();
317-
318316
for (auto keys = client.GetPropertiesOfKeys(); keys.HasPage(); keys.MoveToNextPage())
319317
{
320318
for (auto const& key : keys.Items)

sdk/keyvault/azure-security-keyvault-secrets/README.md

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -51,108 +51,104 @@ For detailed samples please review the samples provided.
5151

5252
First step is to create a SecretClient.
5353

54-
<!-- @insert_snippet: SecretSample1CreateCredential -->
5554
```cpp
56-
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();
55+
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();
5756

58-
// create client
59-
SecretClient secretClient(std::getenv("AZURE_KEYVAULT_URL"), credential);
57+
// create client
58+
SecretClient secretClient(std::getenv("AZURE_KEYVAULT_URL"), credential);
6059
```
6160
6261
### Create a secret
6362
6463
We call the secret client to create a secret.
6564
66-
<!-- @insert_snippet: SecretSample1CreateSecret -->
6765
```cpp
68-
std::string secretName("MySampleSecret");
69-
std::string secretValue("my secret value");
66+
std::string secretName("MySampleSecret");
67+
std::string secretValue("my secret value");
7068
71-
secretClient.SetSecret(secretName, secretValue);
69+
secretClient.SetSecret(secretName, secretValue);
7270
```
7371

7472
### Get a secret
7573

7674
We retrieve a secret by name.
7775

78-
<!-- @insert_snippet: SecretSample1GetSecret -->
7976
```cpp
80-
// get secret
81-
KeyVaultSecret secret = secretClient.GetSecret(secretName).Value;
77+
// get secret
78+
KeyVaultSecret secret = secretClient.GetSecret(secretName).Value;
8279

83-
std::cout << "Secret is returned with name " << secret.Name << " and value "
84-
<< secret.Value.Value() << std::endl;
80+
std::string valueString = secret.Value.HasValue() ? secret.Value.Value() : "NONE RETURNED";
81+
std::cout << "Secret is returned with name " << secret.Name << " and value "
82+
<< valueString << std::endl;
8583
```
8684

8785
### Update a secret
8886

8987
Updating an existing secret
9088

91-
<!-- @insert_snippet: SecretSample1UpdateSecretProperties -->
9289
```cpp
93-
// change one of the properties
94-
secret.Properties.ContentType = "my content";
95-
// update the secret
96-
KeyVaultSecret updatedSecret = secretClient.UpdateSecretProperties(secret.Properties).Value;
97-
std::cout << "Secret's content type is now " << updatedSecret.Properties.ContentType.Value()
98-
<< std::endl;
90+
// change one of the properties
91+
secret.Properties.ContentType = "my content";
92+
// update the secret
93+
KeyVaultSecret updatedSecret = secretClient.UpdateSecretProperties(secret.Properties).Value;
94+
std::string updatedValueString = updatedSecret.Value.HasValue() ? updatedSecret.Value.Value()
95+
: "NONE RETURNED";
96+
std::cout << "Secret's content type is now " << updatedValueString
97+
<< std::endl;
9998
```
10099

101100
### Delete a secret
102101

103102
Delete an existing secret.
104103

105-
<!-- @insert_snippet: SecretSample1DeleteSecret -->
106104
```cpp
107-
// start deleting the secret
108-
DeleteSecretOperation operation = secretClient.StartDeleteSecret(secret.Name);
105+
// start deleting the secret
106+
DeleteSecretOperation operation = secretClient.StartDeleteSecret(secret.Name);
109107

110-
// You only need to wait for completion if you want to purge or recover the secret.
111-
// The duration of the delete operation might vary
112-
// in case returns too fast increase the timeout value
113-
operation.PollUntilDone(20s);
108+
// You only need to wait for completion if you want to purge or recover the secret.
109+
// The duration of the delete operation might vary
110+
// in case returns too fast increase the timeout value
111+
operation.PollUntilDone(20s);
114112

115-
// purge the deleted secret
116-
secretClient.PurgeDeletedSecret(secret.Name);
113+
// purge the deleted secret
114+
secretClient.PurgeDeletedSecret(secret.Name);
117115
```
118116

119117
### Delete and purge a secret
120118

121119
Delete and Purge a secret.
122120

123-
<!-- @insert_snippet: SecretSample1DeleteSecret -->
124121
```cpp
125-
// start deleting the secret
126-
DeleteSecretOperation operation = secretClient.StartDeleteSecret(secret.Name);
122+
// start deleting the secret
123+
DeleteSecretOperation operation = secretClient.StartDeleteSecret(secret.Name);
127124

128-
// You only need to wait for completion if you want to purge or recover the secret.
129-
// The duration of the delete operation might vary
130-
// in case returns too fast increase the timeout value
131-
operation.PollUntilDone(20s);
125+
// You only need to wait for completion if you want to purge or recover the secret.
126+
// The duration of the delete operation might vary
127+
// in case returns too fast increase the timeout value
128+
operation.PollUntilDone(20s);
132129

133-
// purge the deleted secret
134-
secretClient.PurgeDeletedSecret(secret.Name);
130+
// purge the deleted secret
131+
secretClient.PurgeDeletedSecret(secret.Name);
135132
```
136133

137134
### List Secrets
138135

139136
List all the secrets in keyvault.
140137

141-
<!-- @insert_snippet: SecretSample4ListAllSecrets -->
142138
```cpp
143-
// get all the versions of a secret
144-
for (auto secretsVersion = secretClient.GetPropertiesOfSecretsVersions(secret1.Name);
145-
secretsVersion.HasPage();
146-
secretsVersion.MoveToNextPage())
147-
{ // go through each version of the secret
148-
// the number of results returned for in a page is not guaranteed
149-
// it can be anywhere from 0 to 25
150-
for (auto const& secret : secretsVersion.Items)
151-
{
152-
std::cout << "Found Secret with name: " << secret.Name
153-
<< " and with version: " << secret.Version << std::endl;
154-
}
155-
}
139+
// get all the versions of a secret
140+
for (auto secretsVersion = secretClient.GetPropertiesOfSecretsVersions(secret1.Name);
141+
secretsVersion.HasPage();
142+
secretsVersion.MoveToNextPage())
143+
{ // go through each version of the secret
144+
// the number of results returned for in a page is not guaranteed
145+
// it can be anywhere from 0 to 25
146+
for (auto const& secret : secretsVersion.Items)
147+
{
148+
std::cout << "Found Secret with name: " << secret.Name
149+
<< " and with version: " << secret.Version << std::endl;
150+
}
151+
}
156152
```
157153

158154
## Troubleshooting

sdk/keyvault/azure-security-keyvault-secrets/samples/sample1_basic_operations.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Key Vault Secrets client for C++ currently supports any `TokenCredential` for au
1212
In the sample below, you can create a credential by setting the Tenant ID, Client ID and Client Secret as environment variables.
1313

1414
```cpp Snippet:SecretSample1CreateCredential
15-
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();
15+
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();
1616
```
1717

1818
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.
3838

3939
```cpp Snippet:SecretSample1GetSecret
4040
// get secret
41-
Secret secret = secretClient.GetSecret(secretName).Value;
42-
std::cout << "Secret is returned with name " << secret.Name << " and value " << secret.Value
43-
<< std::endl;
41+
KeyVaultSecret secret = secretClient.GetSecret(secretName).Value;
42+
43+
std::string valueString = secret.Value.HasValue() ? secret.Value.Value() : "NONE RETURNED";
44+
std::cout << "Secret is returned with name " << secret.Name << " and value "
45+
<< valueString << std::endl;
4446
```
4547

4648
## Updating secret properties
4749

4850
Call UpdateSecretProperties to change on of the secret properties.
4951

50-
5152
```cpp Snippet:SecretSample1UpdateSecretProperties
5253
// change one of the properties
5354
secret.Properties.ContentType = "my content";
5455
// update the secret
55-
Secret updatedSecret = secretClient.UpdateSecretProperties(secret.Name, secret.Properties.Version, secret.Properties)
56-
.Value;
57-
std::cout << "Secret's content type is now " << updatedSecret.Properties.ContentType.Value()
56+
KeyVaultSecret updatedSecret = secretClient.UpdateSecretProperties(secret.Properties).Value;
57+
std::string updatedValueString = updatedSecret.Value.HasValue() ? updatedSecret.Value.Value()
58+
: "NONE RETURNED";
59+
std::cout << "Secret's content type is now " << updatedValueString
5860
<< std::endl;
5961
```
6062

sdk/keyvault/azure-security-keyvault-secrets/samples/sample2_backup_restore.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Key Vault Secrets client for C++ currently supports any `TokenCredential` for au
1212
In the sample below, you can create a credential by setting the Tenant ID, Client ID and Client Secret as environment variables.
1313

1414
```cpp Snippet:SecretSample2CreateCredential
15-
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();
15+
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();
1616
```
1717

1818
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.
3838

3939
```cpp Snippet:SecretSample2GetSecret
4040
// get secret
41-
Secret secret = secretClient.GetSecret(secretName).Value;
42-
std::cout << "Secret is returned with name " << secret.Name << " and value " << secret.Value
43-
<< std::endl;
41+
KeyVaultSecret secret = secretClient.GetSecret(secretName).Value;
42+
43+
std::string valueString = secret.Value.HasValue() ? secret.Value.Value() : "NONE RETURNED";
44+
std::cout << "Secret is returned with name " << secret.Name << " and value "
45+
<< valueString << std::endl;
4446
```
4547

4648
## Creating a Backup for the secret properties

sdk/keyvault/azure-security-keyvault-secrets/samples/sample3_delete_recover.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Key Vault Secrets client for C++ currently supports any `TokenCredential` for au
1111
In the sample below, you can create a credential by setting the Tenant ID, Client ID and Client Secret as environment variables.
1212

1313
```cpp Snippet:SecretSample3CreateCredential
14-
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();
14+
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();
1515
```
1616

1717
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.
3737

3838
```cpp Snippet:SecretSample3GetSecret
3939
// get secret
40-
Secret secret = secretClient.GetSecret(secretName).Value;
41-
std::cout << "Secret is returned with name " << secret.Name << " and value " << secret.Value
42-
<< std::endl;
40+
KeyVaultSecret secret = secretClient.GetSecret(secretName).Value;
41+
42+
std::string valueString = secret.Value.HasValue() ? secret.Value.Value() : "NONE RETURNED";
43+
std::cout << "Secret is returned with name " << secret.Name << " and value "
44+
<< valueString << std::endl;
4345
```
4446

4547
## Deleting a secret

sdk/keyvault/azure-security-keyvault-secrets/samples/sample4_get_secrets_deleted.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ Key Vault Secrets client for C++ currently supports any `TokenCredential` for au
1010

1111
In the sample below, you can create a credential by setting the Tenant ID, Client ID and Client Secret as environment variables.
1212

13-
```cpp Snippet:SecretSample4CreateCredential
14-
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();
13+
```cpp
14+
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();
1515
```
1616

1717
Then, in the sample below, you can set `keyVaultUrl` based on an environment variable, configuration setting, or any way that works for your application.
1818

19-
```cpp Snippet:SecretSample4SecretClient
19+
```cpp
2020
SecretClient secretClient(std::getenv("AZURE_KEYVAULT_URL"), credential);
2121
```
2222
2323
## Creating a couple of Secrets
2424
2525
Call SetSecret to create a couple of new secret with names and secret values.
2626
27-
```cpp Snippet:SecretSample4SetSecret
27+
```cpp
2828
std::string secretName("MySampleSecret");
2929
std::string secretName2("MySampleSecret2");
3030
std::string secretValue("my secret value");
@@ -37,7 +37,7 @@ Secret secret2 = secretClient.SetSecret(secretName2, secretValue).Value;
3737

3838
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.
3939

40-
```cpp Snippet:SecretSample4ListAllSecrets
40+
```cpp
4141
// get properties of secrets
4242
for (auto secrets = secretClient.GetPropertiesOfSecrets(); secrets.HasPage(); secrets.MoveToNextPage())
4343
{ // go through every secret of each page returned

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,19 @@ int main()
4242
// get secret
4343
KeyVaultSecret secret = secretClient.GetSecret(secretName).Value;
4444

45-
std::cout << "Secret is returned with name " << secret.Name << " and value "
46-
<< secret.Value.Value() << std::endl;
45+
std::string valueString = secret.Value.HasValue() ? secret.Value.Value() : "NONE RETURNED";
46+
std::cout << "Secret is returned with name " << secret.Name << " and value " << valueString
47+
<< std::endl;
4748
// @end_snippet
4849

4950
// @begin_snippet: SecretSample1UpdateSecretProperties
5051
// change one of the properties
5152
secret.Properties.ContentType = "my content";
5253
// update the secret
5354
KeyVaultSecret updatedSecret = secretClient.UpdateSecretProperties(secret.Properties).Value;
54-
std::cout << "Secret's content type is now " << updatedSecret.Properties.ContentType.Value()
55-
<< std::endl;
55+
std::string updatedValueString
56+
= updatedSecret.Value.HasValue() ? updatedSecret.Value.Value() : "NONE RETURNED";
57+
std::cout << "Secret's content type is now " << updatedValueString << std::endl;
5658
// @end_snippet
5759

5860
// @begin_snippet: SecretSample1DeleteSecret

0 commit comments

Comments
 (0)