Skip to content

Commit

Permalink
refactor(uuid): specify the stream node uuid when using grpc connection
Browse files Browse the repository at this point in the history
use EVN ASTARTE_MSGHUB_NODE_ID or the toml file to specify the node id of the stream application

Signed-off-by: Riccardo Gallo <riccardo.gallo@secomind.com>
  • Loading branch information
rgallor committed Dec 2, 2024
1 parent ac9f8de commit 38cd270
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 2 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 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"
23 changes: 15 additions & 8 deletions src/astarte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ 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;

const DEVICE_DATASTREAM: &str =
include_str!("../interfaces/org.astarte-platform.genericsensors.Values.json");
Expand Down Expand Up @@ -103,9 +101,10 @@ impl ConnectionConfigBuilder {
self.astarte_connection = Some(con);

let endpoint = env::var("ASTARTE_MSGHUB_ENDPOINT")?;
let node_id = env::var("ASTARTE_MSGHUB_NODE_ID")?.parse::<Uuid>()?;

Check warning on line 104 in src/astarte.rs

View check run for this annotation

Codecov / codecov/patch

src/astarte.rs#L104

Added line #L104 was not covered by tests

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

Check warning on line 107 in src/astarte.rs

View check run for this annotation

Codecov / codecov/patch

src/astarte.rs#L107

Added line #L107 was not covered by tests
}
}

Expand Down Expand Up @@ -162,10 +161,10 @@ impl ConnectionConfigBuilder {
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 165 in src/astarte.rs

View check run for this annotation

Codecov / codecov/patch

src/astarte.rs#L164-L165

Added lines #L164 - L165 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 +223,18 @@ impl From<MqttConfigBuilder> for MqttConfig {
/// Config for a gRPC connection to an Astarte Message Hub instance
#[derive(Debug, Default, Deserialize)]
struct GrpcConfigBuilder {
/// 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 234 in src/astarte.rs

View check run for this annotation

Codecov / codecov/patch

src/astarte.rs#L233-L234

Added lines #L233 - L234 were not covered by tests
}
}

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

0 comments on commit 38cd270

Please sign in to comment.