diff --git a/ui/src/views/infra/HostAdd.vue b/ui/src/views/infra/HostAdd.vue index e4edfbc46af..7444f931c19 100644 --- a/ui/src/views/infra/HostAdd.vue +++ b/ui/src/views/infra/HostAdd.vue @@ -17,162 +17,221 @@ @@ -181,12 +240,14 @@ import { api } from '@/api' import DedicateDomain from '../../components/view/DedicateDomain' import ResourceIcon from '@/components/view/ResourceIcon' +import TooltipLabel from '@/components/widgets/TooltipLabel' export default { name: 'HostAdd', components: { DedicateDomain, - ResourceIcon + ResourceIcon, + TooltipLabel }, props: { resource: { @@ -222,12 +283,30 @@ export default { domainError: false, params: [], placeholder: { + zoneid: null, + podid: null, + clusterid: null, + url: null, username: null, password: null, - hosttags: null + hosttags: null, + isdedicated: null } } }, + computed: { + hostTagRules () { + let rules = [] + if (this.selectedClusterHyperVisorType === 'BareMetal') { + rules = [{ required: true, message: this.$t('message.error.select') }] + } + + return rules + } + }, + beforeCreate () { + this.form = this.$form.createForm(this) + }, created () { this.fetchData() }, @@ -274,7 +353,7 @@ export default { this.clustersList = response.listclustersresponse.cluster || [] this.clusterId = this.clustersList[0].id || null if (this.clusterId) { - this.handleChangeCluster() + this.handleChangeCluster(this.clusterId) } }).catch(error => { this.$notifyError(error) @@ -287,7 +366,16 @@ export default { fetchHostTags () { this.loading = true api('listHostTags').then(response => { - this.hostTagsList = response.listhosttagsresponse.hosttag || [] + const listTagExists = [] + const hostTagsList = response.listhosttagsresponse.hosttag || [] + hostTagsList.forEach(tag => { + if (listTagExists.includes(tag.name)) { + return true + } + + listTagExists.push(tag.name) + this.hostTagsList.push(tag) + }) }).catch(error => { this.$notifyError(error) this.hostTagsList = [] @@ -295,7 +383,8 @@ export default { this.loading = false }) }, - handleChangeCluster () { + handleChangeCluster (value) { + this.clusterId = value this.selectedCluster = this.clustersList.find(i => i.id === this.clusterId) this.selectedClusterHyperVisorType = this.selectedCluster.hypervisortype }, @@ -309,66 +398,53 @@ export default { }, handleSubmitForm () { if (this.loading) return - const requiredFields = document.querySelectorAll('.required-field') + this.form.validateFieldsAndScroll((err, values) => { + if (err) return - requiredFields.forEach(field => { - const input = field.querySelector('.ant-input') - if (!input.value) { - input.parentNode.querySelector('.required-label').classList.add('required-label--error') + if (values.hostname.indexOf('http://') === -1) { + this.url = `http://${values.hostname}` } else { - input.parentNode.querySelector('.required-label').classList.remove('required-label--error') + this.url = values.hostname } - }) - if (this.$el.querySelectorAll('.required-label--error').length > 0) return - - if (this.selectedClusterHyperVisorType === 'VMware') { - this.username = '' - this.password = '' - } - - if (this.hostname.indexOf('http://') === -1) { - this.url = `http://${this.hostname}` - } else { - this.url = this.hostname - } - - if (this.authMethod !== 'password') { - this.password = '' - } - - const args = { - zoneid: this.zoneId, - podid: this.podId, - clusterid: this.clusterId, - hypervisor: this.selectedClusterHyperVisorType, - clustertype: this.selectedCluster.clustertype, - hosttags: this.selectedTags.join(), - username: this.username, - password: this.password, - url: this.url, - agentusername: this.agentusername, - agentpassword: this.agentpassword, - agentport: this.agentport - } - Object.keys(args).forEach((key) => (args[key] == null) && delete args[key]) - - this.loading = true - api('addHost', {}, 'POST', args).then(response => { - const host = response.addhostresponse.host[0] || {} - if (host.id && this.showDedicated) { - this.dedicateHost(host.id) + const args = { + zoneid: values.zoneid, + podid: values.podid, + clusterid: values.clusterid, + hypervisor: this.selectedClusterHyperVisorType, + clustertype: this.selectedCluster.clustertype, + hosttags: values.hosttags ? values.hosttags.join() : null, + username: values.username, + password: this.authMethod !== 'password' ? '' : values.password, + url: this.url, + agentusername: values.agentusername, + agentpassword: values.agentpassword, + agentport: values.agentport } - this.parentFetchData() - this.$parent.$parent.close() - }).catch(error => { - this.$notification.error({ - message: `${this.$t('label.error')} ${error.response.status}`, - description: error.response.data.addhostresponse.errortext, - duration: 0 + if (this.selectedClusterHyperVisorType === 'BareMetal') { + args.cpunumber = values.baremetalcpucores + args.cpuspeed = values.baremetalcpu + args.memory = values.baremetalmemory + args.hostmac = values.baremetalmac + } + Object.keys(args).forEach((key) => (args[key] == null) && delete args[key]) + this.loading = true + api('addHost', {}, 'POST', args).then(response => { + const host = response.addhostresponse.host[0] || {} + if (host.id && this.showDedicated) { + this.dedicateHost(host.id) + } + this.parentFetchData() + this.closeAction() + }).catch(error => { + this.$notification.error({ + message: `${this.$t('label.error')} ${error.response.status}`, + description: error.response.data.addhostresponse.errortext, + duration: 0 + }) + }).finally(() => { + this.loading = false }) - }).finally(() => { - this.loading = false }) }, dedicateHost (hostId) { @@ -409,15 +485,22 @@ export default { this.params.find(i => { if (i.name === field) this.placeholder[field] = i.description }) + }, + closeAction () { + this.$emit('close-action') } } } -