-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@xen-orchestra/rest-api): expose get servers and get server
- Loading branch information
Showing
11 changed files
with
163 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
@xen-orchestra/rest-api/src/abstract-classes/base-controller.mts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Controller } from 'tsoa' | ||
import { Request } from 'express' | ||
import { XoRecord } from '@vates/types' | ||
|
||
import { RestApi } from '../rest-api/rest-api.mjs' | ||
import { makeObjectMapper } from '../helpers/object-wrapper.helper.mjs' | ||
import type { WithHref } from '../helpers/helper.type.mjs' | ||
|
||
export abstract class BaseController<T extends XoRecord, IsSync extends boolean> extends Controller { | ||
abstract getObjects(): IsSync extends false ? Promise<Record<T['id'], T>> : Record<T['id'], T> | ||
abstract getObject(id: T['id']): IsSync extends false ? Promise<T> : T | ||
|
||
restApi: RestApi | ||
|
||
constructor(restApi: RestApi) { | ||
super() | ||
this.restApi = restApi | ||
} | ||
|
||
sendObjects(objects: T[], req: Request): string[] | WithHref<T>[] | WithHref<Partial<T>>[] { | ||
const mapper = makeObjectMapper(req) | ||
const mappedObjects = objects.map(mapper) as string[] | WithHref<T>[] | WithHref<Partial<T>>[] | ||
|
||
return mappedObjects | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
@xen-orchestra/rest-api/src/abstract-classes/xo-controller.mts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as CM from 'complex-matcher' | ||
import type { NonXapiXoRecord } from '@vates/types/xo' | ||
|
||
import { BaseController } from './base-controller.mjs' | ||
|
||
import { RestApi } from '../rest-api/rest-api.mjs' | ||
|
||
export abstract class XoController<T extends NonXapiXoRecord> extends BaseController<T, false> { | ||
abstract _abstractGetObjects(): Promise<T[]> | ||
abstract _abstractGetObject(id: T['id']): Promise<T> | ||
|
||
constructor(restApi: RestApi) { | ||
super(restApi) | ||
} | ||
|
||
async getObjects({ | ||
filter, | ||
limit, | ||
}: { | ||
filter?: string | ||
limit?: number | ||
} = {}): Promise<Record<T['id'], T>> { | ||
const _limit = limit ?? Infinity | ||
let objects = await this._abstractGetObjects() | ||
|
||
if (filter !== undefined) { | ||
const predicate = CM.parse(filter).createPredicate() | ||
objects = objects.filter(predicate) | ||
} | ||
|
||
if (_limit < objects.length) { | ||
objects.length = _limit | ||
} | ||
|
||
const objectById = {} as Record<T['id'], T> | ||
|
||
objects.forEach(obj => { | ||
objectById[obj.id] = obj | ||
}) | ||
|
||
return objectById | ||
} | ||
|
||
async getObject(id: T['id']): Promise<T> { | ||
return this._abstractGetObject(id) | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
@xen-orchestra/rest-api/src/helpers/object-wrapper.helper.mts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import type { XoUser, XapiXoRecord } from '@vates/types/xo' | ||
import type { XoUser, XapiXoRecord, XoServer } from '@vates/types/xo' | ||
|
||
export type XoApp = { | ||
authenticateUser: ( | ||
credentials: { token?: string; username?: string; password?: string }, | ||
userData?: { ip?: string }, | ||
opts?: { bypassOtp?: boolean } | ||
) => Promise<{ bypassOtp: boolean; expiration: number; user: XoUser }> | ||
getAllXenServers(): Promise<XoServer[]> | ||
getObject: <T extends XapiXoRecord>(id: T['id'], type: T['type']) => T | ||
getObjectsByType: <T extends XapiXoRecord>( | ||
type: T['type'], | ||
opts?: { filter?: string; limit?: number } | ||
) => Record<T['id'], T> | ||
getXenServer(id: XoServer['id']): Promise<XoServer> | ||
runWithApiContext: (user: XoUser, fn: () => void) => Promise<unknown> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Get, Path, Query, Request, Response, Route, Security, Tags } from 'tsoa' | ||
import { Request as ExRequest } from 'express' | ||
import { inject } from 'inversify' | ||
import { provide } from 'inversify-binding-decorators' | ||
import type { XoServer } from '@vates/types' | ||
|
||
import { RestApi } from '../rest-api/rest-api.mjs' | ||
import type { Unbrand, WithHref } from '../helpers/helper.type.mjs' | ||
import { XoController } from '../abstract-classes/xo-controller.mjs' | ||
|
||
@Route('servers') | ||
@Security('*') | ||
@Response(401) | ||
@Tags('servers') | ||
@provide(ServerController) | ||
export class ServerController extends XoController<XoServer> { | ||
// --- abstract methods | ||
_abstractGetObjects(): Promise<XoServer[]> { | ||
return this.restApi.getAllXenServers() | ||
} | ||
_abstractGetObject(id: XoServer['id']): Promise<XoServer> { | ||
return this.restApi.getXenServer(id) | ||
} | ||
|
||
constructor(@inject(RestApi) restApi: RestApi) { | ||
super(restApi) | ||
} | ||
|
||
/** | ||
* @example fields "status,uuid" | ||
* @example filter "status:/^connected$/" | ||
* @example limit 42 | ||
*/ | ||
@Get('') | ||
async getServers( | ||
@Request() req: ExRequest, | ||
@Query() fields?: string, | ||
@Query() filter?: string, | ||
@Query() limit?: number | ||
): Promise<string[] | WithHref<Unbrand<XoServer>>[] | WithHref<Partial<Unbrand<XoServer>>>[]> { | ||
const servers = Object.values(await this.getObjects({ filter, limit })) | ||
return this.sendObjects(servers, req) | ||
} | ||
|
||
/** | ||
* @example id "f07ab729-c0e8-721c-45ec-f11276377030" | ||
*/ | ||
@Get('{id}') | ||
getServer(@Path() id: string) { | ||
return this.getObject(id as XoServer['id']) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters