| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Text.Json; |
| | 6 | |
|
| | 7 | | namespace Azure.Security.KeyVault.Secrets |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// <see cref="KeyVaultSecret"/> is the resource consisting of a value and its <see cref="Properties"/>. |
| | 11 | | /// </summary> |
| | 12 | | public class KeyVaultSecret : IJsonDeserializable, IJsonSerializable |
| | 13 | | { |
| | 14 | | private const string ValuePropertyName = "value"; |
| | 15 | |
|
| 4 | 16 | | private static readonly JsonEncodedText s_valuePropertyNameBytes = JsonEncodedText.Encode(ValuePropertyName); |
| | 17 | |
|
| 1184 | 18 | | internal KeyVaultSecret(SecretProperties properties = null) |
| | 19 | | { |
| 1184 | 20 | | Properties = properties ?? new SecretProperties(); |
| 1184 | 21 | | } |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Initializes a new instance of the <see cref="KeyVaultSecret"/> class. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="name">The name of the secret.</param> |
| | 27 | | /// <param name="value">The value of the secret.</param> |
| | 28 | | /// <exception cref="ArgumentException"><paramref name="name"/> is an empty string.</exception> |
| | 29 | | /// <exception cref="ArgumentNullException"><paramref name="name"/> or <paramref name="value"/> is null.</except |
| 740 | 30 | | public KeyVaultSecret(string name, string value) |
| | 31 | | { |
| 740 | 32 | | Properties = new SecretProperties(name); |
| 732 | 33 | | Value = value ?? throw new ArgumentNullException(nameof(value)); |
| 728 | 34 | | } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Gets the secret identifier. |
| | 38 | | /// </summary> |
| 10004 | 39 | | public Uri Id => Properties.Id; |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Gets the the name of the secret. |
| | 43 | | /// </summary> |
| 33780 | 44 | | public string Name => Properties.Name; |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Gets additional properties of the <see cref="KeyVaultSecret"/>. |
| | 48 | | /// </summary> |
| 47788 | 49 | | public SecretProperties Properties { get; } |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Gets the value of the secret. |
| | 53 | | /// </summary> |
| 3032 | 54 | | public string Value { get; internal set; } |
| | 55 | |
|
| | 56 | | internal virtual void ReadProperty(JsonProperty prop) |
| | 57 | | { |
| 3020 | 58 | | switch (prop.Name) |
| | 59 | | { |
| | 60 | | case ValuePropertyName: |
| 748 | 61 | | Value = prop.Value.GetString(); |
| 748 | 62 | | break; |
| | 63 | |
|
| | 64 | | default: |
| 2272 | 65 | | Properties.ReadProperty(prop); |
| | 66 | | break; |
| | 67 | | } |
| 2272 | 68 | | } |
| | 69 | |
|
| | 70 | | internal virtual void WriteProperties(Utf8JsonWriter json) |
| | 71 | | { |
| 728 | 72 | | if (Value != null) |
| | 73 | | { |
| 728 | 74 | | json.WriteString(s_valuePropertyNameBytes, Value); |
| | 75 | | } |
| | 76 | |
|
| 728 | 77 | | Properties.WriteProperties(json); |
| 728 | 78 | | } |
| | 79 | |
|
| | 80 | | void IJsonDeserializable.ReadProperties(JsonElement json) |
| | 81 | | { |
| 11024 | 82 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | 83 | | { |
| 4328 | 84 | | ReadProperty(prop); |
| | 85 | | } |
| 1184 | 86 | | } |
| | 87 | |
|
| 728 | 88 | | void IJsonSerializable.WriteProperties(Utf8JsonWriter json) => WriteProperties(json); |
| | 89 | | } |
| | 90 | | } |