ConnectionsStore.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /* eslint-disable */
  2. /*
  3. Copyright (C) 2017 Cloudbase Solutions SRL
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as
  6. published by the Free Software Foundation, either version 3 of the
  7. License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. import React, { Component, PropTypes } from 'react';
  16. import Reflux from 'reflux';
  17. import ConnectionsActions from '../../actions/ConnectionsActions';
  18. import WizardActions from '../../actions/WizardActions';
  19. import MigrationActions from '../../actions/MigrationActions';
  20. import {defaultLabels} from '../../config'
  21. import Api from '../../components/ApiCaller'
  22. import {servicesUrl, providerType} from '../../config';
  23. class ConnectionsStore extends Reflux.Store
  24. {
  25. connections = [
  26. {
  27. id: "vSphere-Cluster",
  28. name: "vSphere-Cluster",
  29. description: "",
  30. created: new Date(),
  31. cloudName: "vmware",
  32. secretUrl: null,
  33. credentials: {
  34. }
  35. },
  36. {
  37. id: "vSphere-Cluster2",
  38. name: "vSphere-Cluster2",
  39. description: "",
  40. created: new Date(),
  41. cloudName: "vmware",
  42. secretUrl: null,
  43. credentials: {
  44. }
  45. },
  46. {
  47. id: "azure-Cluster",
  48. name: "Azure-Cluster",
  49. description: "",
  50. created: new Date(),
  51. cloudName: "azure",
  52. secretUrl: null,
  53. credentials: {
  54. }
  55. }
  56. ]
  57. constructor()
  58. {
  59. super()
  60. this.listenables = ConnectionsActions
  61. ConnectionsActions.loadConnections()
  62. this.state = {
  63. sourceClouds: [],
  64. targetClouds: [],
  65. allClouds: null,
  66. connections: null
  67. }
  68. }
  69. onLoadProvidersCompleted(response) {
  70. let clouds = []
  71. if (response.data.providers) {
  72. let providers = response.data.providers
  73. for (var provider in providers) {
  74. let cloud = {
  75. name: provider,
  76. credentialSelected: null,
  77. credentials: [],
  78. fields: null,
  79. migration: {
  80. import: providers[provider].types.indexOf(providerType.import_migration) > -1,
  81. export: providers[provider].types.indexOf(providerType.export_migration) > -1
  82. },
  83. replica: {
  84. import: providers[provider].types.indexOf(providerType.import_replica) > -1,
  85. export: providers[provider].types.indexOf(providerType.export_replica) > -1
  86. },
  87. selected: false,
  88. vms: null,
  89. networks: [
  90. {id: "net1", name: "Network-1", migrateNetwork: null, selected: false},
  91. {id: "net2", name: "Network-2", migrateNetwork: null, selected: false},
  92. {id: "net3", name: "Network-3", migrateNetwork: null, selected: false}
  93. ],
  94. }
  95. clouds.push(cloud)
  96. ConnectionsActions.loadProviderType(cloud.name, "endpoint")
  97. }
  98. }
  99. this.setState({allClouds: clouds})
  100. ConnectionsActions.assignConnectionProvider()
  101. }
  102. onLoadProviderType(providerName, type) {
  103. let projectId = Reflux.GlobalState.userStore.currentUser.project.id
  104. Api.sendAjaxRequest({
  105. url: `${servicesUrl.coriolis}/${projectId}/providers/${providerName}/schemas/${providerType[type]}`,
  106. method: "GET"
  107. }).then(response => {
  108. let provider = this.state.allClouds.filter(cloud => cloud.name == providerName)[0]
  109. if (response.data.schemas.connection_info_schema) {
  110. provider[type] = {}
  111. provider[type].fields = ConnectionsStore.processCloud(response.data.schemas.connection_info_schema.oneOf[0])
  112. }
  113. if (response.data.schemas.destination_environment_schema) {
  114. provider[type] = {}
  115. provider[type].fields = ConnectionsStore.processCloud(response.data.schemas.destination_environment_schema.oneOf[0])
  116. }
  117. ConnectionsActions.updateProvider(provider)
  118. }, ConnectionsActions.loadProviders.failed)
  119. .catch(ConnectionsActions.loadProviders.failed);
  120. }
  121. onUpdateProvider(provider) {
  122. let allCLouds = this.state.allClouds
  123. for (var i in allClouds) {
  124. if (allClouds[i].name == provider.name) {
  125. allClouds[i] = provider
  126. }
  127. }
  128. this.setState({ allClouds: allCLouds })
  129. }
  130. onAssignConnectionProvider() {
  131. if (this.state.allClouds == null || this.state.connections == null) {
  132. return false
  133. } else {
  134. let allClouds = this.state.allClouds
  135. for (var i in allClouds) {
  136. allClouds[i].credentials = []
  137. this.state.connections.forEach(connection => {
  138. if (connection.type == allClouds[i].name) {
  139. allClouds[i].credentials.push({id: connection.id, name: connection.name})
  140. }
  141. })
  142. }
  143. this.setState({ allClouds: allClouds })
  144. }
  145. }
  146. onResetSelections()
  147. {
  148. console.log("TODO: Reset Selections")
  149. }
  150. onSetConnection(connection_id)
  151. {
  152. this.state.connections.forEach(connection => {
  153. if (connection.id == connection_id) {
  154. this.setState({connection: connection})
  155. }
  156. }, this)
  157. }
  158. onNewConnectionSuccess(response, data)
  159. {
  160. ConnectionsActions.saveEndpoint(data, response.data.secret_ref)
  161. }
  162. onSaveEndpointSuccess(response) {
  163. let connections = this.state.connections
  164. connections.push(response.data.endpoint)
  165. this.setState({connections: connections})
  166. ConnectionsActions.assignConnectionProvider()
  167. }
  168. onLoadConnectionsCompleted(data) {
  169. let connections = []
  170. if (data.data.endpoints.length) {
  171. data.data.endpoints.forEach(endpoint => {
  172. connections.push(endpoint)
  173. /*let cloudType = endpoint.name.substr(0, endpoint.name.indexOf("::"))
  174. let secretName = endpoint.name.substr(endpoint.name.indexOf("::") + 2)
  175. let secretId = secret.secret_ref.substr(secret.secret_ref.lastIndexOf("/") + 1)
  176. connections.push({
  177. id: secretId,
  178. name: secretName,
  179. description: "",
  180. created: new Date(secret.created),
  181. cloudName: cloudType,
  182. secretUrl: secret.secret_ref,
  183. credentials: {}
  184. })*/
  185. })
  186. }
  187. this.setState({connections: connections})
  188. ConnectionsActions.assignConnectionProvider()
  189. MigrationActions.loadMigrations()
  190. MigrationActions.loadReplicas()
  191. }
  192. onLoadConnectionDetail(connectionId) {
  193. if (this.state.connections) {
  194. let connection = this.state.connections.filter((connection => connection.id == connectionId))[0]
  195. if (connection.connection_info && connection.connection_info.secret_ref) {
  196. console.log("secret_ref", connection.connection_info.secret_ref)
  197. let index = connection.connection_info.secret_ref.lastIndexOf("/")
  198. let uuid = connection.connection_info.secret_ref.substr(index + 1)
  199. Api.sendAjaxRequest({
  200. url: servicesUrl.barbican + "/v1/secrets/" + uuid + "/payload",
  201. method: "GET",
  202. json: false,
  203. headers: {'Accept': 'text/plain'}
  204. }).then(
  205. (response) => ConnectionsActions.loadConnectionDetail.completed(response, connectionId),
  206. ConnectionsActions.loadConnectionDetail.failed)
  207. .catch(ConnectionsActions.loadConnectionDetail.failed);
  208. } else {
  209. let connections = this.state.connections
  210. this.state.connections.forEach(conn => {
  211. if (conn.id == connectionId) {
  212. conn.credentials = conn.connection_info
  213. }
  214. })
  215. this.setState({connections: connections})
  216. }
  217. }
  218. }
  219. onLoadConnectionDetailCompleted(response, uuid) {
  220. let payload = JSON.parse(response.data)
  221. let connections = this.state.connections
  222. this.state.connections.forEach(conn => {
  223. if (conn.id == uuid) {
  224. conn.credentials = payload
  225. }
  226. })
  227. this.setState({connections: connections})
  228. }
  229. onLoadConnectionDetailFailed(response) {
  230. // TODO: when load connections fail
  231. console.log("onLoadConnectionDetailFAILED", response)
  232. }
  233. onDeleteConnectionCompleted(connection) {
  234. console.log("DELETED", connection)
  235. let connections = this.state.connections
  236. console.log(connections.length)
  237. let index = connections.indexOf(connection)
  238. connections.splice(index, 1)
  239. console.log(connections.length)
  240. this.setState({ connections: connections })
  241. ConnectionsActions.assignConnectionProvider()
  242. }
  243. static processCloud(cloudData) {
  244. let fields = []
  245. let sortedFields = [{}, {}]
  246. for (var propName in cloudData.properties) {
  247. let field = {
  248. name: propName,
  249. label: defaultLabels[propName] ? defaultLabels[propName] : propName
  250. }
  251. if (cloudData.properties[propName].default) {
  252. field.default = cloudData.properties[propName].default
  253. }
  254. switch (cloudData.properties[propName].type) {
  255. case "boolean":
  256. field.type = "dropdown"
  257. field.options = [
  258. {label: field.label + ": Yes", value: "true", default: cloudData.properties[propName].default == "true"},
  259. {label: field.label + ": No", value: "false", default: cloudData.properties[propName].default == "false"}
  260. ]
  261. break
  262. case "string":
  263. field.type = "text"
  264. break
  265. case "integer":
  266. if (cloudData.properties[propName].minimum && cloudData.properties[propName].maximum) {
  267. field.type = "dropdown"
  268. field.options = []
  269. for (let i = cloudData.properties[propName].minimum; i<= cloudData.properties[propName].maximum; i++) {
  270. field.options.push({
  271. label: field.label + ": " + i,
  272. value: i,
  273. default: cloudData.properties[propName].default === i},
  274. )
  275. }
  276. } else {
  277. field.type = "text"
  278. }
  279. break
  280. }
  281. if (field.name == 'username') {
  282. field.required = true
  283. sortedFields[0] = field
  284. } else if (field.name == 'password') {
  285. field.type = "password"
  286. field.required = true
  287. sortedFields[1] = field
  288. } else if (cloudData.required.indexOf(field.name) > -1) {
  289. field.required = true
  290. sortedFields.push(field)
  291. } else {
  292. fields.push(field)
  293. }
  294. }
  295. //in case we don't have username and password
  296. if (Object.keys(sortedFields[0]).length === 0 && Object.keys(sortedFields[0]).length === 0) {
  297. sortedFields.shift()
  298. sortedFields.shift()
  299. }
  300. return sortedFields.concat(fields)
  301. }
  302. }
  303. ConnectionsStore.id = "connectionStore"
  304. export default ConnectionsStore;