RevokeNetworkSecgroup.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{ action }}</div>
  4. <div slot="body">
  5. <dialog-selected-tips :name="$t('dictionary.secgroup')" :count="params.data.length" action="移除" />
  6. <dialog-table :data="params.data" :columns="columns" />
  7. </div>
  8. <div slot="footer">
  9. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  10. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  11. </div>
  12. </base-dialog>
  13. </template>
  14. <script>
  15. import DialogMixin from '@/mixins/dialog'
  16. import WindowsMixin from '@/mixins/windows'
  17. import { getStatusTableColumn } from '@/utils/common/tableColumn'
  18. export default {
  19. name: 'VmSidepageRevokeNetworkSecgroupDialog',
  20. mixins: [DialogMixin, WindowsMixin],
  21. data () {
  22. return {
  23. action: this.$t('compute.text_950'),
  24. loading: false,
  25. columns: [
  26. {
  27. field: 'name',
  28. title: this.$t('common.name'),
  29. slots: {
  30. default: ({ row }, h) => {
  31. return this.params.secgroupType === 'network' ? row.secgroup : row.name
  32. },
  33. },
  34. },
  35. getStatusTableColumn({ statusModule: 'secgroup', vm: this, field: this.params.secgroupType === 'network' ? 'secgroup_status' : 'status' }),
  36. ],
  37. }
  38. },
  39. methods: {
  40. async handleConfirm () {
  41. this.loading = true
  42. try {
  43. const manager = new this.$Manager('servers')
  44. if (this.params.secgroupType === 'network') {
  45. // network 类型:params.data 可能来自多台 VM / 多张网卡,这里按 guest_id + network_index 分组批量 revoke
  46. const grouped = {}
  47. ;(this.params.data || []).forEach((row) => {
  48. const guestId = row.guest_id
  49. const networkIndex = row.network_index
  50. const secgroupId = row.secgroup_id
  51. if (!guestId || (networkIndex === undefined || networkIndex === null) || !secgroupId) return
  52. const key = `${guestId}::${networkIndex}`
  53. if (!grouped[key]) {
  54. grouped[key] = {
  55. guestId,
  56. networkIndex,
  57. secgroup_ids: [],
  58. }
  59. }
  60. grouped[key].secgroup_ids.push(secgroupId)
  61. })
  62. const reqs = Object.values(grouped).map((g) => {
  63. return manager.performAction({
  64. id: g.guestId,
  65. action: 'revoke-network-secgroup',
  66. data: {
  67. network_index: g.networkIndex,
  68. guest: g.guestId,
  69. secgroup_ids: g.secgroup_ids,
  70. },
  71. })
  72. })
  73. const results = await Promise.allSettled(reqs)
  74. const failed = results.filter(r => r.status === 'rejected')
  75. if (failed.length) {
  76. const msg = failed[0]?.reason?.message || this.$t('common.text_110') || 'error'
  77. throw new Error(msg)
  78. }
  79. } else {
  80. // 非 network 类型:一次请求即可
  81. const secgroup_ids = (this.params.data || []).map(item => item.id)
  82. await manager.performAction({
  83. id: this.params.detailData.id,
  84. action: 'revoke-network-secgroup',
  85. data: {
  86. mac: this.params.mac,
  87. secgroup_ids,
  88. },
  89. })
  90. }
  91. this.params.refresh && this.params.refresh()
  92. this.cancelDialog()
  93. this.$message.success(this.$t('compute.text_1021'))
  94. } finally {
  95. this.loading = false
  96. }
  97. },
  98. },
  99. }
  100. </script>