Skip to main content

vantage_aws/vista/
source.rs

1//! `AwsTableShell` — owns a `Table<AwsAccount, EmptyEntity>` and
2//! exposes it through the `TableShell` boundary.
3//!
4//! AWS already speaks `ciborium::Value` natively (the wire protocols
5//! parse into CBOR), so this is a passthrough on reads. Conditions
6//! translate `(field, CborValue)` into an `AwsCondition::Eq` and push
7//! it onto the wrapped table; the dispatch layer folds AwsConditions
8//! into the request body at fetch time. AWS is read-only in v0 — only
9//! `can_count` is advertised.
10//!
11//! AWS list APIs expose no HEAD / COUNT / point-get operation, so
12//! `get_vista_count` and `get_vista_value` both materialise the listing
13//! and then count or filter in memory. [`crate::AwsAccount::with_max_pages`]
14//! caps the walk; without it, both methods will keep paginating until the
15//! response stream is exhausted. Callers that need an unbounded count on
16//! an unbounded list should not call these.
17
18use 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        // AWS list endpoints don't expose a point-get; fall back to
80        // narrowing the listed map by id — same shape as the REST shell.
81        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        // `AwsCondition::Eq` carries the value as `CborValue` directly;
99        // the wire-format builders (`build_json1_body`, `build_query_form`)
100        // do the JSON / string conversion at execute time.
101        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        // `dispatch` reads `table.conditions()` when assembling the
147        // request body; a missing or mistranslated condition is invisible
148        // without this introspection.
149        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}