spin_key_value_azure/
lib.rs

1mod store;
2
3use serde::Deserialize;
4use spin_factor_key_value::runtime_config::spin::MakeKeyValueStore;
5
6pub use store::{
7    KeyValueAzureCosmos, KeyValueAzureCosmosAuthOptions, KeyValueAzureCosmosRuntimeConfigOptions,
8};
9
10/// A key-value store that uses Azure Cosmos as the backend.
11pub struct AzureKeyValueStore {
12    app_id: Option<String>,
13}
14
15impl AzureKeyValueStore {
16    /// Creates a new `AzureKeyValueStore`.
17    ///
18    /// When `app_id` is provided, the store will a partition key of `$app_id/$store_name`,
19    /// otherwise the partition key will be `id`.
20    pub fn new(app_id: Option<String>) -> Self {
21        Self { app_id }
22    }
23}
24
25/// Runtime configuration for the Azure Cosmos key-value store.
26#[derive(Deserialize)]
27pub struct AzureCosmosKeyValueRuntimeConfig {
28    /// The authorization token for the Azure Cosmos DB account.
29    key: Option<String>,
30    /// The Azure Cosmos DB account name.
31    account: String,
32    /// The Azure Cosmos DB database.
33    database: String,
34    /// The Azure Cosmos DB container where data is stored.
35    /// The CosmosDB container must be created with the default partition key, /id
36    container: String,
37}
38
39impl MakeKeyValueStore for AzureKeyValueStore {
40    const RUNTIME_CONFIG_TYPE: &'static str = "azure_cosmos";
41
42    type RuntimeConfig = AzureCosmosKeyValueRuntimeConfig;
43
44    type StoreManager = KeyValueAzureCosmos;
45
46    fn make_store(
47        &self,
48        runtime_config: Self::RuntimeConfig,
49    ) -> anyhow::Result<Self::StoreManager> {
50        let auth_options = match runtime_config.key {
51            Some(key) => KeyValueAzureCosmosAuthOptions::RuntimeConfigValues(
52                KeyValueAzureCosmosRuntimeConfigOptions::new(key),
53            ),
54            None => KeyValueAzureCosmosAuthOptions::Environmental,
55        };
56        KeyValueAzureCosmos::new(
57            runtime_config.account,
58            runtime_config.database,
59            runtime_config.container,
60            auth_options,
61            self.app_id.clone(),
62        )
63    }
64}