Skip to content

Commit

Permalink
Implement AivenObject for DynamicObject, eliminating need for specifi…
Browse files Browse the repository at this point in the history
…c types

All relevant objects have data in the same fields, and the DynamicObject allows generic access to those fields.
It makes much more sense to implement mutations on the basis of the generic access in DynamicObject, than
implementing the AivenObject trait for all Aiven types with identical code for each type.
  • Loading branch information
mortenlj committed Sep 29, 2023
1 parent d5a2031 commit 3847aa0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
33 changes: 33 additions & 0 deletions src/aiven_types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use kube::core::DynamicObject;
use std::collections::BTreeMap;

use crate::aiven_types::aiven_opensearches::OpenSearch;
Expand Down Expand Up @@ -34,6 +35,38 @@ pub trait AivenObject {
}
}

impl AivenObject for DynamicObject {
fn get_cloud_name(&self) -> Option<String> {
self.data["spec"]["cloudName"]
.as_str()
.map(|s| s.to_string())
}

fn get_team_name(&self) -> Option<String> {
self.metadata.namespace.clone()
}

fn get_tags(&self) -> Option<BTreeMap<String, String>> {
self.data["spec"]["tags"].as_object().map(|o| {
o.iter()
.map(|(k, v)| (k.to_owned(), v.as_str().unwrap_or("").to_string()))
.collect()
})
}

fn get_termination_protection(&self) -> Option<bool> {
self.data["spec"]["terminationProtection"]
.as_bool()
.map(|b| b.to_owned())
}

fn get_project_vpc_id(&self) -> Option<String> {
self.data["spec"]["projectVpcId"]
.as_str()
.map(|s| s.to_string())
}
}

impl AivenObject for Redis {
fn get_cloud_name(&self) -> Option<String> {
self.spec.cloud_name.clone()
Expand Down
30 changes: 1 addition & 29 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ use kube::core::DynamicObject;
use kube::ResourceExt;
use tracing::{debug, error, info, info_span, instrument, warn};

use crate::aiven_types::aiven_opensearches::OpenSearch;
use crate::aiven_types::aiven_redis::Redis;
use crate::aiven_types::AivenObject;
use crate::mutators;
use crate::settings::AppConfig;
Expand Down Expand Up @@ -93,33 +91,7 @@ async fn mutate_handler(
let _resource_guard = resource_span.enter();
info!("Processing {} resource", req.kind.kind);

let resource: Box<dyn AivenObject> = match req.kind.kind.as_str() {
"Redis" => {
let redis: Redis = match obj.clone().try_parse() {
Ok(redis) => redis,
Err(err) => {
error!("Unable to parse Redis object: {}", err.to_string());
return bad_request("unable to parse Redis object");
},
};
Box::new(redis)
},
"OpenSearch" => {
let open_search: OpenSearch = match obj.clone().try_parse() {
Ok(open_search) => open_search,
Err(err) => {
error!("Unable to parse OpenSearch object: {}", err.to_string());
return bad_request("unable to parse OpenSearch object");
},
};
Box::new(open_search)
},
_ => {
return bad_request("unsupported resource type");
},
};

res = match mutate(res.clone(), resource, &config) {
res = match mutate(res.clone(), Box::new(obj.to_owned()), &config) {
Ok(res) => {
info!("Processing complete");
res
Expand Down

0 comments on commit 3847aa0

Please sign in to comment.