| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Azure.Core; |
| | 8 | | using Azure.Core.Pipeline; |
| | 9 | | using Azure.Template.Models; |
| | 10 | |
|
| | 11 | | namespace Azure.Template |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// The sample secrets client. |
| | 15 | | /// </summary> |
| | 16 | | public class MiniSecretClient |
| | 17 | | { |
| | 18 | | private readonly ClientDiagnostics _clientDiagnostics; |
| | 19 | | private readonly HttpPipeline _pipeline; |
| 4 | 20 | | internal ServiceRestClient RestClient { get; } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Initializes a new instance of the <see cref="MiniSecretClient"/>. |
| | 24 | | /// </summary> |
| 0 | 25 | | public MiniSecretClient(Uri endpoint, TokenCredential credential) : this(endpoint, credential, new MiniSecretCli |
| | 26 | | { |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Initializes a new instance of the <see cref="MiniSecretClient"/>. |
| | 31 | | /// </summary> |
| 4 | 32 | | public MiniSecretClient(Uri endpoint, TokenCredential credential, MiniSecretClientOptions options): this( |
| 4 | 33 | | new ClientDiagnostics(options), |
| 4 | 34 | | HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, "https://vault.azure.net/ |
| 4 | 35 | | endpoint.ToString(), |
| 4 | 36 | | options.Version) |
| | 37 | | { |
| 4 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> Initializes a new instance of MiniSecretClient for mocking. </summary> |
| 4 | 41 | | protected MiniSecretClient() |
| | 42 | | { |
| 4 | 43 | | } |
| | 44 | | /// <summary> Initializes a new instance of MiniSecretClient. </summary> |
| | 45 | | /// <param name="clientDiagnostics"> The handler for diagnostic messaging in the client. </param> |
| | 46 | | /// <param name="pipeline"> The HTTP pipeline for sending and receiving REST requests and responses. </param> |
| | 47 | | /// <param name="vaultBaseUrl"> The vault name, for example https://myvault.vault.azure.net. </param> |
| | 48 | | /// <param name="apiVersion"> Api Version. </param> |
| 4 | 49 | | internal MiniSecretClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, string vaultBaseUrl, strin |
| | 50 | | { |
| 4 | 51 | | RestClient = new ServiceRestClient(clientDiagnostics, pipeline, vaultBaseUrl, apiVersion); |
| 4 | 52 | | _clientDiagnostics = clientDiagnostics; |
| 4 | 53 | | _pipeline = pipeline; |
| 4 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> The GET operation is applicable to any secret stored in Azure Key Vault. This operation requires t |
| | 57 | | /// <param name="secretName"> The name of the secret. </param> |
| | 58 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 59 | | public virtual async Task<Response<SecretBundle>> GetSecretAsync(string secretName, CancellationToken cancellati |
| | 60 | | { |
| 2 | 61 | | using var scope = _clientDiagnostics.CreateScope("MiniSecretClient.GetSecret"); |
| 2 | 62 | | scope.Start(); |
| | 63 | | try |
| | 64 | | { |
| 2 | 65 | | return await RestClient.GetSecretAsync(secretName, cancellationToken).ConfigureAwait(false); |
| | 66 | | } |
| 0 | 67 | | catch (Exception e) |
| | 68 | | { |
| 0 | 69 | | scope.Failed(e); |
| 0 | 70 | | throw; |
| | 71 | | } |
| 2 | 72 | | } |
| | 73 | |
|
| | 74 | | /// <summary> The GET operation is applicable to any secret stored in Azure Key Vault. This operation requires t |
| | 75 | | /// <param name="secretName"> The name of the secret. </param> |
| | 76 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 77 | | public virtual Response<SecretBundle> GetSecret(string secretName, CancellationToken cancellationToken = default |
| | 78 | | { |
| 2 | 79 | | using var scope = _clientDiagnostics.CreateScope("MiniSecretClient.GetSecret"); |
| 2 | 80 | | scope.Start(); |
| | 81 | | try |
| | 82 | | { |
| 2 | 83 | | return RestClient.GetSecret(secretName, cancellationToken); |
| | 84 | | } |
| 0 | 85 | | catch (Exception e) |
| | 86 | | { |
| 0 | 87 | | scope.Failed(e); |
| 0 | 88 | | throw; |
| | 89 | | } |
| 2 | 90 | | } |
| | 91 | | } |
| | 92 | | } |