ScheduleSource.js 5.5 KB

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