Skip to content

Commit

Permalink
refactor: move describe_table_api to control
Browse files Browse the repository at this point in the history
API should be called in control
  • Loading branch information
ryota-sakamoto committed May 14, 2023
1 parent ee4957d commit ba9dc3a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
24 changes: 2 additions & 22 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&region, tbl.clone()).await;
let desc: TableDescription = control::describe_table_api(&region, tbl.clone()).await;
save_using_target(cx, desc)?;
println!("Now you're using the table '{}' ({}).", tbl, &region.name());
},
Expand Down Expand Up @@ -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()' */
)
Expand Down Expand Up @@ -638,26 +638,6 @@ pub fn index_schemas(desc: &TableDescription) -> Option<Vec<IndexSchema>> {
}
}

/// 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);
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = create_table_results
Expand Down
24 changes: 22 additions & 2 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ pub async fn describe_table(cx: app::Context, target_table_to_desc: Option<Strin
cx
};

let desc: TableDescription = app::describe_table_api(
let desc: TableDescription = describe_table_api(
&new_context.effective_region(),
new_context.effective_table_name(),
)
Expand Down Expand Up @@ -207,6 +207,26 @@ pub async fn describe_table(cx: app::Context, target_table_to_desc: Option<Strin
}
}

/// 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
}
}
}

/// 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) {
Expand Down Expand Up @@ -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<Mode> = match mode_string {
Expand Down

0 comments on commit ba9dc3a

Please sign in to comment.