Skip to content

Commit

Permalink
feat(container): use uuid instead of strings
Browse files Browse the repository at this point in the history
Parse the uuid when converting the event.

Signed-off-by: Joshua Chapman <joshua.chapman@secomind.com>
  • Loading branch information
joshuachp committed Jan 16, 2025
1 parent 2717ff7 commit dced497
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 85 deletions.
4 changes: 2 additions & 2 deletions edgehog-device-runtime-containers/src/docker/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,10 +487,10 @@ where
#[derive(Debug, Clone, Default)]
pub struct PortBindingMap<S>(pub HashMap<String, Vec<Binding<S>>>);

impl TryFrom<Vec<String>> for PortBindingMap<String> {
impl TryFrom<&[String]> for PortBindingMap<String> {
type Error = BindingError;

fn try_from(value: Vec<String>) -> Result<Self, Self::Error> {
fn try_from(value: &[String]) -> Result<Self, Self::Error> {
value
.iter()
.try_fold(
Expand Down
63 changes: 36 additions & 27 deletions edgehog-device-runtime-containers/src/requests/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ use tracing::{instrument, trace};
use crate::{
container::{Binding, Container, PortBindingMap},
requests::BindingError,
service::{Id, ResourceType, ServiceError},
service::{Id, ResourceType},
store::container::RestartPolicy,
};

use super::ReqError;
use super::{ReqError, ReqUuid, VecReqUuid};

/// Request to pull a Docker Container.
#[derive(Debug, Clone, FromEvent, PartialEq, Eq, PartialOrd, Ord)]
Expand All @@ -40,10 +40,10 @@ use super::ReqError;
rename_all = "camelCase"
)]
pub struct CreateContainer {
pub(crate) id: String,
pub(crate) image_id: String,
pub(crate) network_ids: Vec<String>,
pub(crate) volume_ids: Vec<String>,
pub(crate) id: ReqUuid,
pub(crate) image_id: ReqUuid,
pub(crate) network_ids: VecReqUuid,
pub(crate) volume_ids: VecReqUuid,
// TODO: remove this image, use the image id
pub(crate) image: String,
pub(crate) hostname: String,
Expand All @@ -56,17 +56,17 @@ pub struct CreateContainer {
}

impl CreateContainer {
pub(crate) fn dependencies(&self) -> Result<Vec<Id>, ServiceError> {
std::iter::once(Id::try_from_str(ResourceType::Image, &self.image_id))
pub(crate) fn dependencies(&self) -> Vec<Id> {
std::iter::once(Id::new(ResourceType::Image, *self.image_id))
.chain(
self.network_ids
.iter()
.map(|id| Id::try_from_str(ResourceType::Network, id)),
.map(|id| Id::new(ResourceType::Network, **id)),
)
.chain(
self.volume_ids
.iter()
.map(|id| Id::try_from_str(ResourceType::Volume, id)),
.map(|id| Id::new(ResourceType::Volume, **id)),
)
.collect()
}
Expand All @@ -83,18 +83,18 @@ impl TryFrom<CreateContainer> for Container<String> {
};

let restart_policy = RestartPolicy::from_str(&value.restart_policy)?.into();
let port_bindings = PortBindingMap::try_from(value.port_bindings)?;
let port_bindings = PortBindingMap::try_from(value.port_bindings.as_slice())?;

Ok(Container {
id: None,
name: value.id,
name: value.id.to_string(),
image: value.image,
hostname,
restart_policy,
env: value.env,
network_mode: value.network_mode,
// Use the network ids
networks: value.network_ids,
networks: value.network_ids.iter().map(|id| id.to_string()).collect(),
binds: value.binds,
port_bindings,
privileged: value.privileged,
Expand Down Expand Up @@ -207,15 +207,17 @@ pub(crate) mod tests {

use astarte_device_sdk::{AstarteType, DeviceEvent, Value};
use bollard::secret::RestartPolicyNameEnum;
use itertools::Itertools;
use pretty_assertions::assert_eq;
use uuid::Uuid;

use super::*;

pub fn create_container_request_event(
pub fn create_container_request_event<S: Display>(
id: impl Display,
image_id: &str,
image: &str,
network_ids: &[&str],
network_ids: &[S],
) -> DeviceEvent {
let fields = [
("id", AstarteType::String(id.to_string())),
Expand Down Expand Up @@ -250,20 +252,19 @@ pub(crate) mod tests {

#[test]
fn create_container_request() {
let event = create_container_request_event(
"id",
"image_id",
"image",
&["9808bbd5-2e81-4f99-83e7-7cc60623a196"],
);
let id = ReqUuid(Uuid::new_v4());
let image_id = ReqUuid(Uuid::new_v4());
let network_ids = VecReqUuid(vec![ReqUuid(Uuid::new_v4())]);
let event =
create_container_request_event(id, &image_id.to_string(), "image", &network_ids);

let request = CreateContainer::from_event(event).unwrap();

let expect = CreateContainer {
id: "id".to_string(),
image_id: "image_id".to_string(),
network_ids: vec!["9808bbd5-2e81-4f99-83e7-7cc60623a196".to_string()],
volume_ids: vec![],
id,
image_id,
network_ids,
volume_ids: VecReqUuid(vec![]),
image: "image".to_string(),
hostname: "hostname".to_string(),
restart_policy: "no".to_string(),
Expand All @@ -278,12 +279,20 @@ pub(crate) mod tests {

let container = Container::try_from(request).unwrap();

let network_ids = expect
.network_ids
.iter()
.map(|s| s.to_string())
.collect_vec();
let network_ids = network_ids.iter().map(|s| s.as_str()).collect_vec();
let name = id.to_string();

let exp = Container {
id: None,
name: "id",
name: name.as_str(),
image: "image",
network_mode: "bridge",
networks: vec!["9808bbd5-2e81-4f99-83e7-7cc60623a196"],
networks: network_ids,
hostname: Some("hostname"),
restart_policy: RestartPolicyNameEnum::NO,
env: vec!["env"],
Expand Down
19 changes: 10 additions & 9 deletions edgehog-device-runtime-containers/src/requests/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use astarte_device_sdk::{event::FromEventError, types::TypeError, AstarteType, F
use tracing::error;
use uuid::Uuid;

use super::{ReqUuid, VecReqUuid};

/// Request to pull a Docker Deployment.
#[derive(Debug, Clone, FromEvent, PartialEq, Eq, PartialOrd, Ord)]
#[from_event(
Expand All @@ -30,8 +32,8 @@ use uuid::Uuid;
rename_all = "camelCase"
)]
pub struct CreateDeployment {
pub(crate) id: String,
pub(crate) containers: Vec<String>,
pub(crate) id: ReqUuid,
pub(crate) containers: VecReqUuid,
}

/// Command for a previously received deployment
Expand Down Expand Up @@ -150,14 +152,14 @@ struct DeploymentUpdateEvent {
#[cfg(test)]
pub(crate) mod tests {

use std::collections::HashMap;
use std::{collections::HashMap, fmt::Display};

use astarte_device_sdk::{AstarteType, DeviceEvent, Value};
use pretty_assertions::assert_eq;

use super::*;

pub fn create_deployment_request_event(id: &str, containers: &[&str]) -> DeviceEvent {
pub fn create_deployment_request_event<S: Display>(id: &str, containers: &[S]) -> DeviceEvent {
let fields = [
("id", AstarteType::String(id.to_string())),
(
Expand All @@ -178,14 +180,13 @@ pub(crate) mod tests {

#[test]
fn create_deployment_request() {
let event = create_deployment_request_event("id", &["container_id"]);
let id = ReqUuid(Uuid::new_v4());
let containers = VecReqUuid(vec![ReqUuid(Uuid::new_v4())]);
let event = create_deployment_request_event(&id.to_string(), &containers);

let request = CreateDeployment::from_event(event).unwrap();

let expect = CreateDeployment {
id: "id".to_string(),
containers: vec!["container_id".to_string()],
};
let expect = CreateDeployment { id, containers };

assert_eq!(request, expect);
}
Expand Down
25 changes: 21 additions & 4 deletions edgehog-device-runtime-containers/src/requests/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use astarte_device_sdk::FromEvent;

use crate::image::Image;

use super::ReqUuid;

/// Request to pull a Docker Image.
#[derive(Debug, Clone, FromEvent, PartialEq, Eq, PartialOrd, Ord)]
#[from_event(
Expand All @@ -30,7 +32,7 @@ use crate::image::Image;
rename_all = "camelCase"
)]
pub struct CreateImage {
pub(crate) id: String,
pub(crate) id: ReqUuid,
pub(crate) reference: String,
pub(crate) registry_auth: String,
}
Expand All @@ -56,9 +58,22 @@ pub(crate) mod tests {
use std::fmt::Display;

use astarte_device_sdk::{DeviceEvent, Value};
use uuid::Uuid;

use super::*;

pub fn mock_image_req(
id: Uuid,
reference: impl Into<String>,
registry_auth: impl Into<String>,
) -> CreateImage {
CreateImage {
id: ReqUuid(id),
reference: reference.into(),
registry_auth: registry_auth.into(),
}
}

pub fn create_image_request_event(
id: impl Display,
reference: &str,
Expand All @@ -82,12 +97,13 @@ pub(crate) mod tests {

#[test]
fn create_image_request() {
let event = create_image_request_event("id", "reference", "registry_auth");
let id = Uuid::new_v4();
let event = create_image_request_event(id.to_string(), "reference", "registry_auth");

let request = CreateImage::from_event(event).unwrap();

let expect = CreateImage {
id: "id".to_string(),
id: ReqUuid(id),
reference: "reference".to_string(),
registry_auth: "registry_auth".to_string(),
};
Expand All @@ -97,7 +113,8 @@ pub(crate) mod tests {

#[test]
fn should_convert_to_image() {
let event = create_image_request_event("id", "reference", "registry_auth");
let id = Uuid::new_v4();
let event = create_image_request_event(id.to_string(), "reference", "registry_auth");

let request = CreateImage::from_event(event).unwrap();

Expand Down
Loading

0 comments on commit dced497

Please sign in to comment.