Skip to content

Commit

Permalink
feat(kv): add subcommand for renaming key value stores
Browse files Browse the repository at this point in the history
Signed-off-by: Kate Goldenring <kate.goldenring@fermyon.com>
  • Loading branch information
kate-goldenring committed Jan 25, 2024
1 parent 067260b commit 879bb02
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ openssl = { version = "0.10" }

[workspace.dependencies]
tracing = { version = "0.1", features = ["log"] }
cloud-openapi = { git = "https://github.com/fermyon/cloud-openapi", rev = "a461b586e35a0175810d6da80a8e8ffeb2d517a4" }
cloud-openapi = { git = "https://github.com/fermyon/cloud-openapi", rev = "b6549ceb60cb329ce994d05f725a6a0b26287bca" }

[build-dependencies]
vergen = { version = "^8.2.1", default-features = false, features = [
Expand Down
8 changes: 7 additions & 1 deletion crates/cloud/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use cloud_openapi::{
key_value_stores_api::{
api_key_value_stores_get, api_key_value_stores_store_delete,
api_key_value_stores_store_links_delete, api_key_value_stores_store_links_post,
api_key_value_stores_store_post,
api_key_value_stores_store_post, api_key_value_stores_store_rename_patch,
},
revisions_api::{api_revisions_get, api_revisions_post},
sql_databases_api::{
Expand Down Expand Up @@ -278,6 +278,12 @@ impl CloudClientInterface for Client {
.map_err(format_response_error)
}

async fn rename_key_value_store(&self, store_name: &str, new_name: &str) -> anyhow::Result<()> {
api_key_value_stores_store_rename_patch(&self.configuration, store_name, new_name, None)
.await
.map_err(format_response_error)
}

async fn get_key_value_stores(
&self,
app_id: Option<Uuid>,
Expand Down
2 changes: 2 additions & 0 deletions crates/cloud/src/client_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub trait CloudClientInterface: Send + Sync {

async fn delete_key_value_store(&self, store_name: &str) -> anyhow::Result<()>;

async fn rename_key_value_store(&self, store_name: &str, new_name: &str) -> anyhow::Result<()>;

async fn get_key_value_stores(
&self,
app_id: Option<Uuid>,
Expand Down
39 changes: 39 additions & 0 deletions src/commands/key_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub enum KeyValueCommand {
List(ListCommand),
/// Set a key value pair in a store
Set(SetCommand),
/// Rename a key value store
Rename(RenameCommand),
}

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -102,6 +104,18 @@ pub struct SetCommand {
common: CommonArgs,
}

#[derive(Parser, Debug)]
pub struct RenameCommand {
/// Current name of key value store to rename
name: String,

/// New name for the key value store
new_name: String,

#[clap(flatten)]
common: CommonArgs,
}

impl KeyValueCommand {
pub async fn run(&self) -> Result<()> {
match self {
Expand All @@ -121,6 +135,10 @@ impl KeyValueCommand {
let client = create_cloud_client(cmd.common.deployment_env_id.as_deref()).await?;
cmd.run(client).await
}
KeyValueCommand::Rename(cmd) => {
let client = create_cloud_client(cmd.common.deployment_env_id.as_deref()).await?;
cmd.run(client).await
}
}
}
}
Expand Down Expand Up @@ -223,6 +241,27 @@ impl SetCommand {
}
}

impl RenameCommand {
pub async fn run(&self, client: impl CloudClientInterface) -> Result<()> {
let list = client
.get_key_value_stores(None)
.await
.with_context(|| format!("Error listing key value stores '{}'", self.name))?;
let found = list.iter().any(|kv| kv.name == self.name);
if !found {
bail!("No key value store found with name \"{}\"", self.name);
}
client
.rename_key_value_store(&self.name, &self.new_name)
.await?;
println!(
"Key value store \"{}\" is now named \"{}\"",
self.name, self.new_name
);
Ok(())
}
}

fn to_resource_links(stores: Vec<KeyValueStoreItem>) -> Vec<ResourceLinks> {
stores
.into_iter()
Expand Down

0 comments on commit 879bb02

Please sign in to comment.