ReplicaSource.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 type { MainItem } from '../types/MainItem'
  20. import type { Execution } from '../types/Execution'
  21. import type { Field } from '../types/Field'
  22. class ReplicaSourceUtils {
  23. static filterDeletedExecutionsInReplicas(replicas) {
  24. return replicas.map(replica => {
  25. replica.executions = ReplicaSourceUtils.filterDeletedExecutions(replica.executions)
  26. return replica
  27. })
  28. }
  29. static filterDeletedExecutions(executions) {
  30. if (!executions || !executions.length) {
  31. return executions
  32. }
  33. return executions.filter(execution => execution.deleted_at === null || execution.deleted_at === undefined)
  34. }
  35. static sortReplicas(replicas) {
  36. if (replicas.length === 1) {
  37. ReplicaSourceUtils.sortExecutions(replicas[0].executions)
  38. return
  39. }
  40. replicas.sort((a, b) => {
  41. ReplicaSourceUtils.sortExecutions(a.executions)
  42. ReplicaSourceUtils.sortExecutions(b.executions)
  43. let aLastExecution = a.executions && a.executions.length ? a.executions[a.executions.length - 1] : null
  44. let bLastExecution = b.executions && b.executions.length ? b.executions[b.executions.length - 1] : null
  45. let aLastTime = aLastExecution ? aLastExecution.updated_at || aLastExecution.created_at : null
  46. let bLastTime = bLastExecution ? bLastExecution.updated_at || bLastExecution.created_at : null
  47. let aTime = aLastTime || a.updated_at || a.created_at
  48. let bTime = bLastTime || b.updated_at || b.created_at
  49. return moment(bTime).diff(moment(aTime))
  50. })
  51. }
  52. static sortExecutions(executions) {
  53. if (executions) {
  54. executions.sort((a, b) => a.number - b.number)
  55. }
  56. }
  57. static sortExecutionsAndTaskUpdates(executions) {
  58. this.sortExecutions(executions)
  59. executions.forEach(execution => {
  60. this.sortTaskUpdates(execution)
  61. })
  62. }
  63. static sortTaskUpdates(execution) {
  64. if (execution.tasks) {
  65. execution.tasks.forEach(task => {
  66. if (task.progress_updates) {
  67. task.progress_updates.sort((a, b) => moment(a.created_at).isBefore(moment(b.created_at)))
  68. }
  69. })
  70. }
  71. }
  72. }
  73. class ReplicaSource {
  74. static getReplicas(): Promise<MainItem[]> {
  75. return new Promise((resolve, reject) => {
  76. let projectId = cookie.get('projectId')
  77. Api.get(`${servicesUrl.coriolis}/${projectId || 'null'}/replicas/detail`).then(response => {
  78. let replicas = response.data.replicas
  79. replicas = ReplicaSourceUtils.filterDeletedExecutionsInReplicas(replicas)
  80. ReplicaSourceUtils.sortReplicas(replicas)
  81. resolve(replicas)
  82. }).catch(reject)
  83. })
  84. }
  85. static getReplicaExecutions(replicaId: string): Promise<Execution[]> {
  86. return new Promise((resolve, reject) => {
  87. let projectId = cookie.get('projectId')
  88. Api.get(`${servicesUrl.coriolis}/${projectId || 'null'}/replicas/${replicaId}/executions/detail`).then((response) => {
  89. let executions = response.data.executions
  90. ReplicaSourceUtils.sortExecutionsAndTaskUpdates(executions)
  91. resolve(executions)
  92. }).catch(reject)
  93. })
  94. }
  95. static getReplica(replicaId: string): Promise<MainItem> {
  96. return new Promise((resolve, reject) => {
  97. let projectId = cookie.get('projectId')
  98. Api.get(`${servicesUrl.coriolis}/${projectId || 'null'}/replicas/${replicaId}`).then(response => {
  99. let replica = response.data.replica
  100. replica.executions = ReplicaSourceUtils.filterDeletedExecutions(replica.executions)
  101. ReplicaSourceUtils.sortExecutions(replica.executions)
  102. resolve(replica)
  103. }).catch(reject)
  104. })
  105. }
  106. static execute(replicaId: string, fields?: Field[]): Promise<Execution> {
  107. return new Promise((resolve, reject) => {
  108. let payload = { execution: { shutdown_instances: false } }
  109. if (fields) {
  110. fields.forEach(f => {
  111. payload.execution[f.name] = f.value || false
  112. })
  113. }
  114. let projectId = cookie.get('projectId')
  115. Api.send({
  116. url: `${servicesUrl.coriolis}/${projectId || 'null'}/replicas/${replicaId}/executions`,
  117. method: 'POST',
  118. data: payload,
  119. }).then((response) => {
  120. let execution = response.data.execution
  121. ReplicaSourceUtils.sortTaskUpdates(execution)
  122. resolve(execution)
  123. }).catch(reject)
  124. })
  125. }
  126. static cancelExecution(replicaId: string, executionId: string): Promise<string> {
  127. return new Promise((resolve, reject) => {
  128. let projectId = cookie.get('projectId')
  129. Api.send({
  130. url: `${servicesUrl.coriolis}/${projectId || 'null'}/replicas/${replicaId}/executions/${executionId}/actions`,
  131. method: 'POST',
  132. data: { cancel: null },
  133. }).then(() => {
  134. resolve(replicaId)
  135. }).catch(reject)
  136. })
  137. }
  138. static deleteExecution(replicaId: string, executionId: string): Promise<string> {
  139. return new Promise((resolve, reject) => {
  140. let projectId = cookie.get('projectId')
  141. Api.send({
  142. url: `${servicesUrl.coriolis}/${projectId || 'null'}/replicas/${replicaId}/executions/${executionId}`,
  143. method: 'DELETE',
  144. }).then(() => {
  145. resolve(replicaId)
  146. }).catch(reject)
  147. })
  148. }
  149. static delete(replicaId: string): Promise<string> {
  150. return new Promise((resolve, reject) => {
  151. let projectId = cookie.get('projectId')
  152. Api.send({
  153. url: `${servicesUrl.coriolis}/${projectId || 'null'}/replicas/${replicaId}`,
  154. method: 'DELETE',
  155. }).then(() => { resolve(replicaId) }, reject).catch(reject)
  156. })
  157. }
  158. static deleteDisks(replicaId: string): Promise<Execution> {
  159. return new Promise((resolve, reject) => {
  160. let projectId = cookie.get('projectId')
  161. Api.send({
  162. url: `${servicesUrl.coriolis}/${projectId || 'null'}/replicas/${replicaId}/actions`,
  163. method: 'POST',
  164. data: { 'delete-disks': null },
  165. }).then(response => {
  166. resolve(response.data.execution)
  167. }).catch(reject)
  168. })
  169. }
  170. }
  171. export default ReplicaSource