ResetPassword.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{$t('compute.text_276')}}</div>
  4. <div slot="body">
  5. <a-alert v-if="enableQgaAlert" class="mb-2" type="warning">
  6. <div slot="message">
  7. {{ $t('compute.qga.alert01') }}<template v-if="showDocsLink()">(<help-link :href="qgaDoc">{{ $t('compute.qga.alert02') }}</help-link>)</template>,{{ $t('compute.qga.alert03') }}
  8. </div>
  9. </a-alert>
  10. <dialog-selected-tips :name="$t('dictionary.server')" :count="params.data.length" :action="$t('compute.text_276')" />
  11. <dialog-table :data="params.data" :columns="params.columns.slice(0, 3)" />
  12. <a-form :form="form.fc" v-bind="formItemLayout">
  13. <a-form-item v-if="isSingle" :label="$t('common_312')">
  14. <a-input v-decorator="decorators.username" />
  15. </a-form-item>
  16. <a-form-item :label="$t('compute.qga.password')">
  17. <server-password ref="serverPassword" :decorator="decorators.loginConfig" :login-types="loginTypes" :disabledLoginTypes="disabledLoginTypes" />
  18. </a-form-item>
  19. <a-form-item v-if="enableAutoStart" :label="$t('compute.text_494')" :extra="$t('compute.text_1229')">
  20. <a-switch :checkedChildren="$t('compute.text_115')" :unCheckedChildren="$t('compute.text_116')" v-decorator="decorators.auto_start" :disabled="form.fi.disableAutoStart" />
  21. </a-form-item>
  22. <p v-if="!checkQgaOK" class="error-color">{{ $t('compute.reset_password.qga_tooltip') }}</p>
  23. </a-form>
  24. </div>
  25. <div slot="footer">
  26. <a-tooltip :title="checkQgaOK ? undefined : $t('compute.reset_password.qga_tooltip')">
  27. <a-button :class="{ 'mr-1': !checkQgaOK }" type="primary" @click="handleConfirm" :loading="loading" :disabled="!checkQgaOK">{{ $t('dialog.ok') }}</a-button>
  28. </a-tooltip>
  29. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  30. </div>
  31. </base-dialog>
  32. </template>
  33. <script>
  34. import ServerPassword from '@Compute/sections/ServerPassword'
  35. import { LOGIN_TYPES_MAP } from '@Compute/constants'
  36. import DialogMixin from '@/mixins/dialog'
  37. import WindowsMixin from '@/mixins/windows'
  38. import { typeClouds } from '@/utils/common/hypervisor'
  39. import { DOCS_MAP, showDocsLink } from '@/constants/docs'
  40. const hypervisorMap = typeClouds.hypervisorMap
  41. export default {
  42. name: 'VmResetPasswordDialog',
  43. components: {
  44. ServerPassword,
  45. },
  46. mixins: [DialogMixin, WindowsMixin],
  47. data () {
  48. let autoStartInitialValue = true
  49. let disableAutoStart = false
  50. const noAutoStartHyper = [hypervisorMap.azure.key, hypervisorMap.openstack.key]
  51. const firstData = this.params.data && this.params.data[0]
  52. if (firstData && firstData.status === 'running') {
  53. autoStartInitialValue = false
  54. disableAutoStart = true
  55. }
  56. if (firstData && firstData.hypervisor) {
  57. const hyper = firstData.hypervisor.toLowerCase()
  58. if (noAutoStartHyper.includes(hyper)) {
  59. autoStartInitialValue = false
  60. disableAutoStart = true
  61. }
  62. }
  63. const login_account = firstData.metadata?.login_account
  64. const userName = {
  65. Linux: 'root',
  66. Windows: 'Administrator',
  67. }
  68. return {
  69. showDocsLink,
  70. loading: false,
  71. loginTypes: [LOGIN_TYPES_MAP.random.key, LOGIN_TYPES_MAP.password.key],
  72. form: {
  73. fc: this.$form.createForm(this, {
  74. onValuesChange: (props, values) => {
  75. Object.keys(values).forEach((key) => {
  76. this.form.fd[key] = values[key]
  77. })
  78. },
  79. }),
  80. fi: {
  81. disableAutoStart,
  82. },
  83. fd: {
  84. username: login_account || userName[firstData.os_type],
  85. },
  86. },
  87. decorators: {
  88. username: [
  89. 'username',
  90. {
  91. initialValue: login_account || userName[firstData.os_type],
  92. },
  93. ],
  94. loginConfig: {
  95. loginType: [
  96. 'loginType',
  97. {
  98. initialValue: LOGIN_TYPES_MAP.random.key,
  99. },
  100. ],
  101. },
  102. auto_start: [
  103. 'auto_start',
  104. {
  105. initialValue: autoStartInitialValue,
  106. valuePropName: 'checked',
  107. },
  108. ],
  109. },
  110. formItemLayout: {
  111. wrapperCol: {
  112. span: 20,
  113. },
  114. labelCol: {
  115. span: 4,
  116. },
  117. },
  118. qgaDoc: DOCS_MAP.qga(),
  119. checkQgaOK: true,
  120. loginAccount: login_account,
  121. }
  122. },
  123. computed: {
  124. enableMFA () {
  125. return this.$store.getters.userInfo.enable_mfa && this.$store.state.auth.auth.system_totp_on
  126. },
  127. selectedItems () {
  128. return this.params.data
  129. },
  130. selectedItem () {
  131. return this.selectedItems[0]
  132. },
  133. enableAutoStart () {
  134. return this.selectedItems.some(item => item.status === 'ready')
  135. },
  136. isAllKvm () {
  137. return this.selectedItems.every(item => item.hypervisor === hypervisorMap.kvm.key)
  138. },
  139. enableQgaAlert () {
  140. return this.isAllKvm && this.selectedItems.some(item => item.status === 'running')
  141. },
  142. isSingle () {
  143. return this.selectedItems?.length === 1
  144. },
  145. isAllRunning () {
  146. return this.selectedItems.every(item => item.status === 'running')
  147. },
  148. isSupportQgaPing () {
  149. return this.isAllKvm && this.isAllRunning && this.isSingle
  150. },
  151. disabledLoginTypes () {
  152. if (this.form.fd.username && this.form.fd.username !== this.loginAccount) {
  153. return [LOGIN_TYPES_MAP.random.key]
  154. }
  155. return []
  156. },
  157. },
  158. watch: {
  159. disabledLoginTypes (val, oldVal) {
  160. if (!oldVal.length && val.length) {
  161. this.form.fc.setFieldsValue({
  162. loginType: LOGIN_TYPES_MAP.password.key,
  163. })
  164. this.$refs.serverPassword.loginTypeChange({ target: { value: LOGIN_TYPES_MAP.password.key } })
  165. }
  166. },
  167. },
  168. created () {
  169. this.isSupportQgaPing && this.fetchQgaPing()
  170. },
  171. methods: {
  172. async fetchQgaPing () {
  173. try {
  174. const res = await new this.$Manager('servers', 'v2').performAction({
  175. id: this.selectedItem.id,
  176. action: 'qga-ping',
  177. data: {
  178. timeout: 100,
  179. },
  180. })
  181. if (res.data?.ping_error) {
  182. this.checkQgaOK = false
  183. } else {
  184. this.checkQgaOK = true
  185. }
  186. } catch (error) {
  187. this.checkQgaOK = false
  188. }
  189. },
  190. async doSetPassword (values) {
  191. const ids = this.selectedItems.map(item => item.id)
  192. const data = { reset_password: true, auto_start: values.auto_start }
  193. if (values.loginType === LOGIN_TYPES_MAP.password.key) {
  194. data.password = values.password
  195. }
  196. if (this.isSingle && values.username) {
  197. data.username = values.username
  198. }
  199. return this.params.onManager('batchPerformAction', {
  200. id: ids,
  201. steadyStatus: ['running', 'ready'],
  202. managerArgs: {
  203. action: 'set-password',
  204. data,
  205. },
  206. })
  207. },
  208. async handleConfirm () {
  209. this.loading = true
  210. try {
  211. const values = await this.form.fc.validateFields()
  212. const callback = async () => {
  213. this.loading = true
  214. await this.doSetPassword(values)
  215. this.$message.success(this.$t('message.exec_success'))
  216. this.cancelDialog()
  217. }
  218. if (this.enableMFA) {
  219. this.createDialog('SecretVertifyDialog', {
  220. action: this.$t('table.title.mfa_validate'),
  221. success: callback,
  222. })
  223. } else {
  224. callback()
  225. }
  226. } finally {
  227. this.loading = false
  228. }
  229. },
  230. },
  231. }
  232. </script>