ScheduleSource.js 5.2 KB

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