foundation_deployment_digitalocean 0.1.3

DigitalOcean API v2 client — droplet provisioning and SSH keys
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! Auto-generated API module for digitalocean droplets.
//!
//! Generated by `cargo run --bin ewe_platform gen_api`.
//! DO NOT EDIT MANUALLY.
//!
//! Feature flag: `digitalocean_droplets `

#![cfg(feature = "digitalocean_droplets")]
#![allow(clippy::too_many_arguments, clippy::type_complexity)]
#![allow(
    clippy::missing_errors_doc,
    clippy::doc_markdown,
    clippy::useless_format
)]
#![allow(unused_imports)]

use foundation_macros::JsonHash;
use foundation_netio::shared::client::http_client::HttpClient;
use foundation_netio::{DynNetClient, PreparedRequestBuilder};
use serde::{Deserialize, Serialize};

use super::shared::ApiResponse;

// =============================================================================
// TYPE DECLARATIONS
// =============================================================================

/// `ActionLink` type.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonHash)]
pub struct ActionLink {
    /// `href` property.
    pub href: Option<String>,
    /// `id` property.
    pub id: Option<i64>,
    /// `rel` property.
    pub rel: Option<String>,
}

/// `AllDropletsResponse` type.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonHash)]
pub struct AllDropletsResponse {
    /// `droplets` property.
    pub droplets: Option<Vec<Droplet>>,
    /// `links` property.
    pub links: Option<PageLinks>,
    /// `meta` property.
    pub meta: serde_json::Value,
}

/// `DiskInfo` type.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonHash)]
pub struct DiskInfo {
    /// `size` property.
    pub size: Option<std::collections::HashMap<String, serde_json::Value>>,
    /// `type` property.
    #[serde(rename = "type")]
    pub r#type: Option<String>,
}

/// `Droplet` type.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonHash)]
pub struct Droplet {
    /// `backup_ids` property.
    pub backup_ids: Vec<i64>,
    /// `created_at` property.
    pub created_at: String,
    /// `disk` property.
    pub disk: i64,
    /// `disk_info` property.
    pub disk_info: Option<Vec<DiskInfo>>,
    /// `features` property.
    pub features: Vec<String>,
    /// `gpu_info` property.
    pub gpu_info: Option<GpuInfo>,
    /// `id` property.
    pub id: i64,
    /// `image` property.
    pub image: serde_json::Value,
    /// `kernel` property.
    pub kernel: Option<Kernel>,
    /// `locked` property.
    pub locked: bool,
    /// `memory` property.
    pub memory: i64,
    /// `name` property.
    pub name: String,
    /// `networks` property.
    pub networks: std::collections::HashMap<String, serde_json::Value>,
    /// `next_backup_window` property.
    pub next_backup_window: serde_json::Value,
    /// `region` property.
    pub region: Region,
    /// `size` property.
    pub size: Size,
    /// `size_slug` property.
    pub size_slug: String,
    /// `snapshot_ids` property.
    pub snapshot_ids: Vec<i64>,
    /// `status` property.
    pub status: String,
    /// `tags` property.
    pub tags: Vec<String>,
    /// `vcpus` property.
    pub vcpus: i64,
    /// `volume_ids` property.
    pub volume_ids: Vec<String>,
    /// `vpc_uuid` property.
    pub vpc_uuid: Option<String>,
}

/// `DropletCreate` type.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonHash)]
pub struct DropletCreate {
    /// `backup_policy` property.
    pub backup_policy: Option<serde_json::Value>,
    /// `backups` property.
    pub backups: Option<bool>,
    /// `image` property.
    pub image: serde_json::Value,
    /// `ipv6` property.
    pub ipv6: Option<bool>,
    /// `monitoring` property.
    pub monitoring: Option<bool>,
    /// `private_networking` property.
    pub private_networking: Option<bool>,
    /// `public_networking` property.
    pub public_networking: Option<bool>,
    /// `region` property.
    pub region: Option<String>,
    /// `size` property.
    pub size: String,
    /// `ssh_keys` property.
    pub ssh_keys: Option<Vec<serde_json::Value>>,
    /// `tags` property.
    pub tags: Option<Vec<String>>,
    /// `user_data` property.
    pub user_data: Option<String>,
    /// `volumes` property.
    pub volumes: Option<Vec<String>>,
    /// `vpc_uuid` property.
    pub vpc_uuid: Option<String>,
    /// `with_droplet_agent` property.
    pub with_droplet_agent: Option<bool>,
}

/// `DropletCreateResponse` type.
pub type DropletCreateResponse = serde_json::Value;

/// `DropletMultiCreate` type.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonHash)]
pub struct DropletMultiCreate {
    /// `backup_policy` property.
    pub backup_policy: Option<serde_json::Value>,
    /// `backups` property.
    pub backups: Option<bool>,
    /// `image` property.
    pub image: serde_json::Value,
    /// `ipv6` property.
    pub ipv6: Option<bool>,
    /// `monitoring` property.
    pub monitoring: Option<bool>,
    /// `names` property.
    pub names: Vec<String>,
    /// `private_networking` property.
    pub private_networking: Option<bool>,
    /// `public_networking` property.
    pub public_networking: Option<bool>,
    /// `region` property.
    pub region: Option<String>,
    /// `size` property.
    pub size: String,
    /// `ssh_keys` property.
    pub ssh_keys: Option<Vec<serde_json::Value>>,
    /// `tags` property.
    pub tags: Option<Vec<String>>,
    /// `user_data` property.
    pub user_data: Option<String>,
    /// `volumes` property.
    pub volumes: Option<Vec<String>>,
    /// `vpc_uuid` property.
    pub vpc_uuid: Option<String>,
    /// `with_droplet_agent` property.
    pub with_droplet_agent: Option<bool>,
}

/// `DropletSingleCreate` type.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonHash)]
pub struct DropletSingleCreate {
    /// `backup_policy` property.
    pub backup_policy: Option<serde_json::Value>,
    /// `backups` property.
    pub backups: Option<bool>,
    /// `image` property.
    pub image: serde_json::Value,
    /// `ipv6` property.
    pub ipv6: Option<bool>,
    /// `monitoring` property.
    pub monitoring: Option<bool>,
    /// `name` property.
    pub name: String,
    /// `private_networking` property.
    pub private_networking: Option<bool>,
    /// `public_networking` property.
    pub public_networking: Option<bool>,
    /// `region` property.
    pub region: Option<String>,
    /// `size` property.
    pub size: String,
    /// `ssh_keys` property.
    pub ssh_keys: Option<Vec<serde_json::Value>>,
    /// `tags` property.
    pub tags: Option<Vec<String>>,
    /// `user_data` property.
    pub user_data: Option<String>,
    /// `volumes` property.
    pub volumes: Option<Vec<String>>,
    /// `vpc_uuid` property.
    pub vpc_uuid: Option<String>,
    /// `with_droplet_agent` property.
    pub with_droplet_agent: Option<bool>,
}

/// `DropletsCreateRequest` type.
pub type DropletsCreateRequest = serde_json::Value;

/// `ExistingDropletResponse` type.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonHash)]
pub struct ExistingDropletResponse {
    /// `droplet` property.
    pub droplet: Option<Droplet>,
}

/// `GpuInfo` type.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonHash)]
pub struct GpuInfo {
    /// `count` property.
    pub count: Option<i64>,
    /// `model` property.
    pub model: Option<String>,
    /// `vram` property.
    pub vram: Option<std::collections::HashMap<String, serde_json::Value>>,
}

/// `Kernel` type.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonHash)]
pub struct Kernel {
    /// `id` property.
    pub id: Option<i64>,
    /// `name` property.
    pub name: Option<String>,
    /// `version` property.
    pub version: Option<String>,
}

/// `Meta` type.
///
/// Same shape as `super::shared::MetaObject`.
pub type Meta = super::shared::MetaObject;

/// `NetworkV4` type.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonHash)]
pub struct NetworkV4 {
    /// `gateway` property.
    pub gateway: Option<String>,
    /// `ip_address` property.
    pub ip_address: Option<String>,
    /// `netmask` property.
    pub netmask: Option<String>,
    /// `type` property.
    #[serde(rename = "type")]
    pub r#type: Option<String>,
}

/// `NetworkV6` type.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonHash)]
pub struct NetworkV6 {
    /// `gateway` property.
    pub gateway: Option<String>,
    /// `ip_address` property.
    pub ip_address: Option<String>,
    /// `netmask` property.
    pub netmask: Option<i64>,
    /// `type` property.
    #[serde(rename = "type")]
    pub r#type: Option<String>,
}

/// `PageLinks` type.
///
/// Same shape as `super::shared::PagesObject`.
pub type PageLinks = super::shared::PagesObject;

/// `Pagination` type.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonHash)]
pub struct Pagination {
    /// `links` property.
    pub links: Option<PageLinks>,
}

/// `Region` type.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonHash)]
pub struct Region {
    /// `available` property.
    pub available: bool,
    /// `features` property.
    pub features: Vec<String>,
    /// `name` property.
    pub name: String,
    /// `sizes` property.
    pub sizes: Vec<String>,
    /// `slug` property.
    pub slug: String,
}

/// `Size` type.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonHash)]
pub struct Size {
    /// `available` property.
    pub available: bool,
    /// `description` property.
    pub description: String,
    /// `disk` property.
    pub disk: i64,
    /// `disk_info` property.
    pub disk_info: Option<Vec<DiskInfo>>,
    /// `gpu_info` property.
    pub gpu_info: Option<GpuInfo>,
    /// `memory` property.
    pub memory: i64,
    /// `price_hourly` property.
    pub price_hourly: f64,
    /// `price_monthly` property.
    pub price_monthly: f64,
    /// `regions` property.
    pub regions: Vec<String>,
    /// `slug` property.
    pub slug: String,
    /// `transfer` property.
    pub transfer: f64,
    /// `vcpus` property.
    pub vcpus: i64,
}

// =============================================================================
// ARGS TYPES (per-endpoint)
// =============================================================================

/// Arguments for [`droplets_list_request`].
#[derive(Debug, Clone, Default, Serialize, JsonHash)]
pub struct DropletsListArgs {
    /// Query parameter: `per_page`.
    pub per_page: Option<String>,
    /// Query parameter: `page`.
    pub page: Option<String>,
    /// Query parameter: `tag_name`.
    pub tag_name: Option<String>,
    /// Query parameter: `name`.
    pub name: Option<String>,
    /// Query parameter: `type`.
    pub r#type: Option<String>,
}

/// Arguments for [`droplets_create_request`].
#[derive(Debug, Clone, Default, Serialize, JsonHash)]
pub struct DropletsCreateArgs {
    /// Request body.
    pub body: DropletsCreateRequest,
}

/// Arguments for [`droplets_get_request`].
///
/// Same shape as `super::shared::DropletIdArgs`.
pub type DropletsGetArgs = super::shared::DropletIdArgs;

/// Arguments for [`droplets_destroy_request`].
///
/// Same shape as `super::shared::DropletIdArgs`.
pub type DropletsDestroyArgs = super::shared::DropletIdArgs;

// =============================================================================
// CLIENT FUNCTIONS (per-endpoint)
// =============================================================================

// -----------------------------------------------------------------------------
// GET /v2/droplets
// -----------------------------------------------------------------------------

/// GET /v2/droplets.
///
/// Takes a client and args, builds the request, optionally applies
/// modifications, then sends it via `send_async().await`.
///
/// # Arguments
///
/// * `client` - HTTP client for making the request: a `DynNetClient`, or an `ApiClient` carrying the `BodyLimits` the response is read under
/// * `args` - Request arguments (path params, query params, body)
/// * `builder_mod` - Optional closure to modify the request builder (e.g., add headers)
///
/// # Example
///
/// ```ignore
/// let response = droplets_list_request(client.clone(), &args, Some(|b: &mut PreparedRequestBuilder| {
///     b.header("X-Custom-Header", "value");
/// })).await?;
/// ```
pub async fn droplets_list_request<F>(
    client: impl Into<super::shared::ApiClient>,
    args: &DropletsListArgs,
    base_url: &str,
    builder_mod: Option<F>,
) -> Result<ApiResponse<AllDropletsResponse>, super::shared::ApiError>
where
    F: FnOnce(&mut PreparedRequestBuilder),
{
    let path = format!("/v2/droplets",);
    let endpoint_url = format!("{}{}", base_url, path);

    let mut builder = PreparedRequestBuilder::get(&endpoint_url)
        .map_err(|e| super::shared::ApiError::RequestBuildFailed(e.to_string()))?;

    builder = builder.query("per_page", args.per_page.as_deref());
    builder = builder.query("page", args.page.as_deref());
    builder = builder.query("tag_name", args.tag_name.as_deref());
    builder = builder.query("name", args.name.as_deref());
    builder = builder.query("type", args.r#type.as_deref());

    if let Some(f) = builder_mod {
        f(&mut builder);
    }

    super::shared::send_json(client.into(), builder.build()).await
}

// -----------------------------------------------------------------------------
// POST /v2/droplets
// -----------------------------------------------------------------------------

/// POST /v2/droplets.
///
/// Takes a client and args, builds the request, optionally applies
/// modifications, then sends it via `send_async().await`.
///
/// # Arguments
///
/// * `client` - HTTP client for making the request: a `DynNetClient`, or an `ApiClient` carrying the `BodyLimits` the response is read under
/// * `args` - Request arguments (path params, query params, body)
/// * `builder_mod` - Optional closure to modify the request builder (e.g., add headers)
///
/// # Example
///
/// ```ignore
/// let response = droplets_create_request(client.clone(), &args, Some(|b: &mut PreparedRequestBuilder| {
///     b.header("X-Custom-Header", "value");
/// })).await?;
/// ```
pub async fn droplets_create_request<F>(
    client: impl Into<super::shared::ApiClient>,
    args: &DropletsCreateArgs,
    base_url: &str,
    builder_mod: Option<F>,
) -> Result<ApiResponse<DropletCreateResponse>, super::shared::ApiError>
where
    F: FnOnce(&mut PreparedRequestBuilder),
{
    let path = format!("/v2/droplets",);
    let endpoint_url = format!("{}{}", base_url, path);

    let mut builder = PreparedRequestBuilder::post(&endpoint_url)
        .map_err(|e| super::shared::ApiError::RequestBuildFailed(e.to_string()))?;

    builder = builder
        .body_json(&args.body)
        .map_err(|e| super::shared::ApiError::RequestBuildFailed(e.to_string()))?;

    if let Some(f) = builder_mod {
        f(&mut builder);
    }

    super::shared::send_json(client.into(), builder.build()).await
}

// -----------------------------------------------------------------------------
// GET /v2/droplets/{droplet_id}
// -----------------------------------------------------------------------------

/// GET /v2/droplets/{droplet_id}.
///
/// Takes a client and args, builds the request, optionally applies
/// modifications, then sends it via `send_async().await`.
///
/// # Arguments
///
/// * `client` - HTTP client for making the request: a `DynNetClient`, or an `ApiClient` carrying the `BodyLimits` the response is read under
/// * `args` - Request arguments (path params, query params, body)
/// * `builder_mod` - Optional closure to modify the request builder (e.g., add headers)
///
/// # Example
///
/// ```ignore
/// let response = droplets_get_request(client.clone(), &args, Some(|b: &mut PreparedRequestBuilder| {
///     b.header("X-Custom-Header", "value");
/// })).await?;
/// ```
pub async fn droplets_get_request<F>(
    client: impl Into<super::shared::ApiClient>,
    args: &DropletsGetArgs,
    base_url: &str,
    builder_mod: Option<F>,
) -> Result<ApiResponse<ExistingDropletResponse>, super::shared::ApiError>
where
    F: FnOnce(&mut PreparedRequestBuilder),
{
    let path = format!("/v2/droplets/{}", args.droplet_id,);
    let endpoint_url = format!("{}{}", base_url, path);

    let mut builder = PreparedRequestBuilder::get(&endpoint_url)
        .map_err(|e| super::shared::ApiError::RequestBuildFailed(e.to_string()))?;

    if let Some(f) = builder_mod {
        f(&mut builder);
    }

    super::shared::send_json(client.into(), builder.build()).await
}

// -----------------------------------------------------------------------------
// DELETE /v2/droplets/{droplet_id}
// -----------------------------------------------------------------------------

/// DELETE /v2/droplets/{droplet_id}.
///
/// Takes a client and args, builds the request, optionally applies
/// modifications, then sends it via `send_async().await`.
///
/// # Arguments
///
/// * `client` - HTTP client for making the request: a `DynNetClient`, or an `ApiClient` carrying the `BodyLimits` the response is read under
/// * `args` - Request arguments (path params, query params, body)
/// * `builder_mod` - Optional closure to modify the request builder (e.g., add headers)
///
/// # Example
///
/// ```ignore
/// let response = droplets_destroy_request(client.clone(), &args, Some(|b: &mut PreparedRequestBuilder| {
///     b.header("X-Custom-Header", "value");
/// })).await?;
/// ```
pub async fn droplets_destroy_request<F>(
    client: impl Into<super::shared::ApiClient>,
    args: &DropletsDestroyArgs,
    base_url: &str,
    builder_mod: Option<F>,
) -> Result<ApiResponse<()>, super::shared::ApiError>
where
    F: FnOnce(&mut PreparedRequestBuilder),
{
    let path = format!("/v2/droplets/{}", args.droplet_id,);
    let endpoint_url = format!("{}{}", base_url, path);

    let mut builder = PreparedRequestBuilder::delete(&endpoint_url)
        .map_err(|e| super::shared::ApiError::RequestBuildFailed(e.to_string()))?;

    if let Some(f) = builder_mod {
        f(&mut builder);
    }

    super::shared::send_without_body(client.into(), builder.build()).await
}