AttachDialog.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{params.title}}</div>
  4. <div slot="body">
  5. <dialog-selected-tips :name="$t('compute.text_113')" :count="params.data.length" :action="params.title" />
  6. <dialog-table :data="params.data" :columns="columns" />
  7. <a-form
  8. :form="form.fc">
  9. <a-form-item :label="$t('compute.text_492', [params.resourceType === 'server_container' ? this.$t('dictionary.server_container') : this.$t('dictionary.server')])" v-bind="formItemLayout" :extra="$t('compute.text_493')">
  10. <a-select v-decorator="decorators.guest">
  11. <a-select-option v-for="item in guestesOpts" :key="item.id">
  12. {{item.name}}
  13. </a-select-option>
  14. </a-select>
  15. </a-form-item>
  16. <a-form-item v-if="isShowAutoStart" :label="$t('compute.text_494')" v-bind="formItemLayout" :extra="$t('compute.text_495')">
  17. <a-switch :checkedChildren="$t('compute.text_115')" :unCheckedChildren="$t('compute.text_116')" v-decorator="decorators.autoStart" />
  18. </a-form-item>
  19. </a-form>
  20. </div>
  21. <div slot="footer">
  22. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  23. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  24. </div>
  25. </base-dialog>
  26. </template>
  27. <script>
  28. import DialogMixin from '@/mixins/dialog'
  29. import WindowsMixin from '@/mixins/windows'
  30. export default {
  31. name: 'GpuAttachServerDialog',
  32. mixins: [DialogMixin, WindowsMixin],
  33. data () {
  34. return {
  35. loading: false,
  36. scope: this.$store.getters.scope,
  37. form: {
  38. fc: this.$form.createForm(this, {
  39. onValuesChange: (props, values) => {
  40. Object.keys(values).forEach((key) => {
  41. // this.form.fd[key] = values[key]
  42. this.$set(this.form.fd, key, values[key])
  43. })
  44. },
  45. }),
  46. fd: {},
  47. },
  48. decorators: {
  49. guest: [
  50. 'guest',
  51. {
  52. rules: [
  53. {
  54. required: true,
  55. message: this.$t('compute.text_496', [
  56. this.$t('dictionary.server'),
  57. ]),
  58. },
  59. ],
  60. },
  61. ],
  62. autoStart: [
  63. 'autoStart',
  64. {
  65. valuePropName: 'checked',
  66. initialValue: true,
  67. },
  68. ],
  69. },
  70. guestesOpts: [],
  71. formItemLayout: {
  72. wrapperCol: {
  73. span: 18,
  74. },
  75. labelCol: {
  76. span: 6,
  77. },
  78. },
  79. }
  80. },
  81. computed: {
  82. dev_type () { // 目前支持USB和GPU卡
  83. const { dev_type } = this.params.data[0]
  84. return dev_type === 'USB' ? 'USB' : 'GPU'
  85. },
  86. columns () {
  87. const ret = []
  88. this.params.columns.map(item => {
  89. if (['name', 'dev_type', 'model', 'host'].indexOf(item.field) !== -1) {
  90. ret.push(item)
  91. }
  92. })
  93. return ret
  94. },
  95. curGuest () {
  96. return this.guestesOpts.find(o => o.id === this.form.fd.guest)
  97. },
  98. isGuestRunning () {
  99. return this.curGuest?.status === 'running'
  100. },
  101. isShowAutoStart () {
  102. return this.curGuest !== undefined && !this.isGuestRunning
  103. },
  104. },
  105. created () {
  106. const params = {
  107. host_id: this.params.data[0].host_id,
  108. filter: this.params.resourceType === 'server_container' ? 'hypervisor.in(pod)' : 'hypervisor.in(kvm)',
  109. scope: this.scope,
  110. limit: 0,
  111. }
  112. new this.$Manager('servers').list({ params })
  113. .then((res) => {
  114. this.guestesOpts = res.data.data.filter(val => this.params.resourceType === 'server_container' ? val.status === 'ready' : val.status === 'ready' || val.status === 'running')
  115. })
  116. },
  117. methods: {
  118. validateForm () {
  119. return new Promise((resolve, reject) => {
  120. this.form.fc.validateFields((err, values) => {
  121. if (!err) {
  122. resolve(values)
  123. } else {
  124. reject(err)
  125. }
  126. })
  127. })
  128. },
  129. doAttach (data) {
  130. return new this.$Manager('servers').performAction({
  131. id: data.guest,
  132. action: 'attach-isolated-device',
  133. data: {
  134. auto_start: this.isShowAutoStart ? data.autoStart : false,
  135. device: this.params.data[0].id,
  136. },
  137. })
  138. },
  139. async handleConfirm () {
  140. this.loading = true
  141. try {
  142. const values = await this.validateForm()
  143. this.loading = true
  144. await this.doAttach(values)
  145. this.loading = false
  146. this.params.refresh()
  147. this.cancelDialog()
  148. } catch (error) {
  149. this.loading = false
  150. }
  151. },
  152. },
  153. }
  154. </script>