ScheduleSource.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // @flow
  15. import moment from 'moment'
  16. import Api from '../utils/ApiCaller'
  17. import configLoader from '../utils/Config'
  18. import DateUtils from '../utils/DateUtils'
  19. import type { Schedule } from '../types/Schedule'
  20. class ScheduleSource {
  21. async scheduleSinge(replicaId: string, scheduleData: Schedule): Promise<Schedule> {
  22. let payload = {
  23. schedule: {},
  24. expiration_date: null,
  25. enabled: scheduleData.enabled == null ? false : scheduleData.enabled,
  26. shutdown_instance: scheduleData.shutdown_instances == null ? false : scheduleData.shutdown_instances,
  27. }
  28. if (scheduleData.expiration_date) {
  29. // $FlowIssue
  30. payload.expiration_date = moment(scheduleData.expiration_date).toISOString()
  31. }
  32. if (scheduleData.schedule != null) {
  33. Object.keys(scheduleData.schedule).forEach(prop => {
  34. if (scheduleData.schedule && scheduleData.schedule[prop] != null) {
  35. payload.schedule[prop] = scheduleData.schedule[prop]
  36. }
  37. })
  38. }
  39. let response = await Api.send({
  40. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/replicas/${replicaId}/schedules`,
  41. method: 'POST',
  42. data: payload,
  43. })
  44. return response.data.schedule
  45. }
  46. async scheduleMultiple(replicaId: string, schedules: Schedule[]): Promise<Schedule[]> {
  47. let scheduledSchedules: Schedule[] = await Promise.all(schedules.map(async schedule => {
  48. let scheduledSchedule: Schedule = await this.scheduleSinge(replicaId, schedule)
  49. return scheduledSchedule
  50. }))
  51. return scheduledSchedules
  52. }
  53. async getSchedules(replicaId: string, opts?: { skipLog?: boolean }): Promise<Schedule[]> {
  54. let response = await Api.send({
  55. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/replicas/${replicaId}/schedules`,
  56. skipLog: opts && opts.skipLog,
  57. })
  58. let schedules = [...response.data.schedules]
  59. schedules.forEach(s => {
  60. if (s.expiration_date) {
  61. s.expiration_date = DateUtils.getLocalTime(s.expiration_date)
  62. }
  63. if (s.shutdown_instance) {
  64. s.shutdown_instances = s.shutdown_instance
  65. }
  66. })
  67. schedules.sort((a, b) => moment(a.created_at).diff(b.created_at))
  68. return schedules
  69. }
  70. async addSchedule(replicaId: string, schedule: Schedule): Promise<Schedule> {
  71. let payload = {
  72. schedule: { hour: 0, minute: 0 },
  73. enabled: false,
  74. }
  75. if (schedule && schedule.schedule) {
  76. payload.schedule = { ...schedule.schedule }
  77. }
  78. let response = await Api.send({
  79. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/replicas/${replicaId}/schedules`,
  80. method: 'POST',
  81. data: payload,
  82. })
  83. return response.data.schedule
  84. }
  85. async removeSchedule(replicaId: string, scheduleId: string): Promise<void> {
  86. await Api.send({
  87. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/replicas/${replicaId}/schedules/${scheduleId}`,
  88. method: 'DELETE',
  89. })
  90. }
  91. async updateSchedule(
  92. replicaId: string,
  93. scheduleId: string,
  94. scheduleData: Schedule,
  95. scheduleOldData: ?Schedule,
  96. unsavedData: ?Schedule
  97. ): Promise<Schedule> {
  98. let payload = {}
  99. if (scheduleData.enabled != null) {
  100. payload.enabled = scheduleData.enabled
  101. }
  102. if (scheduleData.shutdown_instances != null) {
  103. payload.shutdown_instance = scheduleData.shutdown_instances
  104. }
  105. if (unsavedData && unsavedData.expiration_date) {
  106. payload.expiration_date = moment(unsavedData.expiration_date).toISOString()
  107. }
  108. if (unsavedData && unsavedData.schedule != null && Object.keys(unsavedData.schedule).length) {
  109. if (scheduleOldData) {
  110. payload.schedule = { ...scheduleOldData.schedule }
  111. }
  112. Object.keys(unsavedData.schedule).forEach(prop => {
  113. if (unsavedData && unsavedData.schedule && unsavedData.schedule[prop] != null) {
  114. payload.schedule[prop] = unsavedData.schedule[prop]
  115. } else {
  116. delete payload.schedule[prop]
  117. }
  118. })
  119. }
  120. let response = await Api.send({
  121. url: `${configLoader.config.servicesUrls.coriolis}/${Api.projectId}/replicas/${replicaId}/schedules/${scheduleId}`,
  122. method: 'PUT',
  123. data: payload,
  124. })
  125. let s = { ...response.data.schedule }
  126. if (s.expiration_date) {
  127. s.expiration_date = DateUtils.getLocalTime(s.expiration_date)
  128. }
  129. if (s.shutdown_instance) {
  130. s.shutdown_instances = s.shutdown_instance
  131. }
  132. return s
  133. }
  134. }
  135. export default new ScheduleSource()