-
Notifications
You must be signed in to change notification settings - Fork 7
/
zdb.ts
162 lines (149 loc) · 5.33 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
import { GridClientConfig } from "../config";
import { events } from "../helpers/events";
import { expose } from "../helpers/expose";
import { validateInput } from "../helpers/validator";
import { TwinDeployment } from "../high_level/models";
import { ZdbHL } from "../high_level/zdb";
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";
class ZdbsModule extends BaseModule {
fileName = "zdbs.json";
workloadTypes = [WorkloadTypes.zdb];
zdb: ZdbHL;
constructor(config: GridClientConfig) {
super(config);
this.zdb = new ZdbHL(config);
}
async _createDeployment(options: ZDBSModel): Promise<TwinDeployment[]> {
const twinDeployments: TwinDeployment[] = [];
const zdbs_names: string[] = [];
const metadata = JSON.stringify({
type: "zdb",
name: options.name,
projectName: this.config.projectName,
});
for (const instance of options.zdbs) {
if (zdbs_names.includes(instance.name))
throw Error(`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,
options.metadata || metadata,
options.description,
instance.solutionProviderId,
);
twinDeployments.push(twinDeployment);
}
return twinDeployments;
}
@expose
@validateInput
@checkBalance
async deploy(options: ZDBSModel) {
if (await this.exists(options.name)) {
throw Error(`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 };
}
@expose
async list() {
return await this._list();
}
async getObj(deploymentName: string) {
const deployments = await this._get(deploymentName);
const workloads = await this._getWorkloadsByTypes(deploymentName, deployments, [WorkloadTypes.zdb]);
const ret = [];
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;
}
@expose
@validateInput
async get(options: ZDBGetModel) {
return await this._get(options.name);
}
@expose
@validateInput
@checkBalance
async delete(options: ZDBDeleteModel) {
events.emit("logs", `Start deleting the ZDB deployment with name ${options.name}`);
return await this._delete(options.name);
}
@expose
@validateInput
@checkBalance
async update(options: ZDBSModel) {
if (!(await this.exists(options.name))) {
throw Error(`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);
}
@expose
@validateInput
@checkBalance
async add_zdb(options: AddZDBModel) {
if (!(await this.exists(options.deployment_name))) {
throw Error(`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 Error(
`There is another zdb with the same name "${options.name}" in this deployment ${options.deployment_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,
oldDeployments[0].metadata,
oldDeployments[0].metadata,
options.solutionProviderId,
);
return await this._add(options.deployment_name, options.node_id, oldDeployments, [twinDeployment]);
}
@expose
@validateInput
@checkBalance
async delete_zdb(options: DeleteZDBModel) {
if (!(await this.exists(options.deployment_name))) {
throw Error(`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 };