diff --git a/@xen-orchestra/lite/src/libs/xen-api/xen-api.ts b/@xen-orchestra/lite/src/libs/xen-api/xen-api.ts index 34522c7fc44..8c76fba9ac8 100644 --- a/@xen-orchestra/lite/src/libs/xen-api/xen-api.ts +++ b/@xen-orchestra/lite/src/libs/xen-api/xen-api.ts @@ -348,8 +348,9 @@ export default class XenApi { getAllowedVBDDevices: (vmRefs: VmRefs) => Promise.all(castArray(vmRefs).map(vmRef => this.call('VM.get_allowed_VBD_devices', [vmRef]))), - getAllowedVIFDevices: (vmRefs: VmRefs) => - Promise.all(castArray(vmRefs).map(vmRef => this.call('VM.get_allowed_VIF_devices', [vmRef]))), + getAllowedVIFDevices: (vmRefs: VmRefs): Promise => { + return Promise.all(castArray(vmRefs).map(vmRef => this.call('VM.get_allowed_VIF_devices', [vmRef]))) + }, removeFromOtherConfig: (vmRefs: VmRefs, key: string) => { return Promise.all(castArray(vmRefs).map(vmRef => this.call('VM.remove_from_other_config', [vmRef, key]))) @@ -453,6 +454,11 @@ export default class XenApi { } } + // TODO, improve this + getField(type: string, field: string) { + return this.call(`${type}.get_${field}`) + } + // TODO move to another file // WIP get vif() { @@ -463,9 +469,9 @@ export default class XenApi { create: ( vmRefs: VmRefs, device: string, - networkRef: NetworkRef, + networkRef: NetworkRef | string, MAC: string, - MTU: string, + MTU: number, other_config = {}, qos_algorithm_params = {}, qos_algorithm_type = '' diff --git a/@xen-orchestra/lite/src/types/new-vm.ts b/@xen-orchestra/lite/src/types/new-vm.ts index 72bcaceaff2..d73612f5631 100644 --- a/@xen-orchestra/lite/src/types/new-vm.ts +++ b/@xen-orchestra/lite/src/types/new-vm.ts @@ -11,5 +11,4 @@ export interface Disk { export interface NetworkInterface { interface: XenApiNetwork['$ref'] | string macAddress: string - device: string } diff --git a/@xen-orchestra/lite/src/views/new-vm/NewVmView.vue b/@xen-orchestra/lite/src/views/new-vm/NewVmView.vue index 4135c98ac87..7c0878db3ef 100644 --- a/@xen-orchestra/lite/src/views/new-vm/NewVmView.vue +++ b/@xen-orchestra/lite/src/views/new-vm/NewVmView.vue @@ -322,7 +322,7 @@ import FormSelect from '@/components/form/FormSelect.vue' import TitleBar from '@/components/TitleBar.vue' // XenAPI Store imports -import type { XenApiNetwork, XenApiVm } from '@/libs/xen-api/xen-api.types' +import type { XenApiNetwork, XenApiVif, XenApiVm } from '@/libs/xen-api/xen-api.types' import { useNetworkStore } from '@/stores/xen-api/network.store' import { usePifStore } from '@/stores/xen-api/pif.store' import { usePoolStore } from '@/stores/xen-api/pool.store' @@ -385,7 +385,6 @@ const { t } = useI18n() const vmState = reactive({ vm_name: '', vm_description: '', - selectedNetwork: '', toggle: false, installMode: '', tags: '', @@ -537,7 +536,6 @@ const getExistingInterface = (template: XenApiVm) => { existingInterfaces.push({ interface: defaultNetwork.$ref, macAddress: '', - device: '', }) } @@ -547,7 +545,6 @@ const getExistingInterface = (template: XenApiVm) => { existingInterfaces.push({ interface: getOpaqueRefNetwork(vif.network)?.$ref || '', macAddress: vif.MAC || '', - device: vif.device, }) } }) @@ -562,7 +559,6 @@ const addNetworkInterface = () => { vmState.networkInterfaces.push({ interface: defaultNetwork ? defaultNetwork.$ref : '', macAddress: '', - device: '', }) } } @@ -597,8 +593,6 @@ const onTemplateChange = () => { console.log('VDIs Disks:', vmState.VDIs) console.log('Existing Disks:', vmState.existingDisks) - console.log('Network Interfaces:', vmState.networkInterfaces) - console.log('getBootFirmwares:', getBootFirmwares.value) } // TODO to remove, it's just a exemple of data to send @@ -650,7 +644,6 @@ const vmCreationParams = computed(() => ({ VIFs: vmState.networkInterfaces.map(net => ({ network: net.interface, mac: net.macAddress, - device: net.device, })), CPUs: vmState.vCPU, name_description: vmState.vm_description, @@ -683,53 +676,45 @@ const createVM = async () => { ? await xapi.vm.clone({ [templateRef]: newVmName }) : await xapi.vm.copy({ [templateRef]: newVmName }, '') - console.log('Clone/Copy réussi, référence VM :', vmRef) + console.log('Clone/Copy done, ref VM :', vmRef) - // >>>>>>>>>>>>>>>>>>>>> - // WIP - const newVifs = vmCreationParams.value.VIFs - const existingVif = vmState.new_vm_template?.VIFs + await xapi.vm.removeFromOtherConfig(vmRef, 'disks') + console.log('remove disks done') - console.log('y a des vifs ? => ', newVifs) - console.log('y a des vifs 2 ? => ', existingVif) + await xapi.vm.provision(vmRef) + console.log('Provisioning done') - if (existingVif) { - await Promise.all(existingVif.map(vif => xapi.vif.delete(vif))) + const newVifs = vmCreationParams.value.VIFs + // Direct call to the API here because otherwise we do not yet have the vmRef of the clone or the copy before assigning the devices + const existingVifs = await xapi.call('VM.get_VIFs', [vmRef[0]]) + + // Destroys the VIFs cloned from the template. + if (existingVifs) { + await Promise.all(existingVifs.map(vif => xapi.vif.delete(vif as XenApiVif['$ref']))) } if (newVifs && newVifs.length > 0) { + const [allowedDevices = []] = await xapi.vm.getAllowedVIFDevices(vmRef) + await Promise.all( newVifs.map(async vif => { - let device = vif.device - if (!device) { - const allowedDevices = await xapi.vm.getAllowedVIFDevices(vmRef) + let device: string | undefined = '' - if (allowedDevices.length === 0) { - throw new Error('Aucun device VIF autorisé pour cette VM') - } - device = '0' + if (!device) { + device = allowedDevices.shift() } - console.log('device', device) - await xapi.vif.create(vmRef, device, vif.network, vif.mac ?? '', '', {}, {}, '') - console.log('création de vif réussi', newVifs) + await xapi.vif.create(vmRef, device!, vif.network, vif.mac ?? '', 1500, {}, {}, '') }) ) + console.log('vif creation done', newVifs) } - // <<<<<<<<<<<<<<<<<<<<<<< - await Promise.all([ xapi.vm.setNameLabel(vmRef, vmCreationParams.value.name_label), xapi.vm.setNameDescription(vmRef, vmCreationParams.value.name_description), ]) console.log('Set réussi') - - await xapi.vm.removeFromOtherConfig(vmRef, 'disks') - console.log('remove disks réussi') - - await xapi.vm.provision(vmRef) - console.log('Provisioning réussi') } catch (error) { console.error('Erreur lors de la création de la VM :', error) } @@ -737,7 +722,7 @@ const createVM = async () => { watchEffect(() => { console.log('vmState', vmState) - console.log('vmState.new_vm_template?.VIFs', vmState.new_vm_template?.VIFs) + console.log('vmState.networkInterfaces', vmState.networkInterfaces) console.log('tempalte', templates.value) })