ScheduleSource.ts 5.7 KB

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