Skip to main content

edgehog_device_runtime_containers/requests/
volume.rs

1// This file is part of Edgehog.
2//
3// Copyright 2024 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 image request
20
21use astarte_device_sdk::FromEvent;
22
23use super::ReqUuid;
24
25/// Request to pull a Docker Volume.
26#[derive(Debug, Clone, FromEvent, PartialEq, Eq, PartialOrd, Ord)]
27#[from_event(
28    interface = "io.edgehog.devicemanager.apps.CreateVolumeRequest",
29    path = "/volume",
30    rename_all = "camelCase",
31    aggregation = "object"
32)]
33pub struct CreateVolume {
34    pub(crate) id: ReqUuid,
35    pub(crate) deployment_id: ReqUuid,
36    pub(crate) driver: String,
37    pub(crate) options: Vec<String>,
38}
39
40#[cfg(test)]
41pub(crate) mod tests {
42    use std::fmt::Display;
43
44    use astarte_device_sdk::chrono::Utc;
45    use astarte_device_sdk::{AstarteData, DeviceEvent, Value};
46    use itertools::Itertools;
47    use uuid::Uuid;
48
49    use super::*;
50
51    pub fn create_volume_request_event(
52        id: impl Display,
53        deployment_id: impl Display,
54        driver: &str,
55        options: &[&str],
56    ) -> DeviceEvent {
57        let options = options.iter().map(|s| s.to_string()).collect_vec();
58
59        let fields = [
60            ("id".to_string(), AstarteData::String(id.to_string())),
61            (
62                "deploymentId".to_string(),
63                AstarteData::String(deployment_id.to_string()),
64            ),
65            (
66                "driver".to_string(),
67                AstarteData::String(driver.to_string()),
68            ),
69            ("options".to_string(), AstarteData::StringArray(options)),
70        ]
71        .into_iter()
72        .collect();
73
74        DeviceEvent {
75            interface: "io.edgehog.devicemanager.apps.CreateVolumeRequest".to_string(),
76            path: "/volume".to_string(),
77            data: Value::Object {
78                data: fields,
79                timestamp: Utc::now(),
80            },
81        }
82    }
83
84    #[test]
85    fn create_volume_request() {
86        let id = Uuid::new_v4();
87        let deployment_id = Uuid::new_v4();
88        let event = create_volume_request_event(id, deployment_id, "driver", &["foo=bar", "some="]);
89
90        let request = CreateVolume::from_event(event).unwrap();
91
92        let expect = CreateVolume {
93            id: ReqUuid(id),
94            deployment_id: ReqUuid(deployment_id),
95            driver: "driver".to_string(),
96            options: ["foo=bar", "some="].map(str::to_string).to_vec(),
97        };
98
99        assert_eq!(request, expect);
100    }
101}