spin_key_value_aws/
lib.rs

1mod store;
2
3use serde::Deserialize;
4use spin_factor_key_value::runtime_config::spin::MakeKeyValueStore;
5use store::{
6    KeyValueAwsDynamo, KeyValueAwsDynamoAuthOptions, KeyValueAwsDynamoRuntimeConfigOptions,
7};
8
9/// A key-value store that uses AWS Dynamo as the backend.
10#[derive(Default)]
11pub struct AwsDynamoKeyValueStore {
12    _priv: (),
13}
14
15impl AwsDynamoKeyValueStore {
16    /// Creates a new `AwsKeyValueStore`.
17    pub fn new() -> Self {
18        Self::default()
19    }
20}
21
22/// Runtime configuration for the AWS Dynamo key-value store.
23#[derive(Deserialize)]
24pub struct AwsDynamoKeyValueRuntimeConfig {
25    /// The access key for the AWS Dynamo DB account role.
26    access_key: Option<String>,
27    /// The secret key for authorization on the AWS Dynamo DB account.
28    secret_key: Option<String>,
29    /// The token for authorization on the AWS Dynamo DB account.
30    token: Option<String>,
31    /// The AWS region where the database is located
32    region: String,
33    /// Boolean determining whether to use strongly consistent reads.
34    /// Defaults to `false` but can be set to `true` to improve atomicity
35    consistent_read: Option<bool>,
36    /// The AWS Dynamo DB table.
37    table: String,
38}
39
40impl MakeKeyValueStore for AwsDynamoKeyValueStore {
41    const RUNTIME_CONFIG_TYPE: &'static str = "aws_dynamo";
42
43    type RuntimeConfig = AwsDynamoKeyValueRuntimeConfig;
44
45    type StoreManager = KeyValueAwsDynamo;
46
47    fn make_store(
48        &self,
49        runtime_config: Self::RuntimeConfig,
50    ) -> anyhow::Result<Self::StoreManager> {
51        let AwsDynamoKeyValueRuntimeConfig {
52            access_key,
53            secret_key,
54            token,
55            region,
56            consistent_read,
57            table,
58        } = runtime_config;
59        let auth_options = match (access_key, secret_key) {
60            (Some(access_key), Some(secret_key)) => {
61                KeyValueAwsDynamoAuthOptions::RuntimeConfigValues(
62                    KeyValueAwsDynamoRuntimeConfigOptions::new(access_key, secret_key, token),
63                )
64            }
65            _ => KeyValueAwsDynamoAuthOptions::Environmental,
66        };
67        KeyValueAwsDynamo::new(
68            region,
69            consistent_read.unwrap_or(false),
70            table,
71            auth_options,
72        )
73    }
74}