ShutDown.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_1')}}</div>
  8. </template>
  9. </a-alert>
  10. <dialog-selected-tips :name="params.name || $t('dictionary.server')" :count="dataList.length" :action="action" />
  11. <dialog-table :data="dataList" :columns="columns" />
  12. <a-form
  13. :form="form.fc">
  14. <a-form-item :label="$t('compute.text_1041')" v-bind="formItemLayout" v-if="isOpenWorkflow">
  15. <a-input v-decorator="decorators.reason" :placeholder="$t('compute.text_1105')" />
  16. </a-form-item>
  17. <a-form-item class="mb-0" v-show="canStopPaying">
  18. <a-checkbox
  19. v-model="form.fd.stopPaying"
  20. @change="stopPayingChange">
  21. {{$t('compute.shutdown_stop_paying')}}
  22. </a-checkbox>
  23. <help-tooltip name="shutdownStopCharging" />
  24. </a-form-item>
  25. <a-form-item v-if="isSupportForce" :label="$t('compute.force_shutdown')" v-bind="formItemLayout">
  26. <a-switch v-model="form.fd.is_force" @change="isForceChange" />
  27. </a-form-item>
  28. </a-form>
  29. </div>
  30. <div slot="footer">
  31. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  32. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  33. </div>
  34. </base-dialog>
  35. </template>
  36. <script>
  37. import { mapGetters } from 'vuex'
  38. import DialogMixin from '@/mixins/dialog'
  39. import WindowsMixin from '@/mixins/windows'
  40. import WorkflowMixin from '@/mixins/workflow'
  41. import { BATCH_OPERATE_SERVERS_MAX } from '@/constants/workflow'
  42. import { HYPERVISORS_MAP } from '@/constants'
  43. import { getNameDescriptionTableColumn, getStatusTableColumn } from '@/utils/common/tableColumn'
  44. export default {
  45. name: 'VmShutDownDialog',
  46. mixins: [DialogMixin, WindowsMixin, WorkflowMixin],
  47. data () {
  48. const { type, formData = {} } = this.params
  49. return {
  50. loading: false,
  51. action: type === 'modifyWorkflow' ? this.$t('common.modify_workflow') + `(${this.$t('compute.text_273')})` : this.$t('compute.text_273'),
  52. form: {
  53. fc: this.$form.createForm(this),
  54. fd: {
  55. stopPaying: formData.stop_charging || false,
  56. is_force: formData.is_force || false,
  57. },
  58. },
  59. type,
  60. decorators: {
  61. reason: [
  62. 'reason',
  63. {
  64. initialValue: formData.reason || '',
  65. },
  66. ],
  67. stopPaying: [
  68. 'stopPaying',
  69. {
  70. valuePropName: 'checked',
  71. initialValue: formData.stop_charging || false,
  72. },
  73. ],
  74. is_force: [
  75. 'is_force',
  76. {
  77. initialValue: formData.is_force || false,
  78. valuePropName: 'checked',
  79. },
  80. ],
  81. },
  82. formItemLayout: {
  83. wrapperCol: {
  84. span: 21,
  85. },
  86. labelCol: {
  87. span: 3,
  88. },
  89. },
  90. dataList: [],
  91. }
  92. },
  93. computed: {
  94. ...mapGetters(['userInfo']),
  95. columns () {
  96. if (this.type === 'modifyWorkflow') {
  97. return [getNameDescriptionTableColumn(), getStatusTableColumn({ statusModule: 'server' })]
  98. }
  99. const showFields = ['name', 'ip', 'instance_type']
  100. return this.params.columns.filter((item) => { return showFields.includes(item.field) })
  101. },
  102. // 腾讯云、阿里云、火山云、金山云的按量付费机器,关机可停止付费
  103. // 腾讯云、阿里云包年包月机器,关机停止付费会转为按量付费机器
  104. canStopPaying () {
  105. return this.dataList.length && this.dataList.every(item => {
  106. return ['qcloud', 'aliyun', 'ksyun'].includes(item.brand.toLocaleLowerCase()) || (['volcengine'].includes(item.brand.toLocaleLowerCase()) && item.billing_type === 'postpaid')
  107. })
  108. },
  109. isOpenWorkflow () {
  110. return this.checkWorkflowEnabled(this.WORKFLOW_TYPES.APPLY_SERVER_STOP)
  111. },
  112. isSupportForce () {
  113. return this.dataList.length && this.dataList.every(item => {
  114. return [
  115. HYPERVISORS_MAP.kvm.hypervisor,
  116. HYPERVISORS_MAP.pod.hypervisor,
  117. HYPERVISORS_MAP.esxi.hypervisor,
  118. HYPERVISORS_MAP.huawei.hypervisor,
  119. HYPERVISORS_MAP.aliyun.hypervisor,
  120. HYPERVISORS_MAP.nutanix.hypervisor,
  121. HYPERVISORS_MAP.volcengine.hypervisor,
  122. HYPERVISORS_MAP.ctyun.hypervisor,
  123. HYPERVISORS_MAP.hcso.hypervisor,
  124. HYPERVISORS_MAP.aws.hypervisor,
  125. HYPERVISORS_MAP.zstack.hypervisor,
  126. HYPERVISORS_MAP.qcloud.hypervisor,
  127. HYPERVISORS_MAP.sangfor?.hypervisor,
  128. HYPERVISORS_MAP.uis?.hypervisor,
  129. HYPERVISORS_MAP.ksyun?.hypervisor,
  130. ].includes(item.hypervisor)
  131. })
  132. },
  133. },
  134. async created () {
  135. if (this.type === 'modifyWorkflow' && this.params.ids) {
  136. const list = await new this.$Manager('servers', 'v2').batchGet({
  137. id: this.params.ids,
  138. })
  139. this.dataList = list.data?.data || []
  140. this.$nextTick(() => {
  141. this.form.fc.setFieldsValue(this.params.formData || {})
  142. })
  143. } else {
  144. this.dataList = this.params.data || []
  145. }
  146. },
  147. methods: {
  148. async doShutDownSubmit () {
  149. const data = {
  150. is_force: this.form.fd.is_force,
  151. }
  152. if (this.form.fd.stopPaying) {
  153. data.stop_charging = true
  154. }
  155. const ids = this.dataList.map(item => item.id)
  156. return this.params.onManager('batchPerformAction', {
  157. id: ids,
  158. steadyStatus: 'ready',
  159. managerArgs: {
  160. action: 'stop',
  161. data,
  162. },
  163. })
  164. },
  165. async handleConfirm () {
  166. this.loading = true
  167. try {
  168. if (this.isOpenWorkflow) {
  169. if (this.dataList.length > BATCH_OPERATE_SERVERS_MAX) {
  170. this.$message.error(this.$t('compute.workflow.operate_servers_check.message', [this.$t('compute.text_273'), BATCH_OPERATE_SERVERS_MAX]))
  171. this.loading = false
  172. return
  173. }
  174. const projects = new Set(this.dataList.map(item => item.tenant_id))
  175. if (projects.size > 1) {
  176. this.$message.error(this.$t('compute.text_1348'))
  177. this.loading = false
  178. return
  179. }
  180. await this.handleShutDownByWorkflowSubmit()
  181. } else {
  182. await this.doShutDownSubmit()
  183. }
  184. this.loading = false
  185. this.cancelDialog()
  186. } catch (error) {
  187. this.loading = false
  188. throw error
  189. }
  190. },
  191. isForceChange (val) {
  192. this.form.fd.is_force = val
  193. },
  194. stopPayingChange (val) {
  195. const { checked } = val.target
  196. this.form.fd.stopPaying = checked
  197. },
  198. async handleShutDownByWorkflowSubmit () {
  199. const ids = this.dataList.map(item => item.id)
  200. const values = await this.form.fc.validateFields()
  201. const params = {
  202. stop_charging: this.form.fd.stopPaying,
  203. is_force: this.form.fd.is_force,
  204. }
  205. const variables = {
  206. project: this.dataList[0].tenant_id,
  207. project_domain: this.dataList[0].domain_id,
  208. process_definition_key: this.WORKFLOW_TYPES.APPLY_SERVER_STOP,
  209. initiator: this.userInfo.id,
  210. ids: ids.join(','),
  211. description: values.reason,
  212. paramter: JSON.stringify(params),
  213. }
  214. if (this.type === 'modifyWorkflow') {
  215. await this.updateWorkflow(variables, this.params.workflow)
  216. this.params.success && this.params.success()
  217. } else {
  218. await this.createWorkflow(variables)
  219. }
  220. this.$message.success(this.$t('common.worflow_tip', [this.$t('compute.text_273')]))
  221. this.$router.push('/workflow')
  222. },
  223. },
  224. }
  225. </script>