Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use new struct to combine Manifest and Status structs for get_application #52

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading