Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move variables runtime config to a new crate #2867

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions crates/factor-variables/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@ authors = { workspace = true }
edition = { workspace = true }

[dependencies]
azure_core = { git = "https://github.com/azure/azure-sdk-for-rust", rev = "8c4caa251c3903d5eae848b41bb1d02a4d65231c" }
azure_identity = { git = "https://github.com/azure/azure-sdk-for-rust", rev = "8c4caa251c3903d5eae848b41bb1d02a4d65231c" }
azure_security_keyvault = { git = "https://github.com/azure/azure-sdk-for-rust", rev = "8c4caa251c3903d5eae848b41bb1d02a4d65231c" }
dotenvy = "0.15"
serde = { workspace = true }
spin-expressions = { path = "../expressions" }
spin-factors = { path = "../factors" }
spin-world = { path = "../world" }
tokio = { workspace = true, features = ["rt-multi-thread"] }
toml = { workspace = true }
tracing = { workspace = true }
vaultrs = "0.7"

[dev-dependencies]
spin-factors-test = { path = "../factors-test" }
Expand Down
1 change: 0 additions & 1 deletion crates/factor-variables/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod host;
pub mod runtime_config;
pub mod spin_cli;

use std::sync::Arc;

Expand Down
56 changes: 18 additions & 38 deletions crates/factor-variables/tests/factor_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use spin_factor_variables::{spin_cli, VariablesFactor};
use spin_factors::{
anyhow, Factor, FactorRuntimeConfigSource, RuntimeConfigSourceFinalizer, RuntimeFactors,
};
use spin_expressions::{Key, Provider};
use spin_factor_variables::{runtime_config::RuntimeConfig, VariablesFactor};
use spin_factors::{anyhow, RuntimeFactors};
use spin_factors_test::{toml, TestEnvironment};
use spin_world::v2::variables::Host;

Expand All @@ -11,10 +10,14 @@ struct TestFactors {
}

#[tokio::test(flavor = "multi_thread")]
async fn static_provider_works() -> anyhow::Result<()> {
async fn provider_works() -> anyhow::Result<()> {
let factors = TestFactors {
variables: VariablesFactor::default(),
};
let providers = vec![Box::new(MockProvider) as _];
let runtime_config = TestFactorsRuntimeConfig {
variables: Some(RuntimeConfig { providers }),
};
let env = TestEnvironment::new(factors)
.extend_manifest(toml! {
[variables]
Expand All @@ -24,46 +27,23 @@ async fn static_provider_works() -> anyhow::Result<()> {
source = "does-not-exist.wasm"
variables = { baz = "<{{ foo }}>" }
})
.runtime_config(TomlConfig::new(toml! {
[[variables_provider]]
type = "static"
values = { foo = "bar" }
}))?;
.runtime_config(runtime_config)?;

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

struct TomlConfig {
table: toml::Table,
}

impl TomlConfig {
fn new(table: toml::Table) -> Self {
Self { table }
}
}

impl TryFrom<TomlConfig> for TestFactorsRuntimeConfig {
type Error = anyhow::Error;

fn try_from(value: TomlConfig) -> Result<Self, Self::Error> {
Self::from_source(value)
}
}

impl FactorRuntimeConfigSource<VariablesFactor> for TomlConfig {
fn get_runtime_config(
&mut self,
) -> anyhow::Result<Option<<VariablesFactor as Factor>::RuntimeConfig>> {
spin_cli::runtime_config_from_toml(&self.table).map(Some)
}
}
#[derive(Debug)]
struct MockProvider;

impl RuntimeConfigSourceFinalizer for TomlConfig {
fn finalize(&mut self) -> anyhow::Result<()> {
Ok(())
#[spin_world::async_trait]
impl Provider for MockProvider {
async fn get(&self, key: &Key) -> anyhow::Result<Option<String>> {
match key.as_str() {
"foo" => Ok(Some("bar".to_string())),
_ => Ok(None),
}
}
}
1 change: 1 addition & 0 deletions crates/runtime-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ spin-key-value-redis = { path = "../key-value-redis" }
spin-key-value-spin = { path = "../key-value-spin" }
spin-sqlite = { path = "../sqlite" }
spin-trigger = { path = "../trigger" }
spin-variables = { path = "../variables" }
toml = { workspace = true }

[dev-dependencies]
Expand Down
6 changes: 4 additions & 2 deletions crates/runtime-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use spin_factor_outbound_networking::OutboundNetworkingFactor;
use spin_factor_outbound_pg::OutboundPgFactor;
use spin_factor_outbound_redis::OutboundRedisFactor;
use spin_factor_sqlite::SqliteFactor;
use spin_factor_variables::{spin_cli as variables, VariablesFactor};
use spin_factor_variables::VariablesFactor;
use spin_factor_wasi::WasiFactor;
use spin_factors::runtime_config::toml::GetTomlValue as _;
use spin_factors::{
Expand Down Expand Up @@ -314,7 +314,9 @@ impl FactorRuntimeConfigSource<VariablesFactor> for TomlRuntimeConfigSource<'_,
fn get_runtime_config(
&mut self,
) -> anyhow::Result<Option<<VariablesFactor as spin_factors::Factor>::RuntimeConfig>> {
Ok(Some(variables::runtime_config_from_toml(&self.toml.table)?))
Ok(Some(spin_variables::runtime_config_from_toml(
&self.toml.table,
)?))
}
}

Expand Down
26 changes: 26 additions & 0 deletions crates/variables/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "spin-variables"
version.workspace = true
authors.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true
rust-version.workspace = true

[dependencies]
azure_core = { git = "https://github.com/azure/azure-sdk-for-rust", rev = "8c4caa251c3903d5eae848b41bb1d02a4d65231c" }
azure_identity = { git = "https://github.com/azure/azure-sdk-for-rust", rev = "8c4caa251c3903d5eae848b41bb1d02a4d65231c" }
azure_security_keyvault = { git = "https://github.com/azure/azure-sdk-for-rust", rev = "8c4caa251c3903d5eae848b41bb1d02a4d65231c" }
dotenvy = "0.15"
serde = { workspace = true }
spin-expressions = { path = "../expressions" }
spin-factors = { path = "../factors" }
spin-factor-variables = { path = "../factor-variables" }
spin-world = { path = "../world" }
tokio = { workspace = true, features = ["rt-multi-thread"] }
tracing = { workspace = true }
vaultrs = "0.7"

[lints]
workspace = true
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use serde::Deserialize;
use spin_expressions::Provider;
use spin_factors::{anyhow, runtime_config::toml::GetTomlValue};

use crate::runtime_config::RuntimeConfig;
use spin_factor_variables::runtime_config::RuntimeConfig;

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