blueprint-remote-providers 0.2.0-alpha.2

Remote service providers for Tangle Blueprints
Documentation
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//! Dynamic machine type discovery from cloud provider APIs
//!
//! Discovers available instance types and their specifications from cloud providers
//! to maintain an up-to-date catalog of available resources.

use crate::core::error::{Error, Result};
use crate::core::remote::CloudProvider;
use blueprint_core::debug;
use blueprint_std::collections::HashMap;
use serde::{Deserialize, Serialize};

/// Machine type discovery service
pub struct MachineTypeDiscovery {
    client: reqwest::Client,
    cache: HashMap<CloudProvider, Vec<MachineType>>,
}

impl Default for MachineTypeDiscovery {
    fn default() -> Self {
        Self::new()
    }
}

impl MachineTypeDiscovery {
    /// Create a new discovery service
    pub fn new() -> Self {
        Self {
            client: reqwest::Client::builder()
                .timeout(blueprint_std::time::Duration::from_secs(30))
                .build()
                .unwrap_or_else(|_| reqwest::Client::new()),
            cache: HashMap::new(),
        }
    }

    /// Discover all machine types for a cloud provider
    pub async fn discover_machine_types(
        &mut self,
        provider: &CloudProvider,
        region: &str,
        credentials: &CloudCredentials,
    ) -> Result<Vec<MachineType>> {
        // Check cache first
        if let Some(cached) = self.cache.get(provider) {
            if !cached.is_empty() {
                debug!("Using cached machine types for {:?}", provider);
                return Ok(cached.clone());
            }
        }

        let machines = match provider {
            CloudProvider::AWS => match self.discover_aws_instances(region, credentials).await {
                Ok(discovered) => discovered,
                Err(e) => {
                    debug!("AWS discovery failed: {}, using fallback", e);
                    self.get_common_aws_instances()
                }
            },
            CloudProvider::GCP => match self.discover_gcp_machines(region, credentials).await {
                Ok(discovered) => discovered,
                Err(e) => {
                    debug!("GCP discovery failed: {}, using fallback", e);
                    self.get_common_gcp_machines()
                }
            },
            CloudProvider::Azure => match self.discover_azure_vms(region, credentials).await {
                Ok(discovered) => discovered,
                Err(e) => {
                    debug!("Azure discovery failed: {}, using fallback", e);
                    self.get_common_azure_vms()
                }
            },
            CloudProvider::DigitalOcean => match self.discover_do_droplets(credentials).await {
                Ok(discovered) => discovered,
                Err(e) => {
                    debug!("DigitalOcean discovery failed: {}, using fallback", e);
                    self.get_common_do_droplets()
                }
            },
            CloudProvider::Vultr => match self.discover_vultr_plans(credentials).await {
                Ok(discovered) => discovered,
                Err(e) => {
                    debug!("Vultr discovery failed: {}, using fallback", e);
                    self.get_common_vultr_plans()
                }
            },
            _ => vec![],
        };

        // Cache the results
        self.cache.insert(provider.clone(), machines.clone());

        Ok(machines)
    }

    /// Discover AWS EC2 instance types
    async fn discover_aws_instances(
        &self,
        region: &str,
        credentials: &CloudCredentials,
    ) -> Result<Vec<MachineType>> {
        // AWS DescribeInstanceTypes API
        let url = format!(
            "https://ec2.{region}.amazonaws.com/?Action=DescribeInstanceTypes&Version=2016-11-15"
        );

        // In production, this would use proper AWS signature v4
        let response = self
            .client
            .get(&url)
            .header(
                "Authorization",
                format!(
                    "AWS4-HMAC-SHA256 Credential={}",
                    credentials.access_key.as_ref().unwrap_or(&String::new())
                ),
            )
            .send()
            .await
            .map_err(|e| Error::ConfigurationError(format!("Failed to query AWS: {e}")))?;

        if !response.status().is_success() {
            // Return standard instance types for each provider
            return Ok(self.get_common_aws_instances());
        }

        // Parse XML response (simplified)
        Ok(self.get_common_aws_instances())
    }

    /// Get common AWS instance types (fallback)
    fn get_common_aws_instances(&self) -> Vec<MachineType> {
        vec![
            MachineType {
                name: "t3.micro".to_string(),
                provider: CloudProvider::AWS,
                vcpus: 2,
                memory_gb: 1.0,
                storage_gb: Some(8.0),
                gpu_count: 0,
                gpu_type: None,
                network_performance: "Up to 5 Gigabit".to_string(),
                hourly_price: Some(0.0104),
                spot_price: Some(0.0031),
            },
            MachineType {
                name: "t3.small".to_string(),
                provider: CloudProvider::AWS,
                vcpus: 2,
                memory_gb: 2.0,
                storage_gb: Some(8.0),
                gpu_count: 0,
                gpu_type: None,
                network_performance: "Up to 5 Gigabit".to_string(),
                hourly_price: Some(0.0208),
                spot_price: Some(0.0062),
            },
            MachineType {
                name: "m6i.xlarge".to_string(),
                provider: CloudProvider::AWS,
                vcpus: 4,
                memory_gb: 16.0,
                storage_gb: None,
                gpu_count: 0,
                gpu_type: None,
                network_performance: "Up to 12.5 Gigabit".to_string(),
                hourly_price: Some(0.192),
                spot_price: Some(0.0576),
            },
            MachineType {
                name: "g4dn.xlarge".to_string(),
                provider: CloudProvider::AWS,
                vcpus: 4,
                memory_gb: 16.0,
                storage_gb: Some(125.0),
                gpu_count: 1,
                gpu_type: Some("NVIDIA T4".to_string()),
                network_performance: "Up to 25 Gigabit".to_string(),
                hourly_price: Some(0.526),
                spot_price: Some(0.1578),
            },
        ]
    }

    /// Discover GCP machine types
    async fn discover_gcp_machines(
        &self,
        zone: &str,
        credentials: &CloudCredentials,
    ) -> Result<Vec<MachineType>> {
        let project_id = credentials
            .project_id
            .as_ref()
            .ok_or_else(|| Error::ConfigurationError("GCP project ID required".into()))?;

        let url = format!(
            "https://compute.googleapis.com/compute/v1/projects/{project_id}/zones/{zone}/machineTypes"
        );

        let response = self
            .client
            .get(&url)
            .bearer_auth(credentials.access_token.as_ref().unwrap_or(&String::new()))
            .send()
            .await
            .map_err(|e| Error::ConfigurationError(format!("Failed to query GCP: {e}")))?;

        if !response.status().is_success() {
            return Ok(self.get_common_gcp_machines());
        }

        let json: serde_json::Value = response
            .json()
            .await
            .map_err(|e| Error::ConfigurationError(format!("Failed to parse GCP response: {e}")))?;

        let mut machines = Vec::new();
        if let Some(items) = json["items"].as_array() {
            for item in items {
                if let (Some(name), Some(vcpus), Some(memory)) = (
                    item["name"].as_str(),
                    item["guestCpus"].as_u64(),
                    item["memoryMb"].as_u64(),
                ) {
                    machines.push(MachineType {
                        name: name.to_string(),
                        provider: CloudProvider::GCP,
                        vcpus: vcpus as u32,
                        memory_gb: memory as f64 / 1024.0,
                        storage_gb: None,
                        gpu_count: 0,
                        gpu_type: None,
                        network_performance: "10 Gbps".to_string(),
                        hourly_price: None, // Pricing integration available via separate PricingFetcher
                        spot_price: None,
                    });
                }
            }
        }

        if machines.is_empty() {
            Ok(self.get_common_gcp_machines())
        } else {
            Ok(machines)
        }
    }

    /// Get common GCP machine types (fallback)
    fn get_common_gcp_machines(&self) -> Vec<MachineType> {
        vec![
            MachineType {
                name: "e2-micro".to_string(),
                provider: CloudProvider::GCP,
                vcpus: 2,
                memory_gb: 1.0,
                storage_gb: None,
                gpu_count: 0,
                gpu_type: None,
                network_performance: "1 Gbps".to_string(),
                hourly_price: Some(0.00838),
                spot_price: Some(0.00251),
            },
            MachineType {
                name: "e2-standard-4".to_string(),
                provider: CloudProvider::GCP,
                vcpus: 4,
                memory_gb: 16.0,
                storage_gb: None,
                gpu_count: 0,
                gpu_type: None,
                network_performance: "10 Gbps".to_string(),
                hourly_price: Some(0.134),
                spot_price: Some(0.0402),
            },
        ]
    }

    /// Discover Azure VM sizes
    async fn discover_azure_vms(
        &self,
        location: &str,
        credentials: &CloudCredentials,
    ) -> Result<Vec<MachineType>> {
        let subscription_id = credentials
            .subscription_id
            .as_ref()
            .ok_or_else(|| Error::ConfigurationError("Azure subscription ID required".into()))?;

        let url = format!(
            "https://management.azure.com/subscriptions/{subscription_id}/providers/Microsoft.Compute/locations/{location}/vmSizes?api-version=2023-03-01"
        );

        let response = self
            .client
            .get(&url)
            .bearer_auth(credentials.access_token.as_ref().unwrap_or(&String::new()))
            .send()
            .await
            .map_err(|e| Error::ConfigurationError(format!("Failed to query Azure: {e}")))?;

        if !response.status().is_success() {
            return Ok(self.get_common_azure_vms());
        }

        let json: serde_json::Value = response.json().await.map_err(|e| {
            Error::ConfigurationError(format!("Failed to parse Azure response: {e}"))
        })?;

        let mut machines = Vec::new();
        if let Some(values) = json["value"].as_array() {
            for value in values {
                if let (Some(name), Some(cores), Some(memory)) = (
                    value["name"].as_str(),
                    value["numberOfCores"].as_u64(),
                    value["memoryInMB"].as_u64(),
                ) {
                    machines.push(MachineType {
                        name: name.to_string(),
                        provider: CloudProvider::Azure,
                        vcpus: cores as u32,
                        memory_gb: memory as f64 / 1024.0,
                        storage_gb: value["resourceDiskSizeInMB"]
                            .as_u64()
                            .map(|mb| mb as f64 / 1024.0),
                        gpu_count: 0,
                        gpu_type: None,
                        network_performance: "Unknown".to_string(),
                        hourly_price: None,
                        spot_price: None,
                    });
                }
            }
        }

        if machines.is_empty() {
            Ok(self.get_common_azure_vms())
        } else {
            Ok(machines)
        }
    }

    /// Get common Azure VM sizes (fallback)
    fn get_common_azure_vms(&self) -> Vec<MachineType> {
        vec![
            MachineType {
                name: "Standard_B1s".to_string(),
                provider: CloudProvider::Azure,
                vcpus: 1,
                memory_gb: 1.0,
                storage_gb: Some(4.0),
                gpu_count: 0,
                gpu_type: None,
                network_performance: "Moderate".to_string(),
                hourly_price: Some(0.012),
                spot_price: Some(0.0036),
            },
            MachineType {
                name: "Standard_D4s_v5".to_string(),
                provider: CloudProvider::Azure,
                vcpus: 4,
                memory_gb: 16.0,
                storage_gb: None,
                gpu_count: 0,
                gpu_type: None,
                network_performance: "12500 Mbps".to_string(),
                hourly_price: Some(0.192),
                spot_price: Some(0.0576),
            },
        ]
    }

    /// Discover DigitalOcean droplet sizes
    async fn discover_do_droplets(
        &self,
        credentials: &CloudCredentials,
    ) -> Result<Vec<MachineType>> {
        let url = "https://api.digitalocean.com/v2/sizes";

        let response = self
            .client
            .get(url)
            .bearer_auth(credentials.api_token.as_ref().unwrap_or(&String::new()))
            .send()
            .await
            .map_err(|e| Error::ConfigurationError(format!("Failed to query DigitalOcean: {e}")))?;

        if !response.status().is_success() {
            return Ok(self.get_common_do_droplets());
        }

        let json: serde_json::Value = response
            .json()
            .await
            .map_err(|e| Error::ConfigurationError(format!("Failed to parse DO response: {e}")))?;

        let mut machines = Vec::new();
        if let Some(sizes) = json["sizes"].as_array() {
            for size in sizes {
                if let (Some(slug), Some(vcpus), Some(memory), Some(price_monthly)) = (
                    size["slug"].as_str(),
                    size["vcpus"].as_u64(),
                    size["memory"].as_u64(),
                    size["price_monthly"].as_f64(),
                ) {
                    machines.push(MachineType {
                        name: slug.to_string(),
                        provider: CloudProvider::DigitalOcean,
                        vcpus: vcpus as u32,
                        memory_gb: memory as f64 / 1024.0,
                        storage_gb: size["disk"].as_u64().map(|gb| gb as f64),
                        gpu_count: 0,
                        gpu_type: None,
                        network_performance: format!(
                            "{} Gbps",
                            size["transfer"].as_f64().unwrap_or(1.0)
                        ),
                        hourly_price: Some(price_monthly / 730.0), // Approximate
                        spot_price: None,                          // DO doesn't have spot
                    });
                }
            }
        }

        if machines.is_empty() {
            Ok(self.get_common_do_droplets())
        } else {
            Ok(machines)
        }
    }

    /// Get common DigitalOcean droplet sizes (fallback)
    fn get_common_do_droplets(&self) -> Vec<MachineType> {
        vec![
            MachineType {
                name: "s-1vcpu-1gb".to_string(),
                provider: CloudProvider::DigitalOcean,
                vcpus: 1,
                memory_gb: 1.0,
                storage_gb: Some(25.0),
                gpu_count: 0,
                gpu_type: None,
                network_performance: "1 Gbps".to_string(),
                hourly_price: Some(0.009),
                spot_price: None,
            },
            MachineType {
                name: "s-2vcpu-4gb".to_string(),
                provider: CloudProvider::DigitalOcean,
                vcpus: 2,
                memory_gb: 4.0,
                storage_gb: Some(80.0),
                gpu_count: 0,
                gpu_type: None,
                network_performance: "4 Gbps".to_string(),
                hourly_price: Some(0.036),
                spot_price: None,
            },
        ]
    }

    /// Discover Vultr plans
    async fn discover_vultr_plans(
        &self,
        credentials: &CloudCredentials,
    ) -> Result<Vec<MachineType>> {
        let url = "https://api.vultr.com/v2/plans";

        let response = self
            .client
            .get(url)
            .header(
                "Authorization",
                format!(
                    "Bearer {}",
                    credentials.api_key.as_ref().unwrap_or(&String::new())
                ),
            )
            .send()
            .await
            .map_err(|e| Error::ConfigurationError(format!("Failed to query Vultr: {e}")))?;

        if !response.status().is_success() {
            return Ok(self.get_common_vultr_plans());
        }

        let json: serde_json::Value = response.json().await.map_err(|e| {
            Error::ConfigurationError(format!("Failed to parse Vultr response: {e}"))
        })?;

        let mut machines = Vec::new();
        if let Some(plans) = json["plans"].as_array() {
            for plan in plans {
                if let (Some(id), Some(vcpu), Some(ram), Some(price)) = (
                    plan["id"].as_str(),
                    plan["vcpu_count"].as_u64(),
                    plan["ram"].as_u64(),
                    plan["monthly_cost"].as_f64(),
                ) {
                    machines.push(MachineType {
                        name: id.to_string(),
                        provider: CloudProvider::Vultr,
                        vcpus: vcpu as u32,
                        memory_gb: ram as f64 / 1024.0,
                        storage_gb: plan["disk"].as_u64().map(|gb| gb as f64),
                        gpu_count: if plan["gpu_vram_gb"].as_u64().is_some() {
                            1
                        } else {
                            0
                        },
                        gpu_type: plan["gpu_type"].as_str().map(|s| s.to_string()),
                        network_performance: format!(
                            "{} Gbps",
                            plan["bandwidth_gb"].as_u64().unwrap_or(1000) / 1000
                        ),
                        hourly_price: Some(price / 730.0),
                        spot_price: None,
                    });
                }
            }
        }

        if machines.is_empty() {
            Ok(self.get_common_vultr_plans())
        } else {
            Ok(machines)
        }
    }

    /// Get common Vultr plans (fallback)
    fn get_common_vultr_plans(&self) -> Vec<MachineType> {
        vec![
            MachineType {
                name: "vc2-1c-1gb".to_string(),
                provider: CloudProvider::Vultr,
                vcpus: 1,
                memory_gb: 1.0,
                storage_gb: Some(25.0),
                gpu_count: 0,
                gpu_type: None,
                network_performance: "1 Gbps".to_string(),
                hourly_price: Some(0.007),
                spot_price: None,
            },
            MachineType {
                name: "vc2-2c-4gb".to_string(),
                provider: CloudProvider::Vultr,
                vcpus: 2,
                memory_gb: 4.0,
                storage_gb: Some(80.0),
                gpu_count: 0,
                gpu_type: None,
                network_performance: "3 Gbps".to_string(),
                hourly_price: Some(0.024),
                spot_price: None,
            },
        ]
    }

    /// Find best machine type for given requirements
    pub fn find_best_match(
        &self,
        provider: &CloudProvider,
        min_vcpus: u32,
        min_memory_gb: f64,
        needs_gpu: bool,
        max_price_per_hour: Option<f64>,
    ) -> Option<MachineType> {
        self.cache.get(provider).and_then(|machines| {
            machines
                .iter()
                .filter(|m| m.vcpus >= min_vcpus)
                .filter(|m| m.memory_gb >= min_memory_gb)
                .filter(|m| !needs_gpu || m.gpu_count > 0)
                .filter(|m| {
                    max_price_per_hour
                        .is_none_or(|max| m.hourly_price.is_none_or(|price| price <= max))
                })
                .min_by(|a, b| {
                    // Sort by price, then by excess resources
                    match (a.hourly_price, b.hourly_price) {
                        (Some(a_price), Some(b_price)) => a_price.partial_cmp(&b_price).unwrap(),
                        _ => blueprint_std::cmp::Ordering::Equal,
                    }
                })
                .cloned()
        })
    }
}

/// Cloud provider credentials loaded from environment variables
#[derive(Debug, Clone, Default)]
pub struct CloudCredentials {
    // AWS
    pub access_key: Option<String>,
    pub secret_key: Option<String>,

    // GCP
    pub project_id: Option<String>,

    // Azure
    pub subscription_id: Option<String>,

    // Common
    pub access_token: Option<String>,
    pub api_token: Option<String>,
    pub api_key: Option<String>,
}

impl CloudCredentials {
    /// Load credentials from environment variables
    pub fn from_env() -> Self {
        use std::env;

        Self {
            // AWS credentials
            access_key: env::var("AWS_ACCESS_KEY_ID").ok(),
            secret_key: env::var("AWS_SECRET_ACCESS_KEY").ok(),

            // GCP credentials
            project_id: env::var("GOOGLE_CLOUD_PROJECT").ok(),

            // Azure credentials
            subscription_id: env::var("AZURE_SUBSCRIPTION_ID").ok(),

            // DigitalOcean
            access_token: env::var("DIGITALOCEAN_TOKEN")
                .ok()
                .or_else(|| env::var("DO_TOKEN").ok()),

            // Vultr
            api_key: env::var("VULTR_API_KEY").ok(),

            // Generic API token
            api_token: env::var("CLOUD_API_TOKEN").ok(),
        }
    }
}

/// Machine type information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MachineType {
    pub name: String,
    pub provider: CloudProvider,
    pub vcpus: u32,
    pub memory_gb: f64,
    pub storage_gb: Option<f64>,
    pub gpu_count: u32,
    pub gpu_type: Option<String>,
    pub network_performance: String,
    pub hourly_price: Option<f64>,
    pub spot_price: Option<f64>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_machine_type_discovery() {
        let discovery = MachineTypeDiscovery::new();

        // Test getting fallback machines
        let aws_machines = discovery.get_common_aws_instances();
        assert!(!aws_machines.is_empty());
        assert_eq!(aws_machines[0].provider, CloudProvider::AWS);

        let gcp_machines = discovery.get_common_gcp_machines();
        assert!(!gcp_machines.is_empty());
        assert_eq!(gcp_machines[0].provider, CloudProvider::GCP);
    }

    #[test]
    fn test_find_best_match() {
        let mut discovery = MachineTypeDiscovery::new();

        // Populate cache with test data
        discovery
            .cache
            .insert(CloudProvider::AWS, discovery.get_common_aws_instances());

        // Find small instance
        let match1 = discovery.find_best_match(&CloudProvider::AWS, 2, 1.0, false, Some(0.02));
        assert!(match1.is_some());
        assert_eq!(match1.unwrap().name, "t3.micro");

        // Find GPU instance
        let match2 = discovery.find_best_match(&CloudProvider::AWS, 4, 16.0, true, None);
        assert!(match2.is_some());
        assert_eq!(match2.unwrap().name, "g4dn.xlarge");
    }
}