| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- <template>
- <base-dialog @cancel="cancelDialog">
- <div slot="header">{{$t('compute.add_tap_flow')}}</div>
- <div slot="body">
- <a-form
- v-bind="formItemLayout"
- :form="form.fc">
- <a-form-item :label="$t('compute.text_228')">
- <a-input :placeholder="$t('validator.serverCreateName')" v-decorator="decorators.name" />
- </a-form-item>
- <a-form-item :label="$t('common.description')">
- <a-input :placeholder="$t('scope.text_55')" v-decorator="decorators.description" />
- </a-form-item>
- <a-form-item :label="$t('compute.tap')">
- {{params.tapService.name}}
- </a-form-item>
- <a-form-item :label="$t('compute.tap_direction')">
- <a-radio-group v-decorator="decorators.direction">
- <a-radio-button value="BOTH">{{ $t('compute.direction_both') }}</a-radio-button>
- <a-radio-button value="IN">{{ $t('compute.direction_in') }}</a-radio-button>
- <a-radio-button value="OUT">{{ $t('compute.direction_out') }}</a-radio-button>
- </a-radio-group>
- </a-form-item>
- <a-form-item :label="$t('compute.text_175')">
- <a-radio-group v-decorator="decorators.type">
- <a-radio-button value="vnic">{{ $t('compute.vnic') }}</a-radio-button>
- <a-radio-button value="vswitch">{{ $t('compute.vswitch') }}</a-radio-button>
- </a-radio-group>
- </a-form-item>
- <template v-if="serverType === 'vswitch'">
- <a-form-item :label="$t('compute.text_111')">
- <base-select
- v-decorator="decorators.host_id"
- class="w-100"
- remote
- resource="hosts"
- version="v1"
- needParams
- :params="hostParams"
- isDefaultSelect
- :select-props="{ placeholder: $t('compute.text_148', [$t('compute.text_111')]) }"
- @update:item="hostChange" />
- </a-form-item>
- <a-form-item :label="$t('compute.text_844')">
- <base-select
- v-decorator="decorators.wire_id"
- class="w-100"
- :options="hostWires"
- idKey="wire_id"
- nameKey="wire"
- isDefaultSelect
- :select-props="{ placeholder: $t('compute.text_148', [$t('compute.text_844')]) }" />
- </a-form-item>
- <a-form-item :label="$t('compute.vlan_id')">
- <a-input-number :max="4095" :step="1" :min="0" v-decorator="decorators.vlan_id" />
- </a-form-item>
- </template>
- <template v-else>
- <a-form-item :label="$t('compute.text_91')">
- <base-select
- v-decorator="decorators.guest_id"
- class="w-100"
- remote
- resource="servers"
- version="v1"
- needParams
- :params="serverParams"
- isDefaultSelect
- :select-props="{ placeholder: $t('compute.text_148', [$t('compute.text_91')]) }"
- @update:item="serverChange" />
- </a-form-item>
- <a-form-item :label="$t('compute.vm_mac')">
- <base-select
- v-decorator="decorators.mac_addr"
- class="w-100"
- :options="vmMac"
- idKey="mac"
- nameKey="macLabel"
- isDefaultSelect
- :select-props="{ placeholder: $t('compute.text_148', [$t('compute.vm_mac')]) }" />
- </a-form-item>
- </template>
- </a-form>
- </div>
- <div slot="footer">
- <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
- <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
- </div>
- </base-dialog>
- </template>
- <script>
- import { mapGetters } from 'vuex'
- import validateForm from '@/utils/validate'
- import DialogMixin from '@/mixins/dialog'
- import WindowsMixin from '@/mixins/windows'
- export default {
- name: 'TapFlowCreateDialog',
- mixins: [DialogMixin, WindowsMixin],
- data () {
- return {
- loading: false,
- form: {
- fc: this.$form.createForm(this, {
- onValuesChange: this.handleValuesChange,
- }),
- fd: {
- type: 'vnic',
- },
- },
- currentHost: {},
- currentServer: {},
- decorators: {
- name: [
- 'name',
- {
- initialValue: '',
- validateTrigger: ['change', 'blur'],
- validateFirst: true,
- rules: [
- { required: true, message: this.$t('compute.text_210') },
- { validator: validateForm('serverCreateName') },
- ],
- },
- ],
- description: [
- 'description',
- ],
- direction: [
- 'direction',
- { initialValue: 'BOTH' },
- ],
- type: [
- 'type',
- {
- initialValue: 'vnic',
- },
- ],
- host_id: [
- 'host_id',
- {
- rules: [{ required: true, message: this.$t('compute.text_148', [this.$t('compute.text_111')]) }],
- },
- ],
- wire_id: [
- 'wire_id',
- {
- rules: [{ required: true, message: this.$t('compute.text_148', [this.$t('compute.text_844')]) }],
- },
- ],
- vlan_id: ['vlan_id'],
- guest_id: [
- 'guest_id',
- {
- rules: [{ required: true, message: this.$t('compute.text_148', [this.$t('compute.text_91')]) }],
- },
- ],
- mac_addr: [
- 'mac_addr',
- {
- rules: [{ required: true, message: this.$t('compute.text_148', [this.$t('compute.vm_mac')]) }],
- },
- ],
- },
- formItemLayout: {
- wrapperCol: {
- span: 18,
- },
- labelCol: {
- span: 4,
- },
- },
- }
- },
- computed: {
- ...mapGetters(['isAdminMode', 'isDomainMode', 'scope']),
- serverType () {
- return this.form.fd.type
- },
- hostWires () {
- const { nic_info = [] } = this.currentHost
- return nic_info.filter(item => item.wire_id)
- },
- vmMac () {
- const { nics = [] } = this.currentServer
- return nics.filter(item => item.mac).map(item => {
- return {
- ...item,
- macLabel: `${item.mac}${item.ip_addr ? ` (${item.ip_addr})` : ''}`,
- }
- })
- },
- hostParams () {
- const ret = {
- scope: this.scope,
- filter: 'hypervisor.notin(baremetal,container)',
- brand: 'OneCloud',
- limit: 20,
- system: this.isAdminMode,
- }
- const { type, target_id } = this.params.tapService
- if (type === 'host') {
- ret.filter = ['hypervisor.notin(baremetal,container)', `id.notin(${target_id})`]
- }
- return ret
- },
- serverParams () {
- const ret = {
- scope: this.scope,
- filter: 'hypervisor.notin(baremetal,container)',
- brand: 'OneCloud',
- limit: 20,
- system: this.isAdminMode,
- }
- const { type, target_id } = this.params.tapService
- if (type === 'guest') {
- ret.filter = ['hypervisor.notin(baremetal,container)', `id.notin(${target_id})`]
- }
- return ret
- },
- },
- created () {
- this.$m = new this.$Manager('tap_flows', 'v1')
- },
- methods: {
- async handleValuesChange (vm, changedFields) {
- this.form.fd = {
- ...this.form.fd,
- ...changedFields,
- }
- await this.$nextTick()
- if (changedFields.guest_id) {
- this.form.fc.setFieldsValue({
- mac_addr: '',
- })
- }
- if (changedFields.host_id) {
- if (!this.hostWires.some(item => item.wire_id === this.form.fd.wire_id)) {
- this.form.fc.setFieldsValue({
- wire_id: '',
- })
- }
- }
- },
- hostChange (data) {
- if (this.serverType === 'vswitch') {
- this.currentHost = data
- } else {
- this.currentHost = {}
- }
- },
- serverChange (data) {
- if (this.serverType === 'vnic') {
- this.currentServer = data
- } else {
- this.currentServer = {}
- }
- },
- validateForm () {
- return new Promise((resolve, reject) => {
- this.form.fc.validateFields((err, values) => {
- if (!err) {
- resolve(values)
- } else {
- reject(err)
- }
- })
- })
- },
- doCreate (data) {
- return this.$m.create({
- data,
- })
- },
- async handleConfirm () {
- this.loading = true
- try {
- const values = await this.validateForm()
- this.loading = true
- values.tap_id = this.params.tapService.id
- await this.doCreate(values)
- this.loading = false
- this.$bus.$emit('tap-service-refresh')
- this.params.success && this.params.success()
- this.cancelDialog()
- } catch (error) {
- this.loading = false
- }
- },
- },
- }
- </script>
|