1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// Wildland Project
//
// Copyright © 2022 Golem Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as published by
// the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

pub mod rendered_storage;
pub mod storage_manager;
mod template;
pub mod template_factory;
pub mod validator;

use anyhow::Context;
use derivative::Derivative;
use serde::{Deserialize, Serialize};
pub use template::*;
use uuid::Uuid;
pub use validator::*;

/// Storage is basically the same struct as [`super::StorageTemplate`] but it's
/// serializable/deserializable content is filled with values provided by corex
/// for a particular container
///
#[derive(Debug, Clone, Deserialize, Serialize, Eq, Derivative)]
#[derivative(Hash, PartialEq)]
pub struct Storage {
    name: Option<String>,
    uuid: Uuid,
    backend_type: String,
    #[derivative(Hash = "ignore", PartialEq = "ignore")]
    data: serde_json::Value,
    encrypted: bool,
    template_uuid: Option<Uuid>,
}

impl TryFrom<&[u8]> for Storage {
    type Error = anyhow::Error;
    fn try_from(value: &[u8]) -> anyhow::Result<Self> {
        serde_json::from_slice(value).context("Failed to deserialize Storage")
    }
}

impl From<&Storage> for Vec<u8> {
    fn from(value: &Storage) -> Self {
        serde_json::to_vec(&value).unwrap()
    }
}

impl Storage {
    pub fn new(
        uuid: Uuid,
        name: Option<String>,
        backend_type: String,
        data: serde_json::Value,
        encrypted: bool,
        template_uuid: Option<Uuid>,
    ) -> Self {
        Self {
            name,
            uuid,
            backend_type,
            data,
            encrypted,
            template_uuid,
        }
    }

    pub fn name(&self) -> Option<String> {
        self.name.clone()
    }

    pub fn uuid(&self) -> Uuid {
        self.uuid
    }

    pub fn template_uuid(&self) -> Option<Uuid> {
        self.template_uuid
    }

    pub fn backend_type(&self) -> &str {
        self.backend_type.as_str()
    }

    pub fn data(&self) -> serde_json::Value {
        self.data.clone()
    }

    pub fn encrypted(&self) -> bool {
        self.encrypted
    }
}

#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
pub enum StorageAccessMode {
    ReadWrite,
    ReadOnly,
}