osc-cost 0.8.0

osc-cost helps measuring OUTSCALE infrastructure costs
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
use std::{
    collections::{HashMap, HashSet},
    error,
};

use log::{debug, error, info, warn};
use outscale_api::{
    apis::{
        image_api::read_images,
        vm_api::{read_vm_types, read_vms},
    },
    models::{
        FiltersImage, FiltersVm, FiltersVmType, ReadImagesRequest, ReadImagesResponse,
        ReadVmTypesRequest, ReadVmTypesResponse, ReadVmsRequest, ReadVmsResponse,
    },
};

use crate::{
    core::{vms::Vm, Resource, Resources},
    oapi::ImageId,
    VERSION,
};
use lazy_static::lazy_static;
use regex::Regex;

use super::Input;

pub type VmId = String;
const RESOURCE_NAME: &str = "Vm";

impl Input {
    pub fn fetch_vms(&mut self) -> Result<(), Box<dyn error::Error>> {
        if self.skip_fetch(RESOURCE_NAME) {
            return Ok(());
        }
        let result: ReadVmsResponse = {
            let filter_vm: FiltersVm = match &self.filters {
                Some(filter) => FiltersVm {
                    tag_keys: Some(filter.tag_keys.clone()),
                    tag_values: Some(filter.tag_values.clone()),
                    tags: Some(filter.tags.clone()),
                    ..Default::default()
                },
                None => FiltersVm::new(),
            };

            let request = ReadVmsRequest {
                filters: Some(Box::new(filter_vm)),
                ..Default::default()
            };
            read_vms(&self.config, Some(request))?
        };
        debug!("{:#?}", result);

        let vms = match result.vms {
            None => {
                warn!("no vm list provided");
                return Ok(());
            }
            Some(vms) => vms,
        };
        let mut self_vms = HashMap::new();
        for vm in vms {
            if let Some(state) = &vm.state {
                match state.as_str() {
                    "running" | "stopping" | "shutting-down" => {}
                    "pending" | "stopped" | "terminated" | "quarantine" => continue,
                    _ => {
                        warn!("un-managed vm state {} found", state);
                        continue;
                    }
                };
            }
            let vm_id = match &vm.vm_id {
                Some(id) => id,
                None => {
                    warn!("vm has no id");
                    continue;
                }
            };
            if let Some(vm_type) = &vm.vm_type {
                if !self.need_vm_types_fetch && !vm_type.starts_with("tina") {
                    self.need_vm_types_fetch = true;
                }
            }
            self_vms.insert(vm_id.clone(), vm);
        }

        info!("fetched {} vms", self_vms.len());
        self.vms = self_vms;
        Ok(())
    }

    pub fn fetch_vms_images(&mut self) -> Result<(), Box<dyn error::Error>> {
        // Collect all unique images
        let mut images = HashSet::<ImageId>::new();
        for vm_id in self.vms.keys() {
            images.insert(vm_id.clone());
        }
        for img_id in self.vms_images.keys() {
            images.remove(img_id);
        }
        if images.is_empty() {
            //if we have no images to list, we dont need to make a call
            return Ok(());
        }

        let mut filters_image = FiltersImage::new();
        filters_image.image_ids = Some(images.into_iter().collect::<Vec<_>>());

        let mut request = ReadImagesRequest::new();
        request.filters = Some(Box::new(filters_image));

        let result: ReadImagesResponse = read_images(&self.config, Some(request.clone()))?;
        debug!("{:#?}", result);

        let images = match result.images {
            None => {
                warn!("no image list provided");
                return Ok(());
            }
            Some(images) => images,
        };
        for image in images {
            let image_id = image.image_id.clone().unwrap_or_else(|| String::from(""));
            self.vms_images.insert(image_id, image);
        }
        info!("fetched {} images used by vms", self.vms_images.len());
        Ok(())
    }

    pub fn fetch_vm_types(&mut self) -> Result<(), Box<dyn error::Error>> {
        // Collect all unique vm_type
        let mut vm_types = HashSet::<String>::new();
        for vm in self.vms.values() {
            if let Some(vmt) = vm.vm_type.as_ref() {
                vm_types.insert(vmt.clone());
            }
        }
        for t_id in self.vm_types.keys() {
            vm_types.remove(t_id);
        }
        if vm_types.is_empty() {
            //if we have no type to list, we dont need to make a call
            return Ok(());
        }

        let mut filter_type = FiltersVmType::new();
        filter_type.vm_type_names = Some(vm_types.into_iter().collect::<Vec<_>>());

        let mut request = ReadVmTypesRequest::new();
        request.filters = Some(Box::new(filter_type));

        let result: ReadVmTypesResponse = read_vm_types(&self.config, Some(request.clone()))?;
        debug!("{:#?}", result);

        let vm_types = match result.vm_types {
            None => {
                warn!("no vm type list provided");
                return Ok(());
            }
            Some(vm_types) => vm_types,
        };
        for vm_type in vm_types {
            let vm_type_name = match &vm_type.vm_type_name {
                Some(name) => name.clone(),
                None => {
                    warn!("vm type has no name");
                    continue;
                }
            };
            self.vm_types.insert(vm_type_name, vm_type);
        }

        info!("fetched {} vm types", self.vm_types.len());
        Ok(())
    }

    pub fn fill_resource_vm(&mut self, resources: &mut Resources) {
        if self.vms.is_empty() && self.need_default_resource {
            resources.resources.push(Resource::Vm(Vm {
                account_id: self.account_id(),
                read_date_rfc3339: self.fetch_date.map(|date| date.to_rfc3339()),
                region: self.region.clone(),
                ..Default::default()
            }));
        }
        for (vm_id, vm) in &self.vms {
            let specs = match VmSpecs::new(vm, self) {
                Some(s) => s,
                None => {
                    warn!("error while creating the VMSpec of {}", vm_id);
                    continue;
                }
            };

            let tenancy = match &vm.placement {
                Some(placement) => placement.tenancy.clone().unwrap_or_default(),

                None => {
                    warn!("error to get tenancy");
                    "".to_string()
                }
            };

            match tenancy.as_str() {
                "dedicated" => {
                    warn!("use dedicated instance");
                    self.use_dedicated_instance = false
                }
                _ => warn!("use default instance"),
            };

            let core_vm = Vm {
                osc_cost_version: Some(String::from(VERSION)),
                account_id: self.account_id(),
                read_date_rfc3339: self.fetch_date.map(|date| date.to_rfc3339()),
                region: self.region.clone(),
                resource_id: Some(vm_id.clone()),
                price_per_hour: None,
                price_per_month: None,
                vm_type: vm.vm_type.clone(),
                vm_vcpu_gen: Some(specs.generation.clone()),
                vm_core_performance: vm.performance.clone(),
                vm_image: vm.image_id.clone(),
                vm_vcpu: specs.vcpu,
                vm_ram_gb: specs.ram_gb,
                nested_virtualization: vm.nested_virtualization.unwrap_or_default(),
                tenancy,
                price_vcpu_per_hour: specs.price_vcpu_per_hour,
                price_ram_gb_per_hour: specs.price_ram_gb_per_hour,
                // Mandatory to compute price for BoxUsage (aws-type, etc) types
                price_box_per_hour: specs.price_box_per_hour,
                // Mandatory to compute price for dedicated instance
                factor_vm_additional_cost: specs.factor_vm_additional_cost,
                // Mandatory to compute price for all vm types
                price_license_per_ram_gb_per_hour: specs.price_product_per_ram_gb_per_hour,
                price_license_per_cpu_per_hour: specs.price_product_per_cpu_per_hour,
                price_license_per_vm_per_hour: specs.price_product_per_vm_per_hour,
                license_codes: specs.product_codes.join(","),
            };
            resources.resources.push(Resource::Vm(core_vm));
        }
    }
}

pub struct VmSpecs {
    vm_type: String,
    generation: String,
    vcpu: usize,
    ram_gb: usize,
    performance: String,
    tenancy: String,
    product_codes: Vec<String>,
    price_vcpu_per_hour: f32,
    price_ram_gb_per_hour: f32,
    price_box_per_hour: f32,
    price_product_per_ram_gb_per_hour: f32,
    price_product_per_cpu_per_hour: f32,
    price_product_per_vm_per_hour: f32,
    factor_vm_additional_cost: f32,
}

impl VmSpecs {
    fn new(vm: &outscale_api::models::Vm, input: &Input) -> Option<Self> {
        let vm_type = match &vm.vm_type {
            Some(vm_type) => vm_type,
            None => {
                warn!("cannot get vm type in vm details");
                return None;
            }
        };
        let tenancy = match &vm.placement {
            Some(placement) => placement.tenancy.clone().unwrap_or_default(),
            None => {
                warn!("error to get tenancy");
                "".to_string()
            }
        };

        let out = VmSpecs {
            vm_type: vm_type.clone(),
            tenancy,
            generation: String::from(""),
            vcpu: 0,
            ram_gb: 0,
            performance: String::from(""),
            product_codes: vm.product_codes.clone().unwrap_or_default(),
            price_vcpu_per_hour: 0_f32,
            price_ram_gb_per_hour: 0_f32,
            price_box_per_hour: 0_f32,
            price_product_per_ram_gb_per_hour: 0_f32,
            price_product_per_cpu_per_hour: 0_f32,
            price_product_per_vm_per_hour: 0_f32,
            factor_vm_additional_cost: 0_f32,
        };
        match vm_type.starts_with("tina") {
            true => out
                .internal_parse_tina_type()?
                .parse_performance(vm.performance.clone())?
                .parse_product_price(input)?
                .parse_dedicated_instance_additional_prices(input)?
                .parse_prices(input),
            false => out
                .internal_parse_box_type(input)?
                .parse_product_price(input)?
                .parse_dedicated_instance_additional_prices(input)?
                .parse_prices(input),
        }
    }

    fn internal_parse_tina_type(mut self) -> Option<VmSpecs> {
        (self.generation, self.vcpu, self.ram_gb, self.performance) =
            match VmSpecs::parse_tina_type(&self.vm_type) {
                Some((gen, vpcu, ram, performance)) => {
                    (gen, vpcu as usize, ram as usize, performance)
                }
                None => return None,
            };
        Some(self)
    }

    fn parse_performance(mut self, performance_opt: Option<String>) -> Option<VmSpecs> {
        let Some(performance_str) = performance_opt else {
            warn!("the performance is empty, internal error");
            return None;
        };

        // https://docs.outscale.com/en/userguide/Instance-Types.html#_characteristics
        let performance = match performance_str.as_str() {
            "medium" => "3",
            "high" => "2",
            "highest" => "1",
            e => {
                warn!("the performance is not found: {}", e);
                return None;
            }
        };

        if !self.performance.is_empty() && performance != self.performance {
            warn!("the performance is inconsistent between vmType and performance, internal error");
            return None;
        }

        self.performance = performance.to_string();

        Some(self)
    }

    fn parse_product_price(mut self, input: &Input) -> Option<VmSpecs> {
        for product_code in &self.product_codes {
            let Some(price) = input.catalog_entry(
                "TinaOS-FCU",
                "ProductUsage",
                &format!("RunInstances-{product_code}-OD"),
            ) else {
                continue;
            };
            // License calculation is specific to each product code.
            // https://en.outscale.com/pricing/#licenses
            let cores = self.vcpu as f32;
            let Some(price_factor) = VmSpecs::compute_product_price_per_hour(cores, product_code)
            else {
                warn!("product code {} is not managed", product_code);
                continue;
            };

            match product_code.as_str() {
                // Generic Linux vm, should be free
                "0001" => {}
                // Microsoft Windows Server 2019 License
                // Price by 2 cores
                "0002" => {
                    let price_for_vm = price_factor * price;
                    // set back price per cpu per hour
                    self.price_product_per_cpu_per_hour += price_for_vm / cores;
                }
                // mapr license (0003)
                // Oracle Linux OS Distribution (0004)
                // Microsoft Windows 10 E3 VDA License (0005)
                // Red Hat Enterprise Linux OS Distribution (0006)
                // sql server web (0007)
                "0003" | "0004" | "0005" | "0006" | "0007" => {
                    self.price_product_per_vm_per_hour += price
                }
                // Microsoft Windows SQL Server 2019 Standard Edition (0008)
                // Microsoft Windows SQL Server 2019 Enterprise Edition (0009)
                // Price by 2 cores (4 cores min)
                "0008" | "0009" => {
                    let price_for_vm = price_factor * price;
                    // set back price per cpu per hour
                    self.price_product_per_cpu_per_hour += price_for_vm / cores;
                }
                _ => {
                    warn!("product code {} is not managed", product_code);
                    continue;
                }
            };
        }
        Some(self)
    }

    fn parse_prices(mut self, input: &Input) -> Option<VmSpecs> {
        let price_vcpu_per_hour = input.catalog_entry(
            "TinaOS-FCU",
            &format!("CustomCore:v{}-p{}", self.generation, self.performance),
            "RunInstances-OD",
        )?;
        let price_ram_gb_per_hour =
            input.catalog_entry("TinaOS-FCU", "CustomRam", "RunInstances-OD")?;
        self.price_vcpu_per_hour = price_vcpu_per_hour;
        self.price_ram_gb_per_hour = price_ram_gb_per_hour;
        Some(self)
    }

    fn internal_parse_box_type(mut self, input: &Input) -> Option<VmSpecs> {
        (self.generation, self.vcpu, self.ram_gb, self.performance) =
            match VmSpecs::parse_box_type(&self.vm_type, input) {
                Some((gen, vpcu, ram, performance)) => {
                    (gen, vpcu as usize, ram as usize, performance)
                }
                None => return None,
            };

        Some(self)
    }

    fn parse_dedicated_instance_additional_prices(mut self, input: &Input) -> Option<VmSpecs> {
        // factor_dedicated_vm_additional_cost_per_thousand is expressed as a per thousand (‰)
        // The doc is here https://docs.outscale.com/en/userguide/Getting-the-Price-of-Your-Resources.html but seems wrong, the good formula is: Instance_price + ((Instance_price x Additional_price * 1000 )/100
        let factor_dedicated_vm_additional_cost_per_thousand =
            input.catalog_entry("TinaOS-FCU", "DedicatedInstanceSurplus", "RunInstances")?;
        self.factor_vm_additional_cost = match self.tenancy.as_str() {
            "dedicated" => 1.0 + (factor_dedicated_vm_additional_cost_per_thousand * 10.0),
            "default" => 1.0,
            unkown_tenancy => {
                error!(
                    "vm additional costs for {} is not supported",
                    unkown_tenancy
                );
                1.0
            }
        };
        Some(self)
    }

    pub fn parse_tina_type(vm_type: &str) -> Option<(String, f32, f32, String)> {
        // format: tinav5.c20r40p1
        //              │  ││ ││ │
        //              │  ││ ││ └── vcpu performance
        //              │  ││ └┴── ram quantity
        //              │  └┴── number of vcores
        //              └── generation
        lazy_static! {
            static ref REG: Regex = Regex::new(r"^tinav(\d+)\.c(\d+)r(\d+)(p(\d+))?$").unwrap();
        }
        let cap = REG.captures_iter(vm_type).next()?;
        let generation = String::from(&cap[1]);
        let vcpu = cap[2].parse::<usize>().ok()?;
        let ram_gb = cap[3].parse::<usize>().ok()?;
        let performance = String::from(cap.get(5).map_or("2", |m| m.as_str()));
        Some((generation, vcpu as f32, ram_gb as f32, performance))
    }

    pub fn parse_box_type(vm_type: &String, input: &Input) -> Option<(String, f32, f32, String)> {
        let vm_type_obj = input.vm_types.get(vm_type)?;
        let Some(vcpu) = vm_type_obj.vcore_count else {
            warn!("vpcu is not defined for this vm type {}", vm_type);
            return None;
        };
        let Some(ram_gb) = vm_type_obj.memory_size else {
            warn!("ram_gb is not defined for this vm type {}", vm_type);
            return None;
        };
        // We don't have this information through API for now but we have it on public documentation:
        // https://docs.outscale.com/en/userguide/Instance-Types.html
        // format: m4.4xlarge
        //         └┴── vm family
        lazy_static! {
            static ref REG: Regex = Regex::new(r"^([a-z]+\d+)\..*$").unwrap();
        }
        let cap = match REG.captures_iter(vm_type).next() {
            Some(cap) => cap,
            // family and generation is not mandatory to extract price.
            None => {
                warn!("Cannot extract vm family from {}", vm_type);
                return None;
            }
        };
        let family = String::from(&cap[1]);
        let (generation, performance) = match family.as_str() {
            "c1" => ("1", "1"),
            "c3" => ("3", "1"),
            "c4" => ("4", "1"),
            "c5" => ("5", "1"),
            "cc1" => ("1", "1"),
            "cc2" => ("2", "2"),
            "cr1" => ("1", "2"),
            "g2" => ("2", "1"),
            "g3" => ("3", "1"),
            "hi1" => ("3", "1"),
            "i2" => ("4", "2"),
            "io5" => ("4", "2"),
            "m1" => ("1", "2"),
            "m2" => ("2", "2"),
            "m3" => ("3", "2"),
            "m4" => ("4", "2"),
            "m5" => ("5", "2"),
            "mv3" => ("4", "2"),
            "nv1" => ("4", "2"),
            "nv2" => ("4", "2"),
            "oc1" => ("1", "1"),
            "oc2" => ("2", "1"),
            "oc5" => ("4", "1"),
            "og3" => ("4", "2"),
            "og4" => ("4", "1"),
            "om5" => ("4", "2"),
            "os1" => ("1", "2"),
            "os3" => ("3", "2"),
            "p100" => ("5", "1"),
            "p3" => ("5", "1"),
            "p6" => ("5", "1"),
            "r3" => ("3", "2"),
            "r4" => ("4", "2"),
            "t1" => ("1", "3"),
            "t2" => ("2", "3"),
            unknown_family => {
                warn!("unkown family name for {}", unknown_family);
                ("", "")
            }
        };
        let generation = String::from(generation);
        let performance = String::from(performance);

        Some((generation, vcpu as f32, ram_gb, performance))
    }

    pub fn compute_product_price_per_hour(vcpu: f32, product_code: &String) -> Option<f32> {
        // License calculation is specific to each product code.
        // https://en.outscale.com/pricing/#licenses
        let cores = vcpu;
        match product_code.as_str() {
            // Generic Linux vm, should be free
            "0001" => Some(0.0),
            // Microsoft Windows Server 2019 License
            // Price by 2 cores
            "0002" => {
                let cores_to_pay = ((cores + 1.0) / 2.0).floor();
                Some(cores_to_pay)
            }
            // mapr license (0003)
            // Oracle Linux OS Distribution (0004)
            // Microsoft Windows 10 E3 VDA License (0005)
            // Red Hat Enterprise Linux OS Distribution (0006)
            // sql server web (0007)
            "0003" | "0004" | "0005" | "0006" | "0007" => Some(1.0),
            // Microsoft Windows SQL Server 2019 Standard Edition (0008)
            // Microsoft Windows SQL Server 2019 Enterprise Edition (0009)
            // Price by 2 cores (4 cores min)
            "0008" | "0009" => {
                let cores_to_pay = ((cores + 1.0) / 2.0).floor().max(4.0);
                Some(cores_to_pay)
            }
            _ => {
                warn!("product code {} is not managed", product_code);
                None
            }
        }
    }
}