Skip to content

Commit

Permalink
refactor/nanocld: object inspect trait
Browse files Browse the repository at this point in the history
  • Loading branch information
leon3s committed Jan 7, 2024
1 parent c9f9c83 commit 56f5330
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 74 deletions.
23 changes: 22 additions & 1 deletion bin/nanocld/src/objects/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bollard_next::{
use nanocl_error::http::{HttpResult, HttpError};
use nanocl_stubs::{
process::ProcessKind,
cargo::{Cargo, CargoDeleteQuery},
cargo::{Cargo, CargoDeleteQuery, CargoInspect},
cargo_spec::{ReplicationMode, CargoSpecPartial},
};

Expand Down Expand Up @@ -273,3 +273,24 @@ impl ObjPatchByPk for CargoDb {
CargoDb::fn_put_obj_by_pk(pk, obj, state).await
}
}

impl ObjInspectByPk for CargoDb {
type ObjInspectOut = CargoInspect;

async fn inspect_obj_by_pk(
pk: &str,
state: &SystemState,
) -> HttpResult<Self::ObjInspectOut> {
let cargo = CargoDb::transform_read_by_pk(pk, &state.pool).await?;
let processes = ProcessDb::read_by_kind_key(pk, &state.pool).await?;
let (_, _, _, running_instances) = utils::process::count_status(&processes);
Ok(CargoInspect {
created_at: cargo.created_at,
namespace_name: cargo.namespace_name,
instance_total: processes.len(),
instance_running: running_instances,
spec: cargo.spec,
instances: processes,
})
}
}
12 changes: 12 additions & 0 deletions bin/nanocld/src/objects/generic/inspect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use nanocl_error::http::HttpResult;

use crate::models::SystemState;

pub trait ObjInspectByPk {
type ObjInspectOut;

async fn inspect_obj_by_pk(
pk: &str,
state: &SystemState,
) -> HttpResult<Self::ObjInspectOut>;
}
2 changes: 2 additions & 0 deletions bin/nanocld/src/objects/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ mod create;
mod delete;
mod patch;
mod put;
mod inspect;

pub use create::*;
pub use delete::*;
pub use patch::*;
pub use put::*;
pub use inspect::*;
25 changes: 24 additions & 1 deletion bin/nanocld/src/objects/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bollard_next::container::RemoveContainerOptions;
use futures_util::{StreamExt, stream::FuturesUnordered};

use nanocl_error::http::{HttpResult, HttpError};
use nanocl_stubs::job::{Job, JobPartial};
use nanocl_stubs::job::{Job, JobPartial, JobInspect};

use crate::{
utils,
Expand Down Expand Up @@ -89,3 +89,26 @@ impl ObjDelByPk for JobDb {
Ok(job)
}
}

impl ObjInspectByPk for JobDb {
type ObjInspectOut = JobInspect;

async fn inspect_obj_by_pk(
pk: &str,
state: &crate::models::SystemState,
) -> HttpResult<Self::ObjInspectOut> {
let job = JobDb::read_by_pk(pk, &state.pool).await?.try_to_spec()?;
let instances = ProcessDb::read_by_kind_key(pk, &state.pool).await?;
let (instance_total, instance_failed, instance_success, instance_running) =
utils::process::count_status(&instances);
let job_inspect = JobInspect {
spec: job,
instance_total,
instance_success,
instance_running,
instance_failed,
instances,
};
Ok(job_inspect)
}
}
30 changes: 28 additions & 2 deletions bin/nanocld/src/objects/vm.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use bollard_next::container::RemoveContainerOptions;

use nanocl_error::http::{HttpResult, HttpError};
use nanocl_stubs::{vm::Vm, process::ProcessKind, vm_spec::VmSpecPartial};
use nanocl_stubs::{
vm::{Vm, VmInspect},
process::ProcessKind,
vm_spec::VmSpecPartial,
};

use crate::{
utils,
repositories::generic::*,
models::{
VmDb, SystemState, VmObjCreateIn, VmImageDb, SpecDb, VmObjPutIn,
VmObjPatchIn,
VmObjPatchIn, ProcessDb,
},
};
use super::generic::*;
Expand Down Expand Up @@ -170,3 +174,25 @@ impl ObjPatchByPk for VmDb {
VmDb::fn_put_obj_by_pk(pk, obj, state).await
}
}

impl ObjInspectByPk for VmDb {
type ObjInspectOut = VmInspect;

async fn inspect_obj_by_pk(
pk: &str,
state: &SystemState,
) -> HttpResult<Self::ObjInspectOut> {
let vm = VmDb::transform_read_by_pk(pk, &state.pool).await?;
let processes =
ProcessDb::read_by_kind_key(&vm.spec.vm_key, &state.pool).await?;
let (_, _, _, running_instances) = utils::process::count_status(&processes);
Ok(VmInspect {
created_at: vm.created_at,
namespace_name: vm.namespace_name,
spec: vm.spec,
instance_total: processes.len(),
instance_running: running_instances,
instances: processes,
})
}
}
24 changes: 2 additions & 22 deletions bin/nanocld/src/repositories/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ use nanocl_error::{

use nanocl_stubs::{
generic::{GenericFilter, GenericClause},
cargo::{Cargo, CargoDeleteQuery, CargoInspect},
cargo::{Cargo, CargoDeleteQuery},
cargo_spec::{CargoSpecPartial, CargoSpec},
};

use crate::{
gen_multiple, gen_where4string, utils,
objects::generic::*,
models::{
Pool, CargoDb, SpecDb, CargoUpdateDb, SystemState, NamespaceDb, ProcessDb,
},
models::{Pool, CargoDb, SpecDb, CargoUpdateDb, SystemState, NamespaceDb},
schema::cargoes,
};

Expand Down Expand Up @@ -181,24 +179,6 @@ impl CargoDb {
Ok(count)
}

/// Return detailed information about the cargo for the given key
pub async fn inspect_by_pk(
key: &str,
state: &SystemState,
) -> HttpResult<CargoInspect> {
let cargo = CargoDb::transform_read_by_pk(key, &state.pool).await?;
let processes = ProcessDb::read_by_kind_key(key, &state.pool).await?;
let (_, _, _, running_instances) = utils::process::count_status(&processes);
Ok(CargoInspect {
created_at: cargo.created_at,
namespace_name: cargo.namespace_name,
instance_total: processes.len(),
instance_running: running_instances,
spec: cargo.spec,
instances: processes,
})
}

/// This remove all cargo in the given namespace and all their instances (containers)
/// from the system (database and docker).
pub async fn delete_by_namespace(
Expand Down
25 changes: 2 additions & 23 deletions bin/nanocld/src/repositories/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ use nanocl_error::{

use nanocl_stubs::{
generic::{GenericFilter, GenericClause},
vm::{Vm, VmInspect, VmSummary},
vm::{Vm, VmSummary},
vm_spec::{VmSpecPartial, VmSpec},
};

use crate::{
gen_multiple, gen_where4string, utils,
schema::vms,
models::{
Pool, VmDb, VmUpdateDb, SpecDb, SystemState, ProcessDb, NamespaceDb,
},
models::{Pool, VmDb, VmUpdateDb, SpecDb, ProcessDb, NamespaceDb},
};

use super::generic::*;
Expand Down Expand Up @@ -147,25 +145,6 @@ impl VmDb {
VmDb::transform_read_by(&filter, pool).await
}

/// Get detailed information about a VM by his key
pub async fn inspect_by_pk(
vm_key: &str,
state: &SystemState,
) -> HttpResult<VmInspect> {
let vm = VmDb::transform_read_by_pk(vm_key, &state.pool).await?;
let processes =
ProcessDb::read_by_kind_key(&vm.spec.vm_key, &state.pool).await?;
let (_, _, _, running_instances) = utils::process::count_status(&processes);
Ok(VmInspect {
created_at: vm.created_at,
namespace_name: vm.namespace_name,
spec: vm.spec,
instance_total: processes.len(),
instance_running: running_instances,
instances: processes,
})
}

/// List VMs by namespace
pub async fn list_by_namespace(
nsp: &str,
Expand Down
2 changes: 1 addition & 1 deletion bin/nanocld/src/services/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub async fn inspect_cargo(
) -> HttpResult<web::HttpResponse> {
let namespace = utils::key::resolve_nsp(&qs.namespace);
let key = utils::key::gen_key(&namespace, &path.1);
let cargo = CargoDb::inspect_by_pk(&key, &state).await?;
let cargo = CargoDb::inspect_obj_by_pk(&key, &state).await?;
Ok(web::HttpResponse::Ok().json(&cargo))
}

Expand Down
2 changes: 1 addition & 1 deletion bin/nanocld/src/services/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub async fn inspect_job(
state: web::types::State<SystemState>,
path: web::types::Path<(String, String)>,
) -> HttpResult<web::HttpResponse> {
let job = utils::job::inspect_by_name(&path.1, &state).await?;
let job = JobDb::inspect_obj_by_pk(&path.1, &state).await?;
Ok(web::HttpResponse::Ok().json(&job))
}

Expand Down
2 changes: 1 addition & 1 deletion bin/nanocld/src/services/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub async fn inspect_vm(
let name = path.1.to_owned();
let namespace = utils::key::resolve_nsp(&qs.namespace);
let key = utils::key::gen_key(&namespace, &name);
let vm = VmDb::inspect_by_pk(&key, &state).await?;
let vm = VmDb::inspect_obj_by_pk(&key, &state).await?;
Ok(web::HttpResponse::Ok().json(&vm))
}

Expand Down
22 changes: 1 addition & 21 deletions bin/nanocld/src/utils/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use nanocl_error::{
};
use nanocl_stubs::{
generic::GenericFilter,
job::{Job, JobInspect, JobWaitResponse, WaitCondition, JobSummary},
job::{Job, JobWaitResponse, WaitCondition, JobSummary},
};

use crate::{
Expand Down Expand Up @@ -131,26 +131,6 @@ pub async fn list(state: &SystemState) -> HttpResult<Vec<JobSummary>> {
Ok(job_summaries)
}

/// Inspect a job by name and return a detailed view of the job
pub async fn inspect_by_name(
name: &str,
state: &SystemState,
) -> HttpResult<JobInspect> {
let job = JobDb::read_by_pk(name, &state.pool).await?.try_to_spec()?;
let instances = ProcessDb::read_by_kind_key(name, &state.pool).await?;
let (instance_total, instance_failed, instance_success, instance_running) =
utils::process::count_status(&instances);
let job_inspect = JobInspect {
spec: job,
instance_total,
instance_success,
instance_running,
instance_failed,
instances,
};
Ok(job_inspect)
}

/// Wait a job to finish
pub async fn wait(
name: &str,
Expand Down
4 changes: 3 additions & 1 deletion bin/nanocld/src/utils/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use nanocl_stubs::{
};

use crate::{
objects::generic::*,
repositories::generic::*,
models::{Pool, SystemState, CargoDb, NamespaceDb},
};
Expand Down Expand Up @@ -123,7 +124,8 @@ pub async fn inspect_by_name(
let models = CargoDb::read_by_namespace(&namespace.name, &state.pool).await?;
let mut cargoes = Vec::new();
for cargo in models {
let cargo = CargoDb::inspect_by_pk(&cargo.spec.cargo_key, state).await?;
let cargo =
CargoDb::inspect_obj_by_pk(&cargo.spec.cargo_key, state).await?;

Check warning on line 128 in bin/nanocld/src/utils/namespace.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/utils/namespace.rs#L127-L128

Added lines #L127 - L128 were not covered by tests
cargoes.push(cargo);
}
let network = state
Expand Down

0 comments on commit 56f5330

Please sign in to comment.