Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions crates/common/tedge_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ impl TEdgeConfig {
self.location().update_toml(update).await
}

pub async fn migrate_mapper_config(
self,
cloud_type: models::CloudType,
) -> Result<(), TEdgeConfigError> {
self.location().migrate_mapper_config(cloud_type).await
}

#[cfg(feature = "test")]
/// A test only method designed for injecting configuration into tests
///
Expand Down
34 changes: 26 additions & 8 deletions crates/common/tedge_config/src/tedge_toml/tedge_config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod version;
use futures::Stream;
use reqwest::NoProxy;
use serde::Deserialize;
use version::TEdgeTomlVersion;

mod append_remove;
Expand Down Expand Up @@ -132,7 +131,7 @@ impl TEdgeConfigDto {
use futures::StreamExt;
use futures::TryStreamExt;

let mappers_dir = location.tedge_config_root_path().join("mappers");
let mappers_dir = location.mappers_config_dir();
let all_profiles = location.mapper_config_profiles::<T>().await;
let ty = T::expected_cloud_type();
if let Some(profiles) = all_profiles {
Expand All @@ -150,6 +149,7 @@ impl TEdgeConfigDto {
},
)?;
default_profile_config.set_mapper_config_dir(mappers_dir.clone());
default_profile_config.set_mapper_config_file(toml_path);
dto.non_profile = default_profile_config;

dto.profiles = profiles
Expand All @@ -160,6 +160,7 @@ impl TEdgeConfigDto {
let mut profiled_config: T::CloudDto = toml::from_str(&profile_toml)
.context("failed to deserialise mapper config")?;
profiled_config.set_mapper_config_dir(mappers_dir.clone());
profiled_config.set_mapper_config_file(toml_path);
Ok::<_, anyhow::Error>((profile, profiled_config))
})
.try_collect()
Expand Down Expand Up @@ -421,6 +422,12 @@ impl CloudConfig for DynCloudConfig<'_> {
Self::Borrow(config) => config.key_pin(),
}
}
fn mapper_config_location(&self) -> &Utf8Path {
match self {
Self::Arc(config) => config.mapper_config_location(),
Self::Borrow(config) => config.mapper_config_location(),
}
}
}

/// The keys that can be read from the configuration
Expand All @@ -439,12 +446,6 @@ pub static READABLE_KEYS: Lazy<Vec<(Cow<'static, str>, doku::Type)>> = Lazy::new
struct_field_paths(None, &fields)
});

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, doku::Document, serde::Serialize)]
pub enum MapperConfigLocation {
TedgeToml,
SeparateFile(#[doku(as = "String")] camino::Utf8PathBuf),
}

define_tedge_config! {
#[tedge_config(reader(skip))]
config: {
Expand Down Expand Up @@ -576,6 +577,10 @@ define_tedge_config! {
#[serde(skip)]
mapper_config_dir: Utf8PathBuf,

#[tedge_config(reader(skip))]
#[serde(skip)]
mapper_config_file: Utf8PathBuf,

/// Endpoint URL of Cumulocity tenant
#[tedge_config(example = "your-tenant.cumulocity.com")]
// Config consumers should use `c8y.http`/`c8y.mqtt` as appropriate, hence this field is private
Expand Down Expand Up @@ -820,6 +825,10 @@ define_tedge_config! {
#[serde(skip)]
mapper_config_dir: Utf8PathBuf,

#[tedge_config(reader(skip))]
#[serde(skip)]
mapper_config_file: Utf8PathBuf,

/// Endpoint URL of Azure IoT tenant
#[tedge_config(example = "myazure.azure-devices.net")]
url: ConnectUrl,
Expand Down Expand Up @@ -911,6 +920,10 @@ define_tedge_config! {
#[serde(skip)]
mapper_config_dir: Utf8PathBuf,

#[tedge_config(reader(skip))]
#[serde(skip)]
mapper_config_file: Utf8PathBuf,

/// Endpoint URL of AWS IoT tenant
#[tedge_config(example = "your-endpoint.amazonaws.com")]
url: ConnectUrl,
Expand Down Expand Up @@ -1411,6 +1424,7 @@ pub trait CloudConfig {
fn root_cert_path(&self) -> &Utf8Path;
fn key_uri(&self) -> Option<Arc<str>>;
fn key_pin(&self) -> Option<Arc<str>>;
fn mapper_config_location(&self) -> &Utf8Path;
}

impl<T: SpecialisedCloudConfig> CloudConfig for MapperConfig<T> {
Expand All @@ -1433,6 +1447,10 @@ impl<T: SpecialisedCloudConfig> CloudConfig for MapperConfig<T> {
fn key_pin(&self) -> Option<Arc<str>> {
self.device.key_pin.clone()
}

fn mapper_config_location(&self) -> &Utf8Path {
&self.location
}
}

fn c8y_topic_prefix() -> TopicPrefix {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,21 @@ impl FromCloudConfig for C8yMapperSpecificConfig {
let c8y_config = tedge_config.c8y.try_get(profile).map_err(|_| {
MapperConfigError::ConfigRead(format!("C8y profile '{}' not found", profile.unwrap()))
})?;

build_mapper_config(c8y_config.clone(), profile)
let location = tedge_config
.dto
.c8y
.try_get(profile, "c8y")
.unwrap()
.mapper_config_file
.clone()
.unwrap_or(
tedge_config
.location
.tedge_config_root_path()
.join("tedge.toml"),
);

build_mapper_config(c8y_config.clone(), profile, location)
}

fn from_cloud_config(c8y: &Self::CloudConfigReader, profile: Option<&str>) -> Self {
Expand Down Expand Up @@ -139,8 +152,21 @@ impl FromCloudConfig for AzMapperSpecificConfig {
let az_config = tedge_config.az.try_get(profile).map_err(|_| {
MapperConfigError::ConfigRead(format!("Azure profile '{}' not found", profile.unwrap()))
})?;

build_mapper_config(az_config.clone(), profile)
let location = tedge_config
.dto
.az
.try_get(profile, "az")
.unwrap()
.mapper_config_file
.clone()
.unwrap_or_else(|| {
tedge_config
.location
.tedge_config_root_path()
.join("tedge.toml")
});

build_mapper_config(az_config.clone(), profile, location)
}

fn from_cloud_config(az: &Self::CloudConfigReader, _profile: Option<&str>) -> Self {
Expand All @@ -163,8 +189,21 @@ impl FromCloudConfig for AwsMapperSpecificConfig {
let aws_config = tedge_config.aws.try_get(profile).map_err(|_| {
MapperConfigError::ConfigRead(format!("AWS profile '{}' not found", profile.unwrap()))
})?;

build_mapper_config(aws_config.clone(), profile)
let location = tedge_config
.dto
.aws
.try_get(profile, "aws")
.unwrap()
.mapper_config_file
.clone()
.unwrap_or(
tedge_config
.location
.tedge_config_root_path()
.join("tedge.toml"),
);

build_mapper_config(aws_config.clone(), profile, location)
}

fn from_cloud_config(aws: &Self::CloudConfigReader, _profile: Option<&str>) -> Self {
Expand All @@ -181,6 +220,7 @@ impl FromCloudConfig for AwsMapperSpecificConfig {
pub fn build_mapper_config<T>(
cloud_config: T::CloudConfigReader,
profile: Option<&str>,
location: Utf8PathBuf,
) -> Result<MapperConfig<T>, MapperConfigError>
where
T: SpecialisedCloudConfig,
Expand Down Expand Up @@ -213,6 +253,7 @@ where
let cloud_specific = T::from_cloud_config(&cloud_config, profile);

Ok(MapperConfig {
location,
url,
root_cert_path,
device,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl MapperConfigPath<'_> {
pub trait HasPath {
fn set_mapper_config_dir(&mut self, path: Utf8PathBuf);
fn config_path(&self) -> Option<MapperConfigPath<'_>>;
fn set_mapper_config_file(&mut self, path: Utf8PathBuf);
}

impl HasPath for TEdgeConfigDtoC8y {
Expand All @@ -141,6 +142,10 @@ impl HasPath for TEdgeConfigDtoC8y {
cloud_type: CloudType::C8y,
})
}

fn set_mapper_config_file(&mut self, path: Utf8PathBuf) {
self.mapper_config_file = Some(path)
}
}

impl HasPath for TEdgeConfigDtoAz {
Expand All @@ -154,6 +159,10 @@ impl HasPath for TEdgeConfigDtoAz {
cloud_type: CloudType::Az,
})
}

fn set_mapper_config_file(&mut self, path: Utf8PathBuf) {
self.mapper_config_file = Some(path)
}
}

impl HasPath for TEdgeConfigDtoAws {
Expand All @@ -167,10 +176,16 @@ impl HasPath for TEdgeConfigDtoAws {
cloud_type: CloudType::Aws,
})
}

fn set_mapper_config_file(&mut self, path: Utf8PathBuf) {
self.mapper_config_file = Some(path)
}
}

/// Base mapper configuration with common fields and cloud-specific fields via generics
pub struct MapperConfig<T: SpecialisedCloudConfig> {
pub(crate) location: Utf8PathBuf,

/// Endpoint URL of the cloud tenant
url: OptionalConfig<ConnectUrl>,

Expand Down
Loading