Skip to main content

edgehog_device_runtime_containers/requests/
image.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 Image.
26#[derive(Debug, Clone, FromEvent, PartialEq, Eq, PartialOrd, Ord)]
27#[from_event(
28    interface = "io.edgehog.devicemanager.apps.CreateImageRequest",
29    path = "/image",
30    rename_all = "camelCase",
31    aggregation = "object"
32)]
33pub struct CreateImage {
34    pub(crate) id: ReqUuid,
35    pub(crate) deployment_id: ReqUuid,
36    pub(crate) reference: String,
37    pub(crate) registry_auth: 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::{DeviceEvent, Value};
46    use uuid::Uuid;
47
48    use super::*;
49
50    pub fn create_image_request_event(
51        id: impl Display,
52        deployment_id: impl Display,
53        reference: &str,
54        auth: &str,
55    ) -> DeviceEvent {
56        let fields = [
57            ("id", id.to_string()),
58            ("deploymentId", deployment_id.to_string()),
59            ("reference", reference.to_string()),
60            ("registryAuth", auth.to_string()),
61        ]
62        .into_iter()
63        .map(|(k, v)| (k.to_string(), v.into()))
64        .collect();
65
66        DeviceEvent {
67            interface: "io.edgehog.devicemanager.apps.CreateImageRequest".to_string(),
68            path: "/image".to_string(),
69            data: Value::Object {
70                data: fields,
71                timestamp: Utc::now(),
72            },
73        }
74    }
75
76    #[test]
77    fn create_image_request() {
78        let id = Uuid::new_v4();
79        let deployment_id = Uuid::new_v4();
80        let event =
81            create_image_request_event(id.to_string(), deployment_id, "reference", "registry_auth");
82
83        let request = CreateImage::from_event(event).unwrap();
84
85        let expect = CreateImage {
86            id: ReqUuid(id),
87            deployment_id: ReqUuid(deployment_id),
88            reference: "reference".to_string(),
89            registry_auth: "registry_auth".to_string(),
90        };
91
92        assert_eq!(request, expect);
93    }
94}