MinionPoolsPage.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. Copyright (C) 2020 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. import React from 'react'
  15. import styled from 'styled-components'
  16. import { observer } from 'mobx-react'
  17. import { RouteComponentProps } from 'react-router-dom'
  18. import Modal from '@src/components/ui/Modal'
  19. import MainTemplate from '@src/components/modules/TemplateModule/MainTemplate'
  20. import Navigation from '@src/components/modules/NavigationModule/Navigation'
  21. import FilterList from '@src/components/ui/Lists/FilterList'
  22. import PageHeader from '@src/components/smart/PageHeader'
  23. import type { DropdownAction } from '@src/components/ui/Dropdowns/ActionDropdown'
  24. import projectStore from '@src/stores/ProjectStore'
  25. import configLoader from '@src/utils/Config'
  26. import { MinionPool } from '@src/@types/MinionPool'
  27. import providerStore from '@src/stores/ProviderStore'
  28. import endpointStore from '@src/stores/EndpointStore'
  29. import minionPoolStore from '@src/stores/MinionPoolStore'
  30. import { Endpoint } from '@src/@types/Endpoint'
  31. import MinionEndpointModal from '@src/components/modules/MinionModule/MinionEndpointModal'
  32. import MinionPoolModal from '@src/components/modules/MinionModule/MinionPoolModal'
  33. import MinionPoolListItem from '@src/components/modules/MinionModule/MinionPoolListItem'
  34. import { ThemePalette } from '@src/components/Theme'
  35. import AlertModal from '@src/components/ui/AlertModal'
  36. import MinionPoolConfirmationModal from '@src/components/modules/MinionModule/MinionPoolConfirmationModal'
  37. import notificationStore from '@src/stores/NotificationStore'
  38. import ObjectUtils from '@src/utils/ObjectUtils'
  39. import emptyListImage from './images/minion-pool-empty-list.svg'
  40. const Wrapper = styled.div<any>``
  41. type State = {
  42. modalIsOpen: boolean,
  43. selectedMinionPools: MinionPool[],
  44. showChooseMinionEndpointModal: boolean,
  45. showMinionPoolModal: boolean,
  46. selectedMinionPoolEndpoint: Endpoint | null
  47. showDeletePoolsModal: boolean,
  48. showDeallocateConfirmation: boolean,
  49. selectedMinionPoolPlatform: 'source' | 'destination'
  50. }
  51. @observer
  52. class MinionPoolsPage extends React.Component<RouteComponentProps, State> {
  53. state: State = {
  54. modalIsOpen: false,
  55. selectedMinionPools: [],
  56. showChooseMinionEndpointModal: false,
  57. selectedMinionPoolEndpoint: null,
  58. showMinionPoolModal: false,
  59. showDeletePoolsModal: false,
  60. showDeallocateConfirmation: false,
  61. selectedMinionPoolPlatform: 'source',
  62. }
  63. pollTimeout: number = 0
  64. stopPolling: boolean = false
  65. componentDidMount() {
  66. document.title = 'Coriolis Minion Pools'
  67. projectStore.getProjects()
  68. endpointStore.getEndpoints()
  69. this.stopPolling = false
  70. this.pollData(minionPoolStore.minionPools.length === 0)
  71. }
  72. componentWillUnmount() {
  73. clearTimeout(this.pollTimeout)
  74. this.stopPolling = true
  75. }
  76. getFilterItems() {
  77. return [
  78. { label: 'All', value: 'all' },
  79. { label: 'Allocated', value: 'ALLOCATED' },
  80. { label: 'Allocating', value: 'ALLOCATING_MACHINES' },
  81. { label: 'Deallocated', value: 'DEALLOCATED' },
  82. { label: 'Deallocating', value: 'DEALLOCATING_MACHINES' },
  83. { label: 'Error', value: 'ERROR' },
  84. ]
  85. }
  86. getMinionsThatCanBe(action: 'allocated' | 'deallocated' | 'refreshed' | 'deleted'): MinionPool[] {
  87. const minions = this.state.selectedMinionPools
  88. switch (action) {
  89. case 'allocated':
  90. return minions.filter(minion => minion.status === 'DEALLOCATED')
  91. case 'deallocated':
  92. return minions.filter(minion => minion.status === 'ALLOCATED' || minion.status === 'ERROR')
  93. case 'refreshed':
  94. return minions.filter(minion => minion.status === 'ALLOCATED')
  95. default:
  96. return minions.filter(minion => minion.status === 'DEALLOCATED' || minion.status === 'ERROR')
  97. }
  98. }
  99. getEndpoint(endpointId: string) {
  100. return endpointStore.endpoints.find(endpoint => endpoint.id === endpointId)
  101. }
  102. async pollData(showLoading: boolean) {
  103. if (this.state.modalIsOpen || this.stopPolling) {
  104. return
  105. }
  106. await Promise.all([
  107. minionPoolStore.loadMinionPools({ showLoading }),
  108. ])
  109. this.pollTimeout = window.setTimeout(() => {
  110. this.pollData(false)
  111. }, configLoader.config.requestPollTimeout)
  112. }
  113. searchText(item: MinionPool, text?: string | null) {
  114. const result = false
  115. if (item.name.toLowerCase().indexOf(text || '') > -1) {
  116. return true
  117. }
  118. return result
  119. }
  120. itemFilterFunction(item: MinionPool, filterStatus?: string | null, filterText?: string) {
  121. if ((filterStatus !== 'all' && item.status !== filterStatus)
  122. || !this.searchText(item, filterText)
  123. ) {
  124. return false
  125. }
  126. return true
  127. }
  128. deleteSelectedMinionPools() {
  129. const pools = this.getMinionsThatCanBe('deleted')
  130. pools.forEach(pool => {
  131. minionPoolStore.deleteMinionPool(pool.id)
  132. })
  133. this.setState({ showDeletePoolsModal: false })
  134. }
  135. handleProjectChange() {
  136. projectStore.getProjects()
  137. endpointStore.getEndpoints()
  138. minionPoolStore.loadMinionPools({ showLoading: true })
  139. }
  140. handleItemClick(item: MinionPool) {
  141. this.props.history.push(`/minion-pools/${item.id}`)
  142. }
  143. handleReloadButtonClick() {
  144. projectStore.getProjects()
  145. endpointStore.getEndpoints()
  146. minionPoolStore.loadMinionPools({ showLoading: true })
  147. }
  148. handleEmptyListButtonClick() {
  149. providerStore.loadProviders()
  150. endpointStore.getEndpoints({ showLoading: true })
  151. this.setState({ showChooseMinionEndpointModal: true, modalIsOpen: true })
  152. }
  153. handleCloseChooseMinionPoolEndpointModal() {
  154. this.setState({
  155. showChooseMinionEndpointModal: false,
  156. modalIsOpen: false,
  157. }, () => { this.pollData(false) })
  158. }
  159. handleBackMinionPoolModal() {
  160. this.setState({
  161. showChooseMinionEndpointModal: true,
  162. showMinionPoolModal: false,
  163. })
  164. }
  165. handleCloseMinionPoolModalRequest() {
  166. this.setState({
  167. showMinionPoolModal: false,
  168. modalIsOpen: false,
  169. }, () => { this.pollData(false) })
  170. }
  171. handleChooseMinionPoolSelectEndpoint(selectedMinionPoolEndpoint: Endpoint, platform: 'source' | 'destination') {
  172. this.setState({
  173. showChooseMinionEndpointModal: false,
  174. showMinionPoolModal: true,
  175. selectedMinionPoolEndpoint,
  176. selectedMinionPoolPlatform: platform,
  177. })
  178. }
  179. handleModalOpen() {
  180. this.setState({ modalIsOpen: true })
  181. }
  182. handleModalClose() {
  183. this.setState({ modalIsOpen: false }, () => {
  184. this.pollData(false)
  185. })
  186. }
  187. async handleAllocate() {
  188. const pools = this.getMinionsThatCanBe('allocated')
  189. const plural = pools.length === 1 ? '' : 's'
  190. notificationStore.alert(`Allocating minion pool${plural}...`)
  191. await Promise.all(pools.map(minionPool => minionPoolStore.runAction(minionPool.id, 'allocate')))
  192. await minionPoolStore.loadMinionPools()
  193. }
  194. handleDeallocate() {
  195. this.setState({
  196. showDeallocateConfirmation: true,
  197. })
  198. }
  199. async handleDeallocateConfirmation(force: boolean) {
  200. this.setState({
  201. showDeallocateConfirmation: false,
  202. })
  203. const pools = this.getMinionsThatCanBe('deallocated')
  204. const plural = pools.length === 1 ? '' : 's'
  205. notificationStore.alert(`Deallocating minion pool${plural}...`)
  206. await Promise.all(pools.map(minionPool => minionPoolStore.runAction(minionPool.id, 'deallocate', { force })))
  207. await minionPoolStore.loadMinionPools()
  208. }
  209. async handleRefreshAction() {
  210. const pools = this.getMinionsThatCanBe('refreshed')
  211. const plural = pools.length === 1 ? '' : 's'
  212. notificationStore.alert(`Refreshing minion pool${plural}...`)
  213. await Promise.all(pools.map(minionPool => minionPoolStore.runAction(minionPool.id, 'refresh')))
  214. await minionPoolStore.loadMinionPools()
  215. }
  216. render() {
  217. const canBeAllocated = this.getMinionsThatCanBe('allocated').length > 0
  218. const canBeDeallocated = this.getMinionsThatCanBe('deallocated').length > 0
  219. const canBeRefreshed = this.getMinionsThatCanBe('refreshed').length > 0
  220. const canBeDeleted = this.getMinionsThatCanBe('deleted').length > 0
  221. const BulkActions: DropdownAction[] = [
  222. {
  223. label: 'Allocate',
  224. color: ThemePalette.primary,
  225. action: () => { this.handleAllocate() },
  226. disabled: !canBeAllocated,
  227. title: !canBeAllocated ? 'The minion pool should be deallocated' : '',
  228. },
  229. {
  230. label: 'Deallocate',
  231. action: () => {
  232. this.handleDeallocate()
  233. },
  234. disabled: !canBeDeallocated,
  235. title: !canBeDeallocated ? 'The minion pool should be allocated' : '',
  236. },
  237. {
  238. label: 'Refresh',
  239. action: () => {
  240. this.handleRefreshAction()
  241. },
  242. disabled: !canBeRefreshed,
  243. title: !canBeRefreshed ? 'The minion pool should be allocated' : '',
  244. },
  245. {
  246. label: 'Delete Minion Pools',
  247. color: ThemePalette.alert,
  248. action: () => {
  249. this.setState({ showDeletePoolsModal: true })
  250. },
  251. disabled: !canBeDeleted,
  252. title: !canBeDeleted ? 'The minion pool should be deallocated' : '',
  253. },
  254. ]
  255. return (
  256. <Wrapper>
  257. <MainTemplate
  258. navigationComponent={<Navigation currentPage="minion-pools" />}
  259. listComponent={(
  260. <FilterList
  261. filterItems={this.getFilterItems()}
  262. selectionLabel="minion pool"
  263. loading={minionPoolStore.loadingMinionPools}
  264. items={minionPoolStore.minionPools}
  265. dropdownActions={BulkActions}
  266. largeDropdownActionItems
  267. onItemClick={item => { this.handleItemClick(item) }}
  268. onReloadButtonClick={() => { this.handleReloadButtonClick() }}
  269. itemFilterFunction={(...args) => this.itemFilterFunction(...args)}
  270. onSelectedItemsChange={selectedMinionPools => {
  271. this.setState({ selectedMinionPools })
  272. }}
  273. renderItemComponent={options => (
  274. <MinionPoolListItem
  275. {...options}
  276. endpointType={id => {
  277. const endpoint = this.getEndpoint(id)
  278. if (endpoint) {
  279. return endpoint.type
  280. }
  281. if (endpointStore.loading) {
  282. return 'Loading...'
  283. }
  284. return 'Not Found'
  285. }}
  286. />
  287. )}
  288. emptyListImage={emptyListImage}
  289. emptyListMessage="It seems like you don’t have any Minion Pools in this project."
  290. emptyListExtraMessage="A minion pool defines a set of machines to be created on a certain endpoint with a certain set of options. These machines can then be used during Migrations/Replicas to avoid having to create/delete them during each transfer, thus reducing the time duration."
  291. emptyListButtonLabel="Create a Minion Pool"
  292. onEmptyListButtonClick={() => { this.handleEmptyListButtonClick() }}
  293. />
  294. )}
  295. headerComponent={(
  296. <PageHeader
  297. title="Coriolis Minion Pools"
  298. onProjectChange={() => { this.handleProjectChange() }}
  299. onModalOpen={() => { this.handleModalOpen() }}
  300. onModalClose={() => { this.handleModalClose() }}
  301. />
  302. )}
  303. />
  304. {this.state.showChooseMinionEndpointModal ? (
  305. <MinionEndpointModal
  306. providers={providerStore.providers}
  307. endpoints={endpointStore.endpoints}
  308. loading={providerStore.providersLoading || endpointStore.loading}
  309. onRequestClose={() => { this.handleCloseChooseMinionPoolEndpointModal() }}
  310. onSelectEndpoint={(endpoint, platform) => {
  311. this.handleChooseMinionPoolSelectEndpoint(endpoint, platform)
  312. }}
  313. />
  314. ) : null}
  315. {this.state.showMinionPoolModal ? (
  316. <Modal
  317. isOpen
  318. title={`New ${ObjectUtils.capitalizeFirstLetter(this.state.selectedMinionPoolPlatform)} Minion Pool`}
  319. onRequestClose={() => { this.handleCloseMinionPoolModalRequest() }}
  320. >
  321. <MinionPoolModal
  322. platform={this.state.selectedMinionPoolPlatform}
  323. cancelButtonText="Back"
  324. endpoint={this.state.selectedMinionPoolEndpoint!}
  325. onCancelClick={() => { this.handleBackMinionPoolModal() }}
  326. onRequestClose={() => { this.handleCloseMinionPoolModalRequest() }}
  327. />
  328. </Modal>
  329. ) : null}
  330. {this.state.showDeletePoolsModal ? (
  331. <AlertModal
  332. isOpen
  333. title="Delete Minion Pools?"
  334. message="Are you sure you want to delete the selected Minion Pools?"
  335. extraMessage="Deleting a Coriolis Minion Pool is permanent!"
  336. onConfirmation={() => { this.deleteSelectedMinionPools() }}
  337. onRequestClose={() => { this.setState({ showDeletePoolsModal: false }) }}
  338. />
  339. ) : null}
  340. {this.state.showDeallocateConfirmation ? (
  341. <MinionPoolConfirmationModal
  342. onCancelClick={() => { this.setState({ showDeallocateConfirmation: false }) }}
  343. onExecuteClick={force => { this.handleDeallocateConfirmation(force) }}
  344. />
  345. ) : null}
  346. </Wrapper>
  347. )
  348. }
  349. }
  350. export default MinionPoolsPage