AzureStore.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 cookie from 'js-cookie'
  17. import AzureSource from '../sources/AzureSource'
  18. import type { Assessment, VmItem, Location } from '../types/Assessment'
  19. import type { NetworkMap } from '../types/Network'
  20. import type { Endpoint } from '../types/Endpoint'
  21. export type LocalData = {
  22. endpoint: Endpoint,
  23. sourceEndpoint: ?Endpoint,
  24. connectionInfo: any,
  25. resourceGroupName: string,
  26. locationName: string,
  27. assessmentName: string,
  28. groupName: string,
  29. projectName: string,
  30. selectedVmSizes: { [string]: string },
  31. selectedVms: string[],
  32. selectedNetworks: NetworkMap[],
  33. [string]: mixed,
  34. }
  35. class AzureLocalStorage {
  36. static loadLocalData(assessmentName: string): ?LocalData {
  37. let localDataArray: LocalData[] = JSON.parse(localStorage.getItem(`assessments-${cookie.get('projectId') || ''}`) || '[]')
  38. return localDataArray.find(a => a.assessmentName === assessmentName)
  39. }
  40. static setLocalData(data: LocalData) {
  41. let localDataArray: LocalData[] = JSON.parse(localStorage.getItem(`assessments-${cookie.get('projectId') || ''}`) || '[]')
  42. let assessmentIndex = localDataArray.findIndex(a => a.assessmentName === data.assessmentName)
  43. if (assessmentIndex > -1) {
  44. localDataArray.splice(assessmentIndex, 1)
  45. }
  46. localDataArray.push(data)
  47. localStorage.setItem(`assessments-${cookie.get('projectId') || ''}`, JSON.stringify(localDataArray))
  48. }
  49. }
  50. class AzureStore {
  51. @observable authenticating: boolean = false
  52. @observable loadingResourceGroups: boolean = false
  53. @observable assessmentResourceGroups: $PropertyType<Assessment, 'group'>[] = []
  54. @observable coriolisResourceGroups: string[] = []
  55. @observable loadingAssessments: boolean = false
  56. @observable loadingAssessmentDetails: boolean = false
  57. @observable assessmentDetails: ?Assessment = null
  58. @observable assessments: Assessment[] = []
  59. @observable loadingAssessedVms: boolean = false
  60. @observable assessedVms: VmItem[] = []
  61. @observable loadingVmSizes: boolean = false
  62. // @observable vmSizes: VmSize[] = []
  63. @observable assessmentsProjectId: string = ''
  64. @observable locations: Location[] = []
  65. @observable localData: ?LocalData = null
  66. @observable vmSizes: string[] = []
  67. @action loadLocalData(assessmentName: string): boolean {
  68. this.localData = AzureLocalStorage.loadLocalData(assessmentName)
  69. return Boolean(this.localData)
  70. }
  71. @action setLocalData(data: LocalData) {
  72. data.selectedVmSizes = data.selectedVmSizes || {}
  73. data.selectedVms = data.selectedVms || []
  74. data.selectedNetworks = data.selectedNetworks || []
  75. this.localData = data
  76. AzureLocalStorage.setLocalData(data)
  77. }
  78. @action updateResourceGroup(resourceGroupName: string) {
  79. if (!this.localData) {
  80. return
  81. }
  82. this.localData.resourceGroupName = resourceGroupName
  83. AzureLocalStorage.setLocalData(this.localData)
  84. }
  85. @action updateNetworkMap(selectedNetworks: NetworkMap[]) {
  86. if (!this.localData) {
  87. return
  88. }
  89. this.localData.selectedNetworks = selectedNetworks
  90. AzureLocalStorage.setLocalData(this.localData)
  91. }
  92. @action updateSourceEndpoint(sourceEndpoint: ?Endpoint) {
  93. if (!this.localData) {
  94. return
  95. }
  96. this.localData.sourceEndpoint = sourceEndpoint
  97. AzureLocalStorage.setLocalData(this.localData)
  98. }
  99. @action updateSelectedVms(selectedVms: string[]) {
  100. if (!this.localData) {
  101. return
  102. }
  103. this.localData.selectedVms = selectedVms
  104. AzureLocalStorage.setLocalData(this.localData)
  105. }
  106. @action updateVmSize(vmId: string, vmSize: string) {
  107. if (!this.localData) {
  108. return
  109. }
  110. this.localData.selectedVmSizes[vmId] = vmSize
  111. if (this.localData) {
  112. AzureLocalStorage.setLocalData(this.localData)
  113. }
  114. }
  115. @action updateVmSizes(vmSizes: { [string]: string }) {
  116. if (!this.localData) {
  117. return
  118. }
  119. this.localData.selectedVmSizes = vmSizes
  120. AzureLocalStorage.setLocalData(this.localData)
  121. }
  122. @action updateLocation(locationName: string) {
  123. if (!this.localData) {
  124. return
  125. }
  126. this.localData.locationName = locationName
  127. AzureLocalStorage.setLocalData(this.localData)
  128. }
  129. @action updateTargetEndpoint(endpoint: Endpoint) {
  130. if (!this.localData) {
  131. return
  132. }
  133. this.localData.endpoint = endpoint
  134. AzureLocalStorage.setLocalData(this.localData)
  135. }
  136. @action authenticate(username: string, password: string): Promise<void> {
  137. this.authenticating = true
  138. return AzureSource.authenticate(username, password).then(() => {
  139. this.authenticating = false
  140. }).catch(() => {
  141. this.authenticating = false
  142. return Promise.reject()
  143. })
  144. }
  145. @action getResourceGroups(subscriptionId: string): Promise<void> {
  146. this.loadingResourceGroups = true
  147. return AzureSource.getResourceGroups(subscriptionId).then((groups: $PropertyType<Assessment, 'group'>[]) => {
  148. this.loadingResourceGroups = false
  149. this.assessmentResourceGroups = groups
  150. }).catch(() => {
  151. this.loadingResourceGroups = false
  152. })
  153. }
  154. @action isLoadedForCurrentProject() {
  155. return this.assessmentsProjectId === (cookie.get('projectId') || 'null')
  156. }
  157. @action getAssessments(
  158. subscriptionId: string,
  159. resourceGroupName: string,
  160. projectId: string,
  161. options?: { backgroundLoading: boolean, skipLog?: boolean },
  162. ): Promise<void> {
  163. let cookieProjectId = cookie.get('projectId') || 'null'
  164. if (projectId !== cookieProjectId) {
  165. return Promise.resolve()
  166. }
  167. if (!options || !options.backgroundLoading) {
  168. this.loadingAssessments = true
  169. }
  170. return AzureSource.getAssessments(subscriptionId, resourceGroupName, options && options.skipLog).then((assessments: Assessment[]) => {
  171. this.loadingAssessments = false
  172. cookieProjectId = cookie.get('projectId') || 'null'
  173. if (projectId !== cookieProjectId) {
  174. return
  175. }
  176. this.assessmentsProjectId = cookieProjectId
  177. this.assessments = assessments
  178. })
  179. }
  180. @action getAssessmentDetails(info: Assessment): Promise<void> {
  181. this.loadingAssessmentDetails = true
  182. return AzureSource.getAssessmentDetails(info).then((assessment: Assessment) => {
  183. this.loadingAssessmentDetails = false
  184. this.assessmentDetails = assessment
  185. }).catch(() => {
  186. this.loadingAssessmentDetails = false
  187. })
  188. }
  189. @action saveLocations(locations: Location[]) {
  190. this.locations = locations
  191. }
  192. @action saveResourceGroups(resourceGroups: string[]) {
  193. this.coriolisResourceGroups = resourceGroups
  194. }
  195. @action saveTargetVmSizes(targetVmSizes: string[]) {
  196. this.vmSizes = targetVmSizes
  197. }
  198. @action setLocation(location: string) {
  199. if (!this.localData || this.localData.locationName) {
  200. return
  201. }
  202. this.localData.locationName = location
  203. }
  204. @action clearAssessmentDetails() {
  205. this.assessmentDetails = null
  206. this.assessedVms = []
  207. }
  208. @action getAssessedVms(info: Assessment): Promise<void> {
  209. this.loadingAssessedVms = true
  210. return AzureSource.getAssessedVms(info).then((vms: VmItem[]) => {
  211. this.loadingAssessedVms = false
  212. this.assessedVms = vms
  213. }).catch(() => {
  214. this.loadingAssessedVms = false
  215. })
  216. }
  217. // @action getVmSizes(info: Assessment): Promise<void> {
  218. // this.loadingVmSizes = true
  219. // return AzureSource.getVmSizes(info).then((sizes: VmSize[]) => {
  220. // this.loadingVmSizes = false
  221. // this.vmSizes = sizes
  222. // }).catch(() => {
  223. // this.loadingVmSizes = false
  224. // })
  225. // }
  226. @action clearAssessedVms() {
  227. this.assessedVms = []
  228. }
  229. @action clearAssessments() {
  230. this.assessmentResourceGroups = []
  231. this.assessments = []
  232. this.locations = []
  233. this.coriolisResourceGroups = []
  234. }
  235. }
  236. export default new AzureStore()