feat(vif.set): support for network, MAC and currently_attached (#403)

Fixes vatesfr/xo-web#1446
This commit is contained in:
Pierre Donias 2016-09-28 15:09:17 +02:00 committed by Julien Fontanet
parent 70e9e1c706
commit 57092ee788
4 changed files with 99 additions and 22 deletions

View File

@ -34,7 +34,7 @@ delete_.resolve = {
// TODO: move into vm and rename to disconnectInterface // TODO: move into vm and rename to disconnectInterface
export async function disconnect ({vif}) { export async function disconnect ({vif}) {
// TODO: check if VIF is attached before // TODO: check if VIF is attached before
await this.getXapi(vif).call('VIF.unplug_force', vif._xapiRef) await this.getXapi(vif).disconnectVif(vif._xapiId)
} }
disconnect.params = { disconnect.params = {
@ -49,7 +49,7 @@ disconnect.resolve = {
// TODO: move into vm and rename to connectInterface // TODO: move into vm and rename to connectInterface
export async function connect ({vif}) { export async function connect ({vif}) {
// TODO: check if VIF is attached before // TODO: check if VIF is attached before
await this.getXapi(vif).call('VIF.plug', vif._xapiRef) await this.getXapi(vif).connectVif(vif._xapiId)
} }
connect.params = { connect.params = {
@ -62,7 +62,41 @@ connect.resolve = {
// ------------------------------------------------------------------- // -------------------------------------------------------------------
export function set ({ vif, allowedIpv4Addresses, allowedIpv6Addresses }) { export async function set ({
vif,
network,
mac,
allowedIpv4Addresses,
allowedIpv6Addresses,
attached
}) {
if (network || mac) {
const xapi = this.getXapi(vif)
const vm = xapi.getObject(vif.$VM)
mac == null && (mac = vif.MAC)
network = xapi.getObject(network && network.id || vif.$network)
allowedIpv4Addresses == null && (allowedIpv4Addresses = vif.allowedIpv4Addresses)
allowedIpv6Addresses == null && (allowedIpv6Addresses = vif.allowedIpv6Addresses)
attached == null && (attached = vif.attached)
// remove previous VIF
const dealloc = address => {
this.deallocIpAddress(address, vif.id)::pCatch(noop)
}
forEach(vif.allowedIpv4Addresses, dealloc)
forEach(vif.allowedIpv6Addresses, dealloc)
xapi.deleteVif(vif._xapiId)::pCatch(noop)
// create new VIF with new parameters
await xapi.createVif(vm.$id, network.$id, {
mac,
currently_attached: attached
})
return
}
const { id } = vif const { id } = vif
const handle = ([ newAddresses, oldAddresses ]) => { const handle = ([ newAddresses, oldAddresses ]) => {
forEach(newAddresses, address => { forEach(newAddresses, address => {
@ -82,6 +116,9 @@ export function set ({ vif, allowedIpv4Addresses, allowedIpv6Addresses }) {
} }
set.params = { set.params = {
id: { type: 'string' },
network: { type: 'string', optional: true },
mac: { type: 'string', optional: true },
allowedIpv4Addresses: { allowedIpv4Addresses: {
type: 'array', type: 'array',
items: { items: {
@ -95,9 +132,11 @@ set.params = {
type: 'string' type: 'string'
}, },
optional: true optional: true
} },
attached: { type: 'boolean', optional: true }
} }
set.resolve = { set.resolve = {
vif: ['id', 'VIF', 'operate'] vif: ['id', 'VIF', 'operate'],
network: ['network', 'network', 'operate']
} }

View File

@ -1054,21 +1054,47 @@ exports.attachDisk = attachDisk
#--------------------------------------------------------------------- #---------------------------------------------------------------------
# TODO: implement resource sets # TODO: implement resource sets
createInterface = $coroutine ({vm, network, position, mtu, mac}) -> createInterface = $coroutine ({
vm,
network,
position,
mac,
allowedIpv4Addresses,
allowedIpv6Addresses
}) ->
vif = yield @getXapi(vm).createVif(vm._xapiId, network._xapiId, { vif = yield @getXapi(vm).createVif(vm._xapiId, network._xapiId, {
mac, mac,
mtu, position,
position ipv4_allowed: allowedIpv4Addresses,
ipv6_allowed: allowedIpv6Addresses
}) })
alloc = (address) =>
@allocIpAddress(address, vif.$id)::pCatch(noop)
forEach(allowedIpv4Addresses, alloc)
forEach(allowedIpv6Addresses, alloc)
return vif.$id return vif.$id
createInterface.params = { createInterface.params = {
vm: { type: 'string' } vm: { type: 'string' }
network: { type: 'string' } network: { type: 'string' }
position: { type: ['integer', 'string'], optional: true } position: { type: ['integer', 'string'], optional: true }
mtu: { type: ['integer', 'string'], optional: true }
mac: { type: 'string', optional: true } mac: { type: 'string', optional: true }
allowedIpv4Addresses: {
type: 'array',
items: {
type: 'string'
},
optional: true
},
allowedIpv6Addresses: {
type: 'array',
items: {
type: 'string'
},
optional: true
}
} }
createInterface.resolve = { createInterface.resolve = {

View File

@ -1799,15 +1799,14 @@ export default class Xapi extends XapiBase {
async _createVif (vm, network, { async _createVif (vm, network, {
mac = '', mac = '',
mtu = network.MTU,
position = undefined, position = undefined,
currently_attached = true,
device = position != null ? String(position) : undefined, device = position != null ? String(position) : undefined,
ipv4_allowed = undefined, ipv4_allowed = undefined,
ipv6_allowed = undefined, ipv6_allowed = undefined,
locking_mode = undefined, locking_mode = undefined,
MAC = mac, MAC = mac,
MTU = mtu,
other_config = {}, other_config = {},
qos_algorithm_params = {}, qos_algorithm_params = {},
qos_algorithm_type = '' qos_algorithm_type = ''
@ -1824,7 +1823,7 @@ export default class Xapi extends XapiBase {
ipv6_allowed, ipv6_allowed,
locking_mode, locking_mode,
MAC, MAC,
MTU: asInteger(MTU), MTU: asInteger(network.MTU),
network: network.$ref, network: network.$ref,
other_config, other_config,
qos_algorithm_params, qos_algorithm_params,
@ -1832,18 +1831,13 @@ export default class Xapi extends XapiBase {
VM: vm.$ref VM: vm.$ref
})) }))
if (isVmRunning(vm)) { if (currently_attached && isVmRunning(vm)) {
await this.call('VIF.plug', vifRef) await this.call('VIF.plug', vifRef)
} }
return vifRef return vifRef
} }
// TODO: check whether the VIF was unplugged before.
async _deleteVif (vif) {
await this.call('VIF.destroy', vif.$ref)
}
async createVif (vmId, networkId, opts = undefined) { async createVif (vmId, networkId, opts = undefined) {
return /* await */ this._getOrWaitObject( return /* await */ this._getOrWaitObject(
await this._createVif( await this._createVif(
@ -1854,10 +1848,6 @@ export default class Xapi extends XapiBase {
) )
} }
async deleteVif (vifId) {
await this._deleteVif(this.getObject(vifId))
}
async createNetwork ({ async createNetwork ({
name, name,
description = 'Created with Xen Orchestra', description = 'Created with Xen Orchestra',

View File

@ -3,6 +3,28 @@ import { isEmpty } from '../../utils'
import { makeEditObject } from '../utils' import { makeEditObject } from '../utils'
export default { export default {
async _connectVif (vif) {
await this.call('VIF.plug', vif.$ref)
},
async connectVif (vifId) {
await this._connectVif(this.getObject(vifId))
},
async _deleteVif (vif) {
await this.call('VIF.destroy', vif.$ref)
},
async deleteVif (vifId) {
const vif = this.getObject(vifId)
if (vif.currently_attached) {
await this._disconnectVif(vif)
}
await this._deleteVif(vif)
},
async _disconnectVif (vif) {
await this.call('VIF.unplug_force', vif.$ref)
},
async disconnectVif (vifId) {
await this._disconnectVif(this.getObject(vifId))
},
editVif: makeEditObject({ editVif: makeEditObject({
ipv4Allowed: { ipv4Allowed: {
get: true, get: true,