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
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// Wildland Project
//
// Copyright © 2023 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/>.

use derivative::Derivative;
use thiserror::Error;
use uuid::Uuid;
use wildland_corex::{CoreXError, ErrContext};
use wildland_dfs::{DfsFrontendError, SpaceUsage};

use super::cargo_lib::DfsApi;

#[derive(Derivative, Clone)]
#[derivative(Debug)]
pub struct ContainerStorage {
    corex_storage: wildland_corex::Storage,

    #[derivative(Debug = "ignore")]
    dfs_api: DfsApi,
}

#[derive(Debug, Clone, Error)]
pub enum QuotaControlError {
    #[error("{0}: {1}")]
    CoreXError(String, CoreXError),
    #[error("{0}: {1}")]
    DfsFrontendError(String, DfsFrontendError),
}

impl<T> ErrContext<QuotaControlError, T> for Result<T, CoreXError> {
    fn context(self, ctx: impl std::fmt::Display) -> Result<T, QuotaControlError> {
        self.map_err(|e| QuotaControlError::CoreXError(ctx.to_string(), e))
    }
}
impl<T> ErrContext<QuotaControlError, T> for Result<T, DfsFrontendError> {
    fn context(self, ctx: impl std::fmt::Display) -> Result<T, QuotaControlError> {
        self.map_err(|e| QuotaControlError::DfsFrontendError(ctx.to_string(), e))
    }
}

impl ContainerStorage {
    pub fn from_corex_storage(corex_storage: wildland_corex::Storage, dfs_api: DfsApi) -> Self {
        Self {
            corex_storage,
            dfs_api,
        }
    }

    //
    // Methods calling DFS
    //

    /// Returns (used, total) space in bytes
    ///
    pub fn get_space_usage(&self) -> Result<SpaceUsage, QuotaControlError> {
        self.dfs_api
            .get_space_usage(&self.corex_storage)
            .context(format!(
                "Space usage check failed for storage: uuid: {}, type: {}",
                self.corex_storage.uuid(),
                self.corex_storage.backend_type()
            ))
    }

    /// Checks whether the Storage is accessible
    ///
    pub fn is_accessible(&self) -> Result<bool, CoreXError> {
        Ok(self
            .dfs_api
            .is_accessible(&self.corex_storage)
            .map_err(|e| tracing::error!("Storage inaccessible: {e}"))
            .unwrap_or(false))
    }

    //
    // Delegations to corex methods
    //

    /// `uuid` attribute getter
    ///
    pub fn uuid(&self) -> Uuid {
        self.corex_storage.uuid()
    }

    /// `template_uuid` attribute getter
    ///
    pub fn template_uuid(&self) -> Option<Uuid> {
        self.corex_storage.template_uuid()
    }

    /// `data` attribute getter
    ///
    pub fn data(&self) -> Vec<u8> {
        serde_json::to_vec(&self.corex_storage.data()).unwrap()
    }

    /// `name` attribute getter
    ///
    pub fn name(&self) -> Option<String> {
        self.corex_storage.name()
    }

    /// `backend_type` attribute getter
    ///
    pub fn backend_type(&self) -> String {
        self.corex_storage.backend_type().to_owned()
    }
}