-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfarms.ts
More file actions
145 lines (135 loc) · 5.43 KB
/
farms.ts
File metadata and controls
145 lines (135 loc) · 5.43 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
import { Farm } from "@threefold/tfchain_client";
import { TFClient } from "../clients/tf-grid/client";
import { GridClientConfig } from "../config";
import { expose } from "../helpers/expose";
import { validateInput } from "../helpers/validator";
import {
AddFarmIPModel,
AddStellarAddressToFarmModel,
CreateFarmModel,
FarmIdModel,
RemoveFarmIPModel,
RemoveFarmIPsModel,
} from "./models";
import { checkBalance } from "./utils";
class Farms {
client: TFClient;
/**
* Represents a class that handles operations related to farms.
* This class provides methods to create farms, add/remove farm IPs, add Stellar addresses to farms,
* get farms by ID, and perform balance checks before executing operations.
*
* @param {GridClientConfig} config - The configuration object for initializing the client.
*/
constructor(public config: GridClientConfig) {
this.client = config.tfclient;
}
/**
* Creates a new farm using the provided options.
*
* This method first checks the balance to ensure there are enough funds available.
*
* @param {CreateFarmModel} options - The options for creating the farm, including the name and public IPs.
* @returns {Promise<Farm>} A promise that resolves when the farm creation is successful.
* @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 create(options: CreateFarmModel): Promise<Farm> {
return (await this.client.farms.create(options)).apply();
}
/**
* Adds a new IP address to an existing farm.
*
* This method first validates the input, checks the balance to ensure there are enough funds available,
* and then adds the IP address to the specified farm.
*
* @param {AddFarmIPModel} options - The options for adding the IP address to the farm, including the farm ID, IP, and gateway.
* @returns {Promise<Farm>} A promise that resolves when the IP address is successfully added to the farm.
* @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 addFarmIp(options: AddFarmIPModel): Promise<Farm> {
return (await this.client.farms.addFarmIp(options)).apply();
}
/**
* Removes a specific IP address from an existing farm.
*
* This method first validates the input, checks the balance to ensure there are enough funds available,
* and then removes the specified IP address from the farm.
*
* @param {RemoveFarmIPModel} options - The options for removing the IP address from the farm, including the farm ID and IP.
* @returns {Promise<unknown>} A promise that resolves when the IP address is successfully removed from the farm.
* @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 removeFarmIp(options: RemoveFarmIPModel): Promise<Farm> {
return (await this.client.farms.removeFarmIp(options)).apply();
}
/**
* Removes farm IPs
*
* @param {RemoveFarmIPModel[]} options - An array of options used to remove farm IPs
* @returns {Promise<void>} A promise that resolves when all farm IPs have been removed
* @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 removeFarmIps(options: RemoveFarmIPsModel): Promise<void> {
return await this.client.farms.removeFarmIps(options);
}
/**
* Adds a `Stellar` address to a farm.
*
* This method first validates the input and checks the balance to ensure there are enough funds available.
* It then adds the `Stellar` address to the specified farm.
*
* @param {AddStellarAddressToFarmModel} options - The options for adding the `Stellar` address to the farm, including the `farm ID` and `Stellar address`.
* @returns {Promise<number>} A promise that resolves number as the `Farm ID` after the `Stellar` address is successfully added to the farm.
* @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 addStellarAddress(options: AddStellarAddressToFarmModel): Promise<number> {
return (await this.client.farms.addStellarAddress(options)).apply();
}
/**
* Retrieves a farm by its ID.
*
* This method fetches the farm details based on the provided farm ID.
*
* @param {FarmIdModel} options - The options containing the ID of the farm to retrieve.
* @returns {Promise<Farm>} A promise that resolves with the farm details.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getFarmByID(options: FarmIdModel): Promise<Farm> {
return this.client.farms.get(options);
}
}
export { Farms as farms };