SaveImage.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{$t('compute.text_1236')}}</div>
  4. <div slot="body">
  5. <a-alert class="mb-2" type="warning" v-if="isPublic">
  6. <div slot="message">
  7. {{ $t('compute.save_image_prompt.public_cloud', [$t('common.public_cloud_customized_image')]) }}
  8. </div>
  9. </a-alert>
  10. <dialog-selected-tips :name="$t('dictionary.server')" :count="params.data.length" :action="$t('compute.text_1236')" />
  11. <dialog-table :data="params.data" :columns="params.columns.slice(0, 3)" />
  12. <a-form :form="form.fc" hideRequiredMark v-bind="formItemLayout">
  13. <a-form-item :label="$t('compute.text_1237')" v-if="isKvm">
  14. <a-radio-group v-decorator="decorators.type" @change="handleTypeChange">
  15. <a-radio-button :value="types.system.key">{{ types.system.label }}</a-radio-button>
  16. <a-radio-button :value="types.host.key">{{ types.host.label }}</a-radio-button>
  17. </a-radio-group>
  18. </a-form-item>
  19. <a-form-item :label="$t('compute.text_228')">
  20. <a-input v-decorator="decorators.generate_name" :placeholder="$t('validator.imageName')" @change="e => {form.fi.generate_name = e.target.value}" />
  21. </a-form-item>
  22. <a-form-item :label="$t('compute.text_494')">
  23. <a-switch :checkedChildren="$t('compute.text_115')" :unCheckedChildren="$t('compute.text_116')" v-decorator="decorators.auto_start" />
  24. <template v-slot:extra>
  25. <div>{{$t('compute.text_1238')}}</div>
  26. <div v-if="form.fi.type === types.host.key" class="mt-2">{{$t('compute.text_1239', [ diskCount ])}}</div>
  27. </template>
  28. </a-form-item>
  29. </a-form>
  30. </div>
  31. <div slot="footer">
  32. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  33. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  34. </div>
  35. </base-dialog>
  36. </template>
  37. <script>
  38. import * as R from 'ramda'
  39. import DialogMixin from '@/mixins/dialog'
  40. import WindowsMixin from '@/mixins/windows'
  41. import { typeClouds, findPlatform } from '@/utils/common/hypervisor'
  42. import { SERVER_TYPE } from '@Compute/constants'
  43. const hypervisorMap = typeClouds.hypervisorMap
  44. export default {
  45. name: 'VmSaveImageDialog',
  46. mixins: [DialogMixin, WindowsMixin],
  47. data () {
  48. const types = {
  49. host: {
  50. key: 'host',
  51. label: this.$t('compute.text_1240'),
  52. },
  53. system: {
  54. key: 'system',
  55. label: this.$t('compute.text_1241'),
  56. },
  57. }
  58. const typeInitialValue = types.system.key
  59. return {
  60. loading: false,
  61. types,
  62. images: [],
  63. form: {
  64. fc: this.$form.createForm(this),
  65. fi: {
  66. type: typeInitialValue,
  67. generate_name: '',
  68. },
  69. },
  70. decorators: {
  71. type: [
  72. 'type',
  73. {
  74. initialValue: typeInitialValue,
  75. },
  76. ],
  77. generate_name: [
  78. 'generate_name',
  79. {
  80. validateFirst: true,
  81. rules: [
  82. { required: true, message: this.$t('compute.text_210') },
  83. { validator: this.$validate('imageName') },
  84. { validator: this.checkTemplateName },
  85. ],
  86. },
  87. ],
  88. auto_start: [
  89. 'auto_start',
  90. {
  91. initialValue: true,
  92. valuePropName: 'checked',
  93. },
  94. ],
  95. },
  96. formItemLayout: {
  97. wrapperCol: {
  98. span: 20,
  99. },
  100. labelCol: {
  101. span: 4,
  102. },
  103. },
  104. }
  105. },
  106. computed: {
  107. isKvm () {
  108. return this.params.data[0].hypervisor === hypervisorMap.kvm.key
  109. },
  110. isPublic () {
  111. const noSupportBrand = [
  112. typeClouds.hypervisorMap.ucloud.brand,
  113. typeClouds.hypervisorMap.ctyun.brand,
  114. ]
  115. return (findPlatform(this.params.data[0].hypervisor) === SERVER_TYPE.public && !noSupportBrand.includes(this.params.data[0].brand))
  116. },
  117. diskCount () {
  118. return this.params.data[0].disk_count
  119. },
  120. },
  121. methods: {
  122. async handleConfirm () {
  123. this.loading = true
  124. try {
  125. const values = await this.form.fc.validateFields()
  126. const ids = this.params.data.map(item => item.id)
  127. const action = values.type === this.types.host.key ? 'save-guest-image' : 'save-image'
  128. values.id = ids
  129. values.is_public = false
  130. values.name = values.generate_name
  131. delete values.type
  132. await this.params.onManager('batchPerformAction', {
  133. id: ids,
  134. steadyStatus: ['running', 'ready'],
  135. managerArgs: {
  136. action,
  137. data: values,
  138. },
  139. })
  140. this.cancelDialog()
  141. } finally {
  142. this.loading = false
  143. }
  144. },
  145. handleTypeChange (e) {
  146. this.form.fi.type = e.target.value
  147. },
  148. checkTemplateName (rule, value, callback) {
  149. if (!value) {
  150. return callback(new Error(this.$t('compute.text_660')))
  151. }
  152. return new this.$Manager('images', 'v1').list({
  153. params: {
  154. name: value,
  155. scope: this.$store.getters.scope,
  156. },
  157. }).then(res => {
  158. const data = res.data.data
  159. if (!R.isNil(data) && !R.isEmpty(data)) {
  160. callback(new Error(this.$t('compute.text_662')))
  161. } else {
  162. callback()
  163. }
  164. })
  165. },
  166. },
  167. }
  168. </script>