YamlResourceCollection.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System.Collections.Specialized;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Persistence.Yaml;
  4. using RackPeek.Domain.Resources;
  5. using RackPeek.Domain.Resources.Models;
  6. using RackPeek.Domain.Resources.Services;
  7. using RackPeek.Domain.Resources.SystemResources;
  8. using RackPeek.Yaml;
  9. using YamlDotNet.Core;
  10. using YamlDotNet.Serialization;
  11. using YamlDotNet.Serialization.NamingConventions;
  12. public class ResourceCollection
  13. {
  14. public List<Resource> Resources { get; } = new();
  15. public readonly SemaphoreSlim FileLock = new(1, 1);
  16. }
  17. public sealed class YamlResourceCollection(
  18. string filePath,
  19. ITextFileStore fileStore,
  20. ResourceCollection resourceCollection)
  21. : IResourceCollection
  22. {
  23. public Task<bool> Exists(string name)
  24. {
  25. return Task.FromResult(resourceCollection.Resources.Exists(r =>
  26. r.Name.Equals(name, StringComparison.OrdinalIgnoreCase)));
  27. }
  28. public Task<Dictionary<string, int>> GetTagsAsync()
  29. {
  30. var result = resourceCollection.Resources
  31. .Where(r => r.Tags != null)
  32. .SelectMany(r => r.Tags!) // flatten all tag arrays
  33. .Where(t => !string.IsNullOrWhiteSpace(t))
  34. .GroupBy(t => t)
  35. .ToDictionary(g => g.Key, g => g.Count());
  36. return Task.FromResult(result);
  37. }
  38. public Task<IReadOnlyList<Resource>> GetByTagAsync(string name)
  39. {
  40. return Task.FromResult<IReadOnlyList<Resource>>(resourceCollection.Resources.Where(r => r.Tags.Contains(name)).ToList());
  41. }
  42. public async Task LoadAsync()
  43. {
  44. var loaded = await LoadFromFileAsync();
  45. try
  46. {
  47. resourceCollection.Resources.Clear();
  48. }
  49. catch
  50. {
  51. // ignore
  52. }
  53. resourceCollection.Resources.AddRange(loaded);
  54. }
  55. public IReadOnlyList<Hardware> HardwareResources =>
  56. resourceCollection.Resources.OfType<Hardware>().ToList();
  57. public IReadOnlyList<SystemResource> SystemResources =>
  58. resourceCollection.Resources.OfType<SystemResource>().ToList();
  59. public IReadOnlyList<Service> ServiceResources =>
  60. resourceCollection.Resources.OfType<Service>().ToList();
  61. public Resource? GetByName(string name) =>
  62. resourceCollection.Resources.FirstOrDefault(r =>
  63. r.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
  64. public Task AddAsync(Resource resource) =>
  65. UpdateWithLockAsync(list =>
  66. {
  67. if (list.Any(r => r.Name.Equals(resource.Name, StringComparison.OrdinalIgnoreCase)))
  68. throw new InvalidOperationException($"'{resource.Name}' already exists.");
  69. resource.Kind = GetKind(resource);
  70. list.Add(resource);
  71. });
  72. public Task UpdateAsync(Resource resource) =>
  73. UpdateWithLockAsync(list =>
  74. {
  75. var index = list.FindIndex(r => r.Name.Equals(resource.Name, StringComparison.OrdinalIgnoreCase));
  76. if (index == -1) throw new InvalidOperationException("Not found.");
  77. resource.Kind = GetKind(resource);
  78. list[index] = resource;
  79. });
  80. public Task DeleteAsync(string name) =>
  81. UpdateWithLockAsync(list =>
  82. list.RemoveAll(r => r.Name.Equals(name, StringComparison.OrdinalIgnoreCase)));
  83. private async Task UpdateWithLockAsync(Action<List<Resource>> action)
  84. {
  85. await resourceCollection.FileLock.WaitAsync();
  86. try
  87. {
  88. action(resourceCollection.Resources);
  89. var serializer = new SerializerBuilder()
  90. .WithNamingConvention(CamelCaseNamingConvention.Instance)
  91. .WithTypeConverter(new StorageSizeYamlConverter())
  92. .WithTypeConverter(new NotesStringYamlConverter())
  93. .ConfigureDefaultValuesHandling(
  94. DefaultValuesHandling.OmitNull |
  95. DefaultValuesHandling.OmitEmptyCollections
  96. )
  97. .Build();
  98. var payload = new OrderedDictionary
  99. {
  100. ["resources"] = resourceCollection.Resources.Select(SerializeResource).ToList()
  101. };
  102. await fileStore.WriteAllTextAsync(
  103. filePath,
  104. serializer.Serialize(payload));
  105. }
  106. finally
  107. {
  108. resourceCollection.FileLock.Release();
  109. }
  110. }
  111. private async Task<List<Resource>> LoadFromFileAsync()
  112. {
  113. var yaml = await fileStore.ReadAllTextAsync(filePath);
  114. if (string.IsNullOrWhiteSpace(yaml))
  115. return new();
  116. var deserializer = new DeserializerBuilder()
  117. .WithNamingConvention(CamelCaseNamingConvention.Instance)
  118. .WithCaseInsensitivePropertyMatching()
  119. .WithTypeConverter(new StorageSizeYamlConverter())
  120. .WithTypeConverter(new NotesStringYamlConverter())
  121. .WithTypeDiscriminatingNodeDeserializer(options =>
  122. {
  123. options.AddKeyValueTypeDiscriminator<Resource>("kind", new Dictionary<string, Type>
  124. {
  125. { Server.KindLabel, typeof(Server) },
  126. { Switch.KindLabel, typeof(Switch) },
  127. { Firewall.KindLabel, typeof(Firewall) },
  128. { Router.KindLabel, typeof(Router) },
  129. { Desktop.KindLabel, typeof(Desktop) },
  130. { Laptop.KindLabel, typeof(Laptop) },
  131. { AccessPoint.KindLabel, typeof(AccessPoint) },
  132. { Ups.KindLabel, typeof(Ups) },
  133. { SystemResource.KindLabel, typeof(SystemResource) },
  134. { Service.KindLabel, typeof(Service) }
  135. });
  136. })
  137. .Build();
  138. try
  139. {
  140. var root = deserializer.Deserialize<YamlRoot>(yaml);
  141. return root?.Resources ?? new();
  142. }
  143. catch (YamlException)
  144. {
  145. return new();
  146. }
  147. }
  148. private string GetKind(Resource resource) => resource switch
  149. {
  150. Server => "Server",
  151. Switch => "Switch",
  152. Firewall => "Firewall",
  153. Router => "Router",
  154. Desktop => "Desktop",
  155. Laptop => "Laptop",
  156. AccessPoint => "AccessPoint",
  157. Ups => "Ups",
  158. SystemResource => "System",
  159. Service => "Service",
  160. _ => throw new InvalidOperationException($"Unknown resource type: {resource.GetType().Name}")
  161. };
  162. private OrderedDictionary SerializeResource(Resource resource)
  163. {
  164. var map = new OrderedDictionary
  165. {
  166. ["kind"] = GetKind(resource)
  167. };
  168. var serializer = new SerializerBuilder()
  169. .WithNamingConvention(CamelCaseNamingConvention.Instance)
  170. .WithTypeConverter(new NotesStringYamlConverter())
  171. .ConfigureDefaultValuesHandling(
  172. DefaultValuesHandling.OmitNull |
  173. DefaultValuesHandling.OmitEmptyCollections
  174. )
  175. .Build();
  176. var yaml = serializer.Serialize(resource);
  177. var props = new DeserializerBuilder()
  178. .Build()
  179. .Deserialize<Dictionary<string, object?>>(yaml);
  180. foreach (var (key, value) in props)
  181. if (key != "kind")
  182. map[key] = value;
  183. return map;
  184. }
  185. }
  186. public class YamlRoot
  187. {
  188. public List<Resource>? Resources { get; set; }
  189. }