Skip to main content

edgehog_device_runtime_containers/requests/
device_mapping.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::{OptString, 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.CreateDeviceMappingRequest",
29    path = "/deviceMapping",
30    rename_all = "camelCase",
31    aggregation = "object"
32)]
33pub struct CreateDeviceMapping {
34    pub(crate) id: ReqUuid,
35    pub(crate) deployment_id: ReqUuid,
36    pub(crate) path_on_host: String,
37    pub(crate) path_in_container: String,
38    pub(crate) c_group_permissions: OptString,
39}
40
41#[cfg(test)]
42pub(crate) mod tests {
43
44    use std::fmt::Display;
45
46    use astarte_device_sdk::chrono::Utc;
47    use astarte_device_sdk::{AstarteData, DeviceEvent, Value};
48    use pretty_assertions::assert_eq;
49    use uuid::Uuid;
50
51    use super::*;
52
53    pub fn create_device_mapping_request_event(
54        id: impl Display,
55        deployment_id: impl Display,
56        host: &str,
57        container: &str,
58    ) -> DeviceEvent {
59        let fields = [
60            ("id", AstarteData::String(id.to_string())),
61            (
62                "deploymentId",
63                AstarteData::String(deployment_id.to_string()),
64            ),
65            ("pathOnHost", AstarteData::from(host)),
66            ("pathInContainer", AstarteData::from(container)),
67            ("cGroupPermissions", AstarteData::from("msv")),
68        ]
69        .into_iter()
70        .map(|(k, v)| (k.to_string(), v))
71        .collect();
72
73        DeviceEvent {
74            interface: "io.edgehog.devicemanager.apps.CreateDeviceMappingRequest".to_string(),
75            path: "/deviceMapping".to_string(),
76            data: Value::Object {
77                data: fields,
78                timestamp: Utc::now(),
79            },
80        }
81    }
82
83    #[test]
84    fn create_device_mapping() {
85        let id = Uuid::new_v4();
86        let deployment_id = Uuid::new_v4();
87        let event =
88            create_device_mapping_request_event(id, deployment_id, "/dev/tty12", "/dev/tty12");
89
90        let request = CreateDeviceMapping::from_event(event).unwrap();
91
92        let expect = CreateDeviceMapping {
93            id: ReqUuid(id),
94            deployment_id: ReqUuid(deployment_id),
95            path_on_host: "/dev/tty12".to_string(),
96            path_in_container: "/dev/tty12".to_string(),
97            c_group_permissions: OptString(Some("msv".to_string())),
98        };
99
100        assert_eq!(request, expect);
101    }
102}