MetalHubSource.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright (C) 202 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import { Endpoint } from "@src/@types/Endpoint";
  15. import { MetalHubServer } from "@src/@types/MetalHub";
  16. import EndpointSource from "@src/sources/EndpointSource";
  17. import apiCaller from "@src/utils/ApiCaller";
  18. import configLoader from "@src/utils/Config";
  19. class MetalHubSource {
  20. private _endpoint: Endpoint | null = null;
  21. async getMetalHubEndpoint(): Promise<Endpoint> {
  22. if (this._endpoint) {
  23. return this._endpoint;
  24. }
  25. const endpoints = await EndpointSource.getEndpoints(true);
  26. const metalHubEndpointName = configLoader.config.bareMetalEndpointName;
  27. const metalHubEndpoint = endpoints.find(
  28. endpoint => endpoint.name === metalHubEndpointName
  29. );
  30. if (!metalHubEndpoint) {
  31. throw new Error(
  32. `Could not find endpoint '${metalHubEndpointName}'. The endpoint name was configured in the config file and is needed in order to communicate with the Coriolis Metal Hub service.`
  33. );
  34. }
  35. return metalHubEndpoint;
  36. }
  37. async getServers(skipLog?: boolean): Promise<MetalHubServer[]> {
  38. const response = await apiCaller.send({
  39. url: `${configLoader.config.servicesUrls.metalhub}/servers`,
  40. skipLog,
  41. quietError: true,
  42. });
  43. const servers: MetalHubServer[] = response.data;
  44. servers.sort((a, b) => {
  45. if (new Date(a.updated_at) > new Date(b.updated_at)) {
  46. return -1;
  47. }
  48. if (new Date(a.updated_at) < new Date(b.updated_at)) {
  49. return 1;
  50. }
  51. return 0;
  52. });
  53. return servers;
  54. }
  55. async getServerDetails(serverId: number): Promise<MetalHubServer> {
  56. const response = await apiCaller.send({
  57. url: `${configLoader.config.servicesUrls.metalhub}/servers/${serverId}`,
  58. });
  59. return response.data;
  60. }
  61. async loadFingerprint(): Promise<string> {
  62. return (
  63. await apiCaller.send({
  64. url: "/proxy/metal-hub/fingerprint",
  65. quietError: true,
  66. })
  67. ).data;
  68. }
  69. async addServer(apiEndpoint: string): Promise<MetalHubServer> {
  70. const response = await apiCaller.send({
  71. url: `${configLoader.config.servicesUrls.metalhub}/servers`,
  72. method: "POST",
  73. data: { api_endpoint: apiEndpoint },
  74. });
  75. return response.data;
  76. }
  77. async deleteServer(serverId: number): Promise<void> {
  78. const response = await apiCaller.send({
  79. url: `${configLoader.config.servicesUrls.metalhub}/servers/${serverId}`,
  80. method: "DELETE",
  81. });
  82. return response.data;
  83. }
  84. async patchServer(
  85. serverId: number,
  86. apiEndpoint: string
  87. ): Promise<MetalHubServer> {
  88. const response = await apiCaller.send({
  89. url: `${configLoader.config.servicesUrls.metalhub}/servers/${serverId}`,
  90. method: "PUT",
  91. data: { api_endpoint: apiEndpoint },
  92. });
  93. return response.data;
  94. }
  95. async refreshServer(serverId: number): Promise<MetalHubServer> {
  96. const response = await apiCaller.send({
  97. url: `${configLoader.config.servicesUrls.metalhub}/servers/${serverId}/refresh`,
  98. method: "GET",
  99. });
  100. return response.data;
  101. }
  102. }
  103. export default new MetalHubSource();