diff --git a/Makefile b/Makefile index cebf5a3..574176d 100644 --- a/Makefile +++ b/Makefile @@ -108,4 +108,3 @@ help: @echo ' - Build, install and all its dependencies and generate code' @echo ' -install - Install files into the repo folder' @echo ' -dirclean - Remove build directory' - diff --git a/proto/astarteplatform/msghub/message_hub_service.proto b/proto/astarteplatform/msghub/message_hub_service.proto index b7a0e3b..809ceb2 100644 --- a/proto/astarteplatform/msghub/message_hub_service.proto +++ b/proto/astarteplatform/msghub/message_hub_service.proto @@ -25,7 +25,6 @@ package astarteplatform.msghub; import "google/protobuf/empty.proto"; import "astarteplatform/msghub/astarte_message.proto"; -import "astarteplatform/msghub/message_hub_error.proto"; import "astarteplatform/msghub/node.proto"; import "astarteplatform/msghub/interface.proto"; diff --git a/proto/astarteplatform/msghub/node.proto b/proto/astarteplatform/msghub/node.proto index bcd706b..c949a7d 100644 --- a/proto/astarteplatform/msghub/node.proto +++ b/proto/astarteplatform/msghub/node.proto @@ -20,12 +20,9 @@ syntax = "proto3"; -import "astarteplatform/msghub/interface.proto"; - package astarteplatform.msghub; /* This message defines a node to be attached to the Astarte message hub. */ message Node { - string uuid = 1; // The node identifier. repeated string interfaces_json = 2; // Array of string representing all .json interface files of the node. } diff --git a/python/astarteplatform/msghub/message_hub_service_pb2.py b/python/astarteplatform/msghub/message_hub_service_pb2.py index 9e6f0a5..7972afc 100644 --- a/python/astarteplatform/msghub/message_hub_service_pb2.py +++ b/python/astarteplatform/msghub/message_hub_service_pb2.py @@ -14,18 +14,17 @@ from google.protobuf import empty_pb2 as google_dot_protobuf_dot_empty__pb2 from astarteplatform.msghub import astarte_message_pb2 as astarteplatform_dot_msghub_dot_astarte__message__pb2 -from astarteplatform.msghub import message_hub_error_pb2 as astarteplatform_dot_msghub_dot_message__hub__error__pb2 from astarteplatform.msghub import node_pb2 as astarteplatform_dot_msghub_dot_node__pb2 from astarteplatform.msghub import interface_pb2 as astarteplatform_dot_msghub_dot_interface__pb2 -DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n0astarteplatform/msghub/message_hub_service.proto\x12\x16\x61starteplatform.msghub\x1a\x1bgoogle/protobuf/empty.proto\x1a,astarteplatform/msghub/astarte_message.proto\x1a.astarteplatform/msghub/message_hub_error.proto\x1a!astarteplatform/msghub/node.proto\x1a&astarteplatform/msghub/interface.proto2\x90\x03\n\nMessageHub\x12S\n\x06\x41ttach\x12\x1c.astarteplatform.msghub.Node\x1a\'.astarteplatform.msghub.MessageHubEvent\"\x00\x30\x01\x12H\n\x04Send\x12&.astarteplatform.msghub.AstarteMessage\x1a\x16.google.protobuf.Empty\"\x00\x12:\n\x06\x44\x65tach\x12\x16.google.protobuf.Empty\x1a\x16.google.protobuf.Empty\"\x00\x12Q\n\rAddInterfaces\x12&.astarteplatform.msghub.InterfacesJson\x1a\x16.google.protobuf.Empty\"\x00\x12T\n\x10RemoveInterfaces\x12&.astarteplatform.msghub.InterfacesName\x1a\x16.google.protobuf.Empty\"\x00\x62\x06proto3') +DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n0astarteplatform/msghub/message_hub_service.proto\x12\x16\x61starteplatform.msghub\x1a\x1bgoogle/protobuf/empty.proto\x1a,astarteplatform/msghub/astarte_message.proto\x1a!astarteplatform/msghub/node.proto\x1a&astarteplatform/msghub/interface.proto2\x90\x03\n\nMessageHub\x12S\n\x06\x41ttach\x12\x1c.astarteplatform.msghub.Node\x1a\'.astarteplatform.msghub.MessageHubEvent\"\x00\x30\x01\x12H\n\x04Send\x12&.astarteplatform.msghub.AstarteMessage\x1a\x16.google.protobuf.Empty\"\x00\x12:\n\x06\x44\x65tach\x12\x16.google.protobuf.Empty\x1a\x16.google.protobuf.Empty\"\x00\x12Q\n\rAddInterfaces\x12&.astarteplatform.msghub.InterfacesJson\x1a\x16.google.protobuf.Empty\"\x00\x12T\n\x10RemoveInterfaces\x12&.astarteplatform.msghub.InterfacesName\x1a\x16.google.protobuf.Empty\"\x00\x62\x06proto3') _globals = globals() _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'astarteplatform.msghub.message_hub_service_pb2', _globals) if not _descriptor._USE_C_DESCRIPTORS: DESCRIPTOR._loaded_options = None - _globals['_MESSAGEHUB']._serialized_start=275 - _globals['_MESSAGEHUB']._serialized_end=675 + _globals['_MESSAGEHUB']._serialized_start=227 + _globals['_MESSAGEHUB']._serialized_end=627 # @@protoc_insertion_point(module_scope) diff --git a/python/astarteplatform/msghub/message_hub_service_pb2.pyi b/python/astarteplatform/msghub/message_hub_service_pb2.pyi index c354f91..e7332ec 100644 --- a/python/astarteplatform/msghub/message_hub_service_pb2.pyi +++ b/python/astarteplatform/msghub/message_hub_service_pb2.pyi @@ -1,6 +1,5 @@ from google.protobuf import empty_pb2 as _empty_pb2 from astarteplatform.msghub import astarte_message_pb2 as _astarte_message_pb2 -from astarteplatform.msghub import message_hub_error_pb2 as _message_hub_error_pb2 from astarteplatform.msghub import node_pb2 as _node_pb2 from astarteplatform.msghub import interface_pb2 as _interface_pb2 from google.protobuf import descriptor as _descriptor diff --git a/python/astarteplatform/msghub/node_pb2.py b/python/astarteplatform/msghub/node_pb2.py index 0df91e5..d914083 100644 --- a/python/astarteplatform/msghub/node_pb2.py +++ b/python/astarteplatform/msghub/node_pb2.py @@ -12,16 +12,15 @@ _sym_db = _symbol_database.Default() -from astarteplatform.msghub import interface_pb2 as astarteplatform_dot_msghub_dot_interface__pb2 -DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n!astarteplatform/msghub/node.proto\x12\x16\x61starteplatform.msghub\x1a&astarteplatform/msghub/interface.proto\"-\n\x04Node\x12\x0c\n\x04uuid\x18\x01 \x01(\t\x12\x17\n\x0finterfaces_json\x18\x02 \x03(\tb\x06proto3') +DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n!astarteplatform/msghub/node.proto\x12\x16\x61starteplatform.msghub\"\x1f\n\x04Node\x12\x17\n\x0finterfaces_json\x18\x02 \x03(\tb\x06proto3') _globals = globals() _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'astarteplatform.msghub.node_pb2', _globals) if not _descriptor._USE_C_DESCRIPTORS: DESCRIPTOR._loaded_options = None - _globals['_NODE']._serialized_start=101 - _globals['_NODE']._serialized_end=146 + _globals['_NODE']._serialized_start=61 + _globals['_NODE']._serialized_end=92 # @@protoc_insertion_point(module_scope) diff --git a/python/astarteplatform/msghub/node_pb2.pyi b/python/astarteplatform/msghub/node_pb2.pyi index 1011635..8c5637a 100644 --- a/python/astarteplatform/msghub/node_pb2.pyi +++ b/python/astarteplatform/msghub/node_pb2.pyi @@ -1,4 +1,3 @@ -from astarteplatform.msghub import interface_pb2 as _interface_pb2 from google.protobuf.internal import containers as _containers from google.protobuf import descriptor as _descriptor from google.protobuf import message as _message @@ -7,9 +6,7 @@ from typing import ClassVar as _ClassVar, Iterable as _Iterable, Optional as _Op DESCRIPTOR: _descriptor.FileDescriptor class Node(_message.Message): - __slots__ = ("uuid", "interfaces_json") - UUID_FIELD_NUMBER: _ClassVar[int] + __slots__ = ("interfaces_json",) INTERFACES_JSON_FIELD_NUMBER: _ClassVar[int] - uuid: str interfaces_json: _containers.RepeatedScalarFieldContainer[str] - def __init__(self, uuid: _Optional[str] = ..., interfaces_json: _Optional[_Iterable[str]] = ...) -> None: ... + def __init__(self, interfaces_json: _Optional[_Iterable[str]] = ...) -> None: ... diff --git a/rust/astarte-message-hub-proto/README.md b/rust/astarte-message-hub-proto/README.md index fdf45ad..48b1599 100644 --- a/rust/astarte-message-hub-proto/README.md +++ b/rust/astarte-message-hub-proto/README.md @@ -23,13 +23,14 @@ This module provides access to the Astarte message hub protocol buffers through ```rust use std::time; -use clap::Parser; - use astarte_message_hub_proto::astarte_message::Payload; use astarte_message_hub_proto::message_hub_client::MessageHubClient; use astarte_message_hub_proto::AstarteMessage; use astarte_message_hub_proto::Node; use astarte_message_hub_proto::pbjson_types::Empty; +use clap::Parser; +use tonic::metadata::MetadataValue; +use tonic::transport::channel::Endpoint; use log::info; use uuid::Uuid; @@ -52,10 +53,21 @@ struct Cli { async fn run_example_client() { env_logger::init(); let args = Cli::parse(); + + let uuid = Uuid::parse_str(&args.uuid).unwrap(); - let mut client = MessageHubClient::connect("http://[::1]:50051") + let channel = Endpoint::from_static("http://[::1]:50051") + .connect() .await .unwrap(); + + // adding the interceptor layer will include the Node ID inside the metadata + let mut client = + MessageHubClient::with_interceptor(channel, move |mut req: tonic::Request<()>| { + req.metadata_mut() + .insert_bin("node-id-bin", MetadataValue::from_bytes(uuid.as_ref())); + Ok(req) + }); let device_datastream_interface: &str = r#"{ "interface_name": "org.astarte-platform.rust.examples.datastream.DeviceDatastream", @@ -71,10 +83,9 @@ async fn run_example_client() { } ] }"#; - - let node_id = Uuid::parse_str(&args.uuid).unwrap(); + let interfaces_json = vec![device_datastream_interface.to_string()]; - let node = Node::new(&node_id, interfaces_json); + let node = Node::new(interfaces_json); let mut stream = client.attach(node.clone()).await.unwrap().into_inner(); diff --git a/rust/astarte-message-hub-proto/src/astarteplatform.msghub.rs b/rust/astarte-message-hub-proto/src/astarteplatform.msghub.rs index 67e71aa..9721ca9 100644 --- a/rust/astarte-message-hub-proto/src/astarteplatform.msghub.rs +++ b/rust/astarte-message-hub-proto/src/astarteplatform.msghub.rs @@ -204,6 +204,14 @@ pub mod astarte_message { #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct AstarteUnset {} +/// This message defines a node to be attached to the Astarte message hub. +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct Node { + /// Array of string representing all .json interface files of the node. + #[prost(string, repeated, tag = "2")] + pub interfaces_json: ::prost::alloc::vec::Vec<::prost::alloc::string::String>, +} /// This message defines a list of json interfaces to be added/removed to the Astarte message hub. #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] @@ -220,17 +228,6 @@ pub struct InterfacesName { #[prost(string, repeated, tag = "1")] pub names: ::prost::alloc::vec::Vec<::prost::alloc::string::String>, } -/// This message defines a node to be attached to the Astarte message hub. -#[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Clone, PartialEq, ::prost::Message)] -pub struct Node { - /// The node identifier. - #[prost(string, tag = "1")] - pub uuid: ::prost::alloc::string::String, - /// Array of string representing all .json interface files of the node. - #[prost(string, repeated, tag = "2")] - pub interfaces_json: ::prost::alloc::vec::Vec<::prost::alloc::string::String>, -} /// Generated client implementations. pub mod message_hub_client { #![allow(unused_variables, dead_code, missing_docs, clippy::let_unit_value)] diff --git a/rust/astarte-message-hub-proto/src/proto_message_hub.rs b/rust/astarte-message-hub-proto/src/proto_message_hub.rs index c26eed3..0cf7899 100644 --- a/rust/astarte-message-hub-proto/src/proto_message_hub.rs +++ b/rust/astarte-message-hub-proto/src/proto_message_hub.rs @@ -26,7 +26,6 @@ use self::{astarte_data_type::Data, astarte_message::Payload}; use serde::Serialize; use std::fmt::{Display, Formatter}; -use uuid::Uuid; include!("astarteplatform.msghub.rs"); @@ -213,18 +212,13 @@ impl AstarteDataType { } impl Node { - /// Create a new [Node] with the given [uuid](Node::uuid) and [interfaces_json](Node::interfaces_json). - pub fn new(uuid: &Uuid, interfaces_json: Vec) -> Self { - Self { - uuid: uuid.to_string(), - interfaces_json, - } + /// Create a new [Node] with the given `interfaces_json`. + pub fn new(interfaces_json: Vec) -> Self { + Self { interfaces_json } } - pub fn from_interfaces<'a, I, T>( - uuid: &Uuid, - interfaces: I, - ) -> Result + /// Create a new [Node] from an iterator of interfaces. + pub fn from_interfaces<'a, I, T>(interfaces: I) -> Result where I: IntoIterator, T: ?Sized + Serialize + 'a, @@ -234,7 +228,7 @@ impl Node { .map(serde_json::to_string) .collect::, serde_json::error::Error>>()?; - Ok(Self::new(uuid, interfaces_json)) + Ok(Self::new(interfaces_json)) } } @@ -348,10 +342,9 @@ impl MessageHubError { #[cfg(test)] mod test { - use std::collections::HashMap; - use super::astarte_data_type_individual::IndividualData; use super::*; + use std::collections::HashMap; #[test] fn test_astarte_message_data() { @@ -445,8 +438,6 @@ mod test { #[test] fn create_node_from_interface_files() { - let uuid = Uuid::new_v4(); - let device_datastream_interface = r#"{ "interface_name": "org.astarte-platform.rust.examples.datastream.DeviceDatastream", "version_major": 0, @@ -481,9 +472,8 @@ mod test { .map(|s| s.to_string()) .to_vec(); - let node = Node::new(&uuid, interfaces_json.clone()); + let node = Node::new(interfaces_json.clone()); - assert_eq!(node.uuid, uuid.to_string()); assert_eq!(node.interfaces_json.len(), 2); for (interface, expected) in node.interfaces_json.iter().zip(interfaces_json.iter()) { diff --git a/scripts/python_codegen.sh b/scripts/python_codegen.sh index 1711247..d7eb35b 100755 --- a/scripts/python_codegen.sh +++ b/scripts/python_codegen.sh @@ -39,6 +39,11 @@ install_code (){ OUT_DIR=$1 INSTALL_DIR=$2 + # Check if the directory exists and is not empty + if [ -d "$INSTALL_DIR"/astarteplatform/msghub ] && [ "$(ls -A "$INSTALL_DIR"/astarteplatform/msghub)" ]; then + rm -v "$INSTALL_DIR"/astarteplatform/msghub/* + fi + install -d "$INSTALL_DIR"/astarteplatform/msghub install -m 644 "$OUT_DIR"/astarteplatform/msghub/* "$INSTALL_DIR"/astarteplatform/msghub }