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 1 commit
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: 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"
rgallor marked this conversation as resolved.
Show resolved Hide resolved
```

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::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 @@
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
rgallor marked this conversation as resolved.
Show resolved Hide resolved

// 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 @@
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 @@
/// 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
Loading