| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <template>
- <base-dialog @cancel="cancelDialog">
- <div slot="header">{{action}}</div>
- <div slot="body">
- <a-alert class="mb-2" type="warning">
- <div slot="message">{{$t('compute.text_1199')}}<br />{{$t('compute.text_1200')}}<br />{{$t('compute.text_1201')}}<br />
- </div>
- </a-alert>
- <dialog-selected-tips :name="$t('dictionary.server')" :count="params.data.length" :action="action" />
- <dialog-table
- :data="params.data"
- :columns="params.columns.slice(0, 3)" />
- <a-form :form="form.fc" v-bind="formItemLayout" v-if="!isBatch">
- <a-form-item :label="$t('compute.text_1202')">
- <a-radio-group
- v-decorator="decorators.cloneType">
- <a-radio value="newSnapshot">{{$t('compute.text_1203')}}</a-radio>
- <a-radio value="oldSnapshot" :disabled="oldSnapshotDisabled">{{$t('compute.text_1204')}}</a-radio>
- </a-radio-group>
- </a-form-item>
- <a-form-item
- :label="$t('compute.text_228')">
- <a-input
- v-decorator="decorators.name" />
- <template #extra>
- <name-repeated
- res="servers"
- :name="form.fd.name"
- :default-text="$t('compute.text_893')" />
- </template>
- </a-form-item>
- <a-form-item :label="$t('compute.text_294')">
- <a-input-number v-decorator="decorators.count" :max="5" :min="1" :step="1" step-strictly />
- </a-form-item>
- <a-form-item :label="$t('compute.text_1205')" :extra="$t('compute.text_1206')" v-if="isNewSnapshotClone">
- <a-switch :checkedChildren="$t('compute.text_115')" :unCheckedChildren="$t('compute.text_116')" v-decorator="decorators.auto_delete_instance_snapshot" />
- </a-form-item>
- <a-form-item :label="$t('compute.text_102')" v-else>
- <base-select
- :options="snapshotOptions"
- v-decorator="decorators.snapshot"
- :select-props="{ placeholder: $t('compute.text_1207') }" />
- </a-form-item>
- </a-form>
- </div>
- <div slot="footer">
- <a-button type="primary" @click="handleConfirm" :loading="loading">{{
- $t("dialog.ok")
- }}</a-button>
- <a-button @click="cancelDialog">{{ $t("dialog.cancel") }}</a-button>
- </div>
- </base-dialog>
- </template>
- <script>
- import DialogMixin from '@/mixins/dialog'
- import WindowsMixin from '@/mixins/windows'
- import NameRepeated from '@/sections/NameRepeated'
- export default {
- name: 'VmCloneDeepDialog',
- components: {
- NameRepeated,
- },
- mixins: [DialogMixin, WindowsMixin],
- data () {
- return {
- loading: false,
- action: this.$t('compute.text_1208'),
- form: {
- fc: this.$form.createForm(this, {
- name: 'clone_deep_create_form',
- onValuesChange: this.onValuesChange,
- }),
- fd: {
- name: '',
- cloneType: 'newSnapshot',
- },
- },
- decorators: {
- cloneType: [
- 'cloneType',
- {
- initialValue: 'newSnapshot',
- },
- ],
- name: [
- 'name',
- {
- validateFirst: true,
- rules: [
- { required: true, message: this.$t('compute.text_1043') },
- ],
- },
- ],
- count: [
- 'count',
- {
- initialValue: 1,
- rules: [
- { required: true, message: this.$t('compute.text_1195') },
- ],
- },
- ],
- auto_delete_instance_snapshot: [
- 'auto_delete_instance_snapshot',
- {
- valuePropName: 'checked',
- initialValue: true,
- },
- ],
- snapshot: [
- 'snapshot',
- {
- rules: [
- { required: true, message: this.$t('compute.text_1207'), trigger: 'change' },
- ],
- },
- ],
- },
- formItemLayout: {
- labelCol: { span: 5 },
- wrapperCol: { span: 19 },
- },
- snapshotParams: {
- scope: this.$store.getters.scope,
- usable: true,
- guest_id: this.params.data[0].id,
- },
- snapshotOptions: [],
- }
- },
- computed: {
- isNewSnapshotClone () {
- return this.form.fd.cloneType === 'newSnapshot'
- },
- manager () {
- return new this.$Manager('servers', 'v2')
- },
- diskCount () {
- return this.params.data[0].disk_count
- },
- oldSnapshotDisabled () {
- return this.snapshotOptions.length === 0
- },
- isBatch () {
- return this.params.data.length > 1
- },
- },
- created () {
- this.fetchSnapshotList()
- },
- methods: {
- async doCreateByNewSnapshot () {
- const values = await this.form.fc.validateFields()
- const params = {
- generate_name: values.name,
- name: values.name,
- count: values.count,
- auto_start: true,
- auto_delete_instance_snapshot: values.auto_delete_instance_snapshot,
- }
- return this.params.onManager('performAction', {
- id: this.params.data[0].id,
- steadyStatus: ['running', 'ready'],
- managerArgs: {
- action: 'snapshot-and-clone',
- data: params,
- },
- })
- },
- async doBatchCreateByNewSnapshot () {
- return this.params.data.map((obj) => {
- const name = `${obj.name}-copy`
- const params = {
- generate_name: name,
- name: name,
- count: 1,
- auto_start: true,
- auto_delete_instance_snapshot: true,
- }
- return this.params.onManager('performAction', {
- id: obj.id,
- steadyStatus: ['running', 'ready'],
- managerArgs: {
- action: 'snapshot-and-clone',
- data: params,
- },
- })
- })
- },
- async doCreateByOldSnapshot () {
- const values = await this.form.fc.validateFields()
- const params = {
- instance_snapshot_id: values.snapshot,
- generate_name: values.name,
- name: values.name,
- count: values.count,
- auto_start: true,
- auto_delete_instance_snapshot: false,
- }
- return this.manager.create({ data: params })
- },
- async handleConfirm () {
- this.loading = true
- try {
- if (this.isNewSnapshotClone) {
- if (this.isBatch) {
- await this.doBatchCreateByNewSnapshot()
- } else {
- await this.doCreateByNewSnapshot()
- }
- } else {
- await this.doCreateByOldSnapshot()
- }
- setTimeout(() => { // 后端异步任务,需要延迟2s调用 @万垚奇
- this.params.refresh()
- }, 2000)
- this.loading = false
- this.cancelDialog()
- this.$message.success(this.$t('compute.text_423'))
- } catch (error) {
- this.loading = false
- throw error
- }
- },
- onValuesChange (props, values) {
- Object.keys(values).forEach((key) => {
- this.form.fd[key] = values[key]
- })
- },
- async fetchSnapshotList () {
- const params = {
- scope: this.$store.getters.scope,
- guest_id: this.params.data[0].id,
- usable: true,
- }
- const { data } = await new this.$Manager('instance_snapshots', 'v2').list({ params })
- this.snapshotOptions = data.data.map((item) => {
- return {
- key: item.id,
- label: item.name,
- }
- })
- },
- },
- }
- </script>
|