-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.ts
242 lines (216 loc) · 5.9 KB
/
api.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
import { BaseAPI } from "./base";
import { fetcher } from "./utils";
export enum ErrorCode {
"global/unknown",
"global/invalid-api-key",
"global/account-inactive",
"global/invalid-parameters",
"global/object-does-not-exist",
"instance-operations/launch/insufficient-capacity",
"instance-operations/launch/file-system-in-wrong-region",
"instance-operations/launch/file-systems-not-supported",
"ssh-keys/key-in-use",
}
export interface Error {
code: ErrorCode;
message: string;
suggestion: string | null;
}
export interface ErrorResponse {
error: Error;
field_errors: {
[key: string]: Error;
};
}
export interface InstanceType {
name: string;
description: string;
price_cents_per_hour: string;
specs: {
vcpus: number;
memory_gib: number;
storage_gib: number;
};
}
export interface Region {
name: string;
description: string;
}
export interface ListInstanceTypes {
[key: string]: {
instance_type: InstanceType;
regions_with_capacity_available: Region[];
};
}
export interface Instance {
id: string;
name: string;
ip: string;
status: string;
ssh_key_names: string[];
file_system_names: string[];
region: Region;
instance_type: InstanceType;
hostname: string;
jupyter_token: string;
jupyter_url: string;
}
export interface SSHKey {
id: string;
name: string;
public_key: string;
}
export interface SSHKeyWithPrivateKey extends SSHKey {
private_key: string;
}
export interface LaunchInstanceConfiguration {
region_name: string;
instance_type_name: string;
ssh_key_names: [string];
file_system_names?: string[];
quantity?: number;
name: string | null;
}
export interface LaunchInstance {
instance_ids: string[];
}
export interface AddSSHKeyConfiguration {
name: string;
public_key?: string;
}
export interface User {
id: string;
email: string;
status: string;
}
export interface FileSystem {
id: string;
name: string;
created: string;
created_by: User;
mount_point: string;
region: Region;
is_in_use: Boolean;
}
export interface RestartedInstances {
restarted_instances: Instance[];
}
export interface TerminatedInstances {
terminated_instances: Instance[];
}
export class LambdaCloudAPI extends BaseAPI {
/**
* List instance types
* @returns list of instance types, or an empty array if there are none
* @throws {ErrorResponse} if missing permissions
*/
async listInstanceTypes(): Promise<ListInstanceTypes> {
return fetcher<ListInstanceTypes>(
this.configuration,
"/instance-types",
"GET"
);
}
/**
* List running instances
* @returns list of running instances, or an empty array if there are none
*/
async listRunningInstances(): Promise<Instance[]> {
return fetcher<Instance[]>(this.configuration, "/instances", "GET");
}
/**
* Get a running instance by ID
* @param id the ID of the instance to get
* @returns the instance, or null if it does not exist
* @throws {ErrorResponse} if the instance does not exist
*/
async getRunningInstance(id: string): Promise<Instance> {
return fetcher<Instance>(this.configuration, `/instances/${id}`, "GET");
}
/**
* Launch instances
* @param config instance configuration
* @returns object with list of launched instances
* @throws {ErrorResponse} if invalid parameters or missing permissions
* @throws {ErrorResponse} if there is insufficient capacity in the region
*/
async launchInstance(
config: LaunchInstanceConfiguration
): Promise<LaunchInstance> {
return fetcher<LaunchInstance>(
this.configuration,
"/instance-operations/launch",
"POST",
JSON.stringify(config)
);
}
/**
* Terminate instances
* @param instanceIds list of instance IDs to terminate
* @returns object with list of terminated instances
*/
async terminateInstances(
instanceIds: string[] | string
): Promise<TerminatedInstances> {
return fetcher<TerminatedInstances>(
this.configuration,
"/instance-operations/terminate",
"POST",
JSON.stringify({ instance_ids: instanceIds })
);
}
/**
* Restart instances
* @param instanceIds list of instance IDs to restart
* @returns object with list of restarted instances
* @throws {ErrorResponse} if invalid parameters or missing permissions
*/
async restartInstances(instanceIds: string[]): Promise<RestartedInstances> {
return fetcher<RestartedInstances>(
this.configuration,
"/instance-operations/restart",
"POST",
JSON.stringify({ instance_ids: instanceIds })
);
}
/**
* List SSH keys
* @returns list of SSH keys, or an empty array if there are none
* @throws {ErrorResponse} if missing permissions
*/
async listSSHKeys(): Promise<SSHKey[]> {
return fetcher<SSHKey[]>(this.configuration, "/ssh-keys", "GET");
}
/**
* Add a new SSH key
* @param config SSH key configuration. If `public_key` is not provided, a new key pair will be generated and the private key will be returned.
* @returns the SSH key, with the private key if it was generated
* @throws {ErrorResponse} if invalid parameters or missing permissions
*/
async addSSHKey(
config: AddSSHKeyConfiguration
): Promise<SSHKeyWithPrivateKey | SSHKey> {
return fetcher<SSHKey | SSHKeyWithPrivateKey>(
this.configuration,
"/ssh-keys",
"POST",
JSON.stringify(config)
);
}
/**
* Delete an SSH key
* @param id the ID of the SSH key to delete
* @returns nothing
* @throws {ErrorResponse} if the SSH key does not exist or missing permissions
*/
async deleteSSHKey(id: string): Promise<void> {
return fetcher<void>(this.configuration, `/ssh-keys/${id}`, "DELETE");
}
/**
* List file systems
* @returns list of file systems, or an empty array if there are none
*/
async listFileSystems(): Promise<FileSystem[]> {
return fetcher<FileSystem[]>(this.configuration, "/file-systems", "GET");
}
}