@xen-orchestra/rest-api 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,12 @@ export class RestApi {
3
3
  constructor(xoApp) {
4
4
  this.#xoApp = xoApp;
5
5
  }
6
+ get tasks() {
7
+ return this.#xoApp.tasks;
8
+ }
9
+ get xoApp() {
10
+ return this.#xoApp;
11
+ }
6
12
  authenticateUser(...args) {
7
13
  return this.#xoApp.authenticateUser(...args);
8
14
  }
@@ -12,6 +18,9 @@ export class RestApi {
12
18
  getObjectsByType(type, opts) {
13
19
  return this.#xoApp.getObjectsByType(type, opts);
14
20
  }
21
+ getXapiObject(maybeId, type) {
22
+ return this.#xoApp.getXapiObject(maybeId, type);
23
+ }
15
24
  runWithApiContext(...args) {
16
25
  return this.#xoApp.runWithApiContext(...args);
17
26
  }
@@ -0,0 +1,60 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
8
+ return function (target, key) { decorator(target, key, paramIndex); }
9
+ };
10
+ import { Example, Get, Path, Query, Request, Response, Route, Security, Tags } from 'tsoa';
11
+ import { provide } from 'inversify-binding-decorators';
12
+ import { notFoundResp, unauthorizedResp } from '../open-api/common/response.common.mjs';
13
+ import { partialServers, server, serverIds } from '../open-api/oa-examples/server.oa-example.mjs';
14
+ import { XoController } from '../abstract-classes/xo-controller.mjs';
15
+ let ServerController = class ServerController extends XoController {
16
+ // --- abstract methods
17
+ getAllCollectionObjects() {
18
+ return this.restApi.xoApp.getAllXenServers();
19
+ }
20
+ getCollectionObject(id) {
21
+ return this.restApi.xoApp.getXenServer(id);
22
+ }
23
+ /**
24
+ * @example fields "status,id"
25
+ * @example filter "status:/^connected$/"
26
+ * @example limit 42
27
+ */
28
+ async getServers(req, fields, filter, limit) {
29
+ return this.sendObjects(Object.values(await this.getObjects({ filter, limit })), req);
30
+ }
31
+ /**
32
+ * @example id "f07ab729-c0e8-721c-45ec-f11276377030"
33
+ */
34
+ getServer(id) {
35
+ return this.getObject(id);
36
+ }
37
+ };
38
+ __decorate([
39
+ Example(serverIds),
40
+ Example(partialServers),
41
+ Get(''),
42
+ __param(0, Request()),
43
+ __param(1, Query()),
44
+ __param(2, Query()),
45
+ __param(3, Query())
46
+ ], ServerController.prototype, "getServers", null);
47
+ __decorate([
48
+ Example(server),
49
+ Get('{id}'),
50
+ Response(notFoundResp.status, notFoundResp.description),
51
+ __param(0, Path())
52
+ ], ServerController.prototype, "getServer", null);
53
+ ServerController = __decorate([
54
+ Route('servers'),
55
+ Security('*'),
56
+ Response(unauthorizedResp.status, unauthorizedResp.description),
57
+ Tags('servers'),
58
+ provide(ServerController)
59
+ ], ServerController);
60
+ export { ServerController };
@@ -0,0 +1,59 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
8
+ return function (target, key) { decorator(target, key, paramIndex); }
9
+ };
10
+ import { Example, Get, Path, Query, Request, Response, Route, Security, Tags } from 'tsoa';
11
+ import { inject } from 'inversify';
12
+ import { provide } from 'inversify-binding-decorators';
13
+ import { notFoundResp, unauthorizedResp } from '../open-api/common/response.common.mjs';
14
+ import { partialSrs, sr, srIds } from '../open-api/oa-examples/sr.oa-example.mjs';
15
+ import { RestApi } from '../rest-api/rest-api.mjs';
16
+ import { XapiXoController } from '../abstract-classes/xapi-xo-controller.mjs';
17
+ let SrController = class SrController extends XapiXoController {
18
+ constructor(restApi) {
19
+ super('SR', restApi);
20
+ }
21
+ /**
22
+ * @example fields "uuid,name_label,allocationStrategy"
23
+ * @example filter "allocationStrategy:thin"
24
+ * @example limit 42
25
+ */
26
+ getSrs(req, fields, filter, limit) {
27
+ return this.sendObjects(Object.values(this.getObjects({ filter, limit })), req);
28
+ }
29
+ /**
30
+ * @example id "c4284e12-37c9-7967-b9e8-83ef229c3e03"
31
+ */
32
+ getSr(id) {
33
+ return this.getObject(id);
34
+ }
35
+ };
36
+ __decorate([
37
+ Example(srIds),
38
+ Example(partialSrs),
39
+ Get(''),
40
+ __param(0, Request()),
41
+ __param(1, Query()),
42
+ __param(2, Query()),
43
+ __param(3, Query())
44
+ ], SrController.prototype, "getSrs", null);
45
+ __decorate([
46
+ Example(sr),
47
+ Get('{id}'),
48
+ Response(notFoundResp.status, notFoundResp.description),
49
+ __param(0, Path())
50
+ ], SrController.prototype, "getSr", null);
51
+ SrController = __decorate([
52
+ Route('srs'),
53
+ Security('*'),
54
+ Response(unauthorizedResp.status, unauthorizedResp.description),
55
+ Tags('srs'),
56
+ provide(SrController),
57
+ __param(0, inject(RestApi))
58
+ ], SrController);
59
+ export { SrController };
@@ -0,0 +1,61 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
8
+ return function (target, key) { decorator(target, key, paramIndex); }
9
+ };
10
+ import { Example, Get, Path, Query, Request, Response, Route, Security, Tags } from 'tsoa';
11
+ import { inject } from 'inversify';
12
+ import { provide } from 'inversify-binding-decorators';
13
+ import { notFoundResp, unauthorizedResp } from '../open-api/common/response.common.mjs';
14
+ import { partialVbds, vbd, vbdIds } from '../open-api/oa-examples/vbd.oa-example.mjs';
15
+ import { RestApi } from '../rest-api/rest-api.mjs';
16
+ import { XapiXoController } from '../abstract-classes/xapi-xo-controller.mjs';
17
+ let VbdController = class VbdController extends XapiXoController {
18
+ constructor(restApi) {
19
+ super('VBD', restApi);
20
+ }
21
+ /**
22
+ *
23
+ * @example fields "device,bootable,uuid"
24
+ * @example filter "!bootable?"
25
+ * @example limit 42
26
+ */
27
+ getVbds(req, fields, filter, limit) {
28
+ return this.sendObjects(Object.values(this.getObjects({ filter, limit })), req);
29
+ }
30
+ /**
31
+ *
32
+ * @example id "f07ab729-c0e8-721c-45ec-f11276377030"
33
+ */
34
+ getVbd(id) {
35
+ return this.getObject(id);
36
+ }
37
+ };
38
+ __decorate([
39
+ Example(vbdIds),
40
+ Example(partialVbds),
41
+ Get(''),
42
+ __param(0, Request()),
43
+ __param(1, Query()),
44
+ __param(2, Query()),
45
+ __param(3, Query())
46
+ ], VbdController.prototype, "getVbds", null);
47
+ __decorate([
48
+ Example(vbd),
49
+ Get('{id}'),
50
+ Response(notFoundResp.status, notFoundResp.description),
51
+ __param(0, Path())
52
+ ], VbdController.prototype, "getVbd", null);
53
+ VbdController = __decorate([
54
+ Route('vbds'),
55
+ Security('*'),
56
+ Response(unauthorizedResp.status, unauthorizedResp.description),
57
+ Tags('vbds'),
58
+ provide(VbdController),
59
+ __param(0, inject(RestApi))
60
+ ], VbdController);
61
+ export { VbdController };
@@ -0,0 +1,59 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
8
+ return function (target, key) { decorator(target, key, paramIndex); }
9
+ };
10
+ import { Example, Get, Path, Query, Request, Response, Route, Security, Tags } from 'tsoa';
11
+ import { inject } from 'inversify';
12
+ import { provide } from 'inversify-binding-decorators';
13
+ import { notFoundResp, unauthorizedResp } from '../open-api/common/response.common.mjs';
14
+ import { RestApi } from '../rest-api/rest-api.mjs';
15
+ import { partialVdiSnapshots, vdiSnapshot, vdiSnapshotIds } from '../open-api/oa-examples/vdi-snapshot.oa-example.mjs';
16
+ import { XapiXoController } from '../abstract-classes/xapi-xo-controller.mjs';
17
+ let VdiSnapshotController = class VdiSnapshotController extends XapiXoController {
18
+ constructor(restApi) {
19
+ super('VDI-snapshot', restApi);
20
+ }
21
+ /**
22
+ * @example fields "uuid,snapshot_time,$snapshot_of"
23
+ * @example filter "snapshot_time:>1725020038"
24
+ * @example limit 42
25
+ */
26
+ getVdiSnapshots(req, fields, filter, limit) {
27
+ return this.sendObjects(Object.values(this.getObjects({ filter, limit })), req);
28
+ }
29
+ /**
30
+ * @example id "d2727772-735b-478f-b6f9-11e7db56dfd0"
31
+ */
32
+ getVdiSnapshot(id) {
33
+ return this.getObject(id);
34
+ }
35
+ };
36
+ __decorate([
37
+ Example(vdiSnapshotIds),
38
+ Example(partialVdiSnapshots),
39
+ Get(''),
40
+ __param(0, Request()),
41
+ __param(1, Query()),
42
+ __param(2, Query()),
43
+ __param(3, Query())
44
+ ], VdiSnapshotController.prototype, "getVdiSnapshots", null);
45
+ __decorate([
46
+ Example(vdiSnapshot),
47
+ Get('{id}'),
48
+ Response(notFoundResp.status, notFoundResp.description),
49
+ __param(0, Path())
50
+ ], VdiSnapshotController.prototype, "getVdiSnapshot", null);
51
+ VdiSnapshotController = __decorate([
52
+ Route('vdi-snapshots'),
53
+ Security('*'),
54
+ Response(unauthorizedResp.status, unauthorizedResp.description),
55
+ Tags('vdis'),
56
+ provide(VdiSnapshotController),
57
+ __param(0, inject(RestApi))
58
+ ], VdiSnapshotController);
59
+ export { VdiSnapshotController };
@@ -0,0 +1,59 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
8
+ return function (target, key) { decorator(target, key, paramIndex); }
9
+ };
10
+ import { Example, Get, Path, Query, Request, Response, Route, Security, Tags } from 'tsoa';
11
+ import { inject } from 'inversify';
12
+ import { provide } from 'inversify-binding-decorators';
13
+ import { notFoundResp, unauthorizedResp } from '../open-api/common/response.common.mjs';
14
+ import { RestApi } from '../rest-api/rest-api.mjs';
15
+ import { XapiXoController } from '../abstract-classes/xapi-xo-controller.mjs';
16
+ import { partialVdis, vdi, vdiIds } from '../open-api/oa-examples/vdi.oa-example.mjs';
17
+ let VdiController = class VdiController extends XapiXoController {
18
+ constructor(restApi) {
19
+ super('VDI', restApi);
20
+ }
21
+ /**
22
+ * @example fields "*"
23
+ * @example filter "snapshots:length:>2"
24
+ * @example limit 42
25
+ */
26
+ getVdis(req, fields, filter, limit) {
27
+ return this.sendObjects(Object.values(this.getObjects({ filter, limit })), req);
28
+ }
29
+ /**
30
+ * @example id "c77f9955-c1d2-4b39-aa1c-73cdb2dacb7e"
31
+ */
32
+ getVdi(id) {
33
+ return this.getObject(id);
34
+ }
35
+ };
36
+ __decorate([
37
+ Example(vdiIds),
38
+ Example(partialVdis),
39
+ Get(''),
40
+ __param(0, Request()),
41
+ __param(1, Query()),
42
+ __param(2, Query()),
43
+ __param(3, Query())
44
+ ], VdiController.prototype, "getVdis", null);
45
+ __decorate([
46
+ Example(vdi),
47
+ Get('{id}'),
48
+ Response(notFoundResp.status, notFoundResp.description),
49
+ __param(0, Path())
50
+ ], VdiController.prototype, "getVdi", null);
51
+ VdiController = __decorate([
52
+ Route('vdis'),
53
+ Security('*'),
54
+ Response(unauthorizedResp.status, unauthorizedResp.description),
55
+ Tags('vdis'),
56
+ provide(VdiController),
57
+ __param(0, inject(RestApi))
58
+ ], VdiController);
59
+ export { VdiController };
@@ -7,11 +7,14 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  var __param = (this && this.__param) || function (paramIndex, decorator) {
8
8
  return function (target, key) { decorator(target, key, paramIndex); }
9
9
  };
10
- import { Example, Get, Path, Query, Request, Response, Route, Security } from 'tsoa';
10
+ import { Example, Get, Path, Post, Query, Request, Response, Route, Security, Tags, SuccessResponse } from 'tsoa';
11
11
  import { inject } from 'inversify';
12
+ import { incorrectState, invalidParameters } from 'xo-common/api-errors.js';
12
13
  import { provide } from 'inversify-binding-decorators';
13
- import { partialVms, vm, vmIds } from '../open-api/examples/vm.example.mjs';
14
+ import { actionAsyncroneResp, internalServerErrorResp, noContentResp, notFoundResp, unauthorizedResp, } from '../open-api/common/response.common.mjs';
15
+ import { partialVms, vm, vmIds, vmStatsExample } from '../open-api/oa-examples/vm.oa-example.mjs';
14
16
  import { RestApi } from '../rest-api/rest-api.mjs';
17
+ import { taskLocation } from '../open-api/oa-examples/task.oa-example.mjs';
15
18
  import { XapiXoController } from '../abstract-classes/xapi-xo-controller.mjs';
16
19
  let VmController = class VmController extends XapiXoController {
17
20
  constructor(restApi) {
@@ -33,6 +36,40 @@ let VmController = class VmController extends XapiXoController {
33
36
  getVm(id) {
34
37
  return this.getObject(id);
35
38
  }
39
+ /**
40
+ *
41
+ * VM must be running
42
+ *
43
+ * @example id "f07ab729-c0e8-721c-45ec-f11276377030"
44
+ */
45
+ async getVmStats(id, granularity) {
46
+ try {
47
+ return await this.restApi.xoApp.getXapiVmStats(id, granularity);
48
+ }
49
+ catch (error) {
50
+ if (incorrectState.is(error, {
51
+ property: 'resident_on',
52
+ })) {
53
+ /* throw */ invalidParameters(`VM ${id} is halted or host could not be found.`, error);
54
+ }
55
+ throw error;
56
+ }
57
+ }
58
+ /**
59
+ * @example id "f07ab729-c0e8-721c-45ec-f11276377030"
60
+ */
61
+ async startVm(id, sync) {
62
+ const vmId = id;
63
+ const action = () => this.getXapiObject(vmId).$callAsync('start', false, false);
64
+ return this.createAction(action, {
65
+ sync,
66
+ statusCode: noContentResp.status,
67
+ taskProperties: {
68
+ name: 'start VM',
69
+ objectId: vmId,
70
+ },
71
+ });
72
+ }
36
73
  };
37
74
  __decorate([
38
75
  Example(vmIds),
@@ -46,13 +83,32 @@ __decorate([
46
83
  __decorate([
47
84
  Example(vm),
48
85
  Get('{id}'),
49
- Response(404),
86
+ Response(notFoundResp.status, notFoundResp.description),
50
87
  __param(0, Path())
51
88
  ], VmController.prototype, "getVm", null);
89
+ __decorate([
90
+ Example(vmStatsExample),
91
+ Get('{id}/stats'),
92
+ Response(notFoundResp.status, notFoundResp.description),
93
+ Response(422, 'Invalid granularity, VM is halted or host could not be found'),
94
+ __param(0, Path()),
95
+ __param(1, Query())
96
+ ], VmController.prototype, "getVmStats", null);
97
+ __decorate([
98
+ Example(taskLocation),
99
+ Post('{id}/actions/start'),
100
+ SuccessResponse(actionAsyncroneResp.status, actionAsyncroneResp.description, actionAsyncroneResp.produce),
101
+ Response(noContentResp.status, noContentResp.description),
102
+ Response(notFoundResp.status, notFoundResp.description),
103
+ Response(internalServerErrorResp.status, internalServerErrorResp.description),
104
+ __param(0, Path()),
105
+ __param(1, Query())
106
+ ], VmController.prototype, "startVm", null);
52
107
  VmController = __decorate([
53
108
  Route('vms'),
54
109
  Security('*'),
55
- Response(401)
110
+ Response(unauthorizedResp.status, unauthorizedResp.description),
111
+ Tags('vms')
56
112
  // the `provide` decorator is mandatory on class that injects/receives dependencies.
57
113
  // It automatically bind the class to the IOC container that handles dependency injection
58
114
  ,