EndpointStore.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 { observable, action } from 'mobx'
  16. import type { Endpoint, Validation, StorageBackend } from '../types/Endpoint'
  17. import notificationStore from './NotificationStore'
  18. import EndpointSource from '../sources/EndpointSource'
  19. export const passwordFields = ['password', 'private_key_passphrase']
  20. const updateEndpoint = (endpoint, endpoints) => endpoints.map(e => {
  21. if (e.id === endpoint.id) {
  22. return { ...endpoint }
  23. }
  24. return { ...e }
  25. })
  26. class EndpointStore {
  27. @observable endpoints: Endpoint[] = []
  28. @observable loading = false
  29. @observable loading = false
  30. @observable connectionInfo: ?$PropertyType<Endpoint, 'connection_info'> = null
  31. @observable connectionsInfo: Endpoint[] = []
  32. @observable validation: ?Validation = null
  33. @observable validating = false
  34. @observable updating = false
  35. @observable adding = false
  36. @observable connectionInfoLoading = false
  37. @observable connectionsInfoLoading = false
  38. @observable storageBackends: StorageBackend[] = []
  39. @observable storageLoading: boolean = false
  40. @action getEndpoints(options?: { showLoading: boolean }) {
  41. if (options && options.showLoading) {
  42. this.loading = true
  43. }
  44. return EndpointSource.getEndpoints().then(endpoints => {
  45. this.endpoints = endpoints
  46. this.loading = false
  47. }).catch(() => {
  48. this.loading = false
  49. })
  50. }
  51. @action delete(endpoint: Endpoint) {
  52. return EndpointSource.delete(endpoint).then(() => {
  53. this.endpoints = this.endpoints.filter(e => e.id !== endpoint.id)
  54. })
  55. }
  56. @action getConnectionInfo(endpoint: Endpoint) {
  57. this.connectionInfoLoading = true
  58. return EndpointSource.getConnectionInfo(endpoint).then(connectionInfo => {
  59. this.setConnectionInfo(connectionInfo)
  60. }).catch(() => {
  61. this.connectionInfoLoading = false
  62. return Promise.reject()
  63. })
  64. }
  65. @action getConnectionsInfo(endpointsData: Endpoint[]): Promise<void> {
  66. this.connectionsInfoLoading = true
  67. return EndpointSource.getConnectionsInfo(endpointsData).then(endpoints => {
  68. this.connectionsInfoLoading = false
  69. this.connectionsInfo = endpoints
  70. }).catch(() => {
  71. this.connectionsInfoLoading = false
  72. })
  73. }
  74. @action duplicate(opts: {
  75. shouldSwitchProject: boolean,
  76. onSwitchProject: () => Promise<void>,
  77. endpoints: Endpoint[],
  78. }): Promise<void> {
  79. let endpoints = []
  80. return Promise.all(opts.endpoints.map(endpoint => {
  81. return EndpointSource.getConnectionInfo(endpoint).then(connectionInfo => {
  82. endpoints.push({
  83. ...endpoint,
  84. connection_info: connectionInfo,
  85. name: `${endpoint.name}${!opts.shouldSwitchProject ? ' (copy)' : ''}`,
  86. })
  87. })
  88. })).then(() => {
  89. if (opts.shouldSwitchProject) {
  90. return opts.onSwitchProject()
  91. }
  92. return Promise.resolve()
  93. }).then(() => {
  94. return Promise.all(endpoints.map(endpoint => {
  95. return EndpointSource.add(endpoint, true)
  96. }).map((p: Promise<any>) => p.catch(e => e)))
  97. .then((results: (Endpoint | { status: string, data?: { description: string } })[]) => {
  98. let internalServerErrors = results.filter(r => r.status && r.status === 500)
  99. if (internalServerErrors.length > 0) {
  100. notificationStore.alert(`There was a problem duplicating ${internalServerErrors.length} endpoint${internalServerErrors.length > 1 ? 's' : ''}`, 'error')
  101. }
  102. let forbiddenErrors = results.filter(r => r.status && r.status === 403)
  103. if (forbiddenErrors.length > 0 && forbiddenErrors[0].data && forbiddenErrors[0].data.description) {
  104. notificationStore.alert(String(forbiddenErrors[0].data.description), 'error')
  105. }
  106. })
  107. }).catch(e => {
  108. if (e.data && e.data.description) {
  109. notificationStore.alert(e.data.description, 'error')
  110. }
  111. })
  112. }
  113. @action setConnectionInfo(connectionInfo: $PropertyType<Endpoint, 'connection_info'>) {
  114. this.connectionInfo = connectionInfo
  115. this.connectionInfoLoading = false
  116. }
  117. @action validate(endpoint: Endpoint) {
  118. this.validating = true
  119. return EndpointSource.validate(endpoint).then(validation => {
  120. this.validation = validation
  121. this.validating = false
  122. }).catch(() => {
  123. this.validating = false
  124. this.validation = { valid: false, message: '' }
  125. })
  126. }
  127. @action clearValidation() {
  128. this.validating = false
  129. this.validation = null
  130. }
  131. @action update(endpoint: Endpoint) {
  132. this.endpoints = updateEndpoint(endpoint, this.endpoints)
  133. this.connectionInfo = { ...endpoint.connection_info }
  134. this.updating = true
  135. return EndpointSource.update(endpoint).then(updatedEndpoint => {
  136. this.endpoints = updateEndpoint(updatedEndpoint, this.endpoints)
  137. this.connectionInfo = { ...updatedEndpoint.connection_info }
  138. this.updating = false
  139. })
  140. }
  141. @action clearConnectionInfo() {
  142. this.connectionInfo = null
  143. }
  144. @action add(endpoint: Endpoint) {
  145. this.adding = true
  146. return EndpointSource.add(endpoint).then(addedEndpoint => {
  147. this.endpoints = [
  148. addedEndpoint,
  149. ...this.endpoints,
  150. ]
  151. this.connectionInfo = addedEndpoint.connection_info
  152. this.adding = false
  153. }).catch(() => {
  154. this.adding = false
  155. })
  156. }
  157. @action loadStorage(endpointId: string, data: any): Promise<void> {
  158. this.storageBackends = []
  159. this.storageLoading = true
  160. return EndpointSource.loadStorage(endpointId, data).then(storage => {
  161. this.storageBackends = storage.storage_backends
  162. this.storageLoading = false
  163. }).catch(ex => {
  164. this.storageLoading = false
  165. throw ex
  166. })
  167. }
  168. }
  169. export default new EndpointStore()