Skip to content

Commit

Permalink
add vifs creation
Browse files Browse the repository at this point in the history
  • Loading branch information
J0ris-K committed Mar 4, 2025
1 parent fa4a210 commit 8521edb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 41 deletions.
14 changes: 10 additions & 4 deletions @xen-orchestra/lite/src/libs/xen-api/xen-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[][]> => {
return Promise.all(castArray(vmRefs).map(vmRef => this.call<string[]>('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])))
Expand Down Expand Up @@ -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() {
Expand All @@ -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 = ''
Expand Down
1 change: 0 additions & 1 deletion @xen-orchestra/lite/src/types/new-vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ export interface Disk {
export interface NetworkInterface {
interface: XenApiNetwork['$ref'] | string
macAddress: string
device: string
}
57 changes: 21 additions & 36 deletions @xen-orchestra/lite/src/views/new-vm/NewVmView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -385,7 +385,6 @@ const { t } = useI18n()
const vmState = reactive({
vm_name: '',
vm_description: '',
selectedNetwork: '',
toggle: false,
installMode: '',
tags: '',
Expand Down Expand Up @@ -537,7 +536,6 @@ const getExistingInterface = (template: XenApiVm) => {
existingInterfaces.push({
interface: defaultNetwork.$ref,
macAddress: '',
device: '',
})
}
Expand All @@ -547,7 +545,6 @@ const getExistingInterface = (template: XenApiVm) => {
existingInterfaces.push({
interface: getOpaqueRefNetwork(vif.network)?.$ref || '',
macAddress: vif.MAC || '',
device: vif.device,
})
}
})
Expand All @@ -562,7 +559,6 @@ const addNetworkInterface = () => {
vmState.networkInterfaces.push({
interface: defaultNetwork ? defaultNetwork.$ref : '',
macAddress: '',
device: '',
})
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -683,61 +676,53 @@ 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<string[]>('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)
}
}
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)
})
</script>
Expand Down

0 comments on commit 8521edb

Please sign in to comment.