feat: custom order disk max sizes
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-04-01 11:40:05 +01:00
parent b7d7027eec
commit 2ae158c31a
2 changed files with 26 additions and 14 deletions

View File

@ -80,7 +80,7 @@ impl ApiVmIpAssignment {
}
}
#[derive(Clone, Copy, Serialize, Deserialize, JsonSchema)]
#[derive(Clone, Copy, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Hash)]
#[serde(rename_all = "lowercase")]
pub enum DiskType {
HDD = 0,
@ -105,7 +105,7 @@ impl From<DiskType> for lnvps_db::DiskType {
}
}
#[derive(Clone, Copy, Serialize, Deserialize, JsonSchema)]
#[derive(Clone, Copy, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Hash)]
#[serde(rename_all = "lowercase")]
pub enum DiskInterface {
SATA = 0,
@ -166,8 +166,6 @@ pub struct ApiCustomTemplateParams {
pub min_cpu: u16,
pub min_memory: u64,
pub max_memory: u64,
pub min_disk: u64,
pub max_disk: u64,
pub disks: Vec<ApiCustomTemplateDiskParam>,
}
@ -178,7 +176,7 @@ impl ApiCustomTemplateParams {
region: &VmHostRegion,
max_cpu: u16,
max_memory: u64,
max_disk: u64,
max_disk: &HashMap<(DiskType, DiskInterface), u64>,
) -> Result<Self> {
const GB: u64 = 1024 * 1024 * 1024;
Ok(ApiCustomTemplateParams {
@ -192,14 +190,17 @@ impl ApiCustomTemplateParams {
min_cpu: 1,
min_memory: GB,
max_memory,
min_disk: GB * 5,
max_disk,
disks: disks
.iter()
.filter(|d| d.pricing_id == pricing.id)
.map(|d| ApiCustomTemplateDiskParam {
disk_type: d.kind.into(),
disk_interface: d.interface.into(),
.filter_map(|d| {
Some(ApiCustomTemplateDiskParam {
min_disk: GB * 5,
max_disk: *max_disk
.get(&(d.kind.into(), d.interface.into()))?,
disk_type: d.kind.into(),
disk_interface: d.interface.into(),
})
})
.collect(),
})
@ -207,6 +208,8 @@ impl ApiCustomTemplateParams {
}
#[derive(Clone, Serialize, Deserialize, JsonSchema)]
pub struct ApiCustomTemplateDiskParam {
pub min_disk: u64,
pub max_disk: u64,
pub disk_type: DiskType,
pub disk_interface: DiskInterface,
}

View File

@ -356,9 +356,18 @@ async fn v1_list_vm_templates(
let max_memory = templates.iter().map(|t| t.memory).max().unwrap_or(GB * 2);
let max_disk = templates
.iter()
.map(|t| t.disk_size)
.max()
.unwrap_or(GB * 5);
.map(|t| (t.disk_type, t.disk_interface, t.disk_size))
.fold(HashMap::new(), |mut acc, v| {
let k = (v.0.into(), v.1.into());
if let Some(mut x) = acc.get_mut(&k) {
if *x < v.2 {
*x = v.2;
}
} else {
acc.insert(k, v.2);
}
return acc;
});
Some(
custom_templates
.into_iter()
@ -370,7 +379,7 @@ async fn v1_list_vm_templates(
region,
max_cpu,
max_memory,
max_disk,
&max_disk,
)
.ok()
})