Skip to content

Commit 8cd61b3

Browse files
committed
add defer and some improvements
1 parent 670abf9 commit 8cd61b3

File tree

5 files changed

+228
-114
lines changed

5 files changed

+228
-114
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import type { XenApiNetwork, XenApiVif, XenApiVm } from '@/libs/xen-api/xen-api.types'
2+
import { useXenApiStore } from '@/stores/xen-api.store'
3+
import { castArray } from 'lodash-es'
4+
5+
export default class XenApiGetVif {
6+
private xenApi
7+
8+
constructor() {
9+
this.xenApi = useXenApiStore().getXapi()
10+
}
11+
12+
get vif() {
13+
type VifRefs = XenApiVif['$ref'] | XenApiVif['$ref'][]
14+
type VmRefs = XenApiVm['$ref'] | XenApiVm['$ref'][]
15+
type NetworkRef = XenApiNetwork['$ref']
16+
type VifRecord = {
17+
vmRefs: VmRefs
18+
device: string | undefined
19+
network: NetworkRef | string
20+
MAC: string
21+
MTU?: number
22+
other_config?: Record<string, any>
23+
qos_algorithm_params?: Record<string, any>
24+
qos_algorithm_type?: string
25+
}
26+
27+
return {
28+
create: ({
29+
device,
30+
vmRefs,
31+
network,
32+
MAC,
33+
MTU = 1500,
34+
other_config = {},
35+
qos_algorithm_params = {},
36+
qos_algorithm_type = '',
37+
}: VifRecord) => {
38+
return Promise.all(
39+
castArray(vmRefs).map(vmRef => {
40+
const vifRecord = {
41+
device,
42+
VM: vmRef,
43+
network,
44+
MAC,
45+
MTU,
46+
other_config,
47+
qos_algorithm_params,
48+
qos_algorithm_type,
49+
}
50+
51+
return this.xenApi.call('VIF.create', [vifRecord])
52+
})
53+
)
54+
},
55+
56+
delete: (vifRefs: VifRefs) =>
57+
Promise.all(castArray(vifRefs).map(vifRef => this.xenApi.call('VIF.destroy', [vifRef]))),
58+
}
59+
}
60+
}

@xen-orchestra/lite/src/libs/xen-api/xen-api.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ export default class XenApi {
513513
qos_algorithm_type,
514514
}
515515

516-
return this.call('VIF.create', [vifRecord])
516+
return this.call<XenApiVif['$ref']>('VIF.create', [vifRecord])
517517
})
518518
)
519519
},
@@ -561,8 +561,9 @@ export default class XenApi {
561561
tags,
562562
}
563563

564-
return this.call('VDI.create', [vdiRecord])
564+
return this.call<XenApiVdi['$ref']>('VDI.create', [vdiRecord])
565565
},
566+
566567
delete: (vdiRefs: VdiRefs) => Promise.all(castArray(vdiRefs).map(vdiRef => this.call('VDI.destroy', [vdiRef]))),
567568

568569
setNameDescription: (vdiRefs: VdiRefs, nameDescription: string) =>
@@ -581,11 +582,11 @@ export default class XenApi {
581582
type VbdCreateParams = {
582583
vmRefs: VmRefs
583584
vdiRef: VdiRef
584-
userdevice: string | undefined
585-
bootable: boolean
586-
mode: string
587-
type: string
588-
empty: boolean
585+
userdevice?: string | undefined
586+
bootable?: boolean
587+
mode?: string
588+
type?: string
589+
empty?: boolean
589590
other_config?: Record<string, any>
590591
qos_algorithm_params?: Record<string, any>
591592
qos_algorithm_type?: string
@@ -641,10 +642,13 @@ export default class XenApi {
641642
qos_algorithm_type,
642643
}
643644

644-
return this.call('VBD.create', [vbdRecord])
645+
return this.call<XenApiVbd['$ref']>('VBD.create', [vbdRecord])
645646
})
646647
)
647648
},
649+
650+
delete: (vbdRefs: VbdRefs) => Promise.all(castArray(vbdRefs).map(vbdRef => this.call('VBD.destroy', [vbdRef]))),
651+
648652
insert: (vbdRefs: VbdRefs, vdiRef: VdiRef) =>
649653
Promise.all(castArray(vbdRefs).map(vbdRef => this.call('VBD.insert', [vbdRef, vdiRef]))),
650654

@xen-orchestra/lite/src/stores/xen-api.store.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import XapiStats from '@/libs/xapi-stats'
22
import XenApi from '@/libs/xen-api/xen-api'
3+
import XenApiVif from '@/libs/xen-api/xen-api-vif'
34
import { useLocalStorage, useSessionStorage, whenever } from '@vueuse/core'
45
import { defineStore } from 'pinia'
56
import { computed, ref, watchEffect } from 'vue'
6-
import { useRouter, useRoute } from 'vue-router'
7+
import { useRoute, useRouter } from 'vue-router'
78

89
const HOST_URL = import.meta.env.PROD ? window.origin : import.meta.env.VITE_XO_HOST
910

@@ -35,6 +36,7 @@ export const useXenApiStore = defineStore('xen-api', () => {
3536

3637
const isPoolOverridden = hostUrl.origin !== new URL(HOST_URL).origin
3738
const xenApi = new XenApi(hostUrl.origin)
39+
const xenApiVif = new XenApiVif()
3840
const xapiStats = new XapiStats(xenApi)
3941
const storedSessionId = useLocalStorage<string | undefined>('sessionId', undefined)
4042
const currentSessionId = ref(storedSessionId.value)
@@ -43,6 +45,7 @@ export const useXenApiStore = defineStore('xen-api', () => {
4345
const isConnected = computed(() => status.value === STATUS.CONNECTED)
4446
const isConnecting = computed(() => status.value === STATUS.CONNECTING)
4547
const getXapi = () => xenApi
48+
const getXapiVif = () => xenApiVif
4649
const getXapiStats = () => xapiStats
4750

4851
watchEffect(() => {
@@ -99,6 +102,7 @@ export const useXenApiStore = defineStore('xen-api', () => {
99102
reconnect,
100103
disconnect,
101104
getXapi,
105+
getXapiVif,
102106
getXapiStats,
103107
currentSessionId,
104108
resetPoolMasterIp,

@xen-orchestra/lite/src/types/new-vm.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { XenApiNetwork } from '@/libs/xen-api/xen-api.types'
1+
import type { XenApiNetwork, XenApiVdi, XenApiVm } from '@/libs/xen-api/xen-api.types'
22

33
export interface Disk {
44
name_label: string
@@ -12,3 +12,31 @@ export interface NetworkInterface {
1212
interface: XenApiNetwork['$ref'] | string
1313
macAddress: string
1414
}
15+
16+
export interface VmState {
17+
vm_name: string
18+
vm_description: string
19+
toggle: boolean
20+
installMode: string
21+
tags: string
22+
affinity_host: string
23+
boot_firmware: string
24+
new_vm_template: XenApiVm | null
25+
boot_vm: boolean
26+
auto_power: boolean
27+
fast_clone: boolean
28+
ssh_key: string
29+
selectedVdi: XenApiVdi['$ref'] | null
30+
networkConfig: string
31+
cloudConfig: string
32+
vCPU: number
33+
VCPUs_max: number
34+
ram: number
35+
topology: string
36+
copyHostBiosStrings: boolean
37+
sshKeys: string[]
38+
existingDisks: Disk[]
39+
VDIs: Disk[]
40+
networkInterfaces: NetworkInterface[]
41+
defaultNetwork: NetworkInterface | null
42+
}

0 commit comments

Comments
 (0)