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

feat(kv): add subcommand for renaming key value stores #174

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
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
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. All existing links will automatically link to the store's new name.
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
2 changes: 1 addition & 1 deletion src/commands/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub enum SqliteCommand {
Execute(ExecuteCommand),
/// List all your SQLite databases
List(ListCommand),
/// Rename a SQLite database
/// Rename a SQLite database. All existing links will automatically link to the database's new name.
Rename(RenameCommand),
}

Expand Down
Loading