CreateCache.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{$t('compute.image_cache.perform_precache')}}</div>
  4. <div slot="body">
  5. <a-form
  6. :form="form.fc">
  7. <!-- <a-form-item :label="$t('compute.text_228')" v-bind="formItemLayout" v-if="params.title === 'onpremise'">
  8. <a-input v-decorator="decorators.name" :placeholder="$t('compute.text_228')" @change="handleChange" />
  9. </a-form-item>
  10. <a-form-item label="IP" v-bind="formItemLayout" v-if="params.title === 'onpremise'">
  11. <a-input v-decorator="decorators.access_ip" placeholder="IP" @change="handleChange" />
  12. </a-form-item>
  13. <a-form-item :label="$t('compute.text_591')" v-bind="formItemLayout" v-if="params.title === 'onpremise'">
  14. <a-input v-decorator="decorators.sn" :placeholder="$t('compute.text_591')" @change="handleChange" />
  15. </a-form-item> -->
  16. <a-form-item :label="$t('compute.text_176')" v-bind="formItemLayout">
  17. <a-select
  18. v-decorator="decorators.platform"
  19. @change="handlePlatformChange"
  20. :placeholder="$t('compute.text_219')"
  21. allowClear>
  22. <a-select-option
  23. v-for="(item, index) in platformOptions"
  24. :key="index"
  25. :value="item.name">
  26. {{ item.name }}
  27. </a-select-option>
  28. </a-select>
  29. </a-form-item>
  30. <template v-if="params.title === 'onpremise'">
  31. <a-form-item :label="$t('compute.text_111')" v-bind="formItemLayout">
  32. <base-select
  33. :remote="true"
  34. class="w-100"
  35. :needParams="true"
  36. v-decorator="decorators.host"
  37. resource="hosts"
  38. :params="hostParams"
  39. :remote-fn="q => ({ filter: `name.contains(${q})` })"
  40. @change="handleHostChange"
  41. :select-props="{ placeholder: $t('compute.text_314'), allowClear: true }" />
  42. </a-form-item>
  43. </template>
  44. <a-form-item :label="$t('compute.text_653')" v-bind="formItemLayout" v-if="params.title !== 'onpremise'">
  45. <base-select
  46. :remote="true"
  47. class="w-100"
  48. :needParams="true"
  49. v-decorator="decorators.manager"
  50. resource="cloudproviders"
  51. :params="providerParams"
  52. :remote-fn="q => ({ filter: `name.contains(${q})` })"
  53. @change="handleManagerChange"
  54. :select-props="{ placeholder: $t('compute.text_219') }" />
  55. </a-form-item>
  56. <a-form-item :label="$t('compute.text_177')" v-bind="formItemLayout" v-if="params.title !== 'onpremise'">
  57. <base-select
  58. :remote="true"
  59. class="w-100"
  60. :needParams="true"
  61. v-decorator="decorators.region"
  62. resource="cloudregions"
  63. :params="regionParams"
  64. :remote-fn="q => ({ search: q })"
  65. @change="handleRegionChange"
  66. :select-props="{ placeholder: $t('compute.text_219') }" />
  67. </a-form-item>
  68. </a-form>
  69. <div class="d-flex flex-fill mb-2">
  70. <!-- 刷新 -->
  71. <a-button
  72. class="flex-shrink-0"
  73. :disabled="loading"
  74. @click="handleRefresh">
  75. <a-icon v-if="loading" type="sync" spin />
  76. <a-icon v-else type="sync" />
  77. </a-button>
  78. </div>
  79. <page-list
  80. :list="list"
  81. :pagerLayout="['PrevPage', 'Jump', 'PageCount', 'NextPage', 'Total']"
  82. :columns="cacheColumns"
  83. :show-group-actions="false"
  84. :group-actions="[{}]"
  85. :enableVirtualScroll="false" />
  86. </div>
  87. <div slot="footer">
  88. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  89. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  90. </div>
  91. </base-dialog>
  92. </template>
  93. <script>
  94. import DialogMixin from '@/mixins/dialog'
  95. import WindowsMixin from '@/mixins/windows'
  96. import { sizestr } from '@/utils/utils'
  97. import { BRAND_MAP } from '@/constants'
  98. export default {
  99. name: 'ImageCreateCache',
  100. mixins: [DialogMixin, WindowsMixin],
  101. data () {
  102. return {
  103. loading: false,
  104. form: {
  105. fc: this.$form.createForm(this, {
  106. onValuesChange: (props, values) => {
  107. Object.keys(values).forEach((key) => {
  108. this.$set(this.form.fd, key, values[key])
  109. })
  110. },
  111. }),
  112. fd: {},
  113. },
  114. decorators: {
  115. name: [
  116. 'name',
  117. ],
  118. access_ip: [
  119. 'access_ip',
  120. ],
  121. sn: [
  122. 'sn',
  123. ],
  124. platform: [
  125. 'platform',
  126. ],
  127. manager: [
  128. 'manager',
  129. ],
  130. region: [
  131. 'region',
  132. ],
  133. host: [
  134. 'host',
  135. ],
  136. },
  137. formItemLayout: {
  138. wrapperCol: {
  139. span: 20,
  140. },
  141. labelCol: {
  142. span: 4,
  143. },
  144. },
  145. platformOptions: [],
  146. regionParams: {
  147. cloud_env: this.params.title,
  148. scope: this.$store.getters.scope,
  149. },
  150. providerParams: {
  151. cloud_env: this.params.title,
  152. scope: this.$store.getters.scope,
  153. },
  154. hostParams: {
  155. 'filter.0': 'host_type.notequals(baremetal)',
  156. show_emulated: true,
  157. usable: true,
  158. cloud_env: this.params.title,
  159. show_fail_reason: true,
  160. scope: this.$store.getters.scope,
  161. },
  162. filters: ['host_type.notequals(baremetal)'],
  163. list: this.$list.createList(this, {
  164. resource: this.params.title === 'onpremise' ? 'storages' : 'hosts',
  165. limit: 5,
  166. getParams: this.params.title === 'onpremise' ? {
  167. cloud_env: this.params.title,
  168. usable: true,
  169. } : {
  170. show_emulated: true,
  171. cloud_env: this.params.title,
  172. usable: true,
  173. },
  174. }),
  175. timeout: null,
  176. }
  177. },
  178. computed: {
  179. cacheColumns () {
  180. if (this.params.title === 'onpremise') {
  181. return [
  182. {
  183. field: 'name',
  184. title: this.$t('compute.text_228'),
  185. },
  186. {
  187. field: 'capacity',
  188. title: this.$t('storage.text_177'),
  189. width: 180,
  190. slots: {
  191. default: ({ row }, h) => {
  192. const capacity = sizestr(row.capacity, 'M', 1024)
  193. const allowedBrands = ['VMware', 'OneCloud']
  194. const actual_capacity_used = allowedBrands.includes(row.brand) ? sizestr(row.actual_capacity_used, 'M', 1024) : '-'
  195. return [<div>
  196. <div>{this.$t('storage.text_178', [actual_capacity_used])}</div>
  197. <div>{this.$t('storage.text_180', [capacity])}</div>
  198. </div>]
  199. },
  200. },
  201. },
  202. {
  203. field: 'virtual_capacity',
  204. title: this.$t('storage.text_43'),
  205. width: 180,
  206. slots: {
  207. default: ({ row }, h) => {
  208. const virtual_capacity = sizestr(row.virtual_capacity, 'M', 1024)
  209. const used_capacity = sizestr(row.used_capacity, 'M', 1024)
  210. return [<div>
  211. <div>{this.$t('storage.text_181', [used_capacity])}</div>
  212. <div>{this.$t('storage.text_180', [virtual_capacity])}</div>
  213. </div>]
  214. },
  215. },
  216. },
  217. {
  218. field: 'brand',
  219. title: this.$t('compute.text_176'),
  220. },
  221. {
  222. field: 'region',
  223. title: this.$t('compute.text_177'),
  224. },
  225. {
  226. field: 'zone',
  227. title: this.$t('compute.text_270'),
  228. },
  229. ]
  230. } else {
  231. return [
  232. {
  233. field: 'name',
  234. title: this.$t('compute.text_228'),
  235. },
  236. {
  237. field: 'brand',
  238. title: this.$t('compute.text_176'),
  239. },
  240. {
  241. field: 'account',
  242. title: this.$t('compute.text_269'),
  243. },
  244. {
  245. field: 'manager',
  246. title: this.$t('compute.text_653'),
  247. },
  248. {
  249. field: 'region',
  250. title: this.$t('compute.text_177'),
  251. },
  252. {
  253. field: 'zone',
  254. title: this.$t('compute.text_270'),
  255. },
  256. ]
  257. }
  258. },
  259. listOptions () {
  260. return {}
  261. },
  262. },
  263. created () {
  264. this.fetchPlatform()
  265. this.list.fetchData()
  266. },
  267. methods: {
  268. // handleChange (e) {
  269. // if (e.target.value) {
  270. // this.filters.push([e.target.id] + `.contains(${e.target.value})`)
  271. // this.list.getParams = {
  272. // ...this.list.getParams,
  273. // // [e.target.id]: e.target.value,
  274. // // 'filter.1': [e.target.id] + `.contains(${e.target.value})`,
  275. // }
  276. // } else {
  277. // if (Reflect.has(this.list.getParams, e.target.id)) {
  278. // Reflect.deleteProperty(this.list.getParams, e.target.id)
  279. // }
  280. // }
  281. // clearTimeout(this.timeout)
  282. // this.timeout = setTimeout(() => {
  283. // this.list.fetchData()
  284. // }, 4000)
  285. // },
  286. handleRefresh () {
  287. this.list.refresh()
  288. },
  289. async fetchPlatform () {
  290. const res = await new this.$Manager('capabilities').list({
  291. params: {
  292. scope: this.$store.getters.scope,
  293. },
  294. })
  295. const supportBrands = Object.values(BRAND_MAP).filter(v => v.cloud_env === this.params.title)
  296. const brands = res.data.data[0].brands
  297. this.platformOptions = supportBrands.filter(v => {
  298. return brands.includes(v.brand)
  299. }).map(v => {
  300. return {
  301. name: v.brand,
  302. }
  303. })
  304. },
  305. handlePlatformChange (e) {
  306. if (this.params.title !== 'onpremise') {
  307. this.regionParams = {
  308. usable: true,
  309. cloud_env: this.params.title,
  310. show_emulated: true,
  311. brand: e,
  312. scope: this.$store.getters.scope,
  313. }
  314. this.providerParams = {
  315. usable: true,
  316. brand: e,
  317. scope: this.$store.getters.scope,
  318. cloud_env: this.params.title,
  319. }
  320. } else {
  321. this.hostParams = {
  322. ...this.hostParams,
  323. brand: e,
  324. }
  325. }
  326. this.list.getParams = {
  327. ...this.list.getParams,
  328. brand: e,
  329. }
  330. this.list.fetchData()
  331. },
  332. handleRegionChange (e) {
  333. this.list.getParams = {
  334. ...this.list.getParams,
  335. region: e,
  336. }
  337. this.list.fetchData()
  338. },
  339. handleManagerChange (e) {
  340. if (this.params.title !== 'onpremise') {
  341. this.regionParams = {
  342. usable: true,
  343. cloud_env: this.params.title,
  344. show_emulated: true,
  345. brand: this.form.fd.platform,
  346. manager: e,
  347. scope: this.$store.getters.scope,
  348. }
  349. }
  350. this.list.getParams = {
  351. ...this.list.getParams,
  352. manager: e,
  353. }
  354. this.list.fetchData()
  355. },
  356. handleHostChange (v) {
  357. this.list.getParams = {
  358. ...this.list.getParams,
  359. host_id: v,
  360. }
  361. this.list.fetchData()
  362. },
  363. doSave () {
  364. return this.list.onManager('batchPerformAction', {
  365. id: this.list.selectedItems.map(item => { return item.id }),
  366. managerArgs: {
  367. action: 'cache-image',
  368. data: {
  369. image: this.params.imageId,
  370. },
  371. },
  372. })
  373. },
  374. async handleConfirm () {
  375. this.loading = true
  376. try {
  377. if (this.list.selectedItems.length === 0) {
  378. this.$message.info(this.$t('compute.text_213'))
  379. this.loading = false
  380. return
  381. }
  382. await this.doSave()
  383. this.loading = false
  384. this.params.refresh()
  385. this.cancelDialog()
  386. } catch (error) {
  387. this.loading = false
  388. throw error
  389. }
  390. },
  391. },
  392. }
  393. </script>