Skip to content

Commit 967fdf3

Browse files
authored
Merge pull request #2867 from fermyon/separate-variables-runtime-config
Move variables runtime config to a new crate
2 parents b072e1a + a5d12be commit 967fdf3

File tree

12 files changed

+73
-57
lines changed

12 files changed

+73
-57
lines changed

Cargo.lock

Lines changed: 23 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/factor-variables/Cargo.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,12 @@ authors = { workspace = true }
55
edition = { workspace = true }
66

77
[dependencies]
8-
azure_core = { git = "https://github.com/azure/azure-sdk-for-rust", rev = "8c4caa251c3903d5eae848b41bb1d02a4d65231c" }
9-
azure_identity = { git = "https://github.com/azure/azure-sdk-for-rust", rev = "8c4caa251c3903d5eae848b41bb1d02a4d65231c" }
10-
azure_security_keyvault = { git = "https://github.com/azure/azure-sdk-for-rust", rev = "8c4caa251c3903d5eae848b41bb1d02a4d65231c" }
11-
dotenvy = "0.15"
128
serde = { workspace = true }
139
spin-expressions = { path = "../expressions" }
1410
spin-factors = { path = "../factors" }
1511
spin-world = { path = "../world" }
16-
tokio = { workspace = true, features = ["rt-multi-thread"] }
1712
toml = { workspace = true }
1813
tracing = { workspace = true }
19-
vaultrs = "0.7"
2014

2115
[dev-dependencies]
2216
spin-factors-test = { path = "../factors-test" }

crates/factor-variables/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
mod host;
22
pub mod runtime_config;
3-
pub mod spin_cli;
43

54
use std::sync::Arc;
65

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use spin_factor_variables::{spin_cli, VariablesFactor};
2-
use spin_factors::{
3-
anyhow, Factor, FactorRuntimeConfigSource, RuntimeConfigSourceFinalizer, RuntimeFactors,
4-
};
1+
use spin_expressions::{Key, Provider};
2+
use spin_factor_variables::{runtime_config::RuntimeConfig, VariablesFactor};
3+
use spin_factors::{anyhow, RuntimeFactors};
54
use spin_factors_test::{toml, TestEnvironment};
65
use spin_world::v2::variables::Host;
76

@@ -11,10 +10,14 @@ struct TestFactors {
1110
}
1211

1312
#[tokio::test(flavor = "multi_thread")]
14-
async fn static_provider_works() -> anyhow::Result<()> {
13+
async fn provider_works() -> anyhow::Result<()> {
1514
let factors = TestFactors {
1615
variables: VariablesFactor::default(),
1716
};
17+
let providers = vec![Box::new(MockProvider) as _];
18+
let runtime_config = TestFactorsRuntimeConfig {
19+
variables: Some(RuntimeConfig { providers }),
20+
};
1821
let env = TestEnvironment::new(factors)
1922
.extend_manifest(toml! {
2023
[variables]
@@ -24,46 +27,23 @@ async fn static_provider_works() -> anyhow::Result<()> {
2427
source = "does-not-exist.wasm"
2528
variables = { baz = "<{{ foo }}>" }
2629
})
27-
.runtime_config(TomlConfig::new(toml! {
28-
[[variables_provider]]
29-
type = "static"
30-
values = { foo = "bar" }
31-
}))?;
30+
.runtime_config(runtime_config)?;
3231

3332
let mut state = env.build_instance_state().await?;
3433
let val = state.variables.get("baz".into()).await?;
3534
assert_eq!(val, "<bar>");
3635
Ok(())
3736
}
3837

39-
struct TomlConfig {
40-
table: toml::Table,
41-
}
42-
43-
impl TomlConfig {
44-
fn new(table: toml::Table) -> Self {
45-
Self { table }
46-
}
47-
}
48-
49-
impl TryFrom<TomlConfig> for TestFactorsRuntimeConfig {
50-
type Error = anyhow::Error;
51-
52-
fn try_from(value: TomlConfig) -> Result<Self, Self::Error> {
53-
Self::from_source(value)
54-
}
55-
}
56-
57-
impl FactorRuntimeConfigSource<VariablesFactor> for TomlConfig {
58-
fn get_runtime_config(
59-
&mut self,
60-
) -> anyhow::Result<Option<<VariablesFactor as Factor>::RuntimeConfig>> {
61-
spin_cli::runtime_config_from_toml(&self.table).map(Some)
62-
}
63-
}
38+
#[derive(Debug)]
39+
struct MockProvider;
6440

65-
impl RuntimeConfigSourceFinalizer for TomlConfig {
66-
fn finalize(&mut self) -> anyhow::Result<()> {
67-
Ok(())
41+
#[spin_world::async_trait]
42+
impl Provider for MockProvider {
43+
async fn get(&self, key: &Key) -> anyhow::Result<Option<String>> {
44+
match key.as_str() {
45+
"foo" => Ok(Some("bar".to_string())),
46+
_ => Ok(None),
47+
}
6848
}
6949
}

crates/runtime-config/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ spin-key-value-redis = { path = "../key-value-redis" }
2828
spin-key-value-spin = { path = "../key-value-spin" }
2929
spin-sqlite = { path = "../sqlite" }
3030
spin-trigger = { path = "../trigger" }
31+
spin-variables = { path = "../variables" }
3132
toml = { workspace = true }
3233

3334
[dev-dependencies]

crates/runtime-config/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use spin_factor_outbound_networking::OutboundNetworkingFactor;
1313
use spin_factor_outbound_pg::OutboundPgFactor;
1414
use spin_factor_outbound_redis::OutboundRedisFactor;
1515
use spin_factor_sqlite::SqliteFactor;
16-
use spin_factor_variables::{spin_cli as variables, VariablesFactor};
16+
use spin_factor_variables::VariablesFactor;
1717
use spin_factor_wasi::WasiFactor;
1818
use spin_factors::runtime_config::toml::GetTomlValue as _;
1919
use spin_factors::{
@@ -314,7 +314,9 @@ impl FactorRuntimeConfigSource<VariablesFactor> for TomlRuntimeConfigSource<'_,
314314
fn get_runtime_config(
315315
&mut self,
316316
) -> anyhow::Result<Option<<VariablesFactor as spin_factors::Factor>::RuntimeConfig>> {
317-
Ok(Some(variables::runtime_config_from_toml(&self.toml.table)?))
317+
Ok(Some(spin_variables::runtime_config_from_toml(
318+
&self.toml.table,
319+
)?))
318320
}
319321
}
320322

crates/variables/Cargo.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[package]
2+
name = "spin-variables"
3+
version.workspace = true
4+
authors.workspace = true
5+
edition.workspace = true
6+
license.workspace = true
7+
homepage.workspace = true
8+
repository.workspace = true
9+
rust-version.workspace = true
10+
11+
[dependencies]
12+
azure_core = { git = "https://github.com/azure/azure-sdk-for-rust", rev = "8c4caa251c3903d5eae848b41bb1d02a4d65231c" }
13+
azure_identity = { git = "https://github.com/azure/azure-sdk-for-rust", rev = "8c4caa251c3903d5eae848b41bb1d02a4d65231c" }
14+
azure_security_keyvault = { git = "https://github.com/azure/azure-sdk-for-rust", rev = "8c4caa251c3903d5eae848b41bb1d02a4d65231c" }
15+
dotenvy = "0.15"
16+
serde = { workspace = true }
17+
spin-expressions = { path = "../expressions" }
18+
spin-factors = { path = "../factors" }
19+
spin-factor-variables = { path = "../factor-variables" }
20+
spin-world = { path = "../world" }
21+
tokio = { workspace = true, features = ["rt-multi-thread"] }
22+
tracing = { workspace = true }
23+
vaultrs = "0.7"
24+
25+
[lints]
26+
workspace = true

crates/factor-variables/src/spin_cli/mod.rs renamed to crates/variables/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use serde::Deserialize;
1414
use spin_expressions::Provider;
1515
use spin_factors::{anyhow, runtime_config::toml::GetTomlValue};
1616

17-
use crate::runtime_config::RuntimeConfig;
17+
use spin_factor_variables::runtime_config::RuntimeConfig;
1818

1919
/// Resolves a runtime configuration for the variables factor from a TOML table.
2020
pub fn runtime_config_from_toml(table: &impl GetTomlValue) -> anyhow::Result<RuntimeConfig> {

0 commit comments

Comments
 (0)