-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgateway.ts
More file actions
266 lines (253 loc) · 10.9 KB
/
gateway.ts
File metadata and controls
266 lines (253 loc) · 10.9 KB
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
import { ValidationError } from "@threefold/types";
import { GridClientConfig } from "../config";
import { GatewayDeploymentData } from "../helpers";
import { events } from "../helpers/events";
import { expose } from "../helpers/expose";
import { validateInput } from "../helpers/validator";
import { DeploymentResultContracts } from "../high_level";
import { GatewayHL } from "../high_level/gateway";
import { Deployment } from "../zos";
import { GatewayFQDNProxy, GatewayResult } from "../zos/gateway";
import { WorkloadTypes } from "../zos/workload";
import { BaseModule } from "./base";
import {
GatewayFQDNDeleteModel,
GatewayFQDNGetModel,
GatewayFQDNModel,
GatewayNameDeleteModel,
GatewayNameGetModel,
GatewayNameModel,
} from "./models";
import { checkBalance } from "./utils";
class GWModule extends BaseModule {
moduleName = "gateways";
workloadTypes = [WorkloadTypes.gatewayfqdnproxy, WorkloadTypes.gatewaynameproxy];
gateway: GatewayHL;
/**
* Represents a module for managing gateway deployments.
*
* This class extends the `BaseModule` class and provides methods for deploying, listing, getting, and deleting gateway deployments.
* It includes functionality to deploy gateways with FQDN and name, check balance before deployment, and handle deployment updates.
* The module also supports retrieving specific gateway instances, deleting instances, and getting detailed information about deployments.
*
* @class GWModule
* @param {GridClientConfig} config - The configuration object for initializing the client.
*/
constructor(public config: GridClientConfig) {
super(config);
this.gateway = new GatewayHL(config);
}
/**
* Deploys a gateway workload with FQDN.
*
* This method deploys a gateway workload with the provided FQDN configuration.
*
* It checks if another gateway deployment with the same name already exists,
* emits a log event, creates the gateway deployment, handles the twin deployments,
* saves the contracts, and returns the deployed contracts.
*
* @param {GatewayFQDNModel} options - The options for deploying the gateway with FQDN.
* @returns {Promise<{ contracts: DeploymentResultContracts }>} A Promise that resolves an object containing the deployed contracts.
* @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_fqdn(options: GatewayFQDNModel): Promise<{ contracts: DeploymentResultContracts }> {
if (await this.exists(options.name)) {
throw new ValidationError(`Another gateway deployment with the same name ${options.name} already exists.`);
}
events.emit("logs", `Start creating the gateway deployment with name ${options.name}`);
const contractMetadata = JSON.stringify({
version: 3,
type: "gateway",
name: options.name,
projectName: this.config.projectName,
});
const twinDeployments = await this.gateway.create(
options.name,
options.node_id,
options.tls_passthrough,
options.backends,
options.network,
contractMetadata,
options.metadata,
options.description,
options.fqdn,
options.solutionProviderId,
);
const contracts = await this.twinDeploymentHandler.handle(twinDeployments);
await this.save(options.name, contracts);
return { contracts: contracts };
}
/**
* Deploys a gateway workload with a given name, this name will be used as a subdomain to the gateway's domain.
*
* This method deploys a gateway workload with the provided name configuration.
* It checks if another gateway deployment with the same name already exists,
* emits a log event, creates the gateway deployment, handles the twin deployments,
* saves the contracts, and returns the deployed contracts.
*
* @param {GatewayNameModel} options - The options for deploying the gateway with a specific name.
* @returns {Promise<{ contracts: DeploymentResultContracts }>} A Promise that resolves an object containing the deployed contracts.
* @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_name(options: GatewayNameModel): Promise<{ contracts: DeploymentResultContracts }> {
if (await this.exists(options.name)) {
throw new ValidationError(`Another gateway deployment with the same name ${options.name} already exists.`);
}
events.emit("logs", `Start creating the gateway deployment with name ${options.name}`);
const contractMetadata = JSON.stringify({
version: 3,
type: "gateway",
name: options.name,
projectName: this.config.projectName,
});
const twinDeployments = await this.gateway.create(
options.name,
options.node_id,
options.tls_passthrough,
options.backends,
options.network,
contractMetadata,
options.metadata,
options.description,
"",
options.solutionProviderId,
);
const contracts = await this.twinDeploymentHandler.handle(twinDeployments);
await this.save(options.name, contracts);
return { contracts: contracts };
}
/**
* Retrieves a list of gateway deployments.
*
* This method fetches and returns a list of gateway deployments.
*
* @returns {Promise<string[]>} A Promise that resolves to a list of gateway deployments.
* @decorators
* - `@expose`: Exposes the method for external use.
*/
@expose
async list(): Promise<string[]> {
return await this._list();
}
/**
* Retrieves a gateway deployment with FQDN based on the provided options.
*
* This method fetches and returns detailed information about a specific gateway deployment with FQDN,
* identified by the name specified in the options.
*
* @param {GatewayFQDNGetModel} options - The options specifying the name of the gateway deployment to retrieve.
* @returns {Promise<Deployment[]>} A Promise that resolves to the detailed information of the gateway deployment.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async get_fqdn(options: GatewayFQDNGetModel): Promise<Deployment[]> {
return await this._get(options.name);
}
/**
* Deletes a gateway deployment with FQDN.
*
* This method deletes a gateway deployment with the specified name.
* It emits a log event indicating the start of the deletion process,
* and then proceeds to delete the gateway deployment with the provided name.
*
* @param {GatewayFQDNDeleteModel} options - The options specifying the name of the gateway deployment to delete.
* @returns {Promise<{created: Contract[];deleted: Contract[];updated: Contract[];}>} A Promise that resolves once the gateway deployment is successfully deleted.
* @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_fqdn(options: GatewayFQDNDeleteModel): Promise<DeploymentResultContracts> {
events.emit("logs", `Start deleting the gateway deployment with name ${options.name}`);
return await this._delete(options.name);
}
/**
* Retrieves detailed information about a specific gateway deployment with a given name.
*
* This method fetches and returns detailed information about a specific gateway deployment identified by the name specified in the options.
*
* @param {GatewayNameGetModel} options - The options specifying the name of the gateway deployment to retrieve.
* @returns {Promise<Deployment[]>} A Promise that resolves to the detailed information of the gateway deployment.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async get_name(options: GatewayNameGetModel): Promise<Deployment[]> {
return await this._get(options.name);
}
/**
* Deletes a gateway deployment with a specific name.
*
* This method initiates the deletion process for a gateway deployment identified by the provided name.
* It emits a log event indicating the start of the deletion process and proceeds to delete the gateway deployment with the specified name.
*
* @param {GatewayNameDeleteModel} options - The options specifying the name of the gateway deployment to delete.
* @returns {Promise<{ created: Contract[]; deleted: Contract[]; updated: Contract[]; }>} A Promise that resolves once the gateway deployment is successfully deleted.
* @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_name(options: GatewayNameDeleteModel): Promise<DeploymentResultContracts> {
events.emit("logs", `Start deleting the gateway deployment with name ${options.name}`);
return await this._delete(options.name);
}
/**
* Retrieves detailed information about a specific gateway deployment.
*
* This method fetches and returns detailed information about a specific gateway deployment identified by the provided deployment name.
* It retrieves the necessary data from the deployments and workloads.
*
* @param {string} deploymentName - The name of the gateway deployment to retrieve information for.
* @returns {Promise<GatewayDeploymentData[]>} A Promise that resolves to an array of objects containing detailed information about the gateway deployment.
*/
async getObj(deploymentName: string): Promise<GatewayDeploymentData[]> {
const deployments = await this._get(deploymentName);
const workloads = await this._getWorkloadsByTypes(deploymentName, deployments, [
WorkloadTypes.gatewayfqdnproxy,
WorkloadTypes.gatewaynameproxy,
]);
return workloads.map(workload => {
const data = workload.data as GatewayFQDNProxy;
return {
version: workload.version,
contractId: workload["contractId"],
name: workload.name,
created: workload.result.created,
status: workload.result.state,
message: workload.result.message,
type: workload.type,
domain:
workload.type === WorkloadTypes.gatewayfqdnproxy ? data.fqdn : (workload.result.data as GatewayResult).fqdn,
tls_passthrough: data.tls_passthrough,
backends: data.backends,
metadata: workload.metadata,
description: workload.description,
};
});
}
}
export { GWModule as gateway };