Resource.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.OtherHardware;
  11. using RackPeek.Domain.Resources.UpsUnits;
  12. namespace RackPeek.Domain.Resources;
  13. public abstract class Resource {
  14. private static readonly string[] _hardwareTypes =
  15. ["server", "switch", "firewall", "router", "accesspoint", "desktop", "laptop", "ups", "other"];
  16. private static readonly Dictionary<string, string> _kindToPluralDictionary = new()
  17. {
  18. { "hardware", "hardware" },
  19. { "server", "servers" },
  20. { "switch", "switches" },
  21. { "firewall", "firewalls" },
  22. { "router", "routers" },
  23. { "accesspoint", "accesspoints" },
  24. { "desktop", "desktops" },
  25. { "laptop", "laptops" },
  26. { "ups", "ups" },
  27. { "other", "other" },
  28. { "system", "systems" },
  29. { "service", "services" }
  30. };
  31. private static readonly Dictionary<Type, string> _typeToKindMap = new()
  32. {
  33. { typeof(Hardware.Hardware), "Hardware" },
  34. { typeof(Server), "Server" },
  35. { typeof(Switch), "Switch" },
  36. { typeof(Firewall), "Firewall" },
  37. { typeof(Router), "Router" },
  38. { typeof(AccessPoint), "Accesspoint" },
  39. { typeof(Desktop), "Desktop" },
  40. { typeof(Laptop), "Laptop" },
  41. { typeof(Ups), "Ups" },
  42. { typeof(Other), "Other" },
  43. { typeof(SystemResource), "System" },
  44. { typeof(Service), "Service" }
  45. };
  46. public string Kind { get; set; } = string.Empty;
  47. public required string Name { get; set; }
  48. public string[] Tags { get; set; } = [];
  49. public Dictionary<string, string> Labels { get; set; } = new();
  50. public string? Notes { get; set; }
  51. public List<string> RunsOn { get; set; } = new();
  52. public static bool IsHardware(string kind) {
  53. kind = kind.Trim().ToLower();
  54. return kind == "hardware" || _hardwareTypes.Contains(kind);
  55. }
  56. public static string GetResourceUrl(string kind, string name) {
  57. var encoded = Uri.EscapeDataString(name);
  58. kind = kind.Trim().ToLower();
  59. if (IsHardware(kind)) return $"resources/hardware/{encoded}";
  60. if (kind == "system") return $"resources/systems/{encoded}";
  61. if (kind == "service") return $"resources/services/{encoded}";
  62. return "#";
  63. }
  64. public static string KindToPlural(string kind) =>
  65. _kindToPluralDictionary.GetValueOrDefault(kind.ToLower().Trim(), kind);
  66. public static string GetKind<T>() where T : Resource {
  67. if (_typeToKindMap.TryGetValue(typeof(T), out var kind))
  68. return kind;
  69. throw new InvalidOperationException(
  70. $"No kind mapping defined for type {typeof(T).Name}");
  71. }
  72. public static bool CanRunOn<T>(Resource parent) where T : Resource {
  73. var childKind = GetKind<T>().ToLowerInvariant();
  74. var parentKind = parent.Kind.ToLowerInvariant();
  75. // Service -> System
  76. if (childKind == "service" && parentKind == "system")
  77. return true;
  78. // System -> Hardware
  79. if (childKind == "system" && parent is Hardware.Hardware)
  80. return true;
  81. // System -> System
  82. if (childKind == "system" && parent is SystemResource)
  83. return true;
  84. return false;
  85. }
  86. }