Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuRA committed Feb 28, 2025
1 parent b9f3156 commit 1339d8c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions @xen-orchestra/rest-api/.USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The REST API is based on the `TSOA` framework and therefore we use decorators a
@Routes('foo')
@Security('*')
@Response(401)
@Tags('vms')
@provide(Foo)
class Foo extends Controller {}
```
Expand Down
1 change: 1 addition & 0 deletions @xen-orchestra/rest-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The REST API is based on the `TSOA` framework and therefore we use decorators a
@Routes('foo')
@Security('*')
@Response(401)
@Tags('vms')
@provide(Foo)
class Foo extends Controller {}
```
Expand Down
8 changes: 4 additions & 4 deletions @xen-orchestra/rest-api/src/rest-api/rest-api.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export class RestApi {
this.#xoApp = xoApp
}

get xoApp() {
return this.#xoApp
}

authenticateUser(...args: Parameters<XoApp['authenticateUser']>) {
return this.#xoApp.authenticateUser(...args)
}
Expand All @@ -21,10 +25,6 @@ export class RestApi {
return this.#xoApp.getObjectsByType(type, opts)
}

getXapiVmStats(...args: Parameters<XoApp['getXapiVmStats']>) {
return this.#xoApp.getXapiVmStats(...args)
}

runWithApiContext(...args: Parameters<XoApp['runWithApiContext']>) {
return this.#xoApp.runWithApiContext(...args)
}
Expand Down
18 changes: 11 additions & 7 deletions @xen-orchestra/rest-api/src/vms/vm.controller.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Example, Get, Path, Query, Request, Response, Route, Security } from 'tsoa'
import { Example, Get, Path, Query, Request, Response, Route, Security, Tags } from 'tsoa'
import { Request as ExRequest } from 'express'
import { inject } from 'inversify'
import { invalidParameters } from 'xo-common/api-errors.js'
import { incorrectState, invalidParameters } from 'xo-common/api-errors.js'
import { provide } from 'inversify-binding-decorators'
import type { XapiStatsGranularity, XapiVmStats, XoVm } from '@vates/types'

Expand All @@ -13,6 +13,7 @@ import { XapiXoController } from '../abstract-classes/xapi-xo-controller.mjs'
@Route('vms')
@Security('*')
@Response(401, 'Authentication required')
@Tags('vms')
// the `provide` decorator is mandatory on class that injects/receives dependencies.
// It automatically bind the class to the IOC container that handles dependency injection
@provide(VmController)
Expand Down Expand Up @@ -59,14 +60,17 @@ export class VmController extends XapiXoController<XoVm> {
@Example(vmStatsExample)
@Get('{id}/stats')
@Response(404, 'VM not found')
@Response(422, 'VM is halted or host could not be found')
@Response(422, 'Invalid granularity')
@Response(422, 'Invalid granularity, VM is halted or host could not be found')
async getVmStats(@Path() id: string, @Query() granularity?: XapiStatsGranularity): Promise<XapiVmStats> {
try {
return await this.restApi.getXapiVmStats(id as XoVm['id'], granularity)
return await this.restApi.xoApp.getXapiVmStats(id as XoVm['id'], granularity)
} catch (error) {
if (error instanceof Error && error.message === `VM ${id} is halted or host could not be found.`) {
/* throw */ invalidParameters(error.message, error)
if (
incorrectState.is(error, {
property: 'resident_on',
})
) {
/* throw */ invalidParameters(`VM ${id} is halted or host could not be found.`, error)
}
throw error
}
Expand Down
8 changes: 7 additions & 1 deletion packages/xo-server/src/xapi-stats.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import sum from 'lodash/sum.js'
import uniq from 'lodash/uniq.js'
import zipWith from 'lodash/zipWith.js'
import { BaseError } from 'make-error'
import { incorrectState } from 'xo-common/api-errors.js'
import { parseDateTime } from '@xen-orchestra/xapi'

export class FaultyGranularity extends BaseError {}
Expand Down Expand Up @@ -388,7 +389,12 @@ export default class XapiStats {
const vm = xapi.getObject(vmId)
const host = vm.$resident_on
if (!host) {
throw new Error(`VM ${vmId} is halted or host could not be found.`)
/* throw */ incorrectState({
actual: host,
expected: '<host-uuid>',
object: vm,
property: 'resident_on',
})
}

return this._getAndUpdateStats(xapi, {
Expand Down

0 comments on commit 1339d8c

Please sign in to comment.