SetSecgroup.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{$t('compute.text_1116')}}</div>
  4. <div slot="body">
  5. <a-alert class="mb-2" type="warning" :message="message" />
  6. <dialog-selected-tips :name="$t('dictionary.server_container')" :count="params.data.length" :action="$t('compute.text_1116')" />
  7. <dialog-table :data="params.data" :columns="params.columns.slice(0, 3)" />
  8. <loader loading v-if="!(bindedSecgroupsLoaded && secgroupsInitLoaded)" />
  9. <a-form :form="form.fc" hideRequiredMark v-show="bindedSecgroupsLoaded && secgroupsInitLoaded" v-bind="formItemLayout">
  10. <a-form-item :label="$t('compute.text_105')" v-if="bindedSecgroupsLoaded">
  11. <div slot="extra">{{$t('compute.text_1242', [max])}}<!-- <help-link :href="href">{{$t('compute.text_189')}}</help-link> -->
  12. <dialog-trigger :vm="params.vm" :extParams="{ tenant, domain }" :name="$t('compute.text_189')" value="CreateSecgroupDialog" resource="secgroups" @success="successCallback" />
  13. </div>
  14. <base-select
  15. ref="secgroupRef"
  16. class="w-100"
  17. remote
  18. show-sync
  19. v-decorator="decorators.secgroups"
  20. resource="secgroups"
  21. :resList.sync="secgroupOptions"
  22. :mapper="mapperSecgroups"
  23. :params="secgroupsParams"
  24. :init-loaded.sync="secgroupsInitLoaded"
  25. :select-props="{ allowClear: true, placeholder: $t('compute.text_190'), mode: 'multiple' }" />
  26. </a-form-item>
  27. </a-form>
  28. </div>
  29. <div slot="footer">
  30. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  31. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  32. </div>
  33. </base-dialog>
  34. </template>
  35. <script>
  36. import { mapGetters } from 'vuex'
  37. import * as R from 'ramda'
  38. import DialogMixin from '@/mixins/dialog'
  39. import WindowsMixin from '@/mixins/windows'
  40. import { HYPERVISORS_MAP } from '@/constants'
  41. import { SECGROUP_LIST_FOR_VMINSTANCE_SIDEPAGE_REFRESH } from '@/constants/event-bus'
  42. export default {
  43. name: 'VmContainerSetSecgroupDialog',
  44. mixins: [DialogMixin, WindowsMixin],
  45. data () {
  46. const validateSecgroups = (rule, value, callback) => {
  47. const maxError = this.isBindOne ? this.$t('compute.text_1243') : this.$t('compute.text_1244')
  48. const minError = this.$t('compute.text_192')
  49. const max = this.isBindOne ? 1 : 5
  50. if (value.length > max) {
  51. return callback(maxError)
  52. }
  53. if (value.length < 1) {
  54. return callback(minError)
  55. }
  56. return callback()
  57. }
  58. return {
  59. loading: false,
  60. form: {
  61. fc: this.$form.createForm(this),
  62. },
  63. decorators: {
  64. secgroups: [
  65. 'secgroups',
  66. {
  67. initialValue: this.params.data[0].secgroups && this.params.data[0].secgroups.map(item => item.id),
  68. validateFirst: true,
  69. rules: [
  70. { validator: validateSecgroups, trigger: 'change' },
  71. ],
  72. },
  73. ],
  74. },
  75. formItemLayout: {
  76. wrapperCol: {
  77. span: 20,
  78. },
  79. labelCol: {
  80. span: 4,
  81. },
  82. },
  83. secgroupsInitLoaded: false,
  84. bindedSecgroups: [],
  85. bindedSecgroupsLoaded: false,
  86. secgroupOptions: [],
  87. }
  88. },
  89. computed: {
  90. ...mapGetters(['isAdminMode', 'scope']),
  91. isAzure () {
  92. return this.params.data[0].provider === HYPERVISORS_MAP.azure.provider
  93. },
  94. isUCloud () {
  95. return this.params.data[0].provider === HYPERVISORS_MAP.ucloud.provider
  96. },
  97. isZStack () {
  98. return this.params.data[0].provider === HYPERVISORS_MAP.zstack.provider
  99. },
  100. isBindOne () {
  101. return this.isAzure || this.isUCloud || this.isZStack
  102. },
  103. message () {
  104. let str = this.$t('compute.text_1245')
  105. if (this.isBindOne) {
  106. str = this.$t('compute.text_1246')
  107. }
  108. return str
  109. },
  110. href () {
  111. const url = this.$router.resolve('/secgroup')
  112. return url.href
  113. },
  114. max () {
  115. if (this.isBindOne) {
  116. return 1
  117. }
  118. return 5
  119. },
  120. domain () {
  121. return this.params.data[0].domain_id
  122. },
  123. tenant () {
  124. return this.params.data[0].tenant_id
  125. },
  126. secgroupsParams () {
  127. const params = { limit: 20 }
  128. if (this.isAdminMode) {
  129. params.project_domain = this.params.data[0].domain_id
  130. } else {
  131. params.scope = this.scope
  132. }
  133. if (this.params.data[0] && this.params.data[0].vpc_id) {
  134. params.vpc_id = this.params.data[0].vpc_id
  135. }
  136. return params
  137. },
  138. },
  139. created () {
  140. this.fetchBindedSecgroups()
  141. },
  142. methods: {
  143. mapperSecgroups (data) {
  144. let newData = [...data, ...this.bindedSecgroups]
  145. newData = R.uniqBy(item => item.id, newData)
  146. return newData
  147. },
  148. async fetchBindedSecgroups () {
  149. const manager = new this.$Manager('secgroups')
  150. try {
  151. const { data: { data = [] } } = await manager.list({
  152. params: {
  153. scope: this.scope,
  154. server: this.params.data[0].id,
  155. },
  156. })
  157. this.bindedSecgroups = data
  158. const secgroupIds = data.map(item => item.id)
  159. this.decorators.secgroups[1].initialValue = secgroupIds
  160. this.bindedSecgroupsLoaded = true
  161. } catch (error) {
  162. throw error
  163. }
  164. },
  165. async handleConfirm () {
  166. this.loading = true
  167. try {
  168. const values = await this.form.fc.validateFields()
  169. const ids = this.params.data.map(item => item.id)
  170. const data = {
  171. secgroup_ids: values.secgroups,
  172. }
  173. if (this.params.manager) {
  174. await this.params.manager.batchPerformAction({
  175. ids,
  176. action: 'set-secgroup',
  177. data,
  178. })
  179. } else {
  180. await this.params.onManager('batchPerformAction', {
  181. id: ids,
  182. managerArgs: {
  183. action: 'set-secgroup',
  184. data,
  185. },
  186. })
  187. }
  188. this.params.refresh && this.params.refresh()
  189. this.$bus.$emit(SECGROUP_LIST_FOR_VMINSTANCE_SIDEPAGE_REFRESH)
  190. this.cancelDialog()
  191. } finally {
  192. this.loading = false
  193. }
  194. },
  195. async successCallback () {
  196. await this.$refs.secgroupRef.loadOpts()
  197. const secgroups = this.form.fc.getFieldValue('secgroups')
  198. const newSecgroup = this.secgroupOptions[0]
  199. secgroups.push(newSecgroup.id)
  200. this.form.fc.setFieldsValue({ secgroups: secgroups })
  201. },
  202. },
  203. }
  204. </script>