-
-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathenterprise_cloud.rs
More file actions
419 lines (381 loc) · 11.5 KB
/
Copy pathenterprise_cloud.rs
File metadata and controls
419 lines (381 loc) · 11.5 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use eyre::{Result, eyre};
use reqwest::Client;
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use std::collections::BTreeMap;
use std::sync::LazyLock;
const DEFAULT_CLOUD_AUTHORITY: &str = "cloud.helix-db.com";
pub static CLOUD_AUTHORITY: LazyLock<String> = LazyLock::new(|| {
std::env::var("CLOUD_AUTHORITY").unwrap_or_else(|_| DEFAULT_CLOUD_AUTHORITY.to_string())
});
pub fn cloud_base_url() -> String {
let authority = CLOUD_AUTHORITY.as_str();
if authority.starts_with("http://") || authority.starts_with("https://") {
authority.to_string()
} else if authority.starts_with("localhost") || authority.starts_with("127.0.0.1") {
format!("http://{authority}")
} else {
format!("https://{authority}")
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CliWorkspace {
pub id: String,
pub name: String,
pub url_slug: String,
#[serde(default = "default_workspace_type")]
pub workspace_type: String,
}
fn default_workspace_type() -> String {
"organization".to_string()
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CliProject {
pub id: String,
pub name: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CliProjectDetails {
pub id: String,
pub name: String,
pub workspace_id: String,
pub workspace_name: String,
pub workspace_slug: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CliProjectClusters {
pub project_id: String,
pub project_name: String,
#[serde(default)]
pub enterprise: Vec<CliEnterpriseCluster>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CliWorkspaceClusters {
#[serde(default)]
pub enterprise: Vec<CliEnterpriseCluster>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CliClusterIndex {
#[serde(default, alias = "name")]
pub index_name: String,
#[serde(default, alias = "type")]
pub index_type: Option<String>,
#[serde(flatten)]
pub extra: BTreeMap<String, serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CliClusterIndexes {
#[serde(default)]
pub vector_indexes: Vec<CliClusterIndex>,
#[serde(default)]
pub equality_indexes: Vec<CliClusterIndex>,
#[serde(default)]
pub range_indexes: Vec<CliClusterIndex>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CliEnterpriseCluster {
pub cluster_id: String,
#[serde(alias = "cluster_name")]
pub name: String,
#[serde(default)]
pub project_id: Option<String>,
#[serde(default)]
pub project_name: Option<String>,
#[serde(default)]
pub availability_mode: Option<String>,
#[serde(default)]
pub gateway_node_type: Option<String>,
#[serde(default)]
pub db_node_type: Option<String>,
#[serde(default)]
pub gateway_url: Option<String>,
#[serde(default)]
pub query_auth_header: Option<String>,
#[serde(default)]
pub query_auth_env: Option<String>,
#[serde(default)]
pub min_gateway_count: Option<u64>,
#[serde(default)]
pub max_gateway_count: Option<u64>,
#[serde(default)]
pub min_hyperscale_count: Option<u64>,
#[serde(default)]
pub max_hyperscale_count: Option<u64>,
#[serde(default)]
pub gateway_count: Option<u64>,
#[serde(default)]
pub hyperscale_count: Option<u64>,
#[serde(default)]
pub min_instances: Option<u64>,
#[serde(default)]
pub max_instances: Option<u64>,
}
impl CliEnterpriseCluster {
pub fn resolved_gateway_min_count(&self) -> Option<u64> {
self.min_gateway_count
.or(self.gateway_count)
.or(self.max_gateway_count)
.or(self.min_instances)
}
pub fn resolved_gateway_max_count(&self) -> Option<u64> {
self.max_gateway_count
.or(self.gateway_count)
.or(self.min_gateway_count)
.or(self.min_instances)
}
pub fn resolved_hyperscale_min_count(&self) -> Option<u64> {
self.min_hyperscale_count
.or(self.hyperscale_count)
.or(self.max_hyperscale_count)
.or(self.max_instances)
}
pub fn resolved_hyperscale_max_count(&self) -> Option<u64> {
self.max_hyperscale_count
.or(self.hyperscale_count)
.or(self.min_hyperscale_count)
.or(self.max_instances)
}
pub fn resolved_gateway_count(&self) -> Option<u64> {
self.resolved_gateway_min_count()
}
pub fn resolved_hyperscale_count(&self) -> Option<u64> {
self.resolved_hyperscale_min_count()
}
pub fn compatibility_min_instances(&self) -> Option<u64> {
if let (Some(gateway_count), Some(hyperscale_count)) = (
self.resolved_gateway_min_count(),
self.resolved_hyperscale_min_count(),
) {
Some(gateway_count.min(hyperscale_count))
} else {
self.min_instances
}
}
pub fn compatibility_max_instances(&self) -> Option<u64> {
if let (Some(gateway_count), Some(hyperscale_count)) = (
self.resolved_gateway_max_count(),
self.resolved_hyperscale_max_count(),
) {
Some(gateway_count.max(hyperscale_count))
} else {
self.max_instances
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CliClusterProject {
pub cluster_id: String,
pub project_id: String,
pub project_name: String,
pub workspace_id: String,
}
async fn get_json<T: DeserializeOwned>(
client: &Client,
url: String,
api_key: &str,
action: &str,
) -> Result<T> {
let response = client.get(&url).header("x-api-key", api_key).send().await?;
if !response.status().is_success() {
let status = response.status();
let body = response.text().await.unwrap_or_default();
return Err(eyre!("Failed to {action}: HTTP {status} {body}"));
}
Ok(response.json::<T>().await?)
}
pub async fn fetch_workspaces(
client: &Client,
base_url: &str,
api_key: &str,
) -> Result<Vec<CliWorkspace>> {
get_json(
client,
format!("{base_url}/api/cli/workspaces"),
api_key,
"fetch workspaces",
)
.await
}
pub async fn fetch_projects(
client: &Client,
base_url: &str,
api_key: &str,
workspace_id: &str,
) -> Result<Vec<CliProject>> {
get_json(
client,
format!("{base_url}/api/cli/workspaces/{workspace_id}/projects"),
api_key,
"fetch projects",
)
.await
}
pub async fn fetch_project_details(
client: &Client,
base_url: &str,
api_key: &str,
project_id: &str,
) -> Result<CliProjectDetails> {
get_json(
client,
format!("{base_url}/api/cli/projects/{project_id}"),
api_key,
"fetch project details",
)
.await
}
pub async fn fetch_project_clusters(
client: &Client,
base_url: &str,
api_key: &str,
project_id: &str,
) -> Result<CliProjectClusters> {
get_json(
client,
format!("{base_url}/api/cli/projects/{project_id}/clusters"),
api_key,
"fetch project clusters",
)
.await
}
pub async fn fetch_workspace_clusters(
client: &Client,
base_url: &str,
api_key: &str,
workspace_id: &str,
) -> Result<CliWorkspaceClusters> {
get_json(
client,
format!("{base_url}/api/cli/workspaces/{workspace_id}/clusters"),
api_key,
"fetch workspace clusters",
)
.await
}
pub async fn fetch_indexes_for_cluster(
client: &Client,
base_url: &str,
api_key: &str,
cluster_id: &str,
) -> Result<CliClusterIndexes> {
get_json(
client,
format!("{base_url}/api/cli/enterprise-clusters/{cluster_id}/indexes"),
api_key,
"fetch cluster indexes",
)
.await
}
pub async fn fetch_enterprise_cluster_project(
client: &Client,
base_url: &str,
api_key: &str,
cluster_id: &str,
) -> Result<CliClusterProject> {
get_json(
client,
format!("{base_url}/api/cli/enterprise-clusters/{cluster_id}/project"),
api_key,
"fetch enterprise cluster project",
)
.await
}
pub fn find_workspace_by_id<'a>(
workspaces: &'a [CliWorkspace],
id: &str,
) -> Option<&'a CliWorkspace> {
workspaces.iter().find(|workspace| workspace.id == id)
}
pub fn find_workspace_by_slug<'a>(
workspaces: &'a [CliWorkspace],
slug: &str,
) -> Option<&'a CliWorkspace> {
workspaces
.iter()
.find(|workspace| workspace.url_slug == slug)
}
pub fn find_project_by_id<'a>(projects: &'a [CliProject], id: &str) -> Option<&'a CliProject> {
projects.iter().find(|project| project.id == id)
}
pub fn find_project_by_name<'a>(projects: &'a [CliProject], name: &str) -> Option<&'a CliProject> {
projects.iter().find(|project| project.name == name)
}
pub fn find_enterprise_cluster_by_id<'a>(
clusters: &'a [CliEnterpriseCluster],
id: &str,
) -> Option<&'a CliEnterpriseCluster> {
clusters.iter().find(|cluster| cluster.cluster_id == id)
}
/// List the Enterprise clusters available for a project (preferred) or workspace.
pub async fn list_clusters_for_context(
client: &Client,
base_url: &str,
api_key: &str,
project_id: Option<&str>,
workspace_id: Option<&str>,
) -> Result<Vec<CliEnterpriseCluster>> {
if let Some(project_id) = project_id {
Ok(
fetch_project_clusters(client, base_url, api_key, project_id)
.await?
.enterprise,
)
} else if let Some(workspace_id) = workspace_id {
Ok(
fetch_workspace_clusters(client, base_url, api_key, workspace_id)
.await?
.enterprise,
)
} else {
Err(eyre!(
"No workspace selected. Run 'helix workspace switch <workspace>'."
))
}
}
/// A cluster resolved to its full metadata plus the project/workspace it belongs to.
pub struct ResolvedEnterpriseCluster {
pub cluster: CliEnterpriseCluster,
pub project_id: String,
pub project_name: String,
pub workspace_id: Option<String>,
}
/// Resolve a cluster ID to its full record and owning project/workspace.
///
/// Prefers the caller-supplied IDs; otherwise looks up the cluster's project via
/// `fetch_enterprise_cluster_project`. The full cluster record is then pulled from
/// the project's cluster list.
pub async fn resolve_enterprise_cluster(
client: &Client,
base_url: &str,
api_key: &str,
cluster_id: &str,
known_project_id: Option<&str>,
known_workspace_id: Option<&str>,
) -> Result<ResolvedEnterpriseCluster> {
let (project_id, project_name, workspace_id) = if let Some(project_id) = known_project_id {
(
project_id.to_string(),
None,
known_workspace_id.map(str::to_string),
)
} else {
let cluster_project =
fetch_enterprise_cluster_project(client, base_url, api_key, cluster_id).await?;
(
cluster_project.project_id,
Some(cluster_project.project_name),
Some(cluster_project.workspace_id),
)
};
let project_clusters = fetch_project_clusters(client, base_url, api_key, &project_id).await?;
let cluster = find_enterprise_cluster_by_id(&project_clusters.enterprise, cluster_id)
.cloned()
.ok_or_else(|| {
eyre!("Enterprise cluster '{cluster_id}' was not found in project '{project_id}'")
})?;
Ok(ResolvedEnterpriseCluster {
project_id: project_clusters.project_id,
project_name: project_name.unwrap_or(project_clusters.project_name),
workspace_id,
cluster,
})
}