chore(xo-server): use @xen-orchestra/xapi/VIF_create

This commit is contained in:
Julien Fontanet
2022-05-12 14:44:39 +02:00
parent 4bed50b4ed
commit ecae554a78
6 changed files with 67 additions and 87 deletions

View File

@@ -111,21 +111,29 @@ export async function set({
await xapi.deleteVif(vif._xapiId)
// create new VIF with new parameters
const newVif = await xapi.createVif(vm.$id, network.$id, {
mac,
currently_attached: attached,
ipv4_allowed: newIpv4Addresses,
ipv6_allowed: newIpv6Addresses,
// - If locking mode has explicitly passed: use it
// - Else if the network is changing: config it to 'network_default'
// - Else: use the old locking mode
locking_mode: lockingMode ?? (isNetworkChanged ? 'network_default' : vif.lockingMode),
qos_algorithm_type: rateLimit != null ? 'ratelimit' : undefined,
qos_algorithm_params: rateLimit != null ? { kbps: String(rateLimit) } : undefined,
other_config: {
'ethtool-tx': txChecksumming !== undefined ? String(txChecksumming) : undefined,
},
})
const newVif = await xapi._getOrWaitObject(
await xapi.VIF_create(
{
currently_attached: attached,
ipv4_allowed: newIpv4Addresses,
ipv6_allowed: newIpv6Addresses,
// - If locking mode has explicitly passed: use it
// - Else if the network is changing: config it to 'network_default'
// - Else: use the old locking mode
locking_mode: lockingMode ?? (isNetworkChanged ? 'network_default' : vif.lockingMode),
qos_algorithm_type: rateLimit != null ? 'ratelimit' : undefined,
qos_algorithm_params: rateLimit != null ? { kbps: String(rateLimit) } : undefined,
network: network.$ref,
other_config: {
'ethtool-tx': txChecksumming !== undefined ? String(txChecksumming) : undefined,
},
VM: vm.$ref,
},
{
MAC: mac,
}
)
)
await this.allocIpAddresses(newVif.$id, newIpAddresses)

View File

@@ -1217,12 +1217,21 @@ export async function createInterface({ vm, network, position, mac, allowedIpv4A
}
let ipAddresses
const vif = await this.getXapi(vm).createVif(vm._xapiId, network._xapiId, {
mac,
position,
ipv4_allowed: allowedIpv4Addresses,
ipv6_allowed: allowedIpv6Addresses,
})
const xapi = this.getXapi(vm)
const vif = await xapi._getOrWaitObject(
await xapi.VIF_create(
{
device: position !== undefined ? String(position) : undefined,
ipv4_allowed: allowedIpv4Addresses,
ipv6_allowed: allowedIpv6Addresses,
network: network._xapiRef,
VM: vm._xapiRef,
},
{
MAC: mac,
}
)
)
const { push } = (ipAddresses = [])
if (allowedIpv4Addresses) {

View File

@@ -924,7 +924,14 @@ async function _prepareGlusterVm(
if (error.code === 'MESSAGE_METHOD_UNKNOWN') {
// VIF.move has been introduced in xenserver 7.0
await xapi.deleteVif(firstVif.$id)
await xapi.createVif(newVM.$id, xosanNetwork.$id, firstVif)
await xapi.VIF_create(
{
...firstVif,
VM: newVM.$ref,
network: xosanNetwork.$ref,
},
firstVif
)
}
}
}

View File

@@ -848,8 +848,10 @@ export default class Xapi extends XapiBase {
})
}).concat(
map(networks, (networkId, i) =>
this._createVif(vm, this.getObject(networkId), {
this.VIF_create({
device: vifDevices[i],
network: this.getObject(networkId).$ref,
VM: vm.$ref,
})
)
)
@@ -1445,61 +1447,6 @@ export default class Xapi extends XapiBase {
// =================================================================
async _createVif(
vm,
network,
{
mac = '',
position = undefined,
currently_attached = true,
device = position != null ? String(position) : undefined,
ipv4_allowed = undefined,
ipv6_allowed = undefined,
locking_mode = undefined,
MAC = mac,
other_config = {},
qos_algorithm_params = {},
qos_algorithm_type = '',
} = {}
) {
log.debug(`Creating VIF for VM ${vm.name_label} on network ${network.name_label}`)
if (device == null) {
device = (await this.call('VM.get_allowed_VIF_devices', vm.$ref))[0]
}
const vifRef = await this.call(
'VIF.create',
filterUndefineds({
currently_attached: vm.power_state === 'Suspended' ? currently_attached : undefined,
device,
ipv4_allowed,
ipv6_allowed,
locking_mode,
MAC,
MTU: asInteger(network.MTU),
network: network.$ref,
other_config,
qos_algorithm_params,
qos_algorithm_type,
VM: vm.$ref,
})
)
if (currently_attached && isVmRunning(vm)) {
await this.callAsync('VIF.plug', vifRef)
}
return vifRef
}
async createVif(vmId, networkId, opts = undefined) {
return /* await */ this._getOrWaitObject(
await this._createVif(this.getObject(vmId), this.getObject(networkId), opts)
)
}
@decorateWith(deferrable)
async createNetwork($defer, { name, description = 'Created with Xen Orchestra', pifId, mtu, vlan }) {
const networkRef = await this.call('network.create', {

View File

@@ -190,14 +190,20 @@ export default {
const devices = await this.call('VM.get_allowed_VIF_devices', vm.$ref)
await Promise.all(
mapToArray(vifs, (vif, index) =>
this._createVif(vm, this.getObject(vif.network), {
ipv4_allowed: vif.ipv4_allowed,
ipv6_allowed: vif.ipv6_allowed,
device: devices[index],
locking_mode: isEmpty(vif.ipv4_allowed) && isEmpty(vif.ipv6_allowed) ? 'network_default' : 'locked',
mac: vif.mac,
mtu: vif.mtu,
})
this.VIF_create(
{
ipv4_allowed: vif.ipv4_allowed,
ipv6_allowed: vif.ipv6_allowed,
device: devices[index],
locking_mode: isEmpty(vif.ipv4_allowed) && isEmpty(vif.ipv6_allowed) ? 'network_default' : 'locked',
MTU: vif.mtu,
network: this.getObject(vif.network).$ref,
VIM: vm.$ref,
},
{
MAC: vif.mac,
}
)
)
)
}

View File

@@ -226,7 +226,10 @@ export default class Proxy {
$defer.onFailure(() => app.unbindLicense(arg))
if (networkId !== undefined) {
await Promise.all([...vm.VIFs.map(vif => xapi.deleteVif(vif)), xapi.createVif(vm.$id, networkId)])
await Promise.all([
...vm.VIFs.map(vif => xapi.deleteVif(vif)),
xapi.VIF_create({ network: xapi.getObject(networkId).$ref, VM: vm.$ref }),
])
}
const date = new Date()