Skip to content

Commit

Permalink
Merge pull request #52 from joonas/fix/get-application
Browse files Browse the repository at this point in the history
fix: Use new struct to combine Manifest and Status structs for get_application
  • Loading branch information
joonas authored Jul 1, 2024
2 parents 7dcb409 + fa5594b commit 845f788
Showing 1 changed file with 55 additions and 14 deletions.
69 changes: 55 additions & 14 deletions src/resources/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use tracing::error;
use uuid::Uuid;
use wadm_client::{error::ClientError, Client as WadmClient};
use wadm_types::{
api::{ModelSummary, StatusType},
api::{ModelSummary, Status, StatusType},
Manifest,
};

Expand Down Expand Up @@ -237,20 +237,17 @@ impl From<Vec<ModelSummary>> for ApplicationTable {
}
}

impl From<Vec<Manifest>> for ApplicationTable {
fn from(manifests: Vec<Manifest>) -> Self {
impl From<Vec<CombinedManifest>> for ApplicationTable {
fn from(manifests: Vec<CombinedManifest>) -> Self {
let mut table = Self::default();
let rows = manifests
.into_iter()
.map(|m| TableRow {
.map(|cm| TableRow {
cells: vec![
m.metadata.name,
"N/A".to_string(),
match m.metadata.annotations.get("version") {
Some(v) => v.to_owned(),
None => "N/A".to_string(),
},
"N/A".to_string(),
cm.name(),
cm.deployed_version(),
cm.latest_version(),
cm.status(),
],
})
.collect();
Expand All @@ -260,6 +257,42 @@ impl From<Vec<Manifest>> for ApplicationTable {
}
}

struct CombinedManifest {
manifest: Manifest,
status: Status,
}

impl CombinedManifest {
pub(crate) fn new(manifest: Manifest, status: Status) -> Self {
Self { manifest, status }
}

pub(crate) fn name(&self) -> String {
self.manifest.metadata.name.to_owned()
}

pub(crate) fn deployed_version(&self) -> String {
match self.manifest.metadata.annotations.get("version") {
Some(v) => v.to_owned(),
None => "N/A".to_string(),
}
}

pub(crate) fn latest_version(&self) -> String {
self.status.version.to_owned()
}

pub(crate) fn status(&self) -> String {
match self.status.info.status_type {
StatusType::Undeployed => "Undeployed",
StatusType::Reconciling => "Reconciling",
StatusType::Deployed => "Deployed",
StatusType::Failed => "Failed",
}
.to_string()
}
}

#[utoipa::path(
post,
path = "/apis/core.oam.dev/v1beta1/namespaces/{namespace}/applications"
Expand Down Expand Up @@ -502,11 +535,19 @@ pub async fn get_application(
};
let status = match wadm_client.get_manifest_status(&name).await {
Ok(s) => s,
Err(_) => todo!(),
Err(e) => match e {
ClientError::NotFound(_) => {
return not_found_error(anyhow!("applications \"{}\" not found", name))
}
_ => return internal_error(anyhow!("unable to request app status from wadm: {}", e)),
},
};

match accept.into() {
As::Table => Json(ApplicationTable::from(vec![manifest])).into_response(),
As::Table => {
let combined_manifest = CombinedManifest::new(manifest, status);
Json(ApplicationTable::from(vec![combined_manifest])).into_response()
}
As::NotSpecified => {
// TODO(joonas): This is a terrible hack, but for now it's what we need to do to satisfy Argo/Kubernetes since WADM doesn't support this metadata.
let mut manifest_value = serde_json::to_value(&manifest).unwrap();
Expand All @@ -533,7 +574,7 @@ pub async fn get_application(
Json(manifest_value).into_response()
}
// TODO(joonas): Add better error handling here
t => return internal_error(anyhow!("unknown type: {}", t)),
t => internal_error(anyhow!("unknown type: {}", t)),
}
}

Expand Down

0 comments on commit 845f788

Please sign in to comment.