Skip to content

Commit

Permalink
Accept fractional units as input to --compute-size and `--storage-s…
Browse files Browse the repository at this point in the history
…ize` (#1206)

Also, recognize that `units_default` can be `null`.
  • Loading branch information
elprans authored Feb 7, 2024
1 parent d24f57d commit 2c4f0b5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/cloud/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ pub struct Version {
pub struct Price {
pub billable: String,
pub unit_price_cents: String,
pub units_bundled: String,
pub units_default: String,
pub units_bundled: Option<String>,
pub units_default: Option<String>,
}

pub type Prices = HashMap<CloudTier, HashMap<String, Vec<Price>>>;
Expand Down
13 changes: 9 additions & 4 deletions src/portable/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,20 +257,25 @@ fn create_cloud(
.context(format!("could not download pricing information for the {} region", region))?;
let default_compute = region_prices.into_iter()
.find(|&price| price.billable == "compute")
.context("could not download pricing information for compute")?;
.context("could not download pricing information for compute")?
.units_default.clone()
.context("could not find default value for compute")?;

let default_storage = region_prices.into_iter()
.find(|&price| price.billable == "storage")
.context("could not download pricing information for compute")?;
.context("could not download pricing information for storage")?
.units_default.clone()
.context("could not find default value for storage")?;

let mut req_resources: Vec<cloud::ops::CloudInstanceResourceRequest> = vec![];

let compute_size_v = match compute_size {
None => default_compute.units_default.clone(),
None => default_compute,
Some(v) => v.clone(),
};

let storage_size_v = match storage_size {
None => default_storage.units_default.clone(),
None => default_storage,
Some(v) => v.clone(),
};

Expand Down
18 changes: 14 additions & 4 deletions src/portable/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,21 @@ pub enum InstanceName {
}

fn billable_unit(s: &str) -> Result<String, String> {
let unit: usize = s
let (numerator, denominator) = match s.split_once("/") {
Some(v) => v,
None => (s, "1"),
};

let n: u64 = numerator
.parse()
.map_err(|_| format!("`{s}` is not a positive number or valid fraction"))?;

let d: u64 = denominator
.parse()
.map_err(|_| format!("`{s}` is not a positive number"))?;
if unit <= 0 {
Err(String::from("`{s}` is not a positive number"))
.map_err(|_| format!("`{s}` is not a positive number or valid fraction"))?;

if n <= 0 || d <= 0 {
Err(String::from("`{s}` is not a positive number or valid fraction"))
} else {
Ok(s.to_string())
}
Expand Down
8 changes: 5 additions & 3 deletions src/portable/resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,15 @@ fn resize_cloud_cmd(
compute_size = Some(region_prices.into_iter()
.find(|&price| price.billable == "compute")
.context("could not download pricing information for compute")?
.units_default.clone());
.units_default.clone()
.context("could not find default value for compute")?);
}
if storage_size.is_none() {
storage_size = Some(region_prices.into_iter()
.find(|&price| price.billable == "storage")
.context("could not download pricing information for compute")?
.units_default.clone());
.context("could not download pricing information for storage")?
.units_default.clone()
.context("could not find default value for storage")?);
}
}
}
Expand Down

0 comments on commit 2c4f0b5

Please sign in to comment.