-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.ts
185 lines (158 loc) · 4.56 KB
/
Controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Backend/src/modules/Controller/vCenter/index.ts
import { loginVCSA, vCenter } from 'ts-vcenter';
// @ts-ignore
import { controllerMethod, ControllerModule } from 'API/Controller/Decorators';
// @ts-ignore
import {
CreateNodeInput,
Host,
InitControllerParams,
Library,
LibraryItem,
LoginControllerParams,
Network,
Node,
NodeFilter,
NodeInfo,
powerNodeType,
Storage,
NodeOS,
NodePower
} from 'API/Controller/types';
// @ts-ignore
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;
enum LibraryItemType {
'vm-template' = 'nodeTemplate',
'iso' = 'iso',
'ovf' = 'templateFile',
'ova' = 'templateFile',
'file' = 'file',
}
enum VMOS {
'UBUNTU_64' = 'UBUNTU',
}
enum VMPower {
'POWERED_OFF' = 'OFF',
'POWERED_ON' = 'ON',
'SUSPENDED' = 'SUSPENDED',
}
/**
* TypeScript Example Controller Module
*/
@ControllerModule({ name: 'vCenter' })
export class vCenterModule {
public vcsa: vCenter;
@controllerMethod({ type: 'listNodes' })
public async getNodes(filter?: NodeFilter): Promise<Node[]> {
const VMs = await this.vcsa.getVMs(filter);
return VMs.map(({ name, vm, power_state }) => ({
name,
id: vm,
power: NodePower[VMPower[power_state]],
}));
}
@controllerMethod({ type: 'listHosts' })
public async listHosts(): Promise<Host[]> {
const Hosts = await this.vcsa.getHosts();
return Hosts.map(({ name, host }) => ({ name, id: host }));
}
@controllerMethod({ type: 'listNetworks' })
public async listNetworks(): Promise<Network[]> {
const Networks = await this.vcsa.getNetworks();
return Networks.map(({ name, network }) => ({ name, id: network }));
}
@controllerMethod({ type: 'listStorage' })
public async listStorage(): Promise<Storage[]> {
const DS = await this.vcsa.getDataStores();
return DS.map(({ name, datastore }) => ({ name, id: datastore }));
}
@controllerMethod({ type: 'getNodeInfo' })
public async getVMInfo(nodeId: string): Promise<NodeInfo> {
const guestInfo = await this.vcsa.getGuestInfo(nodeId);
return {
network: { host: guestInfo.ip_address },
OS: NodeOS[VMOS[guestInfo.name as keyof typeof VMOS]],
};
}
@controllerMethod({ type: 'powerNode' })
public async powerVM(nodeId: string, state: powerNodeType): Promise<boolean> {
try {
await this.vcsa.powerVM(nodeId, state);
return true;
} catch {
return false;
}
}
@controllerMethod({ type: 'loginController' })
public async loginvCenter({
host,
...params
}: LoginControllerParams): Promise<string> {
return loginVCSA({
...params,
url: host,
});
}
@controllerMethod({ type: 'listLibraries' })
public async listContentLibraries(): Promise<Library[]> {
const Libraries = await this.vcsa.getContentLibrarys();
return Libraries.map(({ info: { name, id, description }, items }) => ({
name,
id,
description,
items: items.map(({ type, ...item }) => ({
...item,
type: LibraryItemType[type as keyof typeof LibraryItemType],
})),
}));
}
@controllerMethod({ type: 'getLibraryItem' })
public async getContentLibraryItem(itemId: string): Promise<LibraryItem> {
const item = await this.vcsa.getContentLibraryItem(itemId);
return {
...item,
type: LibraryItemType[item.type as keyof typeof LibraryItemType],
};
}
@controllerMethod({ type: 'initController' })
public async initController({
token,
host,
}: InitControllerParams): Promise<void> {
this.vcsa = new vCenter({ url: host, token });
}
@controllerMethod({ type: 'createNode' })
public async createNode(input: CreateNodeInput): Promise<Node> {
// const [Libraries] = await Promise.all([this.vcsa.getContentLibrarys()]);
const Folder = await this.vcsa.getFolders({
type: 'VIRTUAL_MACHINE',
names: 'vm',
});
if (!Folder) throw new Error();
const VMTemplate = await this.vcsa.getVMTemplate(input.coreTemplate);
const vmId = await this.vcsa.depoyVMTemplate(input.coreTemplate, {
placement: { host: input.host, folder: Folder[0].folder },
hardware_customization: {
nics: [
{
key: VMTemplate.nics[0].key,
value: {
network: input.network,
},
},
],
},
name: input.name,
disk_storage: { datastore: input.storage },
vm_home_storage: {
datastore: input.storage,
},
});
const NewVM = await this.vcsa.getVM(vmId);
return {
name: NewVM.name,
id: vmId,
power: NodePower[VMPower[NewVM.power_state]],
};
}
}