cbilling 0.3.0

Multi-cloud billing SDK for Rust — query billing data from AWS, GCP, Aliyun, Tencent Cloud, Volcengine, UCloud, Cloudflare
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
// Copyright 2025 OpenObserve Inc.
// SPDX-License-Identifier: AGPL-3.0

//! Cloud Billing Service
//!
//! Provides core billing query functionality for multiple cloud providers.
//! Uses the [`BillingProvider`](crate::providers::traits::BillingProvider) trait
//! and shared aggregation logic to avoid code duplication across providers.

use serde::{Deserialize, Serialize};

use crate::error::{BillingError, Result};

/// Cloud account configuration
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CloudAccountConfig {
    pub id: String,
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub access_key_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub access_key_secret: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub secret_access_key: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub secret_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub secret_key: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub public_key: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub private_key: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub project_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub region: Option<String>,
    #[serde(default = "default_enabled")]
    pub enabled: bool,
}

fn default_enabled() -> bool {
    true
}

/// Billing data from a cloud provider
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BillingData {
    pub billing_cycle: String,
    pub provider: String,
    pub total_cost: f64,
    pub currency: String,
    pub products: Vec<ProductCost>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cost_change: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub previous_cost: Option<f64>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub accounts: Vec<AccountBillingSummary>,
}

/// Product cost information
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ProductCost {
    pub product_name: String,
    pub product_code: String,
    pub cost: f64,
    pub usage: Option<f64>,
    pub unit: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub account_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub account_name: Option<String>,
    /// Number of resource instances (available for Aliyun/Volcengine/UCloud/GCP)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub count: Option<u32>,
    /// Regions where resources are deployed
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub regions: Vec<String>,
    /// Per-region breakdown: (region, cost, count)
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub region_details: Vec<RegionDetail>,
}

/// Per-region cost breakdown for a product
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegionDetail {
    pub region: String,
    pub cost: f64,
    pub count: u32,
}

/// Account billing summary
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountBillingSummary {
    pub account_id: String,
    pub account_name: String,
    pub total_cost: f64,
    pub product_count: usize,
}

/// Cloud billing service
pub struct CloudBillingService;

impl CloudBillingService {
    /// Load accounts for a specific provider from environment variables
    pub fn load_accounts_for_provider(provider: &str) -> Result<Vec<CloudAccountConfig>> {
        let env_key = match provider {
            "aliyun" => "ALIYUN_ACCOUNTS",
            "tencentcloud" => "TENCENTCLOUD_ACCOUNTS",
            "aws" => "AWS_ACCOUNTS",
            "ucloud" => "UCLOUD_ACCOUNTS",
            "volcengine" => "VOLCENGINE_ACCOUNTS",
            "gcp" => "GCP_ACCOUNTS",
            "cloudflare" => "CLOUDFLARE_ACCOUNTS",
            _ => {
                return Err(BillingError::ServiceError(format!(
                    "Unknown provider: {}",
                    provider
                )));
            }
        };

        // Try multi-account JSON configuration first
        if let Ok(json_str) = std::env::var(env_key) {
            match serde_json::from_str::<Vec<CloudAccountConfig>>(&json_str) {
                Ok(accounts) => {
                    let enabled_accounts: Vec<_> =
                        accounts.into_iter().filter(|a| a.enabled).collect();
                    tracing::info!(
                        "Loaded {} enabled accounts from {} env var",
                        enabled_accounts.len(),
                        env_key
                    );
                    return Ok(enabled_accounts);
                }
                Err(e) => {
                    tracing::warn!(
                        "Failed to parse {}: {}, falling back to legacy env vars",
                        env_key,
                        e
                    );
                }
            }
        }

        // Fallback to legacy single-account environment variables
        Self::load_legacy_account(provider)
    }

    fn load_legacy_account(provider: &str) -> Result<Vec<CloudAccountConfig>> {
        match provider {
            "aliyun" => {
                let access_key_id = std::env::var("ALIBABA_CLOUD_ACCESS_KEY_ID").ok();
                let access_key_secret = std::env::var("ALIBABA_CLOUD_ACCESS_KEY_SECRET").ok();

                if let (Some(ak), Some(sk)) = (access_key_id, access_key_secret) {
                    Ok(vec![CloudAccountConfig {
                        id: "default".to_string(),
                        name: "Default Account".to_string(),
                        access_key_id: Some(ak),
                        access_key_secret: Some(sk),
                        secret_access_key: None,
                        secret_id: None,
                        secret_key: None,
                        public_key: None,
                        private_key: None,
                        project_id: None,
                        region: Some("cn-hangzhou".to_string()),
                        enabled: true,
                    }])
                } else {
                    Err(BillingError::ServiceError(
                        "No Aliyun credentials configured".to_string(),
                    ))
                }
            }
            "aws" => {
                // AWS supports default credential chain (env vars, ~/.aws/credentials, IAM role)
                // so we always return a placeholder config — the SDK resolves credentials itself.
                let ak = std::env::var("AWS_ACCESS_KEY_ID").ok();
                let sk = std::env::var("AWS_SECRET_ACCESS_KEY").ok();
                Ok(vec![CloudAccountConfig {
                    id: "default".to_string(),
                    name: "Default Account".to_string(),
                    access_key_id: ak,
                    access_key_secret: None,
                    secret_access_key: sk,
                    secret_id: None,
                    secret_key: None,
                    public_key: None,
                    private_key: None,
                    project_id: None,
                    region: std::env::var("AWS_DEFAULT_REGION").ok(),
                    enabled: true,
                }])
            }
            "tencentcloud" => Self::load_legacy_tencentcloud(),
            "volcengine" => Self::load_legacy_volcengine(),
            "ucloud" => Self::load_legacy_ucloud(),
            "gcp" => Self::load_legacy_gcp(),
            "cloudflare" => Self::load_legacy_cloudflare(),
            _ => Err(BillingError::ServiceError(format!(
                "Provider {} not implemented for legacy mode",
                provider
            ))),
        }
    }

    // ── Unified provider query ──────────────────────────────────────────

    /// Query billing data from any configured provider.
    ///
    /// This is the primary entry point. It loads accounts, creates adapters via
    /// the provider factory, aggregates results, and returns a [`BillingData`].
    pub async fn query_provider(provider: &str, billing_cycle: &str) -> Result<BillingData> {
        let accounts = Self::load_accounts_for_provider(provider)?;

        if accounts.is_empty() {
            return Err(BillingError::ServiceError(format!(
                "No enabled {} accounts",
                provider
            )));
        }

        let mut all_products = Vec::new();
        let mut total_cost = 0.0;
        let mut account_summaries = Vec::new();
        let mut currency = "USD";

        for account in &accounts {
            let (items, cur) =
                crate::providers::factory::query_provider_items(provider, account, billing_cycle)
                    .await?;
            currency = cur;
            let (products, acct_cost) = crate::providers::aggregation::aggregate_bill_items(
                items,
                Some(&account.id),
                Some(&account.name),
            );
            total_cost += acct_cost;
            account_summaries.push(AccountBillingSummary {
                account_id: account.id.clone(),
                account_name: account.name.clone(),
                total_cost: acct_cost,
                product_count: products.len(),
            });
            all_products.extend(products);
        }

        all_products.sort_by(|a, b| {
            b.cost
                .partial_cmp(&a.cost)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        Ok(BillingData {
            billing_cycle: billing_cycle.to_string(),
            provider: provider.to_string(),
            total_cost,
            currency: currency.to_string(),
            products: all_products,
            cost_change: None,
            previous_cost: None,
            accounts: account_summaries,
        })
    }

    // ── Backward-compatible thin wrappers ────────────────────────────────

    /// Query billing data from Aliyun for a specific month
    #[cfg(feature = "aliyun")]
    pub async fn query_aliyun(billing_cycle: &str) -> Result<BillingData> {
        Self::query_provider("aliyun", billing_cycle).await
    }

    /// Query billing data from Tencent Cloud for a specific month
    #[cfg(feature = "tencentcloud")]
    pub async fn query_tencentcloud(billing_cycle: &str) -> Result<BillingData> {
        Self::query_provider("tencentcloud", billing_cycle).await
    }

    /// Query billing data from AWS for a specific month
    #[cfg(feature = "aws")]
    pub async fn query_aws(billing_cycle: &str) -> Result<BillingData> {
        Self::query_provider("aws", billing_cycle).await
    }

    /// Query billing data from Volcengine for a specific month
    #[cfg(feature = "volcengine")]
    pub async fn query_volcengine(billing_cycle: &str) -> Result<BillingData> {
        Self::query_provider("volcengine", billing_cycle).await
    }

    /// Query billing data from UCloud for a specific month
    #[cfg(feature = "ucloud")]
    pub async fn query_ucloud(billing_cycle: &str) -> Result<BillingData> {
        Self::query_provider("ucloud", billing_cycle).await
    }

    /// Query billing data from GCP for a specific month
    #[cfg(feature = "gcp")]
    pub async fn query_gcp(billing_cycle: &str) -> Result<BillingData> {
        Self::query_provider("gcp", billing_cycle).await
    }

    /// Query billing data from Cloudflare for a specific month
    #[cfg(feature = "cloudflare")]
    pub async fn query_cloudflare(billing_cycle: &str) -> Result<BillingData> {
        Self::query_provider("cloudflare", billing_cycle).await
    }

    // ── Legacy account loaders ──────────────────────────────────────────

    fn load_legacy_tencentcloud() -> Result<Vec<CloudAccountConfig>> {
        let secret_id = std::env::var("TENCENTCLOUD_SECRET_ID").ok();
        let secret_key = std::env::var("TENCENTCLOUD_SECRET_KEY").ok();
        if let (Some(id), Some(key)) = (secret_id, secret_key) {
            Ok(vec![CloudAccountConfig {
                id: "default".to_string(),
                name: "Default Account".to_string(),
                access_key_id: None,
                access_key_secret: None,
                secret_access_key: None,
                secret_id: Some(id),
                secret_key: Some(key),
                public_key: None,
                private_key: None,
                project_id: None,
                region: std::env::var("TENCENTCLOUD_REGION").ok(),
                enabled: true,
            }])
        } else {
            Err(BillingError::ServiceError(
                "No TencentCloud credentials configured".to_string(),
            ))
        }
    }

    fn load_legacy_volcengine() -> Result<Vec<CloudAccountConfig>> {
        let ak = std::env::var("VOLCENGINE_ACCESS_KEY_ID").ok();
        let sk = std::env::var("VOLCENGINE_SECRET_ACCESS_KEY").ok();
        if let (Some(ak), Some(sk)) = (ak, sk) {
            Ok(vec![CloudAccountConfig {
                id: "default".to_string(),
                name: "Default Account".to_string(),
                access_key_id: Some(ak),
                access_key_secret: None,
                secret_access_key: Some(sk),
                secret_id: None,
                secret_key: None,
                public_key: None,
                private_key: None,
                project_id: None,
                region: Some("cn-beijing".to_string()),
                enabled: true,
            }])
        } else {
            Err(BillingError::ServiceError(
                "No Volcengine credentials configured".to_string(),
            ))
        }
    }

    fn load_legacy_ucloud() -> Result<Vec<CloudAccountConfig>> {
        let pub_key = std::env::var("UCLOUD_PUBLIC_KEY").ok();
        let priv_key = std::env::var("UCLOUD_PRIVATE_KEY").ok();
        let project = std::env::var("UCLOUD_PROJECT_ID").ok();
        if let (Some(pk), Some(sk), Some(proj)) = (pub_key, priv_key, project) {
            Ok(vec![CloudAccountConfig {
                id: "default".to_string(),
                name: "Default Account".to_string(),
                access_key_id: None,
                access_key_secret: None,
                secret_access_key: None,
                secret_id: None,
                secret_key: None,
                public_key: Some(pk),
                private_key: Some(sk),
                project_id: Some(proj),
                region: None,
                enabled: true,
            }])
        } else {
            Err(BillingError::ServiceError(
                "No UCloud credentials configured".to_string(),
            ))
        }
    }

    fn load_legacy_gcp() -> Result<Vec<CloudAccountConfig>> {
        let project_id = std::env::var("GCP_PROJECT_ID").ok();
        let sa_json = std::env::var("GCP_SERVICE_ACCOUNT_JSON").ok();
        let has_creds_file = std::env::var("GOOGLE_APPLICATION_CREDENTIALS").is_ok();

        if let Some(proj) = project_id {
            Ok(vec![CloudAccountConfig {
                id: "default".to_string(),
                name: "Default Account".to_string(),
                access_key_id: None,
                access_key_secret: None,
                secret_access_key: None,
                secret_id: None,
                secret_key: None,
                public_key: None,
                private_key: sa_json, // service account JSON content
                project_id: Some(proj),
                region: None,
                enabled: true,
            }])
        } else if has_creds_file {
            // Infer project from credentials file
            Ok(vec![CloudAccountConfig {
                id: "default".to_string(),
                name: "Default Account".to_string(),
                access_key_id: None,
                access_key_secret: None,
                secret_access_key: None,
                secret_id: None,
                secret_key: None,
                public_key: None,
                private_key: None,
                project_id: Some("auto".to_string()),
                region: None,
                enabled: true,
            }])
        } else {
            Err(BillingError::ServiceError(
                "No GCP credentials configured".to_string(),
            ))
        }
    }

    fn load_legacy_cloudflare() -> Result<Vec<CloudAccountConfig>> {
        let account_id = std::env::var("CLOUDFLARE_ACCOUNT_ID").ok();
        let api_token = std::env::var("CLOUDFLARE_API_TOKEN").ok();
        let api_key = std::env::var("CLOUDFLARE_API_KEY").ok();
        let api_email = std::env::var("CLOUDFLARE_API_EMAIL").ok();

        if let Some(acct) = account_id {
            Ok(vec![CloudAccountConfig {
                id: "default".to_string(),
                name: "Default Account".to_string(),
                access_key_id: Some(acct),  // account_id
                access_key_secret: api_key, // api_key (for key+email auth)
                secret_access_key: None,
                secret_id: api_email,  // api_email (for key+email auth)
                secret_key: api_token, // api_token (for token auth)
                public_key: None,
                private_key: None,
                project_id: None,
                region: None,
                enabled: true,
            }])
        } else {
            Err(BillingError::ServiceError(
                "No Cloudflare credentials configured".to_string(),
            ))
        }
    }

    /// Get list of configured cloud providers
    pub fn get_configured_providers() -> Vec<String> {
        let mut providers = Vec::new();

        #[cfg(feature = "aliyun")]
        if Self::load_accounts_for_provider("aliyun").is_ok() {
            providers.push("aliyun".to_string());
        }

        #[cfg(feature = "tencentcloud")]
        if Self::load_accounts_for_provider("tencentcloud").is_ok() {
            providers.push("tencentcloud".to_string());
        }

        #[cfg(feature = "aws")]
        {
            let has_aws = std::env::var("AWS_ACCOUNTS").is_ok()
                || std::env::var("AWS_ACCESS_KEY_ID").is_ok()
                || std::path::Path::new(&format!(
                    "{}/.aws/credentials",
                    std::env::var("HOME").unwrap_or_default()
                ))
                .exists();

            if has_aws {
                providers.push("aws".to_string());
            }
        }

        #[cfg(feature = "ucloud")]
        if Self::load_accounts_for_provider("ucloud").is_ok() {
            providers.push("ucloud".to_string());
        }

        #[cfg(feature = "volcengine")]
        if Self::load_accounts_for_provider("volcengine").is_ok() {
            providers.push("volcengine".to_string());
        }

        #[cfg(feature = "gcp")]
        if Self::load_accounts_for_provider("gcp").is_ok() {
            providers.push("gcp".to_string());
        }

        #[cfg(feature = "cloudflare")]
        if Self::load_accounts_for_provider("cloudflare").is_ok() {
            providers.push("cloudflare".to_string());
        }

        providers
    }
}

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

    #[test]
    fn test_cloud_account_config() {
        let config = CloudAccountConfig {
            id: "test".to_string(),
            name: "Test Account".to_string(),
            access_key_id: Some("key".to_string()),
            access_key_secret: Some("secret".to_string()),
            secret_access_key: None,
            secret_id: None,
            secret_key: None,
            public_key: None,
            private_key: None,
            project_id: None,
            region: Some("cn-hangzhou".to_string()),
            enabled: true,
        };

        assert_eq!(config.id, "test");
        assert!(config.enabled);
    }

    #[test]
    fn test_get_configured_providers() {
        let providers = CloudBillingService::get_configured_providers();
        // May be empty in tests without env vars -- just verify it doesn't panic
        let _ = providers;
    }

    #[test]
    fn test_billing_data_serialization() {
        let data = BillingData {
            billing_cycle: "2026-03".to_string(),
            provider: "test".to_string(),
            total_cost: 123.45,
            currency: "USD".to_string(),
            products: vec![ProductCost {
                product_name: "Compute".to_string(),
                product_code: "compute".to_string(),
                cost: 123.45,
                ..Default::default()
            }],
            cost_change: None,
            previous_cost: None,
            accounts: vec![],
        };

        let json = serde_json::to_string(&data).expect("serialize");
        let deserialized: BillingData = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(deserialized.total_cost, 123.45);
        assert_eq!(deserialized.products.len(), 1);
    }

    #[test]
    fn test_product_cost_default() {
        let pc = ProductCost::default();
        assert_eq!(pc.cost, 0.0);
        assert!(pc.product_name.is_empty());
        assert!(pc.regions.is_empty());
        assert!(pc.region_details.is_empty());
        assert!(pc.count.is_none());
    }
}