-
Notifications
You must be signed in to change notification settings - Fork 8
/
zdb.ts
301 lines (286 loc) · 12.1 KB
/
zdb.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import { Contract } from "@threefold/tfchain_client";
import { ValidationError } from "@threefold/types";
import { GridClientConfig } from "../config";
import { events } from "../helpers/events";
import { expose } from "../helpers/expose";
import { validateInput } from "../helpers/validator";
import { DeploymentResultContracts, TwinDeployment } from "../high_level/models";
import { ZdbHL } from "../high_level/zdb";
import { Deployment } from "../zos";
import { WorkloadTypes } from "../zos/workload";
import { Zdb, ZdbResult } from "../zos/zdb";
import { BaseModule } from "./base";
import { AddZDBModel, DeleteZDBModel, ZDBDeleteModel, ZDBGetModel, ZDBSModel } from "./models";
import { checkBalance } from "./utils";
export interface ZdbData {
version: number;
contractId: number;
nodeId: number;
name: string;
created: number;
status: string;
message: string;
size: number;
mode: string;
publicNamespace: boolean;
password: string;
metadata: string;
description: string;
resData: ZdbResult;
}
class ZdbsModule extends BaseModule {
moduleName = "zdb";
workloadTypes = [WorkloadTypes.zdb];
zdb: ZdbHL;
/**
* Represents a module for managing ZDB deployments.
*
* This class extends the BaseModule class and provides methods for creating, updating, listing, and deleting ZDB deployments.
*
* @class ZdbsModule
* @param {GridClientConfig} config - The configuration object for initializing the client.
*/
constructor(public config: GridClientConfig) {
super(config);
this.zdb = new ZdbHL(config);
}
/**
* Creates ZDB deployments based on the provided options.
*
* @param {ZDBSModel} options - The options for creating ZDB deployments.
* @returns {Promise<TwinDeployment[]>} - A promise that resolves to an array of TwinDeployment objects representing the created deployments.
*/
async _createDeployment(options: ZDBSModel): Promise<TwinDeployment[]> {
const twinDeployments: TwinDeployment[] = [];
const zdbs_names: string[] = [];
const contractMetadata = JSON.stringify({
version: 3,
type: "zdb",
name: options.name,
projectName: this.config.projectName || `zdb/${options.name}`,
});
for (const instance of options.zdbs) {
if (zdbs_names.includes(instance.name))
throw new ValidationError(`Another zdb with the same name ${instance.name} already exists.`);
zdbs_names.push(instance.name);
const twinDeployment = await this.zdb.create(
instance.name,
instance.node_id,
instance.disk_size,
instance.mode,
instance.password,
instance.publicNamespace,
contractMetadata,
options.metadata,
options.description,
instance.solutionProviderId,
);
twinDeployments.push(twinDeployment);
}
return twinDeployments;
}
/**
* Creates ZDB deployments based on the provided options.
*
* This method checks if a ZDB deployment with the same name already exists, emits a log event, creates the ZDB deployment, handles the twin deployments, saves the contracts, and returns the contracts.
*
* @param {ZDBSModel} options - The options for creating ZDB deployments.
* @returns {Promise<{ contracts: DeploymentResultContracts }>} - A promise that resolves to an object containing the contracts of the created deployments.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
* - `@checkBalance`: Checks the balance to ensure there are enough funds available.
*/
@expose
@validateInput
@checkBalance
async deploy(options: ZDBSModel): Promise<{ contracts: DeploymentResultContracts }> {
if (await this.exists(options.name)) {
throw new ValidationError(`Another zdb deployment with the same name ${options.name} already exists.`);
}
events.emit("logs", `Start creating the ZDB deployment with name ${options.name}`);
const twinDeployments = await this._createDeployment(options);
const contracts = await this.twinDeploymentHandler.handle(twinDeployments);
await this.save(options.name, contracts);
return { contracts: contracts };
}
/**
* Retrieves a list of ZDB deployments.
*
* This method fetches and returns a list of ZDB deployments.
*
* @returns {Promise<string[]>} - A promise that resolves to an array of string representing the ZDB deployment names.
* @decorators
* - `@expose`: Exposes the method for external use.
*/
@expose
async list(): Promise<string[]> {
return await this._list();
}
/**
* Retrieves information about ZDB deployments based on the provided deployment name.
*
* This method fetches the ZDB deployments associated with the specified deployment name, retrieves relevant information.
*
* @param {string} deploymentName - The name of the ZDB deployment to retrieve information for.
* @returns {Promise<ZdbData[]>} - A promise that resolves to an array of ZdbData objects containing information about the ZDB deployments.
*/
async getObj(deploymentName: string): Promise<ZdbData[]> {
const deployments = await this._get(deploymentName);
const workloads = await this._getWorkloadsByTypes(deploymentName, deployments, [WorkloadTypes.zdb]);
const ret: ZdbData[] = [];
for (const workload of workloads) {
const data = workload.data as Zdb;
ret.push({
version: workload.version,
contractId: workload["contractId"],
nodeId: workload["nodeId"],
name: workload.name,
created: workload.result.created,
status: workload.result.state,
message: workload.result.message,
size: data.size, // GB
mode: data.mode,
publicNamespace: data.public,
password: data.password,
metadata: workload.metadata,
description: workload.description,
resData: workload.result.data as ZdbResult,
});
}
return ret;
}
/**
* Retrieves information about ZDB deployments based on the provided deployment name.
*
* This method fetches the ZDB deployments associated with the specified deployment name, retrieves relevant information.
*
* @param {string} options.name - The name of the ZDB deployment to retrieve information for.
* @returns {Promise<Deployment[]>} - A promise that resolves to an array of ZdbData objects containing information about the ZDB deployments.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async get(options: ZDBGetModel): Promise<Deployment[]> {
return await this._get(options.name);
}
/**
* Deletes a ZDB deployment based on the provided options.
*
* This method emits a log event, starts the deletion process for the ZDB deployment with the specified name, and then deletes the deployment.
*
* @param {ZDBDeleteModel} options - The options for deleting the ZDB deployment.
* @returns { Promise<{created: Contract[];deleted: Contract[];updated: Contract[];}>} - A promise that resolves once the deletion process is completed.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
* - `@checkBalance`: Checks the balance to ensure there are enough funds available.
*/
@expose
@validateInput
@checkBalance
async delete(options: ZDBDeleteModel): Promise<DeploymentResultContracts> {
events.emit("logs", `Start deleting the ZDB deployment with name ${options.name}`);
return await this._delete(options.name);
}
/**
* Updates an existing ZDB deployment with the provided options.
*
* This method first checks if a ZDB deployment with the specified name exists, then retrieves the old deployments, creates new twin deployments based on the provided options, and finally updates the deployment with the new twin deployments.
*
* @param {ZDBSModel} options - The options for updating the ZDB deployment.
* @returns {Promise<{contracts: DeploymentResultContracts;}>} - A promise that resolves once the update process is completed.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
* - `@checkBalance`: Checks the balance to ensure there are enough funds available.
*/
@expose
@validateInput
@checkBalance
async update(options: ZDBSModel): Promise<{
contracts: DeploymentResultContracts;
}> {
if (!(await this.exists(options.name))) {
throw new ValidationError(`There is no zdb deployment with name: ${options.name}.`);
}
const oldDeployments = await this._get(options.name);
const twinDeployments = await this._createDeployment(options);
return await this._update(this.zdb, options.name, oldDeployments, twinDeployments);
}
/**
* Adds a ZDB instance to an existing deployment.
*
* This method first checks if a ZDB deployment with the specified name exists, then retrieves the old deployments.
* It validates if another ZDB with the same name already exists in the deployment.
* If validation passes, it creates a new ZDB instance, emits a log event, and adds the ZDB instance to the deployment.
*
* @param {AddZDBModel} options - The options for adding a ZDB instance to a deployment.
* @returns { Promise<{created: Contract[];deleted: Contract[];updated: Contract[];}>} A promise that resolves once the ZDB instance is successfully added to the deployment.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
* - `@checkBalance`: Checks the balance to ensure there are enough funds available.
*/
@expose
@validateInput
@checkBalance
async add_zdb(options: AddZDBModel): Promise<{
contracts: DeploymentResultContracts;
}> {
if (!(await this.exists(options.deployment_name))) {
throw new ValidationError(`There is no zdb deployment with name: ${options.deployment_name}.`);
}
const oldDeployments = await this._get(options.deployment_name);
if (this.workloadExists(options.name, oldDeployments))
throw new ValidationError(
`There is another zdb with the same name "${options.name}" in this deployment ${options.deployment_name}.`,
);
const contractMetadata = JSON.stringify({
version: 3,
type: "zdb",
name: options.deployment_name,
projectName: this.config.projectName || `zdb/${options.name}`,
});
events.emit("logs", `Start adding ZDB instance: ${options.name} to deployment: ${options.deployment_name}`);
const twinDeployment = await this.zdb.create(
options.name,
options.node_id,
options.disk_size,
options.mode,
options.password,
options.publicNamespace,
contractMetadata,
oldDeployments[0].metadata,
oldDeployments[0].description,
options.solutionProviderId,
);
return await this._add(options.deployment_name, options.node_id, oldDeployments, [twinDeployment]);
}
/**
* Deletes a ZDB instance from a deployment.
*
* This method first checks if a ZDB deployment with the specified name exists, then retrieves the old deployments.
* It emits a log event, starts the deletion process for the ZDB instance with the specified name, and then deletes the instance from the deployment.
*
* @param {DeleteZDBModel} options - The options for deleting the ZDB instance.
* @returns {Promise<DeploymentResultContracts>} - A promise that resolves once the deletion process is completed.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
* - `@checkBalance`: Checks the balance to ensure there are enough funds available.
*/
@expose
@validateInput
@checkBalance
async delete_zdb(options: DeleteZDBModel): Promise<DeploymentResultContracts> {
if (!(await this.exists(options.deployment_name))) {
throw new ValidationError(`There is no zdb deployment with name: ${options.deployment_name}.`);
}
events.emit("logs", `Start deleting ZDB instance: ${options.name} from deployment: ${options.deployment_name}`);
return await this._deleteInstance(this.zdb, options.deployment_name, options.name);
}
}
export { ZdbsModule as zdbs };