Resource.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 string? Notes { get; set; }
  46. public string? RunsOn { get; set; }
  47. public static string KindToPlural(string kind)
  48. {
  49. return KindToPluralDictionary.GetValueOrDefault(kind.ToLower().Trim(), kind);
  50. }
  51. public static string GetKind<T>() where T : Resource
  52. {
  53. if (TypeToKindMap.TryGetValue(typeof(T), out var kind))
  54. return kind;
  55. throw new InvalidOperationException(
  56. $"No kind mapping defined for type {typeof(T).Name}");
  57. }
  58. public static bool CanRunOn<T>(Resource parent) where T : Resource
  59. {
  60. var childKind = GetKind<T>().ToLowerInvariant();
  61. var parentKind = parent.Kind.ToLowerInvariant();
  62. // Service -> System
  63. if (childKind == "service" && parentKind == "system")
  64. return true;
  65. // System -> Hardware
  66. if (childKind == "system" && parent is Hardware.Hardware)
  67. return true;
  68. return false;
  69. }
  70. }