From 133c33cb1882bf8c2d1507cfe7b6e4ceabc40303 Mon Sep 17 00:00:00 2001 From: Heath Stewart Date: Thu, 15 Apr 2021 14:16:40 -0700 Subject: [PATCH] Update Key Vault, Search samples using SNIPPET def (#20439) * Update Key Vault, Search samples using SNIPPET def Resolves Key Vault's and Search's portions of #20431 * Resolve PR feedback --- .../samples/Sample3_BackRestoreResume.md | 2 +- .../samples/AccessControlSampleSnippets.cs | 15 ++-- .../tests/samples/Sample1_BackupHelloWorld.cs | 14 +++- .../tests/samples/Sample1_RbacHelloWorld.cs | 26 +++--- .../samples/Sample2_RbacScopeAssignment.cs | 22 +++-- .../tests/samples/Sample2_SelectiveRestore.cs | 4 +- .../samples/Sample3_BackRestoreResume.cs | 16 +++- .../MigrationGuide.md | 1 - .../tests/CertificateClientLiveTests.cs | 4 +- .../samples/Sample3_ImportCertificate.cs | 14 +++- .../samples/Sample3_ImportCertificateAsync.cs | 14 +++- .../tests/samples/SampleSnippets.cs | 14 +++- .../tests/samples/Sample3_GetKeys.cs | 4 +- .../tests/samples/Sample3_GetKeysAsync.cs | 4 +- .../tests/samples/SampleSnippets.cs | 14 +++- .../tests/samples/Sample3_GetSecrets.cs | 4 +- .../tests/samples/Sample3_GetSecretsAsync.cs | 4 +- .../tests/samples/SampleSnippets.cs | 7 +- .../tests/SampleSnippets.cs | 39 +++++---- .../samples/Sample02_Service.md | 2 +- .../tests/Samples/Readme.cs | 79 ++++++++++++------ .../tests/Samples/Sample01_HelloWorld.cs | 81 ++++++++++++++----- .../Samples/Sample04_FieldBuilderIgnore.cs | 11 ++- .../Samples/Sample05_IndexingDocuments.cs | 15 +++- .../tests/Samples/Sample06_EncryptedIndex.cs | 32 +++++--- 25 files changed, 310 insertions(+), 132 deletions(-) diff --git a/sdk/keyvault/Azure.Security.KeyVault.Administration/samples/Sample3_BackRestoreResume.md b/sdk/keyvault/Azure.Security.KeyVault.Administration/samples/Sample3_BackRestoreResume.md index 5892f875103c5..8b0edcb1fa85f 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Administration/samples/Sample3_BackRestoreResume.md +++ b/sdk/keyvault/Azure.Security.KeyVault.Administration/samples/Sample3_BackRestoreResume.md @@ -12,7 +12,7 @@ and check for status or wait for completion. ```C# Snippet:ResumeBackupAsync // Construct a new KeyVaultBackupClient or use an existing one. -KeyVaultBackupClient Client = new KeyVaultBackupClient(new Uri(keyVaultUrl), new DefaultAzureCredential()); +KeyVaultBackupClient client = new KeyVaultBackupClient(new Uri(keyVaultUrl), new DefaultAzureCredential()); // Construct a BackupOperation using a KeyVaultBackupClient and the Id from a previously started operation. BackupOperation backupOperation = new BackupOperation(client, backupOperationId); diff --git a/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/AccessControlSampleSnippets.cs b/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/AccessControlSampleSnippets.cs index eba349f949d13..07dd743a1ffe5 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/AccessControlSampleSnippets.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/AccessControlSampleSnippets.cs @@ -38,7 +38,9 @@ public void CreateClient() // Create a new access control client using the default credential from Azure.Identity using environment variables previously set, // including AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, and AZURE_TENANT_ID. KeyVaultAccessControlClient client = new KeyVaultAccessControlClient(vaultUri: new Uri(keyVaultUrl), credential: new DefaultAzureCredential()); - /*@@*/ client = Client; +#if !SNIPPET + client = Client; +#endif // Retrieve all the role definitions. List roleDefinitions = client.GetRoleDefinitions(KeyVaultRoleScope.Global).ToList(); @@ -81,14 +83,17 @@ public void CreateRoleAssignment() string servicePrincipalObjectId = _objectId; #region Snippet:ReadmeCreateRoleAssignment +#if SNIPPET // Replace with a role definition Id from the definitions returned from the List the role definitions section above - //@@string definitionIdToAssign = ""; + string definitionIdToAssign = ""; // Replace with the service principal object id from the Create/Get credentials section above - //@@string servicePrincipalObjectId = ""; + string servicePrincipalObjectId = ""; - //@@RoleAssignment createdAssignment = client.CreateRoleAssignment(RoleAssignmentScope.Global, properties); - /*@@*/ KeyVaultRoleAssignment createdAssignment = client.CreateRoleAssignment(KeyVaultRoleScope.Global, definitionIdToAssign, servicePrincipalObjectId, _roleAssignmentId); + RoleAssignment createdAssignment = client.CreateRoleAssignment(RoleAssignmentScope.Global, properties); +#else + KeyVaultRoleAssignment createdAssignment = client.CreateRoleAssignment(KeyVaultRoleScope.Global, definitionIdToAssign, servicePrincipalObjectId, _roleAssignmentId); +#endif Console.WriteLine(createdAssignment.Name); Console.WriteLine(createdAssignment.Properties.PrincipalId); diff --git a/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample1_BackupHelloWorld.cs b/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample1_BackupHelloWorld.cs index 03e80af99b647..0efcd04905415 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample1_BackupHelloWorld.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample1_BackupHelloWorld.cs @@ -92,8 +92,11 @@ public async Task BackupAndRestoreSampleSync() while (!backupOperation.HasCompleted) { backupOperation.UpdateStatus(); - /*@@*/ await DelayAsync(TimeSpan.FromSeconds(3)); - //@@Thread.Sleep(3000); +#if SNIPPET + Thread.Sleep(3000); +#else + await DelayAsync(TimeSpan.FromSeconds(3)); +#endif } // Get the Uri for the location of you backup blob. @@ -113,8 +116,11 @@ public async Task BackupAndRestoreSampleSync() while (!restoreOperation.HasCompleted) { restoreOperation.UpdateStatus(); - /*@@*/ await DelayAsync(TimeSpan.FromSeconds(3)); - //@@Thread.Sleep(3000); +#if SNIPPET + Thread.Sleep(3000); +#else + await DelayAsync(TimeSpan.FromSeconds(3)); +#endif } Uri restoreResult = backupOperation.Value.FolderUri; #endregion diff --git a/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample1_RbacHelloWorld.cs b/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample1_RbacHelloWorld.cs index fc76a8afc921c..0e39e4d31c6ca 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample1_RbacHelloWorld.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample1_RbacHelloWorld.cs @@ -103,11 +103,14 @@ public void CreateRoleAssignment() string servicePrincipalObjectId = _objectId; #region Snippet:CreateRoleAssignment - //@@string definitionIdToAssign = ""; - //@@string servicePrincipalObjectId = ""; - - //@@KeyVaultRoleAssignment createdAssignment = client.CreateRoleAssignment(RoleAssignmentScope.Global, definitionIdToAssign, servicePrincipalObjectI); - /*@@*/KeyVaultRoleAssignment createdAssignment = client.CreateRoleAssignment(KeyVaultRoleScope.Global, definitionIdToAssign, servicePrincipalObjectId, _roleAssignmentId); +#if SNIPPET + string definitionIdToAssign = ""; + string servicePrincipalObjectId = ""; + + KeyVaultRoleAssignment createdAssignment = client.CreateRoleAssignment(RoleAssignmentScope.Global, definitionIdToAssign, servicePrincipalObjectI); +#else + KeyVaultRoleAssignment createdAssignment = client.CreateRoleAssignment(KeyVaultRoleScope.Global, definitionIdToAssign, servicePrincipalObjectId, _roleAssignmentId); +#endif #endregion #region Snippet:GetRoleAssignment @@ -136,11 +139,14 @@ public async Task CreateRoleAssignmentAsync() string servicePrincipalObjectId = _objectId; #region Snippet:CreateRoleAssignmentAsync - //@@string definitionIdToAssign = ""; - //@@string servicePrincipalObjectId = ""; - - //@@KeyVaultRoleAssignment createdAssignment = await client.CreateRoleAssignmentAsync(RoleAssignmentScope.Global, definitionIdToAssign, servicePrincipalObjectId); - /*@@*/KeyVaultRoleAssignment createdAssignment = await client.CreateRoleAssignmentAsync(KeyVaultRoleScope.Global, definitionIdToAssign, servicePrincipalObjectId, _roleAssignmentId).ConfigureAwait(false); +#if SNIPPET + string definitionIdToAssign = ""; + string servicePrincipalObjectId = ""; + + KeyVaultRoleAssignment createdAssignment = await client.CreateRoleAssignmentAsync(RoleAssignmentScope.Global, definitionIdToAssign, servicePrincipalObjectId); +#else + KeyVaultRoleAssignment createdAssignment = await client.CreateRoleAssignmentAsync(KeyVaultRoleScope.Global, definitionIdToAssign, servicePrincipalObjectId, _roleAssignmentId).ConfigureAwait(false); +#endif #endregion #region Snippet:GetRoleAssignmentAsync diff --git a/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample2_RbacScopeAssignment.cs b/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample2_RbacScopeAssignment.cs index 744ccfcf109da..dde6763d0022e 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample2_RbacScopeAssignment.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample2_RbacScopeAssignment.cs @@ -43,11 +43,14 @@ public async Task CreateRoleAssignmentAsync() string servicePrincipalObjectId = _objectId; #region Snippet:CreateRoleAssignmentKeysScope - //@@string definitionIdToAssign = ""; - //@@string servicePrincipalObjectId = ""; +#if SNIPPET + string definitionIdToAssign = ""; + string servicePrincipalObjectId = ""; - //@@RoleAssignment keysScopedAssignment = await client.CreateRoleAssignmentAsync(RoleAssignmentScope.Global, definitionIdToAssign, servicePrincipalObjectId); - /*@@*/KeyVaultRoleAssignment keysScopedAssignment = await client.CreateRoleAssignmentAsync(KeyVaultRoleScope.Keys, definitionIdToAssign, servicePrincipalObjectId , _roleAssignmentId).ConfigureAwait(false); + RoleAssignment keysScopedAssignment = await client.CreateRoleAssignmentAsync(RoleAssignmentScope.Global, definitionIdToAssign, servicePrincipalObjectId); +#else + KeyVaultRoleAssignment keysScopedAssignment = await client.CreateRoleAssignmentAsync(KeyVaultRoleScope.Keys, definitionIdToAssign, servicePrincipalObjectId , _roleAssignmentId).ConfigureAwait(false); +#endif #endregion RegisterForCleanup(keysScopedAssignment); @@ -60,11 +63,16 @@ public async Task CreateRoleAssignmentAsync() RegisterKeyForCleanup(keyName); #region Snippet:CreateRoleAssignmentKeyScope - //@@string keyName = ""; +#if SNIPPET + string keyName = ""; +#endif KeyVaultKey key = await keyClient.GetKeyAsync(keyName); - //@@RoleAssignment keyScopedAssignment = await client.CreateRoleAssignmentAsync(new RoleAssignmentScope(key.Id), definitionIdToAssign, servicePrincipalObjectId); - /*@@*/KeyVaultRoleAssignment keyScopedAssignment = await client.CreateRoleAssignmentAsync(new KeyVaultRoleScope(key.Id), definitionIdToAssign, servicePrincipalObjectId, _roleAssignmentId).ConfigureAwait(false); +#if SNIPPET + RoleAssignment keyScopedAssignment = await client.CreateRoleAssignmentAsync(new RoleAssignmentScope(key.Id), definitionIdToAssign, servicePrincipalObjectId); +#else + KeyVaultRoleAssignment keyScopedAssignment = await client.CreateRoleAssignmentAsync(new KeyVaultRoleScope(key.Id), definitionIdToAssign, servicePrincipalObjectId, _roleAssignmentId).ConfigureAwait(false); +#endif #endregion RegisterForCleanup(keyScopedAssignment); diff --git a/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample2_SelectiveRestore.cs b/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample2_SelectiveRestore.cs index 1c914e617295c..d44941b08f5cb 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample2_SelectiveRestore.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample2_SelectiveRestore.cs @@ -50,7 +50,9 @@ public async Task BackupAndRestoreSampleAsync() Assert.That(backupOperation.HasValue, Is.True); #region Snippet:SelectiveRestoreAsync - //@@ string keyName = ""; +#if SNIPPET + string keyName = ""; +#endif // Start the restore for a specific key that was previously backed up using the backupBlobUri returned from a previous BackupOperation. SelectiveKeyRestoreOperation restoreOperation = await Client.StartSelectiveRestoreAsync(keyName, folderUri, sasToken); diff --git a/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample3_BackRestoreResume.cs b/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample3_BackRestoreResume.cs index 7c1790c6437dc..daa4d1948a0f6 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample3_BackRestoreResume.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Administration/tests/samples/Sample3_BackRestoreResume.cs @@ -32,12 +32,16 @@ public async Task ResumeBackupRestore() var backupOperationId = originalBackupOperation.Id; #region Snippet:ResumeBackupAsync +#if SNIPPET // Construct a new KeyVaultBackupClient or use an existing one. - //@@KeyVaultBackupClient Client = new KeyVaultBackupClient(new Uri(keyVaultUrl), new DefaultAzureCredential()); + KeyVaultBackupClient client = new KeyVaultBackupClient(new Uri(keyVaultUrl), new DefaultAzureCredential()); +#endif // Construct a BackupOperation using a KeyVaultBackupClient and the Id from a previously started operation. BackupOperation backupOperation = new BackupOperation(client, backupOperationId); - /*@@*/backupOperation._retryAfterSeconds = (int)PollingInterval.TotalSeconds; +#if !SNIPPET + backupOperation._retryAfterSeconds = (int)PollingInterval.TotalSeconds; +#endif // Wait for completion of the BackupOperation. Response backupResult = await backupOperation.WaitForCompletionAsync(); @@ -56,12 +60,16 @@ public async Task ResumeBackupRestore() var restoreOperationId = originalRestoreOperation.Id; #region Snippet:ResumeRestoreAsync +#if SNIPPET // Construct a new KeyVaultBackupClient or use an existing one. - //@@KeyVaultBackupClient Client = new KeyVaultBackupClient(new Uri(keyVaultUrl), new DefaultAzureCredential()); + KeyVaultBackupClient Client = new KeyVaultBackupClient(new Uri(keyVaultUrl), new DefaultAzureCredential()); +#endif // Construct a RestoreOperation using a KeyVaultBackupClient and the Id from a previously started operation. RestoreOperation restoreOperation = new RestoreOperation(client, restoreOperationId); - /*@@*/restoreOperation._operationInternal._retryAfterSeconds = (int)PollingInterval.TotalSeconds; +#if !SNIPPET + restoreOperation._operationInternal._retryAfterSeconds = (int)PollingInterval.TotalSeconds; +#endif // Wait for completion of the RestoreOperation. RestoreResult restoreResult = await restoreOperation.WaitForCompletionAsync(); diff --git a/sdk/keyvault/Azure.Security.KeyVault.Certificates/MigrationGuide.md b/sdk/keyvault/Azure.Security.KeyVault.Certificates/MigrationGuide.md index 4b6f7b74b7148..95e379f4d37a4 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Certificates/MigrationGuide.md +++ b/sdk/keyvault/Azure.Security.KeyVault.Certificates/MigrationGuide.md @@ -181,7 +181,6 @@ Previously in `Microsoft.Azure.KeyVault`, you could create a self-signed policy ```C# Snippet:Microsoft_Azure_KeyVault_Certificates_Snippets_MigrationGuide_CreateSelfSignedPolicy CertificatePolicy policy = new CertificatePolicy -policy = new CertificatePolicy { IssuerParameters = new IssuerParameters("Self"), X509CertificateProperties = new X509CertificateProperties("CN=DefaultPolicy") diff --git a/sdk/keyvault/Azure.Security.KeyVault.Certificates/tests/CertificateClientLiveTests.cs b/sdk/keyvault/Azure.Security.KeyVault.Certificates/tests/CertificateClientLiveTests.cs index 485dae79e9e59..182f3aef86b96 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Certificates/tests/CertificateClientLiveTests.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Certificates/tests/CertificateClientLiveTests.cs @@ -447,7 +447,9 @@ public async Task VerifyImportCertificatePemWithoutIssuer() byte[] certificateBytes = Encoding.ASCII.GetBytes(PemCertificateWithV3Extensions); #region Snippet:CertificateClientLiveTests_VerifyImportCertificatePem - //@@byte[] certificateBytes = File.ReadAllBytes("certificate.pem"); +#if SNIPPET + byte[] certificateBytes = File.ReadAllBytes("certificate.pem"); +#endif ImportCertificateOptions options = new ImportCertificateOptions(certificateName, certificateBytes) { diff --git a/sdk/keyvault/Azure.Security.KeyVault.Certificates/tests/samples/Sample3_ImportCertificate.cs b/sdk/keyvault/Azure.Security.KeyVault.Certificates/tests/samples/Sample3_ImportCertificate.cs index a5a1d44a74350..41fecccc50066 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Certificates/tests/samples/Sample3_ImportCertificate.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Certificates/tests/samples/Sample3_ImportCertificate.cs @@ -23,8 +23,11 @@ public void ImportPfxCertificateSync() #region Snippet:CertificatesSample3ImportPfxCertificate string name = $"cert-{Guid.NewGuid()}"; - //@@ byte[] pfx = File.ReadAllBytes("certificate.pfx"); - /*@@*/ byte[] pfx = Convert.FromBase64String(s_pfxBase64); +#if SNIPPET + byte[] pfx = File.ReadAllBytes("certificate.pfx"); +#else + byte[] pfx = Convert.FromBase64String(s_pfxBase64); +#endif ImportCertificateOptions importOptions = new ImportCertificateOptions(name, pfx) { Policy = new CertificatePolicy(WellKnownIssuerNames.Self, "CN=contoso.com") @@ -64,8 +67,11 @@ public void ImportPemCertificateSync() #region Snippet:CertificatesSample3ImportPemCertificate string name = $"cert-{Guid.NewGuid()}"; - //@@ byte[] pem = File.ReadAllBytes("certificate.cer"); - /*@@*/ byte[] pem = Encoding.ASCII.GetBytes(s_pem); +#if SNIPPET + byte[] pem = File.ReadAllBytes("certificate.cer"); +#else + byte[] pem = Encoding.ASCII.GetBytes(s_pem); +#endif ImportCertificateOptions importOptions = new ImportCertificateOptions(name, pem) { Policy = new CertificatePolicy(WellKnownIssuerNames.Self, "CN=contoso.com") diff --git a/sdk/keyvault/Azure.Security.KeyVault.Certificates/tests/samples/Sample3_ImportCertificateAsync.cs b/sdk/keyvault/Azure.Security.KeyVault.Certificates/tests/samples/Sample3_ImportCertificateAsync.cs index f970e152dbbe1..fd2ffcce99ba3 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Certificates/tests/samples/Sample3_ImportCertificateAsync.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Certificates/tests/samples/Sample3_ImportCertificateAsync.cs @@ -20,8 +20,11 @@ public async Task ImportPfxCertificateAsync() CertificateClient client = new CertificateClient(new Uri(keyVaultUrl), new DefaultAzureCredential()); string name = $"cert-{Guid.NewGuid()}"; - //@@ byte[] pfx = File.ReadAllBytes("certificate.pfx"); - /*@@*/ byte[] pfx = Convert.FromBase64String(s_pfxBase64); +#if SNIPPET + byte[] pfx = File.ReadAllBytes("certificate.pfx"); +#else + byte[] pfx = Convert.FromBase64String(s_pfxBase64); +#endif ImportCertificateOptions importOptions = new ImportCertificateOptions(name, pfx) { Policy = new CertificatePolicy(WellKnownIssuerNames.Self, "CN=contoso.com") @@ -54,8 +57,11 @@ public async Task ImportPemCertificateAsync() CertificateClient client = new CertificateClient(new Uri(keyVaultUrl), new DefaultAzureCredential()); string name = $"cert-{Guid.NewGuid()}"; - //@@ byte[] pem = File.ReadAllBytes("certificate.cer"); - /*@@*/ byte[] pem = Encoding.ASCII.GetBytes(s_pem); +#if SNIPPET + byte[] pem = File.ReadAllBytes("certificate.cer"); +#else + byte[] pem = Encoding.ASCII.GetBytes(s_pem); +#endif ImportCertificateOptions importOptions = new ImportCertificateOptions(name, pem) { Policy = new CertificatePolicy(WellKnownIssuerNames.Self, "CN=contoso.com") diff --git a/sdk/keyvault/Azure.Security.KeyVault.Certificates/tests/samples/SampleSnippets.cs b/sdk/keyvault/Azure.Security.KeyVault.Certificates/tests/samples/SampleSnippets.cs index 140322fbf7ab7..4eaeca0c063bf 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Certificates/tests/samples/SampleSnippets.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Certificates/tests/samples/SampleSnippets.cs @@ -186,8 +186,11 @@ private async Task MigrationGuide() Transport = new HttpClientTransport(httpClient) }; - //@@CertificateClient client = new CertificateClient( - /*@@*/ CertificateClient _ = new CertificateClient( +#if SNIPPET + CertificateClient client = new CertificateClient( +#else + CertificateClient _ = new CertificateClient( +#endif new Uri("https://myvault.vault.azure.net"), new DefaultAzureCredential(), options); @@ -221,8 +224,11 @@ private async Task MigrationGuide() #endregion Snippet:Azure_Security_KeyVault_Certificates_Snippets_MigrationGuide_CreateSelfSignedPolicy #region Snippet:Azure_Security_KeyVault_Certificates_Snippets_MigrationGuide_CreateSelfSignedPolicy - //@@CertificatePolicy policy = CertificatePolicy.Default; - /*@@*/ policy = CertificatePolicy.Default; +#if SNIPPET + CertificatePolicy policy = CertificatePolicy.Default; +#else + policy = CertificatePolicy.Default; +#endif #endregion Snippet:Azure_Security_KeyVault_Certificates_Snippets_MigrationGuide_CreateSelfSignedPolicy { #region Snippet:Azure_Security_KeyVault_Certificates_Snippets_MigrationGuide_CreateCertificate diff --git a/sdk/keyvault/Azure.Security.KeyVault.Keys/tests/samples/Sample3_GetKeys.cs b/sdk/keyvault/Azure.Security.KeyVault.Keys/tests/samples/Sample3_GetKeys.cs index 1d85877893a88..4f0d9736a8f03 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Keys/tests/samples/Sample3_GetKeys.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Keys/tests/samples/Sample3_GetKeys.cs @@ -51,7 +51,9 @@ public void GetKeysSync() IEnumerable keys = client.GetPropertiesOfKeys(); foreach (KeyProperties key in keys) { - /*@@*/ if (key.Managed) continue; +#if !SNIPPET + if (key.Managed) continue; +#endif KeyVaultKey keyWithType = client.GetKey(key.Name); Debug.WriteLine($"Key is returned with name {keyWithType.Name} and type {keyWithType.KeyType}"); } diff --git a/sdk/keyvault/Azure.Security.KeyVault.Keys/tests/samples/Sample3_GetKeysAsync.cs b/sdk/keyvault/Azure.Security.KeyVault.Keys/tests/samples/Sample3_GetKeysAsync.cs index b3422abc712a8..98d0a2019e693 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Keys/tests/samples/Sample3_GetKeysAsync.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Keys/tests/samples/Sample3_GetKeysAsync.cs @@ -53,7 +53,9 @@ public async Task GetKeysAsync() // So, for each returned key we call GetKey to get the actual key. await foreach (KeyProperties key in client.GetPropertiesOfKeysAsync()) { - /*@@*/ if (key.Managed) continue; +#if !SNIPPET + if (key.Managed) continue; +#endif KeyVaultKey keyWithType = await client.GetKeyAsync(key.Name); Debug.WriteLine($"Key is returned with name {keyWithType.Name} and type {keyWithType.KeyType}"); } diff --git a/sdk/keyvault/Azure.Security.KeyVault.Keys/tests/samples/SampleSnippets.cs b/sdk/keyvault/Azure.Security.KeyVault.Keys/tests/samples/SampleSnippets.cs index 300c7c74e82d8..cf0b2d766493c 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Keys/tests/samples/SampleSnippets.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Keys/tests/samples/SampleSnippets.cs @@ -284,8 +284,11 @@ private async Task MigrationGuide() Transport = new HttpClientTransport(httpClient) }; - //@@KeyClient client = new KeyClient( - /*@@*/ client = new KeyClient( +#if SNIPPET + KeyClient client = new KeyClient( +#else + client = new KeyClient( +#endif new Uri("https://myvault.vault.azure.net"), new DefaultAzureCredential(), options); @@ -295,8 +298,11 @@ private async Task MigrationGuide() Transport = new HttpClientTransport(httpClient) }; - //@@CryptographyClient cryptoClient = new CryptographyClient( - /*@@*/ cryptoClient = new CryptographyClient( +#if SNIPPET + CryptographyClient cryptoClient = new CryptographyClient( +#else + cryptoClient = new CryptographyClient( +#endif new Uri("https://myvault.vault.azure.net"), new DefaultAzureCredential(), cryptoOptions); diff --git a/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/Sample3_GetSecrets.cs b/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/Sample3_GetSecrets.cs index d36120ae1ea52..c3e33f6ea5da1 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/Sample3_GetSecrets.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/Sample3_GetSecrets.cs @@ -48,7 +48,9 @@ public void GetSecretsSync() IEnumerable secrets = client.GetPropertiesOfSecrets(); foreach (SecretProperties secret in secrets) { - /*@@*/ if (secret.Managed) continue; +#if !SNIPPET + if (secret.Managed) continue; +#endif // Getting a disabled secret will fail, so skip disabled secrets. if (!secret.Enabled.GetValueOrDefault()) { diff --git a/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/Sample3_GetSecretsAsync.cs b/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/Sample3_GetSecretsAsync.cs index a03e3ad6fc9c2..9d309bf82f1a5 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/Sample3_GetSecretsAsync.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/Sample3_GetSecretsAsync.cs @@ -42,7 +42,9 @@ public async Task GetSecretsAsync() await foreach (SecretProperties secret in client.GetPropertiesOfSecretsAsync()) { - /*@@*/ if (secret.Managed) continue; +#if !SNIPPET + if (secret.Managed) continue; +#endif // Getting a disabled secret will fail, so skip disabled secrets. if (!secret.Enabled.GetValueOrDefault()) { diff --git a/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/SampleSnippets.cs b/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/SampleSnippets.cs index ccb5cdd5114fd..6e9f7031d1444 100644 --- a/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/SampleSnippets.cs +++ b/sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/samples/SampleSnippets.cs @@ -206,8 +206,11 @@ private async Task MigrationGuide() Transport = new HttpClientTransport(httpClient) }; - //@@SecretClient client = new SecretClient( - /*@@*/ SecretClient _ = new SecretClient( +#if SNIPPET + SecretClient client = new SecretClient( +#else + SecretClient _ = new SecretClient( +#endif new Uri("https://myvault.vault.azure.net"), new DefaultAzureCredential(), options); diff --git a/sdk/keyvault/Microsoft.Azure.KeyVault/tests/SampleSnippets.cs b/sdk/keyvault/Microsoft.Azure.KeyVault/tests/SampleSnippets.cs index 2990cb82280c2..bc66efbaaef9c 100644 --- a/sdk/keyvault/Microsoft.Azure.KeyVault/tests/SampleSnippets.cs +++ b/sdk/keyvault/Microsoft.Azure.KeyVault/tests/SampleSnippets.cs @@ -32,10 +32,13 @@ private async Task CertificatesMigrationGuide() #region Snippet:Microsoft_Azure_KeyVault_Certificates_Snippets_MigrationGuide_CreateWithOptions using (HttpClient httpClient = new HttpClient()) { - //@@AzureServiceTokenProvider provider = new AzureServiceTokenProvider(); - /*@@*/ provider = new AzureServiceTokenProvider(); - //@@KeyVaultClient client = new KeyVaultClient( - /*@@*/ client = new KeyVaultClient( +#if SNIPPET + AzureServiceTokenProvider provider = new AzureServiceTokenProvider(); + KeyVaultClient client = new KeyVaultClient( +#else + provider = new AzureServiceTokenProvider(); + client = new KeyVaultClient( +#endif new KeyVaultClient.AuthenticationCallback(provider.KeyVaultTokenCallback), httpClient); } @@ -147,9 +150,11 @@ private async Task CertificatesMigrationGuide() } #region Snippet:Microsoft_Azure_KeyVault_Certificates_Snippets_MigrationGuide_CreateSelfSignedPolicy - //@@CertificatePolicy policy = new CertificatePolicy - /*@@*/ +#if SNIPPET + CertificatePolicy policy = new CertificatePolicy +#else policy = new CertificatePolicy +#endif { IssuerParameters = new IssuerParameters("Self"), X509CertificateProperties = new X509CertificateProperties("CN=DefaultPolicy") @@ -224,10 +229,13 @@ private async Task KeysMigrationGuide() #region Snippet:Microsoft_Azure_KeyVault_Keys_Snippets_MigrationGuide_CreateWithOptions using (HttpClient httpClient = new HttpClient()) { - //@@AzureServiceTokenProvider provider = new AzureServiceTokenProvider(); - /*@@*/ provider = new AzureServiceTokenProvider(); - //@@KeyVaultClient client = new KeyVaultClient( - /*@@*/ client = new KeyVaultClient( +#if SNIPPET + AzureServiceTokenProvider provider = new AzureServiceTokenProvider(); + KeyVaultClient client = new KeyVaultClient( +#else + provider = new AzureServiceTokenProvider(); + client = new KeyVaultClient( +#endif new KeyVaultClient.AuthenticationCallback(provider.KeyVaultTokenCallback), httpClient); } @@ -363,10 +371,13 @@ private async Task SecretsMigrationGuide() #region Snippet:Microsoft_Azure_KeyVault_Secrets_Snippets_MigrationGuide_CreateWithOptions using (HttpClient httpClient = new HttpClient()) { - //@@AzureServiceTokenProvider provider = new AzureServiceTokenProvider(); - /*@@*/ provider = new AzureServiceTokenProvider(); - //@@KeyVaultClient client = new KeyVaultClient( - /*@@*/ client = new KeyVaultClient( +#if SNIPPET + AzureServiceTokenProvider provider = new AzureServiceTokenProvider(); + KeyVaultClient client = new KeyVaultClient( +#else + provider = new AzureServiceTokenProvider(); + client = new KeyVaultClient( +#endif new KeyVaultClient.AuthenticationCallback(provider.KeyVaultTokenCallback), httpClient); } diff --git a/sdk/search/Azure.Search.Documents/samples/Sample02_Service.md b/sdk/search/Azure.Search.Documents/samples/Sample02_Service.md index 267eaebec0ffd..f0cf6411aac1b 100644 --- a/sdk/search/Azure.Search.Documents/samples/Sample02_Service.md +++ b/sdk/search/Azure.Search.Documents/samples/Sample02_Service.md @@ -66,7 +66,7 @@ string synonymMapPath = "countries.txt"; SynonymMap synonyms; using (StreamReader file = File.OpenText(synonymMapPath)) { -synonyms = new SynonymMap(synonymMapName, file); + synonyms = new SynonymMap(synonymMapName, file); } await indexClient.CreateSynonymMapAsync(synonyms); diff --git a/sdk/search/Azure.Search.Documents/tests/Samples/Readme.cs b/sdk/search/Azure.Search.Documents/tests/Samples/Readme.cs index a2aa5ca885b32..279285c12cb6d 100644 --- a/sdk/search/Azure.Search.Documents/tests/Samples/Readme.cs +++ b/sdk/search/Azure.Search.Documents/tests/Samples/Readme.cs @@ -63,12 +63,16 @@ public async Task CreateAndQuery() // Get the service endpoint and API key from the environment Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT")); string key = Environment.GetEnvironmentVariable("SEARCH_API_KEY"); - //@@ string indexName = "hotels"; +#if SNIPPET + string indexName = "hotels"; +#endif // Create a client AzureKeyCredential credential = new AzureKeyCredential(key); SearchClient client = new SearchClient(endpoint, indexName, credential); - /*@@*/ client = InstrumentClient(new SearchClient(endpoint, indexName, credential, GetSearchClientOptions())); +#if !SNIPPET + client = InstrumentClient(new SearchClient(endpoint, indexName, credential, GetSearchClientOptions())); +#endif #endregion Snippet:Azure_Search_Tests_Samples_Readme_Client #region Snippet:Azure_Search_Tests_Samples_Readme_Dict @@ -76,17 +80,23 @@ public async Task CreateAndQuery() foreach (SearchResult result in response.GetResults()) { SearchDocument doc = result.Document; - //@@ string id = (string)doc["HotelId"]; - /*@@*/ string id = (string)doc["hotelId"]; - //@@ string name = (string)doc["HotelName"]; - /*@@*/ string name = (string)doc["hotelName"]; +#if SNIPPET + string id = (string)doc["HotelId"]; + string name = (string)doc["HotelName"]; +#else + string id = (string)doc["hotelId"]; + string name = (string)doc["hotelName"]; +#endif Console.WriteLine("{id}: {name}"); } #endregion Snippet:Azure_Search_Tests_Samples_Readme_Dict #region Snippet:Azure_Search_Tests_Samples_Readme_Dynamic - //@@ SearchResults response = client.Search("luxury"); - /*@@*/ response = client.Search("luxury"); +#if SNIPPET + SearchResults response = client.Search("luxury"); +#else + response = client.Search("luxury"); +#endif foreach (SearchResult result in response.GetResults()) { dynamic doc = result.Document; @@ -100,13 +110,19 @@ public async Task CreateAndQuery() #region Snippet:Azure_Search_Tests_Samples_Readme_StaticType public class Hotel { - //@@ [JsonPropertyName("HotelId")] - /*@@*/ [JsonPropertyName("hotelId")] +#if SNIPPET + [JsonPropertyName("HotelId")] +#else + [JsonPropertyName("hotelId")] +#endif [SimpleField(IsKey = true, IsFilterable = true, IsSortable = true)] public string Id { get; set; } - //@@ [JsonPropertyName("HotelName")] - /*@@*/ [JsonPropertyName("hotelName")] +#if SNIPPET + [JsonPropertyName("HotelName")] +#else + [JsonPropertyName("hotelName")] +#endif [SearchableField(IsFilterable = true, IsSortable = true)] public string Name { get; set; } } @@ -129,8 +145,11 @@ public async Task QueryStatic() #endregion Snippet:Azure_Search_Tests_Samples_Readme_StaticQuery #region Snippet:Azure_Search_Tests_Samples_Readme_StaticQueryAsync - //@@SearchResults response = await client.SearchAsync("luxury"); - /*@@*/ response = await client.SearchAsync("luxury"); +#if SNIPPET + SearchResults response = await client.SearchAsync("luxury"); +#else + response = await client.SearchAsync("luxury"); +#endif await foreach (SearchResult result in response.GetResultsAsync()) { Hotel doc = result.Document; @@ -151,11 +170,17 @@ public async Task Options() SearchOptions options = new SearchOptions { // Filter to only Rating greater than or equal our preference - //@@ Filter = SearchFilter.Create($"Rating ge {stars}"), - /*@@*/ Filter = SearchFilter.Create($"rating ge {stars}"), +#if SNIPPET + Filter = SearchFilter.Create($"Rating ge {stars}"), +#else + Filter = SearchFilter.Create($"rating ge {stars}"), +#endif Size = 5, // Take only 5 results - //@@ OrderBy = { "Rating desc" } // Sort by Rating from high to low - /*@@*/ OrderBy = { "rating desc" } // Sort by rating from high to low +#if SNIPPET + OrderBy = { "Rating desc" } // Sort by Rating from high to low +#else + OrderBy = { "rating desc" } // Sort by rating from high to low +#endif }; SearchResults response = client.Search("luxury", options); // ... @@ -177,12 +202,17 @@ public async Task CreateIndex() // Create a service client AzureKeyCredential credential = new AzureKeyCredential(key); SearchIndexClient client = new SearchIndexClient(endpoint, credential); - /*@@*/ client = resources.GetIndexClient(); +#if !SNIPPET + client = resources.GetIndexClient(); +#endif // Create the index using FieldBuilder. #region Snippet:Azure_Search_Tests_Samples_Readme_CreateIndex_New_SearchIndex - //@@SearchIndex index = new SearchIndex("hotels") - /*@@*/ SearchIndex index = new SearchIndex(Recording.Random.GetName()) +#if SNIPPET + SearchIndex index = new SearchIndex("hotels") +#else + SearchIndex index = new SearchIndex(Recording.Random.GetName()) +#endif { Fields = new FieldBuilder().Build(typeof(Hotel)), Suggesters = @@ -209,8 +239,11 @@ public async Task CreateManualIndex() #region Snippet:Azure_Search_Tests_Samples_Readme_CreateManualIndex // Create the index using field definitions. #region Snippet:Azure_Search_Tests_Samples_Readme_CreateManualIndex_New_SearchIndex - //@@SearchIndex index = new SearchIndex("hotels") - /*@@*/ SearchIndex index = new SearchIndex(Recording.Random.GetName()) +#if SNIPPET + SearchIndex index = new SearchIndex("hotels") +#else + SearchIndex index = new SearchIndex(Recording.Random.GetName()) +#endif { Fields = { diff --git a/sdk/search/Azure.Search.Documents/tests/Samples/Sample01_HelloWorld.cs b/sdk/search/Azure.Search.Documents/tests/Samples/Sample01_HelloWorld.cs index b52a987ac3855..664be67f3df6a 100644 --- a/sdk/search/Azure.Search.Documents/tests/Samples/Sample01_HelloWorld.cs +++ b/sdk/search/Azure.Search.Documents/tests/Samples/Sample01_HelloWorld.cs @@ -39,7 +39,9 @@ public async Task CreateClient() // Create a new SearchIndexClient SearchIndexClient indexClient = new SearchIndexClient(endpoint, credential); - /*@@*/ indexClient = InstrumentClient(new SearchIndexClient(endpoint, credential, GetSearchClientOptions())); +#if !SNIPPET + indexClient = InstrumentClient(new SearchIndexClient(endpoint, credential, GetSearchClientOptions())); +#endif // Perform an operation Response stats = indexClient.GetServiceStatistics(); @@ -65,7 +67,9 @@ public async Task CreateClientAsync() // Create a new SearchIndexClient SearchIndexClient indexClient = new SearchIndexClient(endpoint, credential); - /*@@*/ indexClient = InstrumentClient(new SearchIndexClient(endpoint, credential, GetSearchClientOptions())); +#if !SNIPPET + indexClient = InstrumentClient(new SearchIndexClient(endpoint, credential, GetSearchClientOptions())); +#endif // Perform an operation Response stats = await indexClient.GetServiceStatisticsAsync(); @@ -91,7 +95,9 @@ public async Task HandleErrors() // Create an invalid SearchClient string fakeIndexName = "doesnotexist"; SearchClient searchClient = new SearchClient(endpoint, fakeIndexName, credential); - /*@@*/ searchClient = InstrumentClient(new SearchClient(endpoint, fakeIndexName, credential, GetSearchClientOptions())); +#if !SNIPPET + searchClient = InstrumentClient(new SearchClient(endpoint, fakeIndexName, credential, GetSearchClientOptions())); +#endif try { searchClient.GetDocumentCount(); @@ -119,7 +125,9 @@ public async Task HandleErrorsAsync() // Create an invalid SearchClient string fakeIndexName = "doesnotexist"; SearchClient searchClient = new SearchClient(endpoint, fakeIndexName, credential); - /*@@*/ searchClient = InstrumentClient(new SearchClient(endpoint, fakeIndexName, credential, GetSearchClientOptions())); +#if !SNIPPET + searchClient = InstrumentClient(new SearchClient(endpoint, fakeIndexName, credential, GetSearchClientOptions())); +#endif try { await searchClient.GetDocumentCountAsync(); @@ -144,7 +152,9 @@ public async Task GetStatisticsAsync() AzureKeyCredential credential = new AzureKeyCredential( Environment.GetEnvironmentVariable("SEARCH_API_KEY")); SearchIndexClient indexClient = new SearchIndexClient(endpoint, credential); - /*@@*/ indexClient = InstrumentClient(new SearchIndexClient(endpoint, credential, GetSearchClientOptions())); +#if !SNIPPET + indexClient = InstrumentClient(new SearchIndexClient(endpoint, credential, GetSearchClientOptions())); +#endif // Get and report the Search Service statistics Response stats = await indexClient.GetServiceStatisticsAsync(); @@ -173,22 +183,31 @@ public async Task CreateIndexerAsync() AzureKeyCredential credential = new AzureKeyCredential( Environment.GetEnvironmentVariable("SEARCH_API_KEY")); SearchIndexClient indexClient = new SearchIndexClient(endpoint, credential); - /*@@*/ indexClient = resources.GetIndexClient(); +#if !SNIPPET + indexClient = resources.GetIndexClient(); +#endif // Create a synonym map from a file containing country names and abbreviations // using the Solr format with entry on a new line using \n, for example: // United States of America,US,USA\n string synonymMapName = "countries"; - /*@@*/ synonymMapName = Recording.Random.GetName(); +#if !SNIPPET + synonymMapName = Recording.Random.GetName(); +#endif string synonymMapPath = "countries.txt"; - /*@@*/ synonymMapPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "Samples", "countries.txt"); +#if !SNIPPET + synonymMapPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "Samples", "countries.txt"); +#endif SynonymMap synonyms; - //@@using (StreamReader file = File.OpenText(synonymMapPath)) - //@@{ - //@@ synonyms = new SynonymMap(synonymMapName, file); - //@@} - /*@@*/ synonyms = new SynonymMap(synonymMapName, CountriesSolrSynonymMap); +#if SNIPPET + using (StreamReader file = File.OpenText(synonymMapPath)) + { + synonyms = new SynonymMap(synonymMapName, file); + } +#else + synonyms = new SynonymMap(synonymMapName, CountriesSolrSynonymMap); +#endif await indexClient.CreateSynonymMapAsync(synonyms); #endregion Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateSynonymMap @@ -200,7 +219,9 @@ public async Task CreateIndexerAsync() #region Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateIndex // Create the index string indexName = "hotels"; - /*@@*/ indexName = Recording.Random.GetName(); +#if !SNIPPET + indexName = Recording.Random.GetName(); +#endif SearchIndex index = new SearchIndex(indexName) { Fields = @@ -234,10 +255,14 @@ public async Task CreateIndexerAsync() #region Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateDataSourceConnection // Create a new SearchIndexerClient SearchIndexerClient indexerClient = new SearchIndexerClient(endpoint, credential); - /*@@*/ indexerClient = resources.GetIndexerClient(); +#if !SNIPPET + indexerClient = resources.GetIndexerClient(); +#endif string dataSourceConnectionName = "hotels"; - /*@@*/ dataSourceConnectionName = Recording.Random.GetName(); +#if !SNIPPET + dataSourceConnectionName = Recording.Random.GetName(); +#endif SearchIndexerDataSourceConnection dataSourceConnection = new SearchIndexerDataSourceConnection( dataSourceConnectionName, SearchIndexerDataSourceType.AzureBlob, @@ -290,7 +315,9 @@ public async Task CreateIndexerAsync() // Create a SearchIndexerSkillset that processes those skills in the order given below. string skillsetName = "translations"; - /*@@*/ skillsetName = Recording.Random.GetName(); +#if !SNIPPET + skillsetName = Recording.Random.GetName(); +#endif SearchIndexerSkillset skillset = new SearchIndexerSkillset( skillsetName, new SearchIndexerSkill[] { translationSkill, conditionalSkill }) @@ -308,7 +335,9 @@ public async Task CreateIndexerAsync() #region Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_CreateIndexer string indexerName = "hotels"; - /*@@*/ indexerName = Recording.Random.GetName(); +#if !SNIPPET + indexerName = Recording.Random.GetName(); +#endif SearchIndexer indexer = new SearchIndexer( indexerName, dataSourceConnectionName, @@ -352,15 +381,21 @@ public async Task CreateIndexerAsync() #region Snippet:Azure_Search_Tests_Samples_CreateIndexerAsync_Query // Get a SearchClient from the SearchIndexClient to share its pipeline. SearchClient searchClient = indexClient.GetSearchClient(indexName); - /*@@*/ searchClient = InstrumentClient(new SearchClient(endpoint, indexName, credential, GetSearchClientOptions())); +#if !SNIPPET + searchClient = InstrumentClient(new SearchClient(endpoint, indexName, credential, GetSearchClientOptions())); +#endif // Query for hotels with an ocean view. SearchResults results = await searchClient.SearchAsync("ocean view"); - /*@@*/ bool found = false; +#if !SNIPPET + bool found = false; +#endif await foreach (SearchResult result in results.GetResultsAsync()) { Hotel hotel = result.Document; - /*@@*/ if (hotel.HotelId == "6") { Assert.IsNotNull(hotel.DescriptionFr); found = true; } +#if !SNIPPET + if (hotel.HotelId == "6") { Assert.IsNotNull(hotel.DescriptionFr); found = true; } +#endif Console.WriteLine($"{hotel.HotelName} ({hotel.HotelId})"); Console.WriteLine($" Description (English): {hotel.Description}"); @@ -395,7 +430,9 @@ public async Task GetCountAsync() Environment.GetEnvironmentVariable("SEARCH_API_KEY")); string indexName = Environment.GetEnvironmentVariable("SEARCH_INDEX"); SearchClient searchClient = new SearchClient(endpoint, indexName, credential); - /*@@*/ searchClient = InstrumentClient(new SearchClient(endpoint, indexName, credential, GetSearchClientOptions())); +#if !SNIPPET + searchClient = InstrumentClient(new SearchClient(endpoint, indexName, credential, GetSearchClientOptions())); +#endif // Get and report the number of documents in the index Response count = await searchClient.GetDocumentCountAsync(); diff --git a/sdk/search/Azure.Search.Documents/tests/Samples/Sample04_FieldBuilderIgnore.cs b/sdk/search/Azure.Search.Documents/tests/Samples/Sample04_FieldBuilderIgnore.cs index 10f0ec48bc812..d3759877a5e39 100644 --- a/sdk/search/Azure.Search.Documents/tests/Samples/Sample04_FieldBuilderIgnore.cs +++ b/sdk/search/Azure.Search.Documents/tests/Samples/Sample04_FieldBuilderIgnore.cs @@ -48,7 +48,9 @@ public async Task CreateIndex() // Create a service client. AzureKeyCredential credential = new AzureKeyCredential(key); SearchIndexClient indexClient = new SearchIndexClient(endpoint, credential, options); - /*@@*/ indexClient = resources.GetIndexClient(options); +#if !SNIPPET + indexClient = resources.GetIndexClient(options); +#endif // Create the FieldBuilder using the same serializer. FieldBuilder fieldBuilder = new FieldBuilder @@ -57,8 +59,11 @@ public async Task CreateIndex() }; // Create the index using FieldBuilder. - //@@SearchIndex index = new SearchIndex("movies") - /*@@*/ SearchIndex index = new SearchIndex(Recording.Random.GetName()) +#if SNIPPET + SearchIndex index = new SearchIndex("movies") +#else + SearchIndex index = new SearchIndex(Recording.Random.GetName()) +#endif { Fields = fieldBuilder.Build(typeof(Movie)), Suggesters = diff --git a/sdk/search/Azure.Search.Documents/tests/Samples/Sample05_IndexingDocuments.cs b/sdk/search/Azure.Search.Documents/tests/Samples/Sample05_IndexingDocuments.cs index a621f8f798ebf..be64512ddc8f7 100644 --- a/sdk/search/Azure.Search.Documents/tests/Samples/Sample05_IndexingDocuments.cs +++ b/sdk/search/Azure.Search.Documents/tests/Samples/Sample05_IndexingDocuments.cs @@ -51,8 +51,11 @@ public IEnumerable GenerateCatalog(int count = 1000) var suffix = new[] { null, "Express", "Standard", "Professional", "Enterprise", "Ultimate", "Foundation", ".NET", "Framework" }; var components = new[] { prefixes, products, terms, type, suffix }; - //@@ var random = new Random(); - /*@@*/TestRandom random = Recording.Random; +#if SNIPPET + var random = new Random(); +#else + TestRandom random = Recording.Random; +#endif string RandomElement(string[] values) => values[(int)(random.NextDouble() * values.Length)]; double RandomPrice() => (random.Next(2, 20) * 100.0) / 2.0 - .01; @@ -87,7 +90,9 @@ private async Task CreateIndexAsync(SearchResources resources) #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_CreateIndex_Create // Create the search index string indexName = "Products"; - /*@@*/ indexName = Recording.Random.GetName(); +#if !SNIPPET + indexName = Recording.Random.GetName(); +#endif await indexClient.CreateIndexAsync( new SearchIndex(indexName) { @@ -173,7 +178,9 @@ public async Task BufferedSender() // Check #region Snippet:Azure_Search_Documents_Tests_Samples_Sample05_IndexingDocuments_BufferedSender2 - //@@ await indexer.FlushAsync(); +#if SNIPPET + await indexer.FlushAsync(); +#endif Assert.AreEqual(100000, (int)await searchClient.GetDocumentCountAsync()); #endregion } diff --git a/sdk/search/Azure.Search.Documents/tests/Samples/Sample06_EncryptedIndex.cs b/sdk/search/Azure.Search.Documents/tests/Samples/Sample06_EncryptedIndex.cs index 6b0275d62c277..1cc8d5e5d4dd6 100644 --- a/sdk/search/Azure.Search.Documents/tests/Samples/Sample06_EncryptedIndex.cs +++ b/sdk/search/Azure.Search.Documents/tests/Samples/Sample06_EncryptedIndex.cs @@ -67,7 +67,9 @@ public async Task CreateDoubleEncryptedIndex() // Create a connection to our storage blob container using the credential. string dataSourceConnectionName = "hotels-data-source"; - /*@@*/ dataSourceConnectionName = Recording.Random.GetName(); +#if !SNIPPET + dataSourceConnectionName = Recording.Random.GetName(); +#endif SearchIndexerDataSourceConnection dataSourceConnection = new SearchIndexerDataSourceConnection( dataSourceConnectionName, SearchIndexerDataSourceType.AzureBlob, @@ -84,9 +86,11 @@ public async Task CreateDoubleEncryptedIndex() // You can optionally configure a skillset to use cognitive services when processing documents. // Set the SearchIndexerSkillset.EncryptionKey to the same credential if you use a skillset. string indexName = "hotels"; - /*@@*/ indexName = resources.IndexName; string indexerName = "hotels-indexer"; - /*@@*/ indexerName = Recording.Random.GetName(); +#if !SNIPPET + indexName = resources.IndexName; + indexerName = Recording.Random.GetName(); +#endif SearchIndexer indexer = new SearchIndexer( indexerName, dataSourceConnectionName, @@ -120,11 +124,17 @@ public async Task CreateDoubleEncryptedIndex() Environment.GetEnvironmentVariable("SEARCH_API_KEY")); SearchIndexerClient indexerClient = new SearchIndexerClient(endpoint, credential); - /*@@*/ indexerClient = resources.GetIndexerClient(); +#if !SNIPPET + indexerClient = resources.GetIndexerClient(); +#endif indexerClient.CreateDataSourceConnection(dataSourceConnection); - /*@@*/ cleanUpTasks.Push(() => indexerClient.DeleteDataSourceConnectionAsync(dataSourceConnectionName)); +#if !SNIPPET + cleanUpTasks.Push(() => indexerClient.DeleteDataSourceConnectionAsync(dataSourceConnectionName)); +#endif indexerClient.CreateIndexer(indexer); - /*@@*/ cleanUpTasks.Push(() => indexerClient.DeleteIndexerAsync(indexerName)); +#if !SNIPPET + cleanUpTasks.Push(() => indexerClient.DeleteIndexerAsync(indexerName)); +#endif #endregion Snippet:Azure_Search_Tests_Sample06_EncryptedIndex_CreateDoubleEncryptedIndex_Index await WaitForIndexingAsync(indexerClient, indexerName); @@ -132,13 +142,17 @@ public async Task CreateDoubleEncryptedIndex() #region Snippet:Azure_Search_Tests_Sample06_EncryptedIndex_CreateDoubleEncryptedIndex_Query // Create a SearchClient and search for luxury hotels. In production, be sure to use the query key. SearchClient searchClient = new SearchClient(endpoint, "hotels", credential); - /*@@*/ searchClient = resources.GetSearchClient(); - /*@@*/ bool found = false; +#if !SNIPPET + searchClient = resources.GetSearchClient(); + bool found = false; +#endif Response> results = searchClient.Search("luxury hotels"); foreach (SearchResult result in results.Value.GetResults()) { Hotel hotel = result.Document; - /*@@*/ found = true; +#if !SNIPPET + found = true; +#endif Console.WriteLine($"{hotel.HotelName} ({hotel.HotelId})"); Console.WriteLine($" Description: {hotel.Description}");