Resource.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. /// <summary>
  49. /// Stable machine-generated identity, set by <c>rpk discover</c>. Optional, and
  50. /// absent on everything entered by hand. Lets a re-run find this resource again
  51. /// after the user has renamed it. See <c>RackPeek.Domain.Discovery.DiscoveryId</c>.
  52. /// </summary>
  53. public string? DiscoveryId { get; set; }
  54. public string[] Tags { get; set; } = [];
  55. public Dictionary<string, string> Labels { get; set; } = new();
  56. public string? Notes { get; set; }
  57. public List<string> RunsOn { get; set; } = new();
  58. public static bool IsHardware(string kind) {
  59. kind = kind.Trim().ToLower();
  60. return kind == "hardware" || _hardwareTypes.Contains(kind);
  61. }
  62. public static string GetResourceUrl(string kind, string name) {
  63. var encoded = Uri.EscapeDataString(name);
  64. kind = kind.Trim().ToLower();
  65. if (IsHardware(kind)) return $"resources/hardware/{encoded}";
  66. if (kind == "system") return $"resources/systems/{encoded}";
  67. if (kind == "service") return $"resources/services/{encoded}";
  68. return "#";
  69. }
  70. public static string KindToPlural(string kind) =>
  71. _kindToPluralDictionary.GetValueOrDefault(kind.ToLower().Trim(), kind);
  72. public static string GetKind<T>() where T : Resource {
  73. if (_typeToKindMap.TryGetValue(typeof(T), out var kind))
  74. return kind;
  75. throw new InvalidOperationException(
  76. $"No kind mapping defined for type {typeof(T).Name}");
  77. }
  78. public static bool CanRunOn<T>(Resource parent) where T : Resource {
  79. var childKind = GetKind<T>().ToLowerInvariant();
  80. var parentKind = parent.Kind.ToLowerInvariant();
  81. // Service -> System
  82. if (childKind == "service" && parentKind == "system")
  83. return true;
  84. // System -> Hardware
  85. if (childKind == "system" && parent is Hardware.Hardware)
  86. return true;
  87. // System -> System
  88. if (childKind == "system" && parent is SystemResource)
  89. return true;
  90. return false;
  91. }
  92. }