Skip to content

Commit

Permalink
feat(xo-server): ability to manage VIFs using REST API when creating …
Browse files Browse the repository at this point in the history
…a VM (#8137)
  • Loading branch information
MathieuRA authored Nov 27, 2024
1 parent 01cc733 commit 4082f12
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

> Users must be able to say: “Nice enhancement, I'm eager to test it”
- [REST/VM] When creating a VM, the template's VIFs are created. It is also possible to create more VIFs or delete/update template's VIFs (PR [#8137](https://github.com/vatesfr/xen-orchestra/pull/8137))

### Bug fixes

> Users must be able to say: “I had this issue, happy to know it's fixed”
Expand All @@ -31,4 +33,6 @@
<!--packages-start-->

- xo-server minor

<!--packages-end-->
2 changes: 1 addition & 1 deletion packages/xo-server/src/api/vm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export const create = defer(async function ($defer, params) {
params.tags = paramsTags !== undefined ? paramsTags.concat(resourceSetTags) : resourceSetTags
}

const xapiVm = await xapi.createVm(template._xapiId, params, checkLimits, user.id)
const xapiVm = await xapi.createVm(template._xapiId, params, checkLimits, user.id, { destroyAllVifs: true })
$defer.onFailure(() => xapi.VM_destroy(xapiVm.$ref, { deleteDisks: true, force: true }))

const vm = xapi.xo.addObject(xapiVm)
Expand Down
40 changes: 35 additions & 5 deletions packages/xo-server/src/xapi/mixins/vm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import find from 'lodash/find.js'
import gte from 'lodash/gte.js'
import includes from 'lodash/includes.js'
import isEmpty from 'lodash/isEmpty.js'
import keyBy from 'lodash/keyBy.js'
import lte from 'lodash/lte.js'
import mapToArray from 'lodash/map.js'
import mapValues from 'lodash/mapValues.js'
Expand Down Expand Up @@ -46,7 +47,8 @@ const methods = {
...props
} = {},
checkLimits,
creatorId
creatorId,
{ destroyAllVifs = false } = {}
) {
const installMethod = (() => {
if (installRepository == null) {
Expand Down Expand Up @@ -187,19 +189,47 @@ const methods = {
)
}

// Destroys the VIFs cloned from the template.
await Promise.all(vm.$VIFs.map(vif => this._deleteVif(vif)))
if (destroyAllVifs) {
// Destroys the VIFs cloned from the template.
await Promise.all(vm.$VIFs.map(vif => this._deleteVif(vif)))
}

// Creates the VIFs specified by the user.
if (vifs) {
const devices = await this.call('VM.get_allowed_VIF_devices', vm.$ref)
const _vifsToCreate = []
const _vifsToDestroy = []
const vmVifByDevice = keyBy(vm.$VIFs, 'device')

vifs.forEach(vif => {
if (vif.device === undefined) {
vif.device = devices.shift()
_vifsToCreate.push(vif)
return
}

const vmVif = vmVifByDevice[vif.device]
if (vif.destroy) {
if (vmVif !== undefined) {
_vifsToDestroy.push(vmVif)
}
return
}

if (vmVif !== undefined) {
_vifsToDestroy.push(vmVif)
}
_vifsToCreate.push(vif)
})

await Promise.all(_vifsToDestroy.map(vif => this._deleteVif(vif)))
await Promise.all(
mapToArray(vifs, (vif, index) =>
_vifsToCreate.map(vif =>
this.VIF_create(
{
ipv4_allowed: vif.ipv4_allowed,
ipv6_allowed: vif.ipv6_allowed,
device: devices[index],
device: vif.device,
locking_mode: isEmpty(vif.ipv4_allowed) && isEmpty(vif.ipv6_allowed) ? 'network_default' : 'locked',
MTU: vif.mtu,
network: this.getObject(vif.network).$ref,
Expand Down
15 changes: 15 additions & 0 deletions packages/xo-server/src/xo-mixins/rest-api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,21 @@ export default class RestApi {
name_label: { type: 'string' },
network_config: { type: 'string', optional: true },
template: { type: 'string' },
vifs: {
default: [],
type: 'array',
items: {
type: 'object',
properties: {
destroy: { type: 'boolean', optional: true },
device: { type: 'string', optional: true },
ipv4_allowed: { type: 'array', items: { type: 'string' }, optional: true },
ipv6_allowed: { type: 'array', items: { type: 'string' }, optional: true },
mac: { type: 'string', optional: true },
network: { type: 'string', optional: true },
},
},
},
}
),
emergency_shutdown: async ({ xapiObject }) => {
Expand Down

0 comments on commit 4082f12

Please sign in to comment.