Skip to content

Commit

Permalink
Merge pull request #39 from eclipse-zenoh/plugin-api-update
Browse files Browse the repository at this point in the history
corrected to latest plugin api
  • Loading branch information
milyin authored Feb 23, 2024
2 parents ed7abc2 + 5983d11 commit 8c30c1b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 57 deletions.
55 changes: 31 additions & 24 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ zenoh-core = { version = "0.11.0-dev", git = "https://github.com/eclipse-zenoh/z
zenoh-protocol = { version = "0.11.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main" }
zenoh-util = { version = "0.11.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main" }
zenoh-keyexpr = { version = "0.11.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main" }
zenoh-plugin-trait = { version = "0.11.0-dev", git = "https://github.com/eclipse-zenoh/zenoh.git", branch = "main" }

[build-dependencies]
rustc_version = "0.4.0"
Expand Down
71 changes: 38 additions & 33 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use config::{S3Config, TlsClientConfig, TLS_PROP};
use futures::future::join_all;
use futures::stream::FuturesUnordered;
use utils::S3Key;
use zenoh_plugin_trait::{plugin_version, Plugin};

use std::collections::HashMap;
use std::convert::TryFrom;
Expand Down Expand Up @@ -53,44 +54,48 @@ pub const TIMESTAMP_METADATA_KEY: &str = "timestamp_uhlc";
// operations.
const STORAGE_WORKER_THREADS: usize = 2;

const GIT_VERSION: &str = git_version::git_version!(prefix = "v", cargo_prefix = "v");
lazy_static::lazy_static! {
static ref LONG_VERSION: String = format!("{} built with {}", GIT_VERSION, env!("RUSTC_VERSION"));
}
pub struct S3Backend {}
zenoh_plugin_trait::declare_plugin!(S3Backend);

impl Plugin for S3Backend {
type StartArgs = VolumeConfig;
type Instance = VolumeInstance;

#[allow(dead_code)]
const CREATE_BACKEND_TYPECHECK: CreateVolume = create_volume;
const DEFAULT_NAME: &'static str = "s3_backend";
const PLUGIN_VERSION: &'static str = plugin_version!();
const PLUGIN_LONG_VERSION: &'static str = zenoh_plugin_trait::plugin_long_version!();

#[no_mangle]
pub fn create_volume(mut config: VolumeConfig) -> ZResult<Box<dyn Volume>> {
// For some reasons env_logger is sometime not active in a loaded library.
// Try to activate it here, ignoring failures.
let _ = env_logger::try_init();
log::debug!("S3 Backend {}", LONG_VERSION.as_str());
fn start(_name: &str, config: &Self::StartArgs) -> ZResult<Self::Instance> {
// For some reasons env_logger is sometime not active in a loaded library.
// Try to activate it here, ignoring failures.
let _ = env_logger::try_init();
log::debug!("S3 Backend {}", Self::PLUGIN_LONG_VERSION);

config
.rest
.insert("version".into(), LONG_VERSION.clone().into());
let mut config = config.clone();
config
.rest
.insert("version".into(), Self::PLUGIN_LONG_VERSION.into());

let endpoint = get_optional_string_property(PROP_S3_ENDPOINT, &config)?;
let region = get_optional_string_property(PROP_S3_REGION, &config)?;
let endpoint = get_optional_string_property(PROP_S3_ENDPOINT, &config)?;
let region = get_optional_string_property(PROP_S3_REGION, &config)?;

let mut properties = Properties::default();
properties.insert("version".into(), LONG_VERSION.clone());
let mut properties = Properties::default();
properties.insert("version".into(), Self::PLUGIN_LONG_VERSION.into());

let admin_status = HashMap::from(properties)
.into_iter()
.map(|(k, v)| (k, serde_json::Value::String(v)))
.collect();
let admin_status = HashMap::from(properties)
.into_iter()
.map(|(k, v)| (k, serde_json::Value::String(v)))
.collect();

let tls_config = load_tls_config(&config)?;
let tls_config = load_tls_config(&config)?;

Ok(Box::new(S3Backend {
admin_status,
endpoint,
region,
tls_config,
}))
Ok(Box::new(S3Volume {
admin_status,
endpoint,
region,
tls_config,
}))
}
}

fn get_optional_string_property(property: &str, config: &VolumeConfig) -> ZResult<Option<String>> {
Expand All @@ -112,20 +117,20 @@ fn load_tls_config(config: &VolumeConfig) -> ZResult<Option<TlsClientConfig>> {
}
}

pub struct S3Backend {
pub struct S3Volume {
admin_status: serde_json::Value,
endpoint: Option<String>,
region: Option<String>,
tls_config: Option<TlsClientConfig>,
}

#[async_trait]
impl Volume for S3Backend {
impl Volume for S3Volume {
fn get_admin_status(&self) -> serde_json::Value {
self.admin_status.clone()
}

async fn create_storage(&mut self, config: StorageConfig) -> ZResult<Box<dyn Storage>> {
async fn create_storage(&self, config: StorageConfig) -> ZResult<Box<dyn Storage>> {
log::debug!("Creating storage...");
let config: S3Config = S3Config::new(&config).await?;

Expand Down

0 comments on commit 8c30c1b

Please sign in to comment.