ScheduleSource.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Copyright (C) 2017 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 Api from "@src/utils/ApiCaller";
  15. import configLoader from "@src/utils/Config";
  16. import DateUtils from "@src/utils/DateUtils";
  17. import type { Schedule } from "@src/@types/Schedule";
  18. class ScheduleSource {
  19. async scheduleSinge(
  20. replicaId: string,
  21. scheduleData: Schedule
  22. ): Promise<Schedule> {
  23. const payload: any = {
  24. schedule: {},
  25. expiration_date: null,
  26. enabled: scheduleData.enabled == null ? false : scheduleData.enabled,
  27. shutdown_instance:
  28. scheduleData.shutdown_instances == null
  29. ? false
  30. : scheduleData.shutdown_instances,
  31. };
  32. if (scheduleData.expiration_date) {
  33. payload.expiration_date = new Date(
  34. scheduleData.expiration_date
  35. ).toISOString();
  36. }
  37. if (scheduleData.schedule != null) {
  38. Object.keys(scheduleData.schedule).forEach(prop => {
  39. const scheduleDataAny: any = scheduleData;
  40. if (
  41. scheduleDataAny.schedule &&
  42. scheduleDataAny.schedule[prop] != null
  43. ) {
  44. payload.schedule[prop] = scheduleDataAny.schedule[prop];
  45. }
  46. });
  47. }
  48. const response = await Api.send({
  49. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/replicas/${replicaId}/schedules`,
  50. method: "POST",
  51. data: payload,
  52. });
  53. return response.data.schedule;
  54. }
  55. async scheduleMultiple(
  56. replicaId: string,
  57. schedules: Schedule[]
  58. ): Promise<Schedule[]> {
  59. const scheduledSchedules: Schedule[] = await Promise.all(
  60. schedules.map(async schedule => {
  61. const scheduledSchedule: Schedule = await this.scheduleSinge(
  62. replicaId,
  63. schedule
  64. );
  65. return scheduledSchedule;
  66. })
  67. );
  68. return scheduledSchedules;
  69. }
  70. async getSchedules(
  71. replicaId: string,
  72. opts?: { skipLog?: boolean }
  73. ): Promise<Schedule[]> {
  74. const response = await Api.send({
  75. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/replicas/${replicaId}/schedules`,
  76. skipLog: opts && opts.skipLog,
  77. });
  78. const schedules: any[] = response.data.schedules.map((s: any) => ({
  79. ...s,
  80. expiration_date: s.expiration_date
  81. ? DateUtils.getUtcDate(s.expiration_date).toJSDate()
  82. : undefined,
  83. shutdown_instances:
  84. s.shutdown_instance != null ? s.shutdown_instance : undefined,
  85. }));
  86. schedules.sort(
  87. (a, b) =>
  88. new Date(a.created_at).getTime() - new Date(b.created_at).getTime()
  89. );
  90. return schedules;
  91. }
  92. async addSchedule(replicaId: string, schedule: Schedule): Promise<Schedule> {
  93. const payload: any = {
  94. schedule: { hour: 0, minute: 0 },
  95. enabled: false,
  96. };
  97. if (schedule && schedule.schedule) {
  98. payload.schedule = { ...schedule.schedule };
  99. }
  100. const response = await Api.send({
  101. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/replicas/${replicaId}/schedules`,
  102. method: "POST",
  103. data: payload,
  104. });
  105. return response.data.schedule;
  106. }
  107. async removeSchedule(replicaId: string, scheduleId: string): Promise<void> {
  108. await Api.send({
  109. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/replicas/${replicaId}/schedules/${scheduleId}`,
  110. method: "DELETE",
  111. });
  112. }
  113. async updateSchedule(opts: {
  114. replicaId: string;
  115. scheduleId: string;
  116. scheduleData: Schedule;
  117. scheduleOldData: Schedule | null | undefined;
  118. unsavedData: Schedule | null | undefined;
  119. }): Promise<Schedule> {
  120. const {
  121. replicaId,
  122. scheduleId,
  123. scheduleData,
  124. scheduleOldData,
  125. unsavedData,
  126. } = opts;
  127. const payload: any = {};
  128. if (scheduleData.enabled != null) {
  129. payload.enabled = scheduleData.enabled;
  130. }
  131. if (scheduleData.shutdown_instances != null) {
  132. payload.shutdown_instance = scheduleData.shutdown_instances;
  133. }
  134. if (unsavedData?.expiration_date) {
  135. payload.expiration_date = new Date(
  136. unsavedData.expiration_date
  137. ).toISOString();
  138. }
  139. if (
  140. unsavedData &&
  141. unsavedData.schedule != null &&
  142. Object.keys(unsavedData.schedule).length
  143. ) {
  144. if (scheduleOldData) {
  145. payload.schedule = { ...scheduleOldData.schedule };
  146. }
  147. Object.keys(unsavedData.schedule).forEach(prop => {
  148. const unsavedDataAny: any = unsavedData;
  149. if (unsavedDataAny?.schedule && unsavedDataAny.schedule[prop] != null) {
  150. payload.schedule[prop] = unsavedDataAny.schedule[prop];
  151. } else {
  152. delete payload.schedule[prop];
  153. }
  154. });
  155. }
  156. const response = await Api.send({
  157. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/replicas/${replicaId}/schedules/${scheduleId}`,
  158. method: "PUT",
  159. data: payload,
  160. });
  161. const s = { ...response.data.schedule };
  162. if (s.expiration_date) {
  163. s.expiration_date = DateUtils.getUtcDate(s.expiration_date);
  164. }
  165. if (s.shutdown_instance) {
  166. s.shutdown_instances = s.shutdown_instance;
  167. }
  168. return s;
  169. }
  170. }
  171. export default new ScheduleSource();