vantage_aws/vista/
source.rs1use async_trait::async_trait;
19use ciborium::Value as CborValue;
20use indexmap::IndexMap;
21use vantage_core::Result;
22use vantage_dataset::traits::ReadableValueSet;
23use vantage_table::table::Table;
24use vantage_types::{EmptyEntity, Record};
25use vantage_vista::{
26 Column as VistaColumn, Reference as VistaReference, ReferenceKind, TableShell, Vista,
27 VistaCapabilities, VistaMetadata,
28};
29
30use crate::AwsAccount;
31use crate::condition::AwsCondition;
32
33pub struct AwsTableShell {
34 pub(crate) table: Table<AwsAccount, EmptyEntity>,
35 pub(crate) capabilities: VistaCapabilities,
36 pub(crate) metadata: VistaMetadata,
37}
38
39impl AwsTableShell {
40 pub(crate) fn new(
41 table: Table<AwsAccount, EmptyEntity>,
42 capabilities: VistaCapabilities,
43 metadata: VistaMetadata,
44 ) -> Self {
45 Self {
46 table,
47 capabilities,
48 metadata,
49 }
50 }
51}
52
53#[async_trait]
54impl TableShell for AwsTableShell {
55 fn columns(&self) -> &IndexMap<String, VistaColumn> {
56 &self.metadata.columns
57 }
58
59 fn references(&self) -> &IndexMap<String, VistaReference> {
60 &self.metadata.references
61 }
62
63 fn id_column(&self) -> Option<&str> {
64 self.metadata.id_column.as_deref()
65 }
66
67 async fn list_vista_values(
68 &self,
69 _vista: &Vista,
70 ) -> Result<IndexMap<String, Record<CborValue>>> {
71 self.table.list_values().await
72 }
73
74 async fn get_vista_value(
75 &self,
76 _vista: &Vista,
77 id: &String,
78 ) -> Result<Option<Record<CborValue>>> {
79 let mut data = self.table.list_values().await?;
82 Ok(data.shift_remove(id))
83 }
84
85 async fn get_vista_some_value(
86 &self,
87 _vista: &Vista,
88 ) -> Result<Option<(String, Record<CborValue>)>> {
89 let data = self.table.list_values().await?;
90 Ok(data.into_iter().next())
91 }
92
93 async fn get_vista_count(&self, _vista: &Vista) -> Result<i64> {
94 Ok(self.table.list_values().await?.len() as i64)
95 }
96
97 fn add_eq_condition(&mut self, field: &str, value: &CborValue) -> Result<()> {
98 self.table
102 .add_condition(AwsCondition::eq(field.to_string(), value.clone()));
103 Ok(())
104 }
105
106 fn get_ref(&self, relation: &str, row: &Record<CborValue>) -> Result<Vista> {
107 let target = self.table.get_ref_from_row::<EmptyEntity>(relation, row)?;
108 let factory = crate::vista::factory::AwsVistaFactory::new(self.table.data_source().clone());
109 factory.from_table(target)
110 }
111
112 fn get_ref_kinds(&self) -> Vec<(String, ReferenceKind)> {
113 self.table.ref_kinds()
114 }
115
116 fn capabilities(&self) -> &VistaCapabilities {
117 &self.capabilities
118 }
119
120 fn driver_name(&self) -> &'static str {
121 "aws"
122 }
123}
124
125#[cfg(test)]
126mod tests {
127 use super::*;
128 use crate::AwsAccount;
129 use crate::models::iam;
130 use crate::vista::factory::metadata_from_table;
131 use vantage_types::EmptyEntity;
132
133 fn shell_for_iam_users() -> AwsTableShell {
134 let aws = AwsAccount::new("AKIATEST", "secret", "eu-west-2");
135 let table = iam::users_table(aws).into_entity::<EmptyEntity>();
136 let metadata = metadata_from_table(&table);
137 let capabilities = VistaCapabilities {
138 can_count: true,
139 ..VistaCapabilities::default()
140 };
141 AwsTableShell::new(table, capabilities, metadata)
142 }
143
144 #[test]
145 fn add_eq_condition_pushes_aws_condition_eq_onto_wrapped_table() {
146 let mut shell = shell_for_iam_users();
150 shell
151 .add_eq_condition("PathPrefix", &CborValue::Text("/admin/".into()))
152 .expect("add_eq_condition");
153
154 let conditions: Vec<&AwsCondition> = shell.table.conditions().collect();
155 assert_eq!(conditions.len(), 1);
156 match conditions[0] {
157 AwsCondition::Eq { field, value } => {
158 assert_eq!(field, "PathPrefix");
159 assert_eq!(value, &CborValue::Text("/admin/".into()));
160 }
161 other => panic!("expected AwsCondition::Eq, got {other:?}"),
162 }
163 }
164}