ChangeSubIp.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{$t('compute.change_sub_ip')}}</div>
  4. <div slot="body">
  5. <dialog-selected-tips :name="$t('compute.text_193')" :count="params.data.length" :action="$t('compute.change_sub_ip')" />
  6. <dialog-table :data="params.data" :columns="params.columns.filter(item => ['ifname', 'mac_addr', 'ip_addr', 'network_addresses'].includes(item.field))" />
  7. <a-form-model
  8. ref="form"
  9. class="mt-3"
  10. :model="form"
  11. :rules="rules">
  12. <a-form-model-item :label="$t('compute.sub_ip')" v-bind="formItemLayout">
  13. <a-form-model-item v-for="(item, index) in form.subips" :key="index" :prop="`subips.${index}.ip`" class="mb-1">
  14. <a-row :gutter="8">
  15. <a-col v-if="item.from !== 'old'" :span="20" class="d-flex align-items-center">
  16. <a-radio-group v-model="form.subips[index].type">
  17. <a-radio-button value="allocation">{{ $t('compute.automatic_allocation') }}</a-radio-button>
  18. <a-radio-button value="specify">{{ $t('compute.manually_specify') }}</a-radio-button>
  19. </a-radio-group>
  20. <base-select v-if="form.subips[index].type === 'specify'" class="ml-2" minWidth="300px" v-model="form.subips[index].ip" :options="ipList" />
  21. </a-col>
  22. <a-col :span="20" v-else>{{ item.name }}</a-col>
  23. <a-col :span="4">
  24. <a-button shape="circle" icon="minus" size="small" @click="decrease(index)" class="mt-2" />
  25. </a-col>
  26. </a-row>
  27. </a-form-model-item>
  28. <div class="d-flex align-items-center mt-1">
  29. <a-button type="primary" shape="circle" icon="plus" size="small" @click="add" />
  30. <a-button type="link" @click="add">{{$t('compute.text_822')}}</a-button>
  31. </div>
  32. </a-form-model-item>
  33. </a-form-model>
  34. </div>
  35. <div slot="footer">
  36. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  37. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  38. </div>
  39. </base-dialog>
  40. </template>
  41. <script>
  42. import DialogMixin from '@/mixins/dialog'
  43. import WindowsMixin from '@/mixins/windows'
  44. export default {
  45. name: 'VmNetworkChangeSubIpDialog',
  46. mixins: [DialogMixin, WindowsMixin],
  47. data () {
  48. const { network_addresses = [] } = this.params.data[0]
  49. const originIpList = network_addresses.filter(item => item.type === 'sub_ip').map(item => item.ip_addr)
  50. const subips = originIpList.map(item => {
  51. return {
  52. from: 'old',
  53. ip: item,
  54. name: item,
  55. }
  56. })
  57. return {
  58. loading: false,
  59. scope: this.$store.getters.scope,
  60. formItemLayout: {
  61. wrapperCol: {
  62. span: 20,
  63. },
  64. labelCol: {
  65. span: 4,
  66. },
  67. },
  68. form: {
  69. subips,
  70. },
  71. originIpList,
  72. ipList: [],
  73. }
  74. },
  75. created () {
  76. this.$nM = new this.$Manager(`networks/${this.params.data[0].network_id}/available-addresses`, 'v2')
  77. this.$uM = new this.$Manager('servers')
  78. this.loadIpList()
  79. },
  80. methods: {
  81. loadIpList () {
  82. this.fetchLoading = true
  83. this.$nM.list({
  84. data: {
  85. scope: this.scope,
  86. },
  87. }).then(res => {
  88. const { addresses = [] } = res.data || {}
  89. this.ipList = addresses.map(item => {
  90. return { id: item, name: item }
  91. })
  92. })
  93. },
  94. add () {
  95. this.form.subips.push({ type: 'allocation', ip: '' })
  96. },
  97. decrease (index) {
  98. this.form.subips.splice(index, 1)
  99. },
  100. genParams () {
  101. const ret = {
  102. mac: this.params.server.macs,
  103. sub_ips: [],
  104. remove_sub_ips: [],
  105. count: 0,
  106. }
  107. this.form.subips.map(item => {
  108. if (item.from === 'old') return
  109. if (item.type === 'specify' && item.ip) {
  110. if (!this.originIpList.some(ip => ip === item.ip)) {
  111. ret.sub_ips.push(item.ip)
  112. }
  113. ret.count++
  114. }
  115. if (item.type === 'allocation') {
  116. ret.count++
  117. }
  118. })
  119. this.originIpList.map(ip => {
  120. if (!this.form.subips.some(l => (l.type === 'specify' || l.from === 'old') && l.ip === ip)) {
  121. ret.remove_sub_ips.push(ip)
  122. }
  123. })
  124. return ret
  125. },
  126. async handleConfirm () {
  127. this.loading = true
  128. try {
  129. this.loading = true
  130. const params = this.genParams()
  131. await this.$uM.performAction({
  132. id: this.params.server.id,
  133. action: 'update-sub-ips',
  134. data: params,
  135. })
  136. this.loading = false
  137. this.params.refresh()
  138. this.cancelDialog()
  139. } catch (error) {
  140. this.loading = false
  141. }
  142. },
  143. },
  144. }
  145. </script>