Resource.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using RackPeek.Domain.Resources.AccessPoints;
  2. using RackPeek.Domain.Resources.Desktops;
  3. using RackPeek.Domain.Resources.Firewalls;
  4. using RackPeek.Domain.Resources.Laptops;
  5. using RackPeek.Domain.Resources.Routers;
  6. using RackPeek.Domain.Resources.Servers;
  7. using RackPeek.Domain.Resources.Services;
  8. using RackPeek.Domain.Resources.Switches;
  9. using RackPeek.Domain.Resources.SystemResources;
  10. using RackPeek.Domain.Resources.UpsUnits;
  11. namespace RackPeek.Domain.Resources;
  12. public abstract class Resource
  13. {
  14. private static readonly Dictionary<string, string> KindToPluralDictionary = new()
  15. {
  16. { "hardware", "hardware" },
  17. { "server", "servers" },
  18. { "switch", "switches" },
  19. { "firewall", "firewalls" },
  20. { "router", "routers" },
  21. { "accesspoint", "accesspoints" },
  22. { "desktop", "desktops" },
  23. { "laptop", "laptops" },
  24. { "ups", "ups" },
  25. { "system", "systems" },
  26. { "service", "services" }
  27. };
  28. private static readonly Dictionary<Type, string> TypeToKindMap = new()
  29. {
  30. { typeof(Hardware.Hardware), "Hardware" },
  31. { typeof(Server), "Server" },
  32. { typeof(Switch), "Switch" },
  33. { typeof(Firewall), "Firewall" },
  34. { typeof(Router), "Router" },
  35. { typeof(AccessPoint), "Accesspoint" },
  36. { typeof(Desktop), "Desktop" },
  37. { typeof(Laptop), "Laptop" },
  38. { typeof(Ups), "Ups" },
  39. { typeof(SystemResource), "System" },
  40. { typeof(Service), "Service" }
  41. };
  42. public string Kind { get; set; } = string.Empty;
  43. public required string Name { get; set; }
  44. public string[] Tags { get; set; } = [];
  45. public Dictionary<string, string> Labels { get; set; } = new();
  46. public string? Notes { get; set; }
  47. public List<string> RunsOn { get; set; } = new List<string>();
  48. public static string KindToPlural(string kind)
  49. {
  50. return KindToPluralDictionary.GetValueOrDefault(kind.ToLower().Trim(), kind);
  51. }
  52. public static string GetKind<T>() where T : Resource
  53. {
  54. if (TypeToKindMap.TryGetValue(typeof(T), out var kind))
  55. return kind;
  56. throw new InvalidOperationException(
  57. $"No kind mapping defined for type {typeof(T).Name}");
  58. }
  59. public static bool CanRunOn<T>(Resource parent) where T : Resource
  60. {
  61. var childKind = GetKind<T>().ToLowerInvariant();
  62. var parentKind = parent.Kind.ToLowerInvariant();
  63. // Service -> System
  64. if (childKind == "service" && parentKind == "system")
  65. return true;
  66. // System -> Hardware
  67. if (childKind == "system" && parent is Hardware.Hardware)
  68. return true;
  69. return false;
  70. }
  71. }