Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: REST-API with auto-generated swagger documentation #8249

Draft
wants to merge 23 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add small action
  • Loading branch information
MathieuRA committed Jan 24, 2025
commit ce651df896e46cbe5c8ee7f9b7b3368aca3a378d
15 changes: 10 additions & 5 deletions @xen-orchestra/rest-api/src/abstract/xapi-xo.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ import { XapiXoObject } from '../xoApp.type.js'
import { Controller } from 'tsoa'

export abstract class XapiXoController<T extends XapiXoObject> extends Controller {
#restApi = getRestApi()
#type
protected restApi = getRestApi()
protected type

constructor(type: T['type']) {
super()
this.#type = type
this.type = type
}

// wrap these methods with permission (filter only objects that the users can see)
protected getObjects(): Record<T['id'], T> | undefined {
return this.#restApi.getObjectsByType(this.#type) as Record<T['id'], T> | undefined
return this.restApi.getObjectsByType(this.type) as Record<T['id'], T> | undefined
}
protected getById(id: T['id']): T {
return this.#restApi.getObject(id, this.#type) as T
return this.restApi.getObject(id, this.type)
}

protected getXapiObject(objOrId: T['id'] | T) {
return this.restApi.getXapiObject(objOrId, this.type)
}
// --------------------------------------------------------------------------------

Expand Down
2 changes: 2 additions & 0 deletions @xen-orchestra/rest-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class RestApi {
getObjects
getServers: XoApp['getAllXenServers']
getServer: XoApp['getXenServer']
getXapiObject: XoApp['getXapiObject']
getObjectsByType

constructor(xoApp: XoApp) {
Expand All @@ -31,6 +32,7 @@ class RestApi {
this.getObject = xoApp.getObject.bind(xoApp)
this.getObjects = xoApp.getObjects.bind(xoApp)
this.getServers = () => xoApp.getAllXenServers()
this.getXapiObject = (idOrObj, type) => xoApp.getXapiObject(idOrObj, type)
this.getServer = id => xoApp.getXenServer(id)

// helpers
Expand Down
1 change: 1 addition & 0 deletions @xen-orchestra/rest-api/src/middleware/error.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { noSuchObject } from 'xo-common/api-errors.js'
import { NextFunction, Request, Response } from 'express'

export function errorHandler(err: Error, _req: Request, res: Response, next: NextFunction) {
console.error(err) // use log.error instead
if (noSuchObject.is(err)) {
res.status(404).json({ error: err.message })
return next()
Expand Down
18 changes: 16 additions & 2 deletions @xen-orchestra/rest-api/src/vms/vm.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Get, Path, Route } from 'tsoa'
import { Get, Path, Post, Response, Route } from 'tsoa'

import { XoVm } from './vm.type.js'
import { XapiVm, XoVm } from './vm.type.js'
import { provideSingleton } from '../ioc/helper.js'
import { XapiXoController } from '../abstract/xapi-xo.controller.js'

interface ApiError {
error: string
}

@Route('vms')
@provideSingleton(VmsController)
export class VmsController extends XapiXoController<XoVm> {
Expand All @@ -19,7 +23,17 @@ export class VmsController extends XapiXoController<XoVm> {
}

@Get('{id}')
@Response<ApiError>(404, 'Not found')
@Response<ApiError>(401, 'unautorhized')
public getVm(@Path() id: XoVm['id']) {
return this.getById(id)
}

@Post('{id}/actions/start')
@Response(404, 'Not found')
public startVm(@Path() id: XoVm['id']): void {
const vm = this.restApi.getXapiObject(id, this.type) as XapiVm
// create task and add the possibility to watch/await the task
vm.$callAsync<void>('start', false, false).catch(() => {})
}
}
14 changes: 10 additions & 4 deletions @xen-orchestra/rest-api/src/vms/vm.type.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { XapiXoObject } from '../xoApp.type.js'
import { XapiObject, XapiXoObject } from '../xoApp.type.js'

export interface XapiVm extends XapiObject {
uuid: string
name_label: string
power_state: 'Running' | 'Paused' | 'Halted' | 'Suspended'
}

export interface XoVm extends XapiXoObject {
type: 'VM'

name_label: string
uuid: string
power_state: 'Running' | 'Paused' | 'Halted' | 'Suspended'
name_label: XapiVm['name_label']
uuid: XapiVm['uuid']
power_state: XapiVm['power_state']
}
14 changes: 12 additions & 2 deletions @xen-orchestra/rest-api/src/xoApp.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ interface XoUser extends NonXapiObject {
permission: string
}
// -----

export type XapiObject = {
$ref: string
$id: string
$type: 'VM' | 'pool'
$xapi: any
$call: <T>(method: string, ...args: any) => Promise<T>
$callAsync: <T>(method: string, ...args: any) => Promise<T>
}
/**
* XapiXoObject can be every "xapi-to-xo" object
*/
export type XapiXoObject = {
id: string
type: 'VM' | 'pool'
type: XapiObject['$type']
}

/**
Expand Down Expand Up @@ -53,6 +60,9 @@ export interface XoApp extends EventEmitter {
getObjects: (opts?: { filter?: (obj: XapiXoObject) => boolean }) => Record<XapiXoObject['id'], XapiXoObject>
getObject: <T extends XapiXoObject>(id: T['id'], type: T['type']) => T

// how to return the right type for getXapiObject?
getXapiObject: <T extends XapiObject>(objOrId: XapiXoObject['id'] | XapiXoObject, type: XapiXoObject['type']) => T

getAllXenServers: () => Promise<XoServer[]>
getXenServer: (id: XoServer['id']) => Promise<XoServer>

Expand Down
Loading