use dw_models::Bucket;
pub fn find_bucket<'a>(
bucket_filter: &str,
hostname_filter: &Option<String>,
buckets: impl IntoIterator<Item = &'a Bucket>,
) -> Option<String> {
for bucket in buckets {
if bucket.id.starts_with(bucket_filter) {
if let Some(hostname) = hostname_filter {
if hostname == &bucket.hostname {
return Some(bucket.id.to_string());
}
} else {
return Some(bucket.id.to_string());
}
}
}
None
}
#[cfg(test)]
mod tests {
use super::find_bucket;
use dw_models::Bucket;
use dw_models::BucketMetadata;
#[test]
fn test_find_bucket() {
let expected_bucketname = "dw-datastore-test_test-host".to_string();
let expected_hostname = "testhost".to_string();
let b1 = Bucket {
bid: None,
id: "no match".to_string(),
_type: "type".to_string(),
hostname: expected_hostname.clone(),
client: "testclient".to_string(),
created: None,
data: json_map! {},
metadata: BucketMetadata::default(),
events: None,
last_updated: None,
};
let mut b2 = b1.clone();
b2.id = expected_bucketname.clone();
let b3 = b1.clone();
let buckets = vec![b1.clone(), b2.clone(), b3.clone()];
let res = find_bucket("dw-datastore-test", &Some("testhost".to_string()), &buckets);
assert_eq!(res, Some(expected_bucketname));
let res = find_bucket(
"dw-datastore-test",
&Some("unavailablehost".to_string()),
&buckets,
);
assert_eq!(res, None);
let buckets = vec![b1, b3];
let res = find_bucket("dw-datastore-test", &None, &buckets);
assert_eq!(res, None);
}
}