use super::*;
pub(crate) fn resolve_table_name(input: &str) -> &str {
if let Some(rest) = input.strip_prefix("arn:aws:dynamodb:") {
if let Some(after_table) = rest.split(":table/").nth(1) {
return after_table.split('/').next().unwrap_or(after_table);
}
}
input
}
pub(crate) fn get_table<'a>(
tables: &'a BTreeMap<String, DynamoTable>,
name: &str,
) -> Result<&'a DynamoTable, AwsServiceError> {
get_table_with_code(tables, name, "ResourceNotFoundException")
}
pub(crate) fn get_table_with_code<'a>(
tables: &'a BTreeMap<String, DynamoTable>,
name: &str,
code: &str,
) -> Result<&'a DynamoTable, AwsServiceError> {
let resolved = resolve_table_name(name);
tables.get(resolved).ok_or_else(|| {
AwsServiceError::aws_error(
StatusCode::BAD_REQUEST,
code,
format!("Requested resource not found: Table: {resolved} not found"),
)
})
}
pub(crate) fn get_table_mut<'a>(
tables: &'a mut BTreeMap<String, DynamoTable>,
name: &str,
) -> Result<&'a mut DynamoTable, AwsServiceError> {
get_table_mut_with_code(tables, name, "ResourceNotFoundException")
}
pub(crate) fn get_table_mut_with_code<'a>(
tables: &'a mut BTreeMap<String, DynamoTable>,
name: &str,
code: &str,
) -> Result<&'a mut DynamoTable, AwsServiceError> {
let resolved = resolve_table_name(name).to_string();
tables.get_mut(&resolved).ok_or_else(|| {
AwsServiceError::aws_error(
StatusCode::BAD_REQUEST,
code,
format!("Requested resource not found: Table: {resolved} not found"),
)
})
}
pub(crate) fn find_table_by_arn<'a>(
tables: &'a BTreeMap<String, DynamoTable>,
arn: &str,
) -> Result<&'a DynamoTable, AwsServiceError> {
tables.values().find(|t| t.arn == arn).ok_or_else(|| {
AwsServiceError::aws_error(
StatusCode::BAD_REQUEST,
"ResourceNotFoundException",
format!("Requested resource not found: {arn}"),
)
})
}
pub(crate) fn find_table_by_arn_mut<'a>(
tables: &'a mut BTreeMap<String, DynamoTable>,
arn: &str,
) -> Result<&'a mut DynamoTable, AwsServiceError> {
tables.values_mut().find(|t| t.arn == arn).ok_or_else(|| {
AwsServiceError::aws_error(
StatusCode::BAD_REQUEST,
"ResourceNotFoundException",
format!("Requested resource not found: {arn}"),
)
})
}