ScheduleSource.js 4.9 KB

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