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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
use serde::{Deserialize, Serialize};
use crate::client::Client;
use crate::error::Result;
use crate::keys::StatusResponse;
/// A compute instance template describing available GPU configurations.
#[derive(Debug, Clone, Deserialize)]
pub struct ComputeTemplate {
/// Template identifier (e.g. "a100-80gb", "h100-sxm").
pub id: String,
/// Human-readable name.
#[serde(default)]
pub name: Option<String>,
/// GPU type description.
#[serde(default)]
pub gpu: Option<String>,
/// Number of GPUs.
#[serde(default)]
pub gpu_count: Option<i32>,
/// VRAM per GPU in GB.
#[serde(default)]
pub vram_gb: Option<i32>,
/// CPU cores.
#[serde(default)]
pub vcpus: Option<i32>,
/// RAM in GB.
#[serde(default)]
pub ram_gb: Option<i32>,
/// Price per hour in USD.
#[serde(default)]
pub price_per_hour_usd: Option<f64>,
/// Available zones.
#[serde(default)]
pub zones: Option<Vec<String>>,
}
/// Response from listing compute templates.
#[derive(Debug, Clone, Deserialize)]
pub struct TemplatesResponse {
/// Available compute templates.
pub templates: Vec<ComputeTemplate>,
}
/// Request body for provisioning a compute instance.
#[derive(Debug, Clone, Serialize, Default)]
pub struct ProvisionRequest {
/// Template ID to provision.
pub template: String,
/// Preferred zone (e.g. "us-central1-a").
#[serde(skip_serializing_if = "Option::is_none")]
pub zone: Option<String>,
/// Use spot/preemptible pricing.
#[serde(skip_serializing_if = "Option::is_none")]
pub spot: Option<bool>,
/// Auto-teardown after N minutes of inactivity.
#[serde(skip_serializing_if = "Option::is_none")]
pub auto_teardown_minutes: Option<i32>,
/// SSH public key for access.
#[serde(skip_serializing_if = "Option::is_none")]
pub ssh_public_key: Option<String>,
}
/// Response from provisioning a compute instance.
#[derive(Debug, Clone, Deserialize)]
pub struct ProvisionResponse {
/// Instance identifier.
pub instance_id: String,
/// Current instance status.
pub status: String,
/// Template that was provisioned.
#[serde(default)]
pub template: Option<String>,
/// Zone the instance was placed in.
#[serde(default)]
pub zone: Option<String>,
/// SSH connection address.
#[serde(default)]
pub ssh_address: Option<String>,
/// Estimated price per hour.
#[serde(default)]
pub price_per_hour_usd: Option<f64>,
}
/// A running compute instance.
#[derive(Debug, Clone, Deserialize)]
pub struct ComputeInstance {
/// Instance identifier.
pub id: String,
/// Current status (e.g. "running", "provisioning", "stopped").
pub status: String,
/// Template used.
#[serde(default)]
pub template: Option<String>,
/// Zone.
#[serde(default)]
pub zone: Option<String>,
/// SSH connection address.
#[serde(default)]
pub ssh_address: Option<String>,
/// Creation timestamp.
#[serde(default)]
pub created_at: Option<String>,
/// Price per hour.
#[serde(default)]
pub price_per_hour_usd: Option<f64>,
/// Auto-teardown setting in minutes.
#[serde(default)]
pub auto_teardown_minutes: Option<i32>,
}
/// Detailed compute instance info with GPU, cost, and uptime details.
#[derive(Debug, Clone, Deserialize)]
pub struct ComputeInstanceInfo {
/// Unique instance identifier.
pub instance_id: String,
/// Template that was used.
pub template: String,
/// Current instance status ("provisioning", "running", "stopping", "terminated", "failed").
pub status: String,
/// Live GCE instance status.
#[serde(default)]
pub gcp_status: Option<String>,
/// GCP zone.
pub zone: String,
/// GCE machine type.
#[serde(default)]
pub machine_type: Option<String>,
/// Public IP address (available once running).
#[serde(default)]
pub external_ip: Option<String>,
/// GPU accelerator type.
#[serde(default)]
pub gpu_type: Option<String>,
/// Number of GPUs.
#[serde(default)]
pub gpu_count: Option<i32>,
/// Whether this is a spot/preemptible instance.
#[serde(default)]
pub spot: bool,
/// Hourly rate in USD.
#[serde(default)]
pub hourly_usd: f64,
/// Total cost so far in USD.
#[serde(default)]
pub cost_usd: f64,
/// Total uptime in minutes.
#[serde(default)]
pub uptime_minutes: i32,
/// Inactivity timeout in minutes.
#[serde(default)]
pub auto_teardown_minutes: i32,
/// SSH username for the instance.
#[serde(default)]
pub ssh_username: Option<String>,
/// ISO 8601 timestamp of last activity.
#[serde(default)]
pub last_active_at: Option<String>,
/// ISO 8601 creation timestamp.
#[serde(default)]
pub created_at: Option<String>,
/// ISO 8601 termination timestamp (if terminated).
#[serde(default)]
pub terminated_at: Option<String>,
/// Error message if the instance failed.
#[serde(default)]
pub error_message: Option<String>,
}
/// Response from listing compute instances.
#[derive(Debug, Clone, Deserialize)]
pub struct InstancesResponse {
/// Running compute instances.
pub instances: Vec<ComputeInstance>,
}
/// Response from getting a single compute instance.
#[derive(Debug, Clone, Deserialize)]
pub struct InstanceResponse {
/// The compute instance details.
pub instance: ComputeInstance,
}
/// Response from deleting a compute instance.
#[derive(Debug, Clone, Deserialize)]
pub struct DeleteResponse {
/// Status message.
pub status: String,
/// Instance that was deleted.
#[serde(default)]
pub instance_id: Option<String>,
}
/// Request body for adding an SSH key to an instance.
#[derive(Debug, Clone, Serialize, Default)]
pub struct SSHKeyRequest {
/// SSH public key to add.
pub ssh_public_key: String,
}
/// Request for querying compute billing from BigQuery.
#[derive(Debug, Clone, Serialize, Default)]
pub struct BillingRequest {
/// Filter by instance ID.
#[serde(skip_serializing_if = "Option::is_none")]
pub instance_id: Option<String>,
/// Start date for billing period (ISO 8601).
#[serde(skip_serializing_if = "Option::is_none")]
pub start_date: Option<String>,
/// End date for billing period (ISO 8601).
#[serde(skip_serializing_if = "Option::is_none")]
pub end_date: Option<String>,
}
/// A single billing line item from BigQuery.
#[derive(Debug, Clone, Deserialize)]
pub struct BillingEntry {
/// Instance identifier.
pub instance_id: String,
/// Instance name.
#[serde(default)]
pub instance_name: Option<String>,
/// Total cost in USD.
pub cost_usd: f64,
/// Usage duration in hours.
#[serde(default)]
pub usage_hours: Option<f64>,
/// SKU description (e.g. "N1 Predefined Instance Core").
#[serde(default)]
pub sku_description: Option<String>,
/// Billing period start.
#[serde(default)]
pub start_time: Option<String>,
/// Billing period end.
#[serde(default)]
pub end_time: Option<String>,
}
/// Response from billing query.
#[derive(Debug, Clone, Deserialize)]
pub struct BillingResponse {
/// Individual billing entries.
pub entries: Vec<BillingEntry>,
/// Total cost across all entries.
pub total_cost_usd: f64,
}
impl Client {
/// Lists available compute templates (GPU configurations and pricing).
pub async fn compute_templates(&self) -> Result<TemplatesResponse> {
let (resp, _meta) = self
.get_json::<TemplatesResponse>("/qai/v1/compute/templates")
.await?;
Ok(resp)
}
/// Provisions a new GPU compute instance.
pub async fn compute_provision(&self, req: &ProvisionRequest) -> Result<ProvisionResponse> {
let (resp, _meta) = self
.post_json::<ProvisionRequest, ProvisionResponse>("/qai/v1/compute/provision", req)
.await?;
Ok(resp)
}
/// Lists all compute instances for the account.
pub async fn compute_instances(&self) -> Result<InstancesResponse> {
let (resp, _meta) = self
.get_json::<InstancesResponse>("/qai/v1/compute/instances")
.await?;
Ok(resp)
}
/// Gets details for a specific compute instance.
pub async fn compute_instance(&self, id: &str) -> Result<InstanceResponse> {
let path = format!("/qai/v1/compute/instance/{id}");
let (resp, _meta) = self.get_json::<InstanceResponse>(&path).await?;
Ok(resp)
}
/// Deletes (tears down) a compute instance.
pub async fn compute_delete(&self, id: &str) -> Result<DeleteResponse> {
let path = format!("/qai/v1/compute/instance/{id}");
let (resp, _meta) = self.delete_json::<DeleteResponse>(&path).await?;
Ok(resp)
}
/// Adds an SSH public key to a running compute instance.
pub async fn compute_ssh_key(&self, id: &str, req: &SSHKeyRequest) -> Result<StatusResponse> {
let path = format!("/qai/v1/compute/instance/{id}/ssh-key");
let (resp, _meta) = self
.post_json::<SSHKeyRequest, StatusResponse>(&path, req)
.await?;
Ok(resp)
}
/// Sends a keepalive to prevent auto-teardown of a compute instance.
pub async fn compute_keepalive(&self, id: &str) -> Result<StatusResponse> {
let path = format!("/qai/v1/compute/instance/{id}/keepalive");
let (resp, _meta) = self
.post_json::<serde_json::Value, StatusResponse>(&path, &serde_json::json!({}))
.await?;
Ok(resp)
}
/// Queries compute billing from BigQuery via the QAI backend.
pub async fn compute_billing(&self, req: &BillingRequest) -> Result<BillingResponse> {
let (resp, _meta) = self
.post_json::<BillingRequest, BillingResponse>("/qai/v1/compute/billing", req)
.await?;
Ok(resp)
}
}