From ba9dc3ababf5c29a2ae0f1e990509ee2fc5e2a86 Mon Sep 17 00:00:00 2001 From: Ryota Sakamoto Date: Sun, 14 May 2023 16:25:53 +0900 Subject: [PATCH] refactor: move describe_table_api to control API should be called in control --- src/app.rs | 24 ++---------------------- src/bootstrap.rs | 2 +- src/control.rs | 24 ++++++++++++++++++++++-- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/app.rs b/src/app.rs index 541326b3..03790c8a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -455,7 +455,7 @@ pub async fn use_table( debug!("describing the table: {}", tbl); let region = cx.effective_region(); let tbl = tbl.clone(); - let desc: TableDescription = describe_table_api(®ion, tbl.clone()).await; + let desc: TableDescription = control::describe_table_api(®ion, tbl.clone()).await; save_using_target(cx, desc)?; println!("Now you're using the table '{}' ({}).", tbl, ®ion.name()); }, @@ -568,7 +568,7 @@ pub async fn table_schema(cx: &Context) -> TableSchema { // It's possible that users pass --table without calling `dy use` for any table. Thus collect all data from DescribeTable results. Some(table_name) => { // TODO: reduce # of DescribeTable API calls. table_schema function is called every time you do something. - let desc: TableDescription = describe_table_api( + let desc: TableDescription = control::describe_table_api( &cx.effective_region(), table_name, /* should be equal to 'cx.effective_table_name()' */ ) @@ -638,26 +638,6 @@ pub fn index_schemas(desc: &TableDescription) -> Option> { } } -/// Originally intended to be called by describe_table function, which is called from `$ dy desc`, -/// however it turned out that DescribeTable API result is useful in various logic, separated API into this standalone function. -pub async fn describe_table_api(region: &Region, table_name: String) -> TableDescription { - let ddb = DynamoDbClient::new(region.clone()); - let req: DescribeTableInput = DescribeTableInput { table_name }; - - match ddb.describe_table(req).await { - Err(e) => { - debug!("DescribeTable API call got an error -- {:#?}", e); - error!("{}", e.to_string()); - std::process::exit(1); - } - Ok(res) => { - let desc: TableDescription = res.table.expect("This message should not be shown."); - debug!("Received DescribeTable Result: {:?}\n", desc); - desc - } - } -} - pub fn bye(code: i32, msg: &str) { println!("{}", msg); std::process::exit(code); diff --git a/src/bootstrap.rs b/src/bootstrap.rs index 1d7e6737..12153c8b 100644 --- a/src/bootstrap.rs +++ b/src/bootstrap.rs @@ -419,7 +419,7 @@ async fn wait_table_creation(cx: &app::Context, mut processing_tables: Vec<&str> let create_table_results = join_all( processing_tables .iter() - .map(|t| app::describe_table_api(r, (*t).to_string())), + .map(|t| control::describe_table_api(r, (*t).to_string())), ) .await; let statuses: Vec = create_table_results diff --git a/src/control.rs b/src/control.rs index 0c7a452f..a6af3e36 100644 --- a/src/control.rs +++ b/src/control.rs @@ -176,7 +176,7 @@ pub async fn describe_table(cx: app::Context, target_table_to_desc: Option TableDescription { + let ddb = DynamoDbClient::new(region.clone()); + let req: DescribeTableInput = DescribeTableInput { table_name }; + + match ddb.describe_table(req).await { + Err(e) => { + debug!("DescribeTable API call got an error -- {:#?}", e); + error!("{}", e.to_string()); + std::process::exit(1); + } + Ok(res) => { + let desc: TableDescription = res.table.expect("This message should not be shown."); + debug!("Received DescribeTable Result: {:?}\n", desc); + desc + } + } +} + /// Receives region (just to show in one line for reference) and TableDescription, /// print them in readable YAML format. NOTE: '~' representes 'null' or 'no value' in YAML syntax. pub fn print_table_description(region: Region, desc: TableDescription) { @@ -341,7 +361,7 @@ pub async fn update_table( ) { // Retrieve TableDescription of the table to update, current (before update) status. let desc: TableDescription = - app::describe_table_api(&cx.effective_region(), table_name_to_update.clone()).await; + describe_table_api(&cx.effective_region(), table_name_to_update.clone()).await; // Map given string into "Mode" enum. Note that in cmd.rs structopt already limits acceptable values. let switching_to_mode: Option = match mode_string {