BindServerDialog.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{$t('network.text_202')}}</div>
  4. <div slot="body">
  5. <dialog-selected-tips :name="$t('dictionary.eip')" :count="params.data.length" :action="$t('network.text_202')" />
  6. <dialog-table :data="params.data" :columns="params.columns.slice(0, 3)" />
  7. <a-form
  8. v-bind="formItemLayout"
  9. :form="form.fc">
  10. <a-form-item :label="$t('network.eip.instance_type')">
  11. <a-radio-group v-decorator="decorators.instance_type" @change="onAssociateTypeChanged">
  12. <a-radio-button value="server">{{$t('network.eip.instance_type.server')}}</a-radio-button>
  13. <a-radio-button value="natgateway" :disabled="isNatDisabled">{{$t('network.eip.instance_type.nat')}}</a-radio-button>
  14. <a-radio-button value="loadbalancer" :disabled="isLbDisabled">{{$t('network.eip.instance_type.lb')}}</a-radio-button>
  15. </a-radio-group>
  16. </a-form-item>
  17. <a-form-item :label="resource_label" :extra="resource === 'servers' && $t('network.eip_bind_server_tip')">
  18. <base-select
  19. :remote="true"
  20. v-decorator="decorators.instance_id"
  21. :resource="resource"
  22. :params="instanceParams"
  23. :remote-fn="q => ({ search: q })"
  24. :select-props="{ placeholder: $t('network.text_203') }" />
  25. </a-form-item>
  26. </a-form>
  27. </div>
  28. <div slot="footer">
  29. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  30. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  31. </div>
  32. </base-dialog>
  33. </template>
  34. <script>
  35. import { mapGetters } from 'vuex'
  36. import DialogMixin from '@/mixins/dialog'
  37. import WindowsMixin from '@/mixins/windows'
  38. export default {
  39. name: 'EipBindServerDialog',
  40. mixins: [DialogMixin, WindowsMixin],
  41. data () {
  42. return {
  43. loading: false,
  44. form: {
  45. fc: this.$form.createForm(this),
  46. },
  47. resource: 'servers',
  48. resource_label: this.$t('dictionary.server'),
  49. decorators: {
  50. instance_type: [
  51. 'instance_type',
  52. {
  53. initialValue: 'server',
  54. },
  55. ],
  56. instance_id: [
  57. 'instance_id',
  58. {
  59. rules: [
  60. { required: true, message: this.$t('network.text_204') },
  61. ],
  62. },
  63. ],
  64. },
  65. formItemLayout: {
  66. wrapperCol: {
  67. span: 21,
  68. },
  69. labelCol: {
  70. span: 3,
  71. },
  72. },
  73. }
  74. },
  75. computed: {
  76. ...mapGetters(['isAdminMode', 'isDomainMode', 'scope']),
  77. instanceParams () {
  78. const params = {
  79. details: true,
  80. with_meta: true,
  81. scope: this.scope,
  82. }
  83. if (this.resource === 'servers') {
  84. params.filter = 'status.in(ready, running)'
  85. params.without_eip = true
  86. params.eip_associable = true
  87. params.usable_server_for_eip = this.params.data[0].id // 过滤出允许挂载EIP的虚拟机
  88. } else if (this.resource === 'natgateways') {
  89. params.filter = 'status.in("available")'
  90. params.cloudregion_id = this.params.data[0].cloudregion_id
  91. } else if (this.resource === 'loadbalancers') {
  92. params.filter = 'status.in(enabled)'
  93. params.without_eip = true
  94. params.eip_associable = true
  95. params.usable_loadbalancer_for_eip = this.params.data[0].id
  96. }
  97. if (this.isAdminMode || this.isDomainMode) params.project_id = this.params.data[0].project_id
  98. return params
  99. },
  100. isNatDisabled () {
  101. if (this.params.data[0].provider === 'Aliyun') {
  102. return false
  103. }
  104. return true
  105. },
  106. isLbDisabled () {
  107. if (['OneCloud', 'Huawei'].includes(this.params.data[0].provider)) {
  108. return false
  109. }
  110. return true
  111. },
  112. },
  113. methods: {
  114. onAssociateTypeChanged (e) {
  115. if (e.target.value === 'server') {
  116. this.resource = 'servers'
  117. this.resource_label = this.$t('dictionary.server')
  118. } else if (e.target.value === 'natgateway') {
  119. this.resource = 'natgateways'
  120. this.resource_label = this.$t('dictionary.nat')
  121. } else if (e.target.value === 'loadbalancer') {
  122. this.resource = 'loadbalancers'
  123. this.resource_label = 'LB'
  124. }
  125. },
  126. doBind (data) {
  127. return this.params.onManager('performAction', {
  128. id: data.id,
  129. managerArgs: {
  130. action: 'associate',
  131. data,
  132. },
  133. })
  134. },
  135. async handleConfirm () {
  136. this.loading = true
  137. try {
  138. let values = await this.form.fc.validateFields()
  139. values = {
  140. ...values,
  141. id: this.params.data[0].id,
  142. }
  143. this.loading = true
  144. await this.doBind(values)
  145. this.loading = false
  146. this.cancelDialog()
  147. this.params.refresh()
  148. } catch (error) {
  149. this.loading = false
  150. }
  151. },
  152. },
  153. }
  154. </script>