-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
LifetimeAction.cs
116 lines (95 loc) · 4.48 KB
/
LifetimeAction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Text.Json;
namespace Azure.Security.KeyVault.Certificates
{
/// <summary>
/// An action to be executed at a prescribed time in a certificates lifecycle
/// </summary>
public class LifetimeAction : IJsonSerializable, IJsonDeserializable
{
private const string TriggerPropertyName = "trigger";
private const string ActionPropertyName = "action";
private const string LifetimePercentagePropertyName = "lifetime_percentage";
private const string DaysBeforeExpiryPropertyName = "days_before_expiry";
private const string ActionTypePropertyName = "action_type";
private static readonly JsonEncodedText s_triggerPropertyNameBytes = JsonEncodedText.Encode(TriggerPropertyName);
private static readonly JsonEncodedText s_actionPropertyNameBytes = JsonEncodedText.Encode(ActionPropertyName);
private static readonly JsonEncodedText s_lifetimePercentagePropertyNameBytes = JsonEncodedText.Encode(LifetimePercentagePropertyName);
private static readonly JsonEncodedText s_daysBeforeExpiryPropertyNameBytes = JsonEncodedText.Encode(DaysBeforeExpiryPropertyName);
private static readonly JsonEncodedText s_actionTypePropertyNameBytes = JsonEncodedText.Encode(ActionTypePropertyName);
/// <summary>
/// Initializes a new instance of the <see cref="LifetimeAction"/> class.
/// </summary>
/// <param name="action">The <see cref="CertificatePolicyAction"/> to be performed.</param>
public LifetimeAction(CertificatePolicyAction action)
{
Action = action;
}
private LifetimeAction()
{
}
/// <summary>
/// Gets or sets the action should be performed the specified number of days before the certificate will expire.
/// </summary>
public int? DaysBeforeExpiry { get; set; }
/// <summary>
/// Gets or sets the action should be performed when the certificate reaches the specified percentage of its lifetime. Valid values include 1-99.
/// </summary>
public int? LifetimePercentage { get; set; }
/// <summary>
/// Gets the <see cref="CertificatePolicyAction"/> to be performed.
/// </summary>
public CertificatePolicyAction Action { get; private set; }
internal static LifetimeAction FromJsonObject(JsonElement json)
{
var action = new LifetimeAction();
((IJsonDeserializable)action).ReadProperties(json);
return action;
}
void IJsonDeserializable.ReadProperties(JsonElement json)
{
foreach (JsonProperty prop in json.EnumerateObject())
{
switch (prop.Name)
{
case TriggerPropertyName:
foreach (JsonProperty triggerProp in prop.Value.EnumerateObject())
{
switch (triggerProp.Name)
{
case LifetimePercentagePropertyName:
LifetimePercentage = triggerProp.Value.GetInt32();
break;
case DaysBeforeExpiryPropertyName:
DaysBeforeExpiry = triggerProp.Value.GetInt32();
break;
}
}
break;
case ActionPropertyName:
Action = prop.Value.GetProperty(ActionTypePropertyName).GetString();
break;
}
}
}
void IJsonSerializable.WriteProperties(Utf8JsonWriter json)
{
// trigger
json.WriteStartObject(s_triggerPropertyNameBytes);
if (DaysBeforeExpiry.HasValue)
{
json.WriteNumber(s_daysBeforeExpiryPropertyNameBytes, DaysBeforeExpiry.Value);
}
if (LifetimePercentage.HasValue)
{
json.WriteNumber(s_lifetimePercentagePropertyNameBytes, LifetimePercentage.Value);
}
json.WriteEndObject();
// action
json.WriteStartObject(s_actionPropertyNameBytes);
json.WriteString(s_actionTypePropertyNameBytes, Action.ToString());
json.WriteEndObject();
}
}
}