Skip to main content

edgehog_device_runtime_containers/requests/
network.rs

1// This file is part of Edgehog.
2//
3// Copyright 2024 - 2025 SECO Mind Srl
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9//    http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17// SPDX-License-Identifier: Apache-2.0
18
19//! Create network request
20
21use astarte_device_sdk::FromEvent;
22
23use super::ReqUuid;
24
25/// Request to pull a Docker Network.
26#[derive(Debug, Clone, FromEvent, PartialEq, Eq, PartialOrd, Ord)]
27#[from_event(
28    interface = "io.edgehog.devicemanager.apps.CreateNetworkRequest",
29    path = "/network",
30    rename_all = "camelCase",
31    aggregation = "object"
32)]
33pub struct CreateNetwork {
34    pub(crate) id: ReqUuid,
35    pub(crate) deployment_id: ReqUuid,
36    pub(crate) driver: String,
37    pub(crate) internal: bool,
38    pub(crate) enable_ipv6: bool,
39    pub(crate) options: Vec<String>,
40}
41
42#[cfg(test)]
43pub(crate) mod tests {
44
45    use std::fmt::Display;
46
47    use astarte_device_sdk::chrono::Utc;
48    use astarte_device_sdk::{AstarteData, DeviceEvent, Value};
49    use itertools::Itertools;
50    use uuid::Uuid;
51
52    use super::*;
53
54    pub fn create_network_request_event(
55        id: impl Display,
56        deployment_id: impl Display,
57        driver: &str,
58        options: &[&str],
59    ) -> DeviceEvent {
60        let options = options.iter().map(|s| s.to_string()).collect_vec();
61
62        let fields = [
63            ("id", AstarteData::String(id.to_string())),
64            (
65                "deploymentId",
66                AstarteData::String(deployment_id.to_string()),
67            ),
68            ("driver", AstarteData::String(driver.to_string())),
69            ("checkDuplicate", AstarteData::Boolean(false)),
70            ("internal", AstarteData::Boolean(false)),
71            ("enableIpv6", AstarteData::Boolean(false)),
72            ("options", AstarteData::StringArray(options)),
73        ]
74        .into_iter()
75        .map(|(k, v)| (k.to_string(), v))
76        .collect();
77
78        DeviceEvent {
79            interface: "io.edgehog.devicemanager.apps.CreateNetworkRequest".to_string(),
80            path: "/network".to_string(),
81            data: Value::Object {
82                data: fields,
83                timestamp: Utc::now(),
84            },
85        }
86    }
87
88    #[test]
89    fn create_network_request() {
90        let id = Uuid::new_v4();
91        let deployment_id = Uuid::new_v4();
92        let event =
93            create_network_request_event(id, deployment_id, "driver", &["foo=bar", "some="]);
94
95        let request = CreateNetwork::from_event(event).unwrap();
96
97        let expect = CreateNetwork {
98            id: ReqUuid(id),
99            deployment_id: ReqUuid(deployment_id),
100            driver: "driver".to_string(),
101            internal: false,
102            enable_ipv6: false,
103            options: ["foo=bar", "some="].map(str::to_string).to_vec(),
104        };
105
106        assert_eq!(request, expect);
107    }
108}