-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kv): support setting key/value pairs
Signed-off-by: Kate Goldenring <kate.goldenring@fermyon.com>
- Loading branch information
1 parent
f53b8ad
commit 23ed408
Showing
8 changed files
with
225 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/// Module for determining execution target for resources with links | ||
use crate::commands::links_output::{ResourceLinks, ResourceType}; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum ExecuteTarget { | ||
Resource(String), | ||
Label { label: String, app: String }, | ||
} | ||
|
||
impl ExecuteTarget { | ||
pub fn find_in( | ||
&self, | ||
resources: Vec<ResourceLinks>, | ||
resource_type: ResourceType, | ||
) -> anyhow::Result<ResourceLinks> { | ||
match self { | ||
Self::Resource(resource) => resources | ||
.into_iter() | ||
.find(|r| &r.name == resource) | ||
.ok_or_else(|| { | ||
anyhow::anyhow!("No {resource_type} found with name \"{resource}\"") | ||
}), | ||
Self::Label { label, app } => resources | ||
.into_iter() | ||
.find(|r| r.has_link(label, Some(app.as_str()))) | ||
.ok_or_else(|| { | ||
anyhow::anyhow!( | ||
r#"No {resource_type} found with label "{label}" for app "{app}""# | ||
) | ||
}), | ||
} | ||
} | ||
|
||
pub fn from_inputs( | ||
resource: &Option<String>, | ||
label: &Option<String>, | ||
app: &Option<String>, | ||
) -> anyhow::Result<ExecuteTarget> { | ||
match (resource, label, app) { | ||
(Some(r), None, None) => Ok(ExecuteTarget::Resource(r.to_owned())), | ||
(None, Some(l), Some(a)) => Ok(ExecuteTarget::Label { | ||
label: l.to_owned(), | ||
app: a.to_owned(), | ||
}), | ||
_ => Err(anyhow::anyhow!("Invalid combination of arguments")), // Should be prevented by clap | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use cloud_openapi::models::ResourceLabel; | ||
use uuid::Uuid; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_execute_target_from_inputs() { | ||
assert_eq!( | ||
ExecuteTarget::from_inputs(&Some("mykv".to_owned()), &None, &None).unwrap(), | ||
ExecuteTarget::Resource("mykv".to_owned()) | ||
); | ||
assert_eq!( | ||
ExecuteTarget::from_inputs(&None, &Some("label".to_owned()), &Some("app".to_owned())) | ||
.unwrap(), | ||
ExecuteTarget::Label { | ||
label: "label".to_owned(), | ||
app: "app".to_owned(), | ||
} | ||
); | ||
assert!(ExecuteTarget::from_inputs(&None, &None, &None).is_err()); | ||
assert!(ExecuteTarget::from_inputs( | ||
&Some("mykv".to_owned()), | ||
&Some("label".to_owned()), | ||
&Some("app".to_owned()) | ||
) | ||
.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_execute_target_find_in() { | ||
let links = vec![ResourceLabel { | ||
app_id: Uuid::new_v4(), | ||
app_name: Some("app".to_owned()), | ||
label: "label".to_owned(), | ||
}]; | ||
let rl1 = ResourceLinks::new("mykv".to_owned(), vec![]); | ||
let rl2 = ResourceLinks::new("mykv2".to_owned(), links); | ||
let resources = vec![rl1.clone(), rl2.clone()]; | ||
assert_eq!( | ||
ExecuteTarget::Resource("mykv".to_owned()) | ||
.find_in(resources.clone(), ResourceType::KeyValueStore) | ||
.unwrap(), | ||
rl1 | ||
); | ||
assert_eq!( | ||
ExecuteTarget::Label { | ||
label: "label".to_owned(), | ||
app: "app".to_owned(), | ||
} | ||
.find_in(resources.clone(), ResourceType::KeyValueStore) | ||
.unwrap(), | ||
rl2 | ||
); | ||
assert!(ExecuteTarget::Resource("foo".to_owned()) | ||
.find_in(resources.clone(), ResourceType::KeyValueStore) | ||
.is_err()); | ||
assert!(ExecuteTarget::Label { | ||
label: "foo".to_owned(), | ||
app: "app".to_owned(), | ||
} | ||
.find_in(resources.clone(), ResourceType::KeyValueStore) | ||
.is_err()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.