CloneDeep.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{action}}</div>
  4. <div slot="body">
  5. <a-alert class="mb-2" type="warning">
  6. <div slot="message">{{$t('compute.text_1199')}}<br />{{$t('compute.text_1200')}}<br />{{$t('compute.text_1201')}}<br />
  7. </div>
  8. </a-alert>
  9. <dialog-selected-tips :name="$t('dictionary.server')" :count="params.data.length" :action="action" />
  10. <dialog-table
  11. :data="params.data"
  12. :columns="params.columns.slice(0, 3)" />
  13. <a-form :form="form.fc" v-bind="formItemLayout" v-if="!isBatch">
  14. <a-form-item :label="$t('compute.text_1202')">
  15. <a-radio-group
  16. v-decorator="decorators.cloneType">
  17. <a-radio value="newSnapshot">{{$t('compute.text_1203')}}</a-radio>
  18. <a-radio value="oldSnapshot" :disabled="oldSnapshotDisabled">{{$t('compute.text_1204')}}</a-radio>
  19. </a-radio-group>
  20. </a-form-item>
  21. <a-form-item
  22. :label="$t('compute.text_228')">
  23. <a-input
  24. v-decorator="decorators.name" />
  25. <template #extra>
  26. <name-repeated
  27. res="servers"
  28. :name="form.fd.name"
  29. :default-text="$t('compute.text_893')" />
  30. </template>
  31. </a-form-item>
  32. <a-form-item :label="$t('compute.text_294')">
  33. <a-input-number v-decorator="decorators.count" :max="5" :min="1" :step="1" step-strictly />
  34. </a-form-item>
  35. <a-form-item :label="$t('compute.text_1205')" :extra="$t('compute.text_1206')" v-if="isNewSnapshotClone">
  36. <a-switch :checkedChildren="$t('compute.text_115')" :unCheckedChildren="$t('compute.text_116')" v-decorator="decorators.auto_delete_instance_snapshot" />
  37. </a-form-item>
  38. <a-form-item :label="$t('compute.text_102')" v-else>
  39. <base-select
  40. :options="snapshotOptions"
  41. v-decorator="decorators.snapshot"
  42. :select-props="{ placeholder: $t('compute.text_1207') }" />
  43. </a-form-item>
  44. </a-form>
  45. </div>
  46. <div slot="footer">
  47. <a-button type="primary" @click="handleConfirm" :loading="loading">{{
  48. $t("dialog.ok")
  49. }}</a-button>
  50. <a-button @click="cancelDialog">{{ $t("dialog.cancel") }}</a-button>
  51. </div>
  52. </base-dialog>
  53. </template>
  54. <script>
  55. import DialogMixin from '@/mixins/dialog'
  56. import WindowsMixin from '@/mixins/windows'
  57. import NameRepeated from '@/sections/NameRepeated'
  58. export default {
  59. name: 'VmCloneDeepDialog',
  60. components: {
  61. NameRepeated,
  62. },
  63. mixins: [DialogMixin, WindowsMixin],
  64. data () {
  65. return {
  66. loading: false,
  67. action: this.$t('compute.text_1208'),
  68. form: {
  69. fc: this.$form.createForm(this, {
  70. name: 'clone_deep_create_form',
  71. onValuesChange: this.onValuesChange,
  72. }),
  73. fd: {
  74. name: '',
  75. cloneType: 'newSnapshot',
  76. },
  77. },
  78. decorators: {
  79. cloneType: [
  80. 'cloneType',
  81. {
  82. initialValue: 'newSnapshot',
  83. },
  84. ],
  85. name: [
  86. 'name',
  87. {
  88. validateFirst: true,
  89. rules: [
  90. { required: true, message: this.$t('compute.text_1043') },
  91. ],
  92. },
  93. ],
  94. count: [
  95. 'count',
  96. {
  97. initialValue: 1,
  98. rules: [
  99. { required: true, message: this.$t('compute.text_1195') },
  100. ],
  101. },
  102. ],
  103. auto_delete_instance_snapshot: [
  104. 'auto_delete_instance_snapshot',
  105. {
  106. valuePropName: 'checked',
  107. initialValue: true,
  108. },
  109. ],
  110. snapshot: [
  111. 'snapshot',
  112. {
  113. rules: [
  114. { required: true, message: this.$t('compute.text_1207'), trigger: 'change' },
  115. ],
  116. },
  117. ],
  118. },
  119. formItemLayout: {
  120. labelCol: { span: 5 },
  121. wrapperCol: { span: 19 },
  122. },
  123. snapshotParams: {
  124. scope: this.$store.getters.scope,
  125. usable: true,
  126. guest_id: this.params.data[0].id,
  127. },
  128. snapshotOptions: [],
  129. }
  130. },
  131. computed: {
  132. isNewSnapshotClone () {
  133. return this.form.fd.cloneType === 'newSnapshot'
  134. },
  135. manager () {
  136. return new this.$Manager('servers', 'v2')
  137. },
  138. diskCount () {
  139. return this.params.data[0].disk_count
  140. },
  141. oldSnapshotDisabled () {
  142. return this.snapshotOptions.length === 0
  143. },
  144. isBatch () {
  145. return this.params.data.length > 1
  146. },
  147. },
  148. created () {
  149. this.fetchSnapshotList()
  150. },
  151. methods: {
  152. async doCreateByNewSnapshot () {
  153. const values = await this.form.fc.validateFields()
  154. const params = {
  155. generate_name: values.name,
  156. name: values.name,
  157. count: values.count,
  158. auto_start: true,
  159. auto_delete_instance_snapshot: values.auto_delete_instance_snapshot,
  160. }
  161. return this.params.onManager('performAction', {
  162. id: this.params.data[0].id,
  163. steadyStatus: ['running', 'ready'],
  164. managerArgs: {
  165. action: 'snapshot-and-clone',
  166. data: params,
  167. },
  168. })
  169. },
  170. async doBatchCreateByNewSnapshot () {
  171. return this.params.data.map((obj) => {
  172. const name = `${obj.name}-copy`
  173. const params = {
  174. generate_name: name,
  175. name: name,
  176. count: 1,
  177. auto_start: true,
  178. auto_delete_instance_snapshot: true,
  179. }
  180. return this.params.onManager('performAction', {
  181. id: obj.id,
  182. steadyStatus: ['running', 'ready'],
  183. managerArgs: {
  184. action: 'snapshot-and-clone',
  185. data: params,
  186. },
  187. })
  188. })
  189. },
  190. async doCreateByOldSnapshot () {
  191. const values = await this.form.fc.validateFields()
  192. const params = {
  193. instance_snapshot_id: values.snapshot,
  194. generate_name: values.name,
  195. name: values.name,
  196. count: values.count,
  197. auto_start: true,
  198. auto_delete_instance_snapshot: false,
  199. }
  200. return this.manager.create({ data: params })
  201. },
  202. async handleConfirm () {
  203. this.loading = true
  204. try {
  205. if (this.isNewSnapshotClone) {
  206. if (this.isBatch) {
  207. await this.doBatchCreateByNewSnapshot()
  208. } else {
  209. await this.doCreateByNewSnapshot()
  210. }
  211. } else {
  212. await this.doCreateByOldSnapshot()
  213. }
  214. setTimeout(() => { // 后端异步任务,需要延迟2s调用 @万垚奇
  215. this.params.refresh()
  216. }, 2000)
  217. this.loading = false
  218. this.cancelDialog()
  219. this.$message.success(this.$t('compute.text_423'))
  220. } catch (error) {
  221. this.loading = false
  222. throw error
  223. }
  224. },
  225. onValuesChange (props, values) {
  226. Object.keys(values).forEach((key) => {
  227. this.form.fd[key] = values[key]
  228. })
  229. },
  230. async fetchSnapshotList () {
  231. const params = {
  232. scope: this.$store.getters.scope,
  233. guest_id: this.params.data[0].id,
  234. usable: true,
  235. }
  236. const { data } = await new this.$Manager('instance_snapshots', 'v2').list({ params })
  237. this.snapshotOptions = data.data.map((item) => {
  238. return {
  239. key: item.id,
  240. label: item.name,
  241. }
  242. })
  243. },
  244. },
  245. }
  246. </script>