index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <template>
  2. <div>
  3. <page-header :title="$t('compute.text_804')" />
  4. <page-body needMarginBottom>
  5. <a-alert class="mb-3" :message="$t('compute.physicalmachine_add')" />
  6. <a-form :form="form.fc" v-bind="formItemLayout">
  7. <a-form-item :label="$t('compute.text_805')" :extra="desc">
  8. <a-radio-group v-decorator="decorators.type" @change="handleTypeChange">
  9. <template v-for="(item, key) of types">
  10. <a-radio-button :value="key" :key="key">{{ item.label }}</a-radio-button>
  11. </template>
  12. </a-radio-group>
  13. </a-form-item>
  14. <a-form-item :label="$t('compute.text_297', [$t('dictionary.domain')])" v-if="isAdminMode && !isScriptAdd">
  15. <domain-select v-decorator="decorators.project_domain" />
  16. </a-form-item>
  17. <component
  18. :is="form.fd.type"
  19. :decorators="decorators"
  20. :fd="form.fd"
  21. :offset-wrapper-col="offsetWrapperCol"
  22. :fc="form.fc" />
  23. </a-form>
  24. </page-body>
  25. <page-footer>
  26. <template v-slot:right>
  27. <a-button type="primary" :loading="adding" @click="handleAdd" v-if="!isScriptAdd">{{$t('compute.text_162')}}</a-button>
  28. <a-button class="ml-2" @click="handleBack">{{$t('compute.text_135')}}</a-button>
  29. </template>
  30. </page-footer>
  31. </div>
  32. </template>
  33. <script>
  34. import * as R from 'ramda'
  35. import { mapGetters } from 'vuex'
  36. import { resolveValueChangeField } from '@/utils/common/ant'
  37. import validateForm, { isWithinRange, validate } from '@/utils/validate'
  38. import DomainSelect from '@/sections/DomainSelect'
  39. import ScriptAdd from './ScriptAdd'
  40. import PreAdd from './PreAdd'
  41. import ISOAdd from './ISOAdd'
  42. import PXEAdd from './PXEAdd'
  43. export default {
  44. name: 'PhysicalmachineAdd',
  45. components: {
  46. ScriptAdd,
  47. PreAdd,
  48. IsoAdd: ISOAdd,
  49. PxeAdd: PXEAdd,
  50. DomainSelect,
  51. },
  52. provide () {
  53. return {
  54. form: this.form,
  55. }
  56. },
  57. data () {
  58. const typeInitialValue = 'pxeAdd'
  59. const modeInitialValue = 'single'
  60. const projectDomainInitialValue = this.$store.getters.userInfo.projectDomainId
  61. return {
  62. adding: false,
  63. form: {
  64. fc: this.$form.createForm(this, {
  65. onValuesChange: (props, values) => {
  66. this.$nextTick(() => {
  67. const newField = resolveValueChangeField(values)
  68. R.forEachObjIndexed((item, key) => {
  69. this.$set(this.form.fd, key, item)
  70. }, newField)
  71. })
  72. },
  73. }),
  74. fd: {
  75. type: typeInitialValue,
  76. mode: modeInitialValue,
  77. project_domain: projectDomainInitialValue,
  78. },
  79. },
  80. types: this.$t('physicalmachineAddTypes'),
  81. typeInitialValue,
  82. modeInitialValue,
  83. projectDomainInitialValue,
  84. ipmi_ip_addr_required: false,
  85. ipmi_username_required: false,
  86. ipmi_password_required: false,
  87. net_required: false,
  88. formItemLayout: {
  89. wrapperCol: {
  90. md: { span: 16 },
  91. xl: { span: 18 },
  92. xxl: { span: 20 },
  93. },
  94. labelCol: {
  95. md: { span: 8 },
  96. xl: { span: 6 },
  97. xxl: { span: 4 },
  98. },
  99. },
  100. offsetWrapperCol: {
  101. md: { span: 16, offset: 8 },
  102. xl: { span: 18, offset: 6 },
  103. xxl: { span: 20, offset: 4 },
  104. },
  105. }
  106. },
  107. computed: {
  108. ...mapGetters(['isAdminMode']),
  109. decorators () {
  110. return {
  111. type: [
  112. 'type',
  113. {
  114. initialValue: this.typeInitialValue,
  115. },
  116. ],
  117. project_domain: [
  118. 'project_domain',
  119. {
  120. initialValue: this.projectDomainInitialValue,
  121. },
  122. ],
  123. mode: [
  124. 'mode',
  125. {
  126. initialValue: this.modeInitialValue,
  127. },
  128. ],
  129. mac: [
  130. 'mac',
  131. {
  132. validateFirst: true,
  133. rules: [
  134. { required: true, message: this.$t('compute.text_806') },
  135. { validator: validateForm('mac') },
  136. ],
  137. },
  138. ],
  139. name: [
  140. 'name',
  141. {
  142. validateFirst: true,
  143. rules: [
  144. { required: true, message: this.$t('compute.text_807') },
  145. { validator: validateForm('serverName') },
  146. ],
  147. },
  148. ],
  149. description: ['description'],
  150. ipmi_ip_addr: [
  151. 'ipmi_ip_addr',
  152. {
  153. validateFirst: true,
  154. rules: [
  155. { required: this.ipmi_ip_addr_required, message: this.$t('compute.text_808') },
  156. { validator: this.validateIpAddr, trigger: 'change' },
  157. { validator: this.checkIpInNetwork, trigger: 'change' },
  158. ],
  159. },
  160. ],
  161. ipmi_username: [
  162. 'ipmi_username',
  163. {
  164. rules: [
  165. { required: this.ipmi_username_required, message: this.$t('compute.text_809') },
  166. ],
  167. },
  168. ],
  169. ipmi_password: [
  170. 'ipmi_password',
  171. {
  172. rules: [
  173. { required: this.ipmi_password_required, message: this.$t('compute.text_810') },
  174. ],
  175. },
  176. ],
  177. net: [
  178. 'net',
  179. {
  180. validateFirst: true,
  181. rules: [
  182. { required: this.net_required, message: this.$t('compute.text_811') },
  183. { validator: this.validateNet },
  184. ],
  185. },
  186. ],
  187. content: [
  188. 'content',
  189. {
  190. rules: [
  191. { required: true, message: this.$t('compute.text_797') },
  192. ],
  193. },
  194. ],
  195. file: [
  196. 'file',
  197. {
  198. validateFirst: true,
  199. rules: [
  200. { required: true, message: this.$t('compute.text_812') },
  201. { validator: this.validateFile },
  202. ],
  203. },
  204. ],
  205. no_prepare: [
  206. 'no_prepare',
  207. {
  208. valuePropName: 'checked',
  209. },
  210. ],
  211. no_bmc: [
  212. 'no_bmc',
  213. {
  214. valuePropName: 'checked',
  215. },
  216. ],
  217. access_mac: [
  218. 'access_mac',
  219. {
  220. rules: [
  221. { validator: validateForm('mac', false) },
  222. ],
  223. },
  224. ],
  225. __meta__: [
  226. '__meta__',
  227. {
  228. rules: [
  229. { validator: validateForm('tagName') },
  230. ],
  231. },
  232. ],
  233. }
  234. },
  235. desc () {
  236. return this.types[this.form.fd.type].desc
  237. },
  238. isScriptAdd () {
  239. return this.form.fd.type === 'scriptAdd'
  240. },
  241. isPreSingleAdd () {
  242. return this.form.fd.type === 'preAdd' && this.form.fd.mode === 'single'
  243. },
  244. isPreBatchAdd () {
  245. return this.form.fd.type === 'preAdd' && this.form.fd.mode === 'batch'
  246. },
  247. isPreFilehAdd () {
  248. return this.form.fd.type === 'preAdd' && this.form.fd.mode === 'file'
  249. },
  250. isIsoSingleAdd () {
  251. return this.form.fd.type === 'isoAdd' && this.form.fd.mode === 'single'
  252. },
  253. isIsoFileAdd () {
  254. return this.form.fd.type === 'isoAdd' && this.form.fd.mode === 'file'
  255. },
  256. isPxeSingleAdd () {
  257. return this.form.fd.type === 'pxeAdd' && this.form.fd.mode === 'single'
  258. },
  259. isPxeFileAdd () {
  260. return this.form.fd.type === 'pxeAdd' && this.form.fd.mode === 'file'
  261. },
  262. },
  263. watch: {
  264. 'form.fd.type': {
  265. handler (val) {
  266. if (val === 'isoAdd') {
  267. this.ipmi_ip_addr_required = true
  268. this.ipmi_username_required = true
  269. this.ipmi_password_required = true
  270. this.net_required = true
  271. } else if (val === 'pxeAdd') {
  272. this.ipmi_ip_addr_required = true
  273. this.ipmi_username_required = true
  274. this.ipmi_password_required = true
  275. this.net_required = false
  276. } else {
  277. this.ipmi_ip_addr_required = false
  278. this.ipmi_username_required = false
  279. this.ipmi_password_required = false
  280. this.net_required = false
  281. }
  282. },
  283. immediate: true,
  284. },
  285. },
  286. destroyed () {
  287. this.hm = null
  288. this.um = null
  289. },
  290. created () {
  291. this.hm = new this.$Manager('hosts')
  292. this.hvm = new this.$Manager('hosts/validate-ipmi', 'v1')
  293. this.um = new this.$Manager('uploads', 'v1')
  294. },
  295. methods: {
  296. // 预注册单条录入
  297. doPreSingleAdd () {
  298. const data = {
  299. mac: this.form.fd.mac,
  300. access_mac: this.form.fd.mac,
  301. name: this.form.fd.name,
  302. description: this.form.fd.description,
  303. ipmi_ip_addr: this.form.fd.ipmi_ip_addr,
  304. ipmi_username: this.form.fd.ipmi_username,
  305. ipmi_password: this.form.fd.ipmi_password,
  306. __meta__: this.form.fd.__meta__,
  307. no_probe: true,
  308. no_bmc: this.form.fd.no_bmc,
  309. }
  310. if (this.form.fd.project_domain && this.isAdminMode) {
  311. data.project_domain = this.form.fd.project_domain
  312. }
  313. if (this.form.fd.net) {
  314. if (this.form.fd.net.access_net && !this.form.fd.net.access_ip) {
  315. data.access_net = this.form.fd.net.access_net.id
  316. }
  317. if (this.form.fd.net.access_net && this.form.fd.net.access_ip) {
  318. data.access_ip = this.form.fd.net.access_ip
  319. }
  320. }
  321. return this.hm.create({
  322. data,
  323. })
  324. },
  325. // 预注册批量录入
  326. doPreBatchAdd () {
  327. const params = {
  328. hosts: this.form.fd.content,
  329. no_probe: true,
  330. }
  331. if (this.form.fd.project_domain && this.isAdminMode) {
  332. params.project_domain = this.form.fd.project_domain
  333. }
  334. return this.hm.rpc({
  335. methodname: 'DoBatchRegister',
  336. params,
  337. })
  338. },
  339. // 预注册文件录入
  340. doPreFileAdd () {
  341. const fd = new FormData()
  342. fd.append('action', 'BatchHostRegister')
  343. fd.append('hosts', new Blob([this.form.fd.file.file], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }))
  344. fd.append('no_probe', true)
  345. if (this.form.fd.project_domain && this.isAdminMode) {
  346. fd.append('project_domain', this.form.fd.project_domain)
  347. }
  348. return this.um.create({
  349. data: fd,
  350. }).catch(error => {
  351. this.$bus.$emit('PhysicalmachineAddFileSelectClear')
  352. throw error
  353. })
  354. },
  355. // ISO单条录入
  356. doIsoSingleAdd () {
  357. const data = {
  358. name: this.form.fd.name,
  359. description: this.form.fd.description,
  360. ipmi_ip_addr: this.form.fd.ipmi_ip_addr,
  361. ipmi_username: this.form.fd.ipmi_username,
  362. ipmi_password: this.form.fd.ipmi_password,
  363. enable_pxe_boot: false,
  364. no_prepare: this.form.fd.no_prepare || false,
  365. __meta__: this.form.fd.__meta__,
  366. }
  367. if (this.form.fd.project_domain && this.isAdminMode) {
  368. data.project_domain = this.form.fd.project_domain
  369. }
  370. if (this.form.fd.net) {
  371. if (this.form.fd.net.access_net && !this.form.fd.net.access_ip) {
  372. data.access_net = this.form.fd.net.access_net.id
  373. }
  374. if (this.form.fd.net.access_net && this.form.fd.net.access_ip) {
  375. data.access_ip = this.form.fd.net.access_ip
  376. }
  377. }
  378. return this.hm.create({
  379. data,
  380. })
  381. },
  382. // ISO文件录入
  383. doIsoFileAdd () {
  384. const fd = new FormData()
  385. fd.append('action', 'BatchHostRegister')
  386. fd.append('hosts', new Blob([this.form.fd.file.file], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }))
  387. fd.append('no_prepare', this.form.fd.no_prepare || false)
  388. if (this.form.fd.project_domain && this.isAdminMode) {
  389. fd.append('project_domain', this.form.fd.project_domain)
  390. }
  391. return this.um.create({
  392. data: fd,
  393. }).catch(error => {
  394. this.$bus.$emit('PhysicalmachineAddFileSelectClear')
  395. throw error
  396. })
  397. },
  398. // PXE单条录入
  399. doPxeSingleAdd () {
  400. const data = {
  401. name: this.form.fd.name,
  402. description: this.form.fd.description,
  403. ipmi_ip_addr: this.form.fd.ipmi_ip_addr,
  404. ipmi_username: this.form.fd.ipmi_username,
  405. ipmi_password: this.form.fd.ipmi_password,
  406. enable_pxe_boot: true,
  407. no_prepare: this.form.fd.no_prepare || false,
  408. access_mac: this.form.fd.access_mac,
  409. __meta__: this.form.fd.__meta__,
  410. }
  411. if (this.form.fd.project_domain && this.isAdminMode) {
  412. data.project_domain = this.form.fd.project_domain
  413. }
  414. if (this.form.fd.net) {
  415. if (this.form.fd.net.access_net && !this.form.fd.net.access_ip) {
  416. data.access_net = this.form.fd.net.access_net.id
  417. }
  418. if (this.form.fd.net.access_net && this.form.fd.net.access_ip) {
  419. data.access_ip = this.form.fd.net.access_ip
  420. }
  421. }
  422. return this.hm.create({
  423. data,
  424. })
  425. },
  426. // PXE文件录入
  427. doPxeFileAdd () {
  428. const fd = new FormData()
  429. fd.append('action', 'BatchHostRegister')
  430. fd.append('hosts', new Blob([this.form.fd.file.file], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }))
  431. fd.append('no_prepare', this.form.fd.no_prepare || false)
  432. if (this.form.fd.project_domain && this.isAdminMode) {
  433. fd.append('project_domain', this.form.fd.project_domain)
  434. }
  435. return this.um.create({
  436. data: fd,
  437. }).catch(error => {
  438. this.$bus.$emit('PhysicalmachineAddFileSelectClear')
  439. throw error
  440. })
  441. },
  442. async checkMac (values) {
  443. const res = await this.hvm.create({
  444. data: {
  445. ip: values.ipmi_ip_addr,
  446. username: values.ipmi_username,
  447. password: values.ipmi_password,
  448. },
  449. })
  450. if (res.data?.is_redfish_supported || (!res.data?.is_redfish_supported && values.access_mac)) {
  451. return Promise.resolve()
  452. } else {
  453. this.$message.error(this.$t('compute.access_mac_required'))
  454. return Promise.reject(new Error('mac'))
  455. }
  456. },
  457. handleBack () {
  458. this.$router.push('/physicalmachine')
  459. },
  460. async handleAdd () {
  461. try {
  462. const values = await this.form.fc.validateFields()
  463. this.adding = true
  464. if (this.isPreSingleAdd) {
  465. await this.doPreSingleAdd()
  466. }
  467. if (this.isPreBatchAdd) {
  468. await this.doPreBatchAdd()
  469. }
  470. if (this.isPreFilehAdd) {
  471. await this.doPreFileAdd()
  472. }
  473. if (this.isIsoSingleAdd) {
  474. await this.doIsoSingleAdd()
  475. }
  476. if (this.isIsoFileAdd) {
  477. await this.doIsoFileAdd()
  478. }
  479. if (this.isPxeSingleAdd) {
  480. await this.checkMac(values)
  481. await this.doPxeSingleAdd()
  482. }
  483. if (this.isPxeFileAdd) {
  484. await this.doPxeFileAdd()
  485. }
  486. this.$router.push('/physicalmachine')
  487. } catch (error) {
  488. throw error
  489. } finally {
  490. this.adding = false
  491. }
  492. },
  493. validateIpAddr (rule, value, callback) {
  494. if (value) {
  495. const ret = validate(value, 'IPv4')
  496. if (ret !== true) {
  497. return callback(new Error(ret.msg))
  498. }
  499. }
  500. return callback()
  501. },
  502. checkIpInNetwork (rule, value, callback) {
  503. if (value) {
  504. return this.getIpAddr(value).then((res) => {
  505. const data = res.data.data || []
  506. if (data.length === 0) {
  507. return callback(new Error(this.$t('compute.text_1333')))
  508. }
  509. })
  510. }
  511. return callback()
  512. },
  513. validateNet (rule, value, callback) {
  514. if (value && value.access_ip) {
  515. if (!isWithinRange(value.access_ip, value.access_net.guest_ip_start, value.access_net.guest_ip_end)) {
  516. return callback(new Error(this.$t('compute.text_205')))
  517. }
  518. }
  519. return callback()
  520. },
  521. validateFile (rule, value, callback) {
  522. if (value) {
  523. if (value.file && !value.file.name.endsWith('.xlsx')) {
  524. return callback(new Error(this.$t('compute.text_813')))
  525. }
  526. }
  527. return callback()
  528. },
  529. handleTypeChange (e) {
  530. const project_domain = this.form.fc.getFieldValue('project_domain')
  531. this.form.fc.resetFields()
  532. this.$nextTick(() => {
  533. this.form.fc.getFieldDecorator('mode', { preserve: true })
  534. this.form.fc.getFieldDecorator('project_domain', { preserve: true })
  535. this.form.fc.setFieldsValue({
  536. type: e.target.value,
  537. mode: 'single',
  538. project_domain,
  539. })
  540. })
  541. },
  542. getIpAddr (val) {
  543. return new this.$Manager('networks')
  544. .list({
  545. params: {
  546. server_type: 'ipmi',
  547. ip: val,
  548. scope: this.$store.getters.scope,
  549. },
  550. })
  551. },
  552. },
  553. }
  554. </script>