Restart.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{action}}</div>
  4. <div slot="body">
  5. <a-alert class="mb-2" type="warning">
  6. <template v-slot:message>
  7. <div>{{$t('compute.text_1234')}}</div>
  8. </template>
  9. </a-alert>
  10. <dialog-selected-tips :name="params.name || $t('dictionary.server')" :count="dataList.length" :action="action" />
  11. <vxe-grid class="mb-2" :data="dataList" :columns="columns" />
  12. <a-form :form="form.fc" hideRequiredMark>
  13. <a-form-item :label="$t('compute.text_1041')" v-bind="formItemLayout" v-if="isOpenWorkflow">
  14. <a-input v-decorator="decorators.reason" :placeholder="$t('compute.text_1105')" />
  15. </a-form-item>
  16. <a-form-item :label="$t('compute.text_1235')" v-bind="formItemLayout">
  17. <a-switch 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 { mapGetters } from 'vuex'
  29. import DialogMixin from '@/mixins/dialog'
  30. import WindowsMixin from '@/mixins/windows'
  31. import WorkflowMixin from '@/mixins/workflow'
  32. import { BATCH_OPERATE_SERVERS_MAX } from '@/constants/workflow'
  33. import { getNameDescriptionTableColumn, getStatusTableColumn } from '@/utils/common/tableColumn'
  34. export default {
  35. name: 'VmRestartDialog',
  36. mixins: [DialogMixin, WindowsMixin, WorkflowMixin],
  37. data () {
  38. const { type, formData = {} } = this.params
  39. return {
  40. loading: false,
  41. action: type === 'modifyWorkflow' ? this.$t('common.modify_workflow') + `(${this.$t('compute.text_274')})` : this.$t('compute.text_274'),
  42. type,
  43. form: {
  44. fc: this.$form.createForm(this),
  45. },
  46. decorators: {
  47. reason: [
  48. 'reason',
  49. {
  50. initialValue: formData.reason || '',
  51. },
  52. ],
  53. autoStart: [
  54. 'autoStart',
  55. {
  56. initialValue: formData.is_force || false,
  57. valuePropName: 'checked',
  58. },
  59. ],
  60. },
  61. formItemLayout: {
  62. wrapperCol: {
  63. span: 21,
  64. },
  65. labelCol: {
  66. span: 3,
  67. },
  68. },
  69. dataList: [],
  70. }
  71. },
  72. computed: {
  73. ...mapGetters(['userInfo']),
  74. columns () {
  75. if (this.type === 'modifyWorkflow') {
  76. return [getNameDescriptionTableColumn(), getStatusTableColumn({ statusModule: 'server' })]
  77. }
  78. const showFields = ['name', 'ip', 'instance_type']
  79. return this.params.columns.filter((item) => { return showFields.includes(item.field) })
  80. },
  81. isOpenWorkflow () {
  82. return this.checkWorkflowEnabled(this.WORKFLOW_TYPES.APPLY_SERVER_RESTART)
  83. },
  84. },
  85. created () {
  86. this.initData()
  87. },
  88. methods: {
  89. async initData () {
  90. if (this.type === 'modifyWorkflow' && this.params.ids) {
  91. const list = await new this.$Manager('servers', 'v2').batchGet({
  92. id: this.params.ids,
  93. })
  94. this.dataList = list.data?.data || []
  95. } else {
  96. this.dataList = this.params.data || []
  97. }
  98. },
  99. async doRestartSubmit (values) {
  100. const ids = this.dataList.map(item => item.id)
  101. return this.params.onManager('batchPerformAction', {
  102. id: ids,
  103. steadyStatus: 'ready',
  104. managerArgs: {
  105. action: 'restart',
  106. data: {
  107. is_force: values.autoStart,
  108. },
  109. },
  110. })
  111. },
  112. async handleConfirm () {
  113. this.loading = true
  114. try {
  115. const values = await this.form.fc.validateFields()
  116. if (this.isOpenWorkflow) {
  117. if (this.dataList.length > BATCH_OPERATE_SERVERS_MAX) {
  118. this.$message.error(this.$t('compute.workflow.operate_servers_check.message', [this.$t('compute.text_274'), BATCH_OPERATE_SERVERS_MAX]))
  119. this.loading = false
  120. return
  121. }
  122. const projects = new Set(this.dataList.map(item => item.tenant_id))
  123. if (projects.size > 1) {
  124. this.$message.error(this.$t('compute.text_1348'))
  125. this.loading = false
  126. return
  127. }
  128. await this.handleRestartByWorkflowSubmit(values)
  129. } else {
  130. await this.doRestartSubmit(values)
  131. }
  132. this.loading = false
  133. this.cancelDialog()
  134. } catch (error) {
  135. this.loading = false
  136. }
  137. },
  138. async handleRestartByWorkflowSubmit (values) {
  139. const ids = this.dataList.map(item => item.id)
  140. const params = {
  141. is_force: values.autoStart,
  142. }
  143. const variables = {
  144. project: this.dataList[0].tenant_id,
  145. project_domain: this.dataList[0].domain_id,
  146. process_definition_key: this.WORKFLOW_TYPES.APPLY_SERVER_RESTART,
  147. initiator: this.userInfo.id,
  148. ids: ids.join(','),
  149. description: values.reason,
  150. paramter: JSON.stringify(params),
  151. }
  152. if (this.type === 'modifyWorkflow') {
  153. await this.updateWorkflow(variables, this.params.workflow)
  154. this.params.success && this.params.success()
  155. } else {
  156. await this.createWorkflow(variables)
  157. }
  158. this.$message.success(this.$t('common.worflow_tip', [this.$t('compute.text_274')]))
  159. this.$router.push('/workflow')
  160. },
  161. },
  162. }
  163. </script>