Resource.cs 3.4 KB

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