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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//
// 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/>.

use std::fmt::{Debug, Display};

use serde::{Deserialize, Serialize};
use tera::{Context, Tera};
use thiserror::Error;
use uuid::Uuid;

use super::rendered_storage::RenderedStorage;
use super::StorageAccessMode;
use crate::catlib_service::entities::ContainerPath;
use crate::catlib_service::error::CatlibError;
use crate::validator::ValidatedTemplateData;
use crate::ErrContext;

pub const CONTAINER_NAME_PARAM: &str = "CONTAINER_NAME";
pub const OWNER_PARAM: &str = "OWNER";
pub const ACCESS_MODE_PARAM: &str = "ACCESS_MODE"; // read-write / readonly
pub const CONTAINER_UUID_PARAM: &str = "CONTAINER_UUID";
pub const PATH_PARAM: &str = "PATH";

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemplateContext {
    #[serde(rename = "CONTAINER_NAME")]
    pub container_name: String,
    #[serde(rename = "OWNER")]
    pub owner: String,
    #[serde(rename = "ACCESS_MODE")]
    pub access_mode: StorageAccessMode,
    #[serde(rename = "CONTAINER_UUID")]
    pub container_uuid: Uuid,
    #[serde(rename = "PATH")]
    pub path: ContainerPath,
}

#[derive(Debug, Error, Clone)]
#[repr(C)]
pub enum StorageTemplateError {
    #[error("Ser/deserialization error: {0}")]
    SerdeErr(String),
    #[error("Template engine error: {0}")]
    TemplateEngineErr(String),
    #[error("Catlib error: {0}: {1}")]
    CatlibErr(String, CatlibError),
}

impl<T> ErrContext<StorageTemplateError, T> for Result<T, serde_json::Error> {
    fn context(self, ctx: impl Display) -> Result<T, StorageTemplateError> {
        self.map_err(|e| StorageTemplateError::SerdeErr(Self::format(e, ctx)))
    }
}
impl<T> ErrContext<StorageTemplateError, T> for Result<T, serde_yaml::Error> {
    fn context(self, ctx: impl Display) -> Result<T, StorageTemplateError> {
        self.map_err(|e| StorageTemplateError::SerdeErr(Self::format(e, ctx)))
    }
}
impl<T> ErrContext<StorageTemplateError, T> for Result<T, tera::Error> {
    fn context(self, ctx: impl Display) -> Result<T, StorageTemplateError> {
        self.map_err(|e| StorageTemplateError::TemplateEngineErr(Self::format(e, ctx)))
    }
}
impl<T> ErrContext<StorageTemplateError, T> for Result<T, CatlibError> {
    fn context(self, ctx: impl Display) -> Result<T, StorageTemplateError> {
        self.map_err(|e| StorageTemplateError::CatlibErr(ctx.to_string(), e))
    }
}

pub const BACKEND_TYPE_KEY: &str = "backend_type";
pub const TEMPLATE_KEY: &str = "template";
pub const VERSION_KEY: &str = "version";
pub const NAME_KEY: &str = "name";

/// Storage Templates provide some general information about storage location. Their only purpose is to be
/// filled with the container's parameters during container creation and to generate Storage Manifest
/// (in opposition to a template it points to the storage location assigned to the particular container).
///
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(tag = "version")]
pub enum StorageTemplate {
    #[serde(rename = "1")]
    V1(StorageTemplateV1),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageTemplateV1 {
    #[serde(skip)]
    catlib_uuid: Option<Uuid>,
    name: Option<String>,
    backend_type: String,
    template: ValidatedTemplateData,
}

impl StorageTemplate {
    /// Creates new `StorageTemplate`
    ///
    /// # Args:
    /// - `backend_type` - string defining backend type
    /// - `template` - validated template specific fields ("template" property of a whole Storage Template object)
    ///
    pub fn new(
        backend_type: impl ToString,
        template: ValidatedTemplateData,
        name: Option<String>,
    ) -> Self {
        Self::V1(StorageTemplateV1 {
            name,
            catlib_uuid: None,
            backend_type: backend_type.to_string(),
            template,
        })
    }

    pub(crate) fn with_catlib_uuid(mut self, uuid: Uuid) -> Self {
        match &mut self {
            StorageTemplate::V1(template) => {
                template.catlib_uuid = Some(uuid);
                self
            }
        }
    }

    pub fn set_catlib_uuid(&mut self, uuid: Uuid) {
        match self {
            StorageTemplate::V1(template) => {
                template.catlib_uuid = Some(uuid);
            }
        }
    }

    pub fn name(&self) -> Option<String> {
        match self {
            StorageTemplate::V1(template) => template.name.clone(),
        }
    }

    pub fn set_name(&mut self, name: String) {
        match self {
            StorageTemplate::V1(template) => template.name = Some(name),
        }
    }

    pub fn to_json(&self) -> Result<String, StorageTemplateError> {
        serde_json::to_string(&self).context("Error while converting template to json")
    }

    pub fn to_yaml(&self) -> Result<String, StorageTemplateError> {
        serde_yaml::to_string(&self).context("Error while converting template to yaml")
    }

    pub fn backend_type(&self) -> String {
        match self {
            StorageTemplate::V1(template) => template.backend_type.clone(),
        }
    }

    /// Returns backend specific data under the `template` property
    pub fn template(&self) -> &serde_json::Value {
        match self {
            StorageTemplate::V1(tv1) => &tv1.template.0,
        }
    }

    /// If returned Some(_) then this template has been written to Catalog Backend which gave it an id.
    /// Otherwise it returns None
    pub fn catlib_uuid(&self) -> Option<Uuid> {
        match self {
            StorageTemplate::V1(template) => template.catlib_uuid,
        }
    }

    pub fn render(&self, params: TemplateContext) -> Result<RenderedStorage, StorageTemplateError> {
        let template_str = serde_json::to_string(&self.template())
            .context("Error deserializing template while rendering")?;
        let filled_template = Tera::one_off(
            &template_str,
            &Context::from_serialize(params)
                .context("Error while deserializing template params")?,
            true,
        )
        .context("Error while filling template with params")?;
        let storage_data: serde_json::Value = serde_json::from_str(&filled_template)
            .context("Deserialize Storage Error while rendering template")?;
        Ok(RenderedStorage {
            name: self.name(),
            backend_type: self.backend_type(),
            data: storage_data,
        })
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::str::FromStr;
    use std::sync::Arc;

    use pretty_assertions::assert_eq;
    use uuid::Uuid;

    use crate::template_factory::TemplateFactory;
    use crate::{
        GenericValidator,
        StorageTemplate,
        StorageTemplateV1,
        TemplateContext,
        ValidatedTemplateData,
    };

    #[test]
    fn parse_generic_json_template() {
        let json_str = serde_json::json!({
            "version": "1",
            "template": {
                "access":
                    [
                        {
                            "user": "*"
                        }
                    ],
                "credentials":
                    {
                        "access-key": "NOT_SO_SECRET",
                        "secret-key": "VERY_SECRET"
                    },
                "manifest-pattern":
                    {
                        "path": "/{path}.yaml",
                        "type": "glob"
                    },
                "read-only": true,
                "s3_url": "s3://michal-afc03a81-307c-4b41-b9dd-771835617900/{{ CONTAINER_UUID  }}",
                "with-index": false
            },
            "backend_type": "s3",
        })
        .to_string();

        let template_factory = TemplateFactory::new(Arc::new(HashMap::from([(
            "s3".into(),
            Box::<GenericValidator<HashMap<String, serde_json::Value>>>::default() as _,
        )])));

        let mut tpl = template_factory
            .storage_template_from_json(json_str.as_bytes())
            .unwrap();

        assert_eq!(tpl.name(), None);

        tpl.set_name("random name".to_string());

        assert_eq!(tpl.name(), Some("random name".to_string()));
    }

    #[test]
    fn parse_generic_yaml_template() {
        let yaml_content = "
            version: '1'
            template:
                access:
                    - user1: '*'
                credentials:
                    access-key: NOT_SO_SECRET
                    secret-key: VERY_SECRET
                manifest-pattern:
                    path: /{path}.yaml
                    type: glob
                read-only: true
                s3_url: s3://michal-afc03a81-307c-4b41-b9dd-771835617900/{{ CONTAINER_UUID  }}
                with-index: false
            backend_type: s3
        ";

        let template_factory = TemplateFactory::new(Arc::new(HashMap::from([(
            "s3".into(),
            Box::<GenericValidator<HashMap<String, serde_json::Value>>>::default() as _,
        )])));
        let mut tpl = template_factory
            .storage_template_from_yaml(yaml_content.as_bytes())
            .unwrap();

        assert_eq!(tpl.name(), None);

        tpl.set_name("random name".to_string());

        assert_eq!(tpl.name(), Some("random name".to_string()));
    }

    #[test]
    fn test_rendering_template() {
        let storage_template = StorageTemplate::new(
            "InvalidTemplate",
            ValidatedTemplateData(
                serde_json::to_value(HashMap::from([
                    (
                        "field1".to_owned(),
                        "Some value with container name: {{ CONTAINER_NAME }}".to_owned(),
                    ),
                    (
                        "parameter in key: {{ OWNER }}".to_owned(),
                        "enum: {{ ACCESS_MODE }}".to_owned(),
                    ),
                    ("uuid".to_owned(), "{{ CONTAINER_UUID }}".to_owned()),
                    ("path".to_owned(), "{{ PATH }}".to_owned()),
                ]))
                .unwrap(),
            ),
            None,
        );

        let params = TemplateContext {
            container_name: "Books".to_owned(),
            owner: "John Doe".to_owned(),
            access_mode: crate::StorageAccessMode::ReadOnly,
            container_uuid: Uuid::from_str("00000000-0000-0000-0000-000000001111").unwrap(),
            path: "path1".into(),
        };

        let rendered_storage = storage_template.render(params).unwrap();

        let expected_storage_toml = r#"backend_type = "InvalidTemplate"

[data]
field1 = "Some value with container name: Books"
"parameter in key: John Doe" = "enum: ReadOnly"
path = "path1"
uuid = "00000000-0000-0000-0000-000000001111"
"#;

        assert_eq!(
            toml::Value::try_from(rendered_storage).unwrap(),
            toml::Value::from_str(expected_storage_toml).unwrap()
        );
    }

    #[test]
    fn test_to_json() {
        let template = StorageTemplate::V1(StorageTemplateV1 {
            catlib_uuid: Some(Uuid::from_u128(1)),
            name: Some("name".into()),
            backend_type: "backend type".into(),
            template: ValidatedTemplateData(
                serde_json::to_value(HashMap::from([("a", "b")])).unwrap(),
            ),
        });

        let json = serde_json::to_value(template).unwrap();
        let expected = serde_json::json!({
            "version": "1",
            "name": "name",
            "backend_type": "backend type",
            "template": {
                "a": "b"
            }
        });

        assert_eq!(json, expected);
    }

    #[test]
    fn test_to_yaml() {
        let template = StorageTemplate::V1(StorageTemplateV1 {
            catlib_uuid: Some(Uuid::from_u128(1)),
            name: Some("name".into()),
            backend_type: "backend type".into(),
            template: ValidatedTemplateData(
                serde_json::to_value(serde_json::to_value(HashMap::from([("a", "b")])).unwrap())
                    .unwrap(),
            ),
        });

        let yaml = serde_yaml::to_value(template).unwrap();
        let expected: serde_yaml::Value = serde_yaml::from_str(
            r#"
            version: '1'
            name: name
            backend_type: 'backend type'
            template:
                a: b
        "#,
        )
        .unwrap();

        assert_eq!(yaml, expected);
    }
}