-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAzureConfigurationAppRepository.cs
More file actions
175 lines (153 loc) · 7.55 KB
/
Copy pathAzureConfigurationAppRepository.cs
File metadata and controls
175 lines (153 loc) · 7.55 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Azure.ApplicationModel.Configuration;
using Duber.Infrastructure.Chaos;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
namespace Duber.Chaos.API.Infrastructure.Repository
{
/// <summary>
/// // All the nasty code related to Generics is due to Azure App Configuration doesn't support a batch or generic update, it's necessary to update one setting at a time.
/// </summary>
public class AzureConfigurationAppRepository : IChaosRepository
{
private readonly IConfiguration _configuration;
private Dictionary<string, object> _settingsToUpdate;
private readonly GeneralChaosSetting _chaosSettings;
private readonly MethodInfo _getSettingsToUpdateMethodInfo = typeof(AzureConfigurationAppRepository).GetMethod(nameof(GetSettingsToUpdate), BindingFlags.NonPublic | BindingFlags.Instance);
private readonly MethodInfo _settingHasChangedMethodInfo = typeof(AzureConfigurationAppRepository).GetMethod(nameof(SettingHasChanged), BindingFlags.NonPublic | BindingFlags.Instance);
public AzureConfigurationAppRepository(IOptionsSnapshot<GeneralChaosSetting> chaosSettings, IConfiguration configuration)
{
_chaosSettings = chaosSettings.Value;
_configuration = configuration;
}
public Task<GeneralChaosSetting> GetChaosSettingsAsync()
{
return Task.FromResult(_chaosSettings);
}
public async Task UpdateChaosSettings(GeneralChaosSetting settings)
{
_settingsToUpdate = new Dictionary<string, object>();
var currentSettings = await GetChaosSettingsAsync();
GetSettingsToUpdate(settings);
var client = new ConfigurationClient(_configuration.GetConnectionString("AppConfig"));
foreach (var settingToUpdate in _settingsToUpdate)
{
if (!SettingHasChanged(settingToUpdate.Key, settingToUpdate.Value.ToNullString(), currentSettings)) continue;
var setting = new ConfigurationSetting(settingToUpdate.Key, settingToUpdate.Value.ToNullString());
await client.SetAsync(setting);
}
// find which settings are going to be deleted.
var seetingsToDelete = currentSettings?.OperationChaosSettings?
.Where(settingToDelete => !settings.OperationChaosSettings.Select(x => x.Id).Contains(settingToDelete.Id));
if (seetingsToDelete != null)
foreach (var settingToDelete in seetingsToDelete)
{
_settingsToUpdate = new Dictionary<string, object>();
GetSettingsToUpdate(settingToDelete, "OperationChaosSettings", currentSettings.OperationChaosSettings.IndexOf(settingToDelete));
foreach (var settingToUpdate in _settingsToUpdate)
{
await client.DeleteAsync(settingToUpdate.Key);
}
}
// We always update our sentinel in order to all the settings will be refreshed.
await client.SetAsync(new ConfigurationSetting("GeneralChaosSetting:Sentinel", "True"));
}
private void GetSettingsToUpdate<T>(T value, string parent = null, int? index = null)
{
foreach (var property in typeof(T).GetProperties())
{
if (typeof(IList).IsAssignableFrom(property.PropertyType))
{
var list = (IList)property.GetValue(value);
var listType = property.PropertyType.GenericTypeArguments[0];
for (var i = 0; i < list?.Count; i++)
{
var listItem = list[i];
var generic = _getSettingsToUpdateMethodInfo.MakeGenericMethod(listType);
generic.Invoke(this, new[] { listItem, property.Name, i });
}
}
else if (!property.PropertyType.IsPrimitive())
{
var generic = _getSettingsToUpdateMethodInfo.MakeGenericMethod(property.PropertyType);
generic.Invoke(this, new[] { property.GetValue(value), property.Name, null });
}
if (property.PropertyType.IsPrimitive())
{
var settingName = $"{nameof(GeneralChaosSetting)}:{GetParent(parent, index)}{property.Name}";
var settingValue = property.GetValue(value);
_settingsToUpdate.Add(settingName, settingValue);
}
}
}
private static string GetParent(string parent, int? index)
{
if (parent == null)
return string.Empty;
return index.HasValue ? $"{parent}:{index}:" : $"{parent}:";
}
private bool SettingHasChanged<T>(string key, string value, T currentSettings)
{
var properties = key.Split(":");
var propertiesToExclude = new[]
{
nameof(_chaosSettings.Sentinel),
nameof(_chaosSettings.Id),
nameof(_chaosSettings.SubscriptionId),
nameof(_chaosSettings.TenantId),
nameof(_chaosSettings.ClientId),
nameof(_chaosSettings.ClientKey)
};
if (propertiesToExclude.Contains(properties.Last())) return false;
foreach (var property in properties)
{
var propertyInfo = typeof(T).GetProperty(property);
if (propertyInfo == null) continue;
if (propertyInfo.PropertyType.IsPrimitive())
{
return !propertyInfo.GetValue(currentSettings).ToNullString().Equals(value);
}
else if (typeof(IList).IsAssignableFrom(propertyInfo.PropertyType))
{
var list = (IList)propertyInfo.GetValue(currentSettings);
var listType = propertyInfo.PropertyType.GenericTypeArguments[0];
var index = int.Parse(properties[2]);
if (index > list.Count - 1)
return true; // it means it's a new operation setting.
var listItem = list[index];
var generic = _settingHasChangedMethodInfo.MakeGenericMethod(listType);
return (bool)generic.Invoke(this, new[] { properties.Last(), value, listItem });
}
else if (!propertyInfo.PropertyType.IsPrimitive())
{
var generic = _settingHasChangedMethodInfo.MakeGenericMethod(propertyInfo.PropertyType);
return (bool)generic.Invoke(this, new[] { properties.Last(), value, propertyInfo.GetValue(currentSettings) });
}
}
return false;
}
}
internal static class Extensions
{
internal static bool IsPrimitive(this Type t)
{
return t.IsPrimitive ||
t == typeof(decimal) ||
t == typeof(string) ||
t == typeof(DateTime) ||
t == typeof(DateTimeOffset) ||
t == typeof(Guid) ||
t == typeof(TimeSpan) ||
t == typeof(Enum);
}
internal static string ToNullString(this object obj)
{
return obj == null ? string.Empty : obj.ToString();
}
}
}