|
| 1 | +use crate::dynamo::types::ApiResponse; |
| 2 | +use aws_sdk_dynamodb::Client; |
| 3 | +use serde_json::json; |
| 4 | + |
| 5 | +pub async fn describe_table(client: &Client, table_name: &str) -> Result<ApiResponse, String> { |
| 6 | + match client |
| 7 | + .describe_table() |
| 8 | + .table_name(table_name) |
| 9 | + .send() |
| 10 | + .await |
| 11 | + { |
| 12 | + Ok(response) => { |
| 13 | + // Create a custom serializable structure with the data we need |
| 14 | + let table_info = json!({ |
| 15 | + "id": response.table().and_then(|t| t.table_id()), |
| 16 | + "name": response.table().map(|t| t.table_name()), |
| 17 | + "status": response.table().and_then(|t| t.table_status().map(|s| s.as_str().to_string())), |
| 18 | + "itemCount": response.table().and_then(|t| t.item_count()), |
| 19 | + "sizeBytes": response.table().and_then(|t| t.table_size_bytes()), |
| 20 | + "keySchema": response.table().and_then(|t| { |
| 21 | + Some(t.key_schema().iter().map(|k| { |
| 22 | + json!({ |
| 23 | + "attributeName": k.attribute_name(), |
| 24 | + "keyType": format!("{:?}", k.key_type()) |
| 25 | + }) |
| 26 | + }).collect::<Vec<_>>()) |
| 27 | + }), |
| 28 | + "attributeDefinitions": response.table().and_then(|t| { |
| 29 | + Some(t.attribute_definitions().iter().map(|a| { |
| 30 | + json!({ |
| 31 | + "attributeName": a.attribute_name(), |
| 32 | + "attributeType": format!("{:?}", a.attribute_type()) |
| 33 | + }) |
| 34 | + }).collect::<Vec<_>>()) |
| 35 | + }), |
| 36 | + "indices": response.table().map(|t| { |
| 37 | + let mut indices = Vec::new(); |
| 38 | + |
| 39 | + // Add Global Secondary Indexes |
| 40 | + let gsi_list = t.global_secondary_indexes(); |
| 41 | + if !gsi_list.is_empty() { |
| 42 | + for gsi in gsi_list { |
| 43 | + let index_info = json!({ |
| 44 | + "type": "GSI", |
| 45 | + "name": gsi.index_name(), |
| 46 | + "status": gsi.index_status().map(|s| s.as_str().to_string()), |
| 47 | + "keySchema": gsi.key_schema().iter().map(|k| { |
| 48 | + json!({ |
| 49 | + "attributeName": k.attribute_name(), |
| 50 | + "keyType": format!("{:?}", k.key_type()) |
| 51 | + }) |
| 52 | + }).collect::<Vec<_>>(), |
| 53 | + "provisionedThroughput": gsi.provisioned_throughput().map(|pt| json!({ |
| 54 | + "readCapacityUnits": pt.read_capacity_units(), |
| 55 | + "writeCapacityUnits": pt.write_capacity_units() |
| 56 | + })) |
| 57 | + }); |
| 58 | + indices.push(index_info); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Add Local Secondary Indexes |
| 63 | + let lsi_list = t.local_secondary_indexes(); |
| 64 | + if !lsi_list.is_empty() { |
| 65 | + for lsi in lsi_list { |
| 66 | + let index_info = json!({ |
| 67 | + "type": "LSI", |
| 68 | + "name": lsi.index_name(), |
| 69 | + "keySchema": lsi.key_schema().iter().map(|k| { |
| 70 | + json!({ |
| 71 | + "attributeName": k.attribute_name(), |
| 72 | + "keyType": format!("{:?}", k.key_type()) |
| 73 | + }) |
| 74 | + }).collect::<Vec<_>>() |
| 75 | + }); |
| 76 | + indices.push(index_info); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + indices |
| 81 | + }), |
| 82 | + "creationDateTime": response.table().and_then(|t| |
| 83 | + t.creation_date_time().map(|dt| dt.to_string())), |
| 84 | + }); |
| 85 | + |
| 86 | + Ok(ApiResponse { |
| 87 | + status: 200, |
| 88 | + message: "Table described successfully".to_string(), |
| 89 | + data: Some(table_info), |
| 90 | + }) |
| 91 | + } |
| 92 | + Err(e) => Ok(ApiResponse { |
| 93 | + status: 500, |
| 94 | + message: format!("Failed to describe table: {}", e), |
| 95 | + data: None, |
| 96 | + }), |
| 97 | + } |
| 98 | +} |
0 commit comments