Skip to content

Commit

Permalink
fix(xo-server/rest-api/dashboard): fix 'cannot get handler to proxy r…
Browse files Browse the repository at this point in the history
…emote'
  • Loading branch information
MathieuRA committed Sep 30, 2024
1 parent ad8cd37 commit 4797a90
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
16 changes: 16 additions & 0 deletions @xen-orchestra/proxy/app/mixins/remotes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { compose } from '@vates/compose'
import { decorateMethodsWith } from '@vates/decorate-with'
import { deduped } from '@vates/disposable/deduped.js'
import { getHandler } from '@xen-orchestra/fs'
import { RemoteAdapter } from '@xen-orchestra/backups/RemoteAdapter.mjs'

export default class Remotes {
constructor(app) {
Expand Down Expand Up @@ -31,6 +32,21 @@ export default class Remotes {
},
},
],

getTotalBackupSize: [
({ remote }) =>
Disposable.use(this.getHandler(remote), handler => {
const remoteAdapter = new RemoteAdapter(handler, {
debounceResource: app.debounceResource.bind(app),
})
return remoteAdapter.getTotalBackupSize()
}),
{
params: {
remote: { type: 'object' },
},
},
],
},
})
}
Expand Down
13 changes: 13 additions & 0 deletions packages/xo-server/src/xo-mixins/remotes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { synchronized } from 'decorator-synchronized'

import patch from '../patch.mjs'
import { Remotes } from '../models/remote.mjs'
import Disposable from 'promise-toolbox/Disposable'

// ===================================================================

Expand Down Expand Up @@ -297,4 +298,16 @@ export default class {

await this._remotes.remove(id)
}

async getTotalBackupSizeOnRemote(id) {
const remote = await this._getRemote(id)

if (remote.proxy !== undefined) {
return this._app.callProxyMethod(remote.proxy, 'remote.getTotalBackupSize', {
remote,
})
}

return Disposable.use(this._app.getBackupsRemoteAdapter(remote), adapter => adapter.getTotalBackupSize())
}
}
11 changes: 5 additions & 6 deletions packages/xo-server/src/xo-mixins/rest-api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { pipeline } from 'node:stream/promises'
import { json, Router } from 'express'
import { Readable } from 'node:stream'
import cloneDeep from 'lodash/cloneDeep.js'
import Disposable from 'promise-toolbox/Disposable'
import groupBy from 'lodash/groupBy.js'
import path from 'node:path'
import pDefer from 'promise-toolbox/defer'
Expand Down Expand Up @@ -251,9 +250,8 @@ async function _getDashboardStats(app) {
continue
}

const totalBackupSize = await Disposable.use(app.getBackupsRemoteAdapter(backupRepository), adapter =>
adapter.getTotalBackupSize()
)
const totalBackupSize = await app.getTotalBackupSizeOnRemote(backupRepository.id)

const { available, size, used } = backupRepositoryInfo

const isS3 = type === 's3'
Expand Down Expand Up @@ -461,7 +459,8 @@ export default class RestApi {
app.authenticateUser({ token: cookies.authenticationToken ?? cookies.token }, { ip }).then(
({ user }) => {
if (user.permission === 'admin') {
return app.runWithApiContext(user, next)
req.user = user
return next()
}

res.sendStatus(401)
Expand Down Expand Up @@ -657,7 +656,7 @@ export default class RestApi {
params.affinityHost = affinity
params.installRepository = install?.repository

const vm = await $xapi.createVm(template, params, undefined, app.apiContext.user.id)
const vm = await $xapi.createVm(template, params, undefined, req.user.id)
$defer.onFailure.call($xapi, 'VM_destroy', vm.$ref)

if (boot) {
Expand Down

0 comments on commit 4797a90

Please sign in to comment.