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

refactor(uuid): specify the stream node uuid when using grpc connection #19

Merged
merged 3 commits into from
Dec 2, 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ tracing = "0.1.37"
tracing-subscriber = { version = "0.3.0", features = ["env-filter"]}
rand = "0.8.5"
serde = { version = "1.0.207", features = ["derive"] }
uuid = { version = "1.10.0", features = ["v4"] }
uuid = { version = "1.10.0", features = ["v4", "serde"] }
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ If you want to use environment variables to set up the application, you can set
- `ASTARTE_STORE_DIRECTORY`: path to the directory where to store data (e.g., in case of Astarte properties)
- `ASTARTE_IGNORE_SSL_ERRORS`: boolean stating if SSL errors should be ignored (default: false)
- `ASTARTE_MSGHUB_ENDPOINT`: endpoint of the Astarte Message Hub instance
- `ASTARTE_MSGHUB_NODE_ID`: UUID of the Node to connect to the Astarte Message Hub

Instead, if you want to use a configuration file, you must specify its location by using the `ASTARTE_CONFIG_PATH`
environment variable. The `config.toml` file must contain the following information:
Expand All @@ -62,6 +63,7 @@ astarte_ignore_ssl = false
# gRPC connection to the Astarte Message Hub
[astarte.grpc]
endpoint = "http://[::1]:50051"
#node_id = "ASTARTE_MSGHUB_NODE_ID_HERE"
```

NOTE: only one of the `[astarte.mqtt]` or `[astarte.grpc]` sections should be specified in the file.
Expand All @@ -76,6 +78,7 @@ A detailed description of the fields is depicted below:
present, the credential secret will be used.
- `astarte_ignore_ssl`: a flag stating if SSL errors should be ignored when connecting to Astarte.
- `endpoint`: the endpoint where the Astarte Message Hub instance is listening for new connections.
- `node_id`: UUID of the Node to connect to the Astarte Message Hub (optional).

Since the application can be configured with a CLI, when [running the application](#build-and-run) you can specify the
type of connection (`mqtt` or `grpc`) and the path to the `config.toml` file with the `--astarte_connection` (`-c`) and
Expand Down
1 change: 1 addition & 0 deletions astarte-device-conf/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ astarte_ignore_ssl = false
#
#[astarte.grpc]
#endpoint = "http://[::1]:50051"
#node_id = "ASTARTE_MSGHUB_NODE_ID_HERE"
43 changes: 34 additions & 9 deletions src/astarte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@
use astarte_device_sdk::{Client, DeviceClient, DeviceConnection};
use clap::ValueEnum;
use color_eyre::eyre;
use color_eyre::eyre::{eyre, OptionExt, WrapErr};
use color_eyre::eyre::{bail, eyre, OptionExt, WrapErr};
use serde::Deserialize;
use std::env::VarError;
use std::path::{Path, PathBuf};
use std::time::SystemTime;
use std::{env, io};
use tracing::{debug, error};

/// Stream Rust test node identifier
const STREAM_RUST_TEST_NODE_UUID: uuid::Uuid = uuid::uuid!("d72a6187-7cf1-44cc-87e8-e991936166dc");
use uuid::{uuid, Uuid};

const DEVICE_DATASTREAM: &str =
include_str!("../interfaces/org.astarte-platform.genericsensors.Values.json");

const DEFAULT_STREAM_NODE_ID: Uuid = uuid!("d72a6187-7cf1-44cc-87e8-e991936166dc");

/// This function is necessary for serde deserialization
fn default_stream_node_id() -> Uuid {
DEFAULT_STREAM_NODE_ID

Check warning on line 34 in src/astarte.rs

View check run for this annotation

Codecov / codecov/patch

src/astarte.rs#L33-L34

Added lines #L33 - L34 were not covered by tests
}

/// Specify which Astarte library use to connect to Astarte
#[derive(
Debug, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, clap::ValueEnum, Deserialize,
Expand Down Expand Up @@ -104,8 +110,18 @@

let endpoint = env::var("ASTARTE_MSGHUB_ENDPOINT")?;

let node_id = match env::var("ASTARTE_MSGHUB_NODE_ID") {
Ok(uuid) => {
Uuid::parse_str(&uuid).wrap_err("invalid ASTARTE_MSGHUB_NODE_ID {uuid}")?

Check warning on line 115 in src/astarte.rs

View check run for this annotation

Codecov / codecov/patch

src/astarte.rs#L113-L115

Added lines #L113 - L115 were not covered by tests
}
Err(VarError::NotPresent) => DEFAULT_STREAM_NODE_ID,
Err(VarError::NotUnicode(s)) => {
bail!("non unicode ASTARTE_MSGHUB_NODE_ID {s:?}")

Check warning on line 119 in src/astarte.rs

View check run for this annotation

Codecov / codecov/patch

src/astarte.rs#L117-L119

Added lines #L117 - L119 were not covered by tests
}
};

// update the grpc config info
self.grpc_config = Some(GrpcConfigBuilder { endpoint });
self.grpc_config = Some(GrpcConfigBuilder { node_id, endpoint });
}
}

Expand Down Expand Up @@ -162,10 +178,10 @@
Ok((client, SdkConnection::Mqtt(connection)))
}
AstarteConnection::Grpc => {
let grpc_endpoint = self.grpc_config.ok_or_eyre("invalid grpc config")?.endpoint;

let grpc_cfg = GrpcConfig::from_url(STREAM_RUST_TEST_NODE_UUID, grpc_endpoint)
.wrap_err("failed to create a gRPC config")?;
let grpc_cfg = self
.grpc_config

Check warning on line 182 in src/astarte.rs

View check run for this annotation

Codecov / codecov/patch

src/astarte.rs#L181-L182

Added lines #L181 - L182 were not covered by tests
.ok_or_eyre("invalid grpc config")?
.build()?;

debug!("parsed Astarte Message Hub config: {:#?}", grpc_cfg);

Expand Down Expand Up @@ -224,10 +240,19 @@
/// Config for a gRPC connection to an Astarte Message Hub instance
#[derive(Debug, Default, Deserialize)]
struct GrpcConfigBuilder {
#[serde(default = "default_stream_node_id")]
/// Stream Rust test UUID
node_id: Uuid,
/// The Endpoint of the Astarte Message Hub
endpoint: String,
}

impl GrpcConfigBuilder {
fn build(self) -> eyre::Result<GrpcConfig> {
GrpcConfig::from_url(self.node_id, self.endpoint).wrap_err("failed to create a gRPC config")

Check warning on line 252 in src/astarte.rs

View check run for this annotation

Codecov / codecov/patch

src/astarte.rs#L251-L252

Added lines #L251 - L252 were not covered by tests
}
}

/// Send data to Astarte
pub async fn send_data(
client: DeviceClient<SqliteStore>,
Expand Down
Loading