| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- /*
- Copyright (C) 2017 Cloudbase Solutions SRL
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- // @flow
- import moment from 'moment'
- import Api from '../utils/ApiCaller'
- import { servicesUrl } from '../constants'
- import DateUtils from '../utils/DateUtils'
- import type { Schedule } from '../types/Schedule'
- class ScheduleSource {
- static scheduleSinge(replicaId: string, scheduleData: Schedule): Promise<Schedule> {
- let payload = {
- schedule: {},
- expiration_date: null,
- enabled: scheduleData.enabled == null ? false : scheduleData.enabled,
- shutdown_instance: scheduleData.shutdown_instances == null ? false : scheduleData.shutdown_instances,
- }
- if (scheduleData.expiration_date) {
- // $FlowIssue
- payload.expiration_date = moment(scheduleData.expiration_date).toISOString()
- }
- if (scheduleData.schedule != null) {
- Object.keys(scheduleData.schedule).forEach(prop => {
- if (scheduleData.schedule && scheduleData.schedule[prop] != null) {
- payload.schedule[prop] = scheduleData.schedule[prop]
- }
- })
- }
- return Api.send({
- url: `${servicesUrl.coriolis}/${Api.projectId}/replicas/${replicaId}/schedules`,
- method: 'POST',
- data: payload,
- }).then(response => response.data.schedule)
- }
- static scheduleMultiple(replicaId: string, schedules: Schedule[]): Promise<Schedule[]> {
- return Promise.all(schedules.map(schedule => {
- return ScheduleSource.scheduleSinge(replicaId, schedule)
- }))
- }
- static getSchedules(replicaId: string, opts?: { skipLog?: boolean }): Promise<Schedule[]> {
- return Api.send({
- url: `${servicesUrl.coriolis}/${Api.projectId}/replicas/${replicaId}/schedules`,
- skipLog: opts && opts.skipLog,
- }).then(response => {
- let schedules = [...response.data.schedules]
- schedules.forEach(s => {
- if (s.expiration_date) {
- s.expiration_date = DateUtils.getLocalTime(s.expiration_date)
- }
- if (s.shutdown_instance) {
- s.shutdown_instances = s.shutdown_instance
- }
- })
- schedules.sort((a, b) => moment(a.created_at).diff(b.created_at))
- return schedules
- })
- }
- static addSchedule(replicaId: string, schedule: Schedule): Promise<Schedule> {
- let payload = {
- schedule: { hour: 0, minute: 0 },
- enabled: false,
- }
- if (schedule && schedule.schedule) {
- payload.schedule = { ...schedule.schedule }
- }
- return Api.send({
- url: `${servicesUrl.coriolis}/${Api.projectId}/replicas/${replicaId}/schedules`,
- method: 'POST',
- data: payload,
- }).then(response => response.data.schedule)
- }
- static removeSchedule(replicaId: string, scheduleId: string): Promise<void> {
- return Api.send({
- url: `${servicesUrl.coriolis}/${Api.projectId}/replicas/${replicaId}/schedules/${scheduleId}`,
- method: 'DELETE',
- }).then(() => { })
- }
- static updateSchedule(
- replicaId: string,
- scheduleId: string,
- scheduleData: Schedule,
- scheduleOldData: ?Schedule,
- unsavedData: ?Schedule
- ): Promise<Schedule> {
- let payload = {}
- if (scheduleData.enabled != null) {
- payload.enabled = scheduleData.enabled
- }
- if (scheduleData.shutdown_instances != null) {
- payload.shutdown_instance = scheduleData.shutdown_instances
- }
- if (unsavedData && unsavedData.expiration_date) {
- payload.expiration_date = moment(unsavedData.expiration_date).toISOString()
- }
- if (unsavedData && unsavedData.schedule != null && Object.keys(unsavedData.schedule).length) {
- if (scheduleOldData) {
- payload.schedule = { ...scheduleOldData.schedule }
- }
- Object.keys(unsavedData.schedule).forEach(prop => {
- if (unsavedData && unsavedData.schedule && unsavedData.schedule[prop] != null) {
- payload.schedule[prop] = unsavedData.schedule[prop]
- } else {
- delete payload.schedule[prop]
- }
- })
- }
- return Api.send({
- url: `${servicesUrl.coriolis}/${Api.projectId}/replicas/${replicaId}/schedules/${scheduleId}`,
- method: 'PUT',
- data: payload,
- }).then(response => {
- let s = { ...response.data.schedule }
- if (s.expiration_date) {
- s.expiration_date = DateUtils.getLocalTime(s.expiration_date)
- }
- if (s.shutdown_instance) {
- s.shutdown_instances = s.shutdown_instance
- }
- return s
- })
- }
- }
- export default ScheduleSource
|