minio 0.2.0-alpha

MinIO SDK for Amazon S3 compatible object storage access
Documentation
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use crate::s3::error::Error;
use crate::s3::types::Filter;
use crate::s3::utils::to_iso8601utc;
use xmltree::Element;

#[derive(PartialEq, Clone, Debug, Default)]
/// Lifecycle configuration
pub struct LifecycleConfig {
    pub rules: Vec<LifecycleRule>,
}

impl LifecycleConfig {
    pub fn from_xml(root: &Element) -> Result<LifecycleConfig, Error> {
        let mut config = LifecycleConfig { rules: Vec::new() };

        // Process all Rule elements in the XML
        for rule_elem in root.children.iter().filter_map(|c| c.as_element()) {
            if rule_elem.name == "Rule" {
                config.rules.push(LifecycleRule::from_xml(rule_elem)?);
            }
        }

        Ok(config)
    }

    pub fn validate(&self) -> Result<(), Error> {
        // Skip validation if empty
        if self.rules.is_empty() {
            return Ok(());
        }

        for rule in &self.rules {
            rule.validate()?;
        }

        Ok(())
    }

    pub fn empty(&self) -> bool {
        self.rules.is_empty()
    }

    pub fn to_xml(&self) -> String {
        let mut data = String::from("<LifecycleConfiguration>");

        for rule in &self.rules {
            data.push_str("<Rule>");

            // ID should come earlier in XML based on Go ordering
            if !rule.id.is_empty() {
                data.push_str("<ID>");
                data.push_str(&rule.id);
                data.push_str("</ID>");
            }

            // Status comes next
            data.push_str("<Status>");
            if rule.status {
                data.push_str("Enabled");
            } else {
                data.push_str("Disabled");
            }
            data.push_str("</Status>");

            // Filter
            data.push_str(&rule.filter.to_xml());

            // AbortIncompleteMultipartUpload
            if let Some(days) = rule.abort_incomplete_multipart_upload_days_after_initiation {
                data.push_str("<AbortIncompleteMultipartUpload><DaysAfterInitiation>");
                data.push_str(&days.to_string());
                data.push_str("</DaysAfterInitiation></AbortIncompleteMultipartUpload>");
            }

            // Expiration
            let has_expiration = rule.expiration_date.is_some()
                || rule.expiration_days.is_some()
                || rule.expiration_expired_object_delete_marker.is_some()
                || rule.expiration_expired_object_all_versions.is_some();

            if has_expiration {
                data.push_str("<Expiration>");
                if let Some(date) = rule.expiration_date {
                    data.push_str("<Date>");
                    data.push_str(&to_iso8601utc(date));
                    data.push_str("</Date>");
                }
                if let Some(days) = rule.expiration_days {
                    data.push_str("<Days>");
                    data.push_str(&days.to_string());
                    data.push_str("</Days>");
                }
                if let Some(delete_marker) = rule.expiration_expired_object_delete_marker {
                    if delete_marker {
                        data.push_str(
                            "<ExpiredObjectDeleteMarker>true</ExpiredObjectDeleteMarker>",
                        );
                    }
                }
                if let Some(delete_all) = rule.expiration_expired_object_all_versions {
                    if delete_all {
                        data.push_str("<ExpiredObjectAllVersions>true</ExpiredObjectAllVersions>");
                    }
                }
                data.push_str("</Expiration>");
            }

            // DelMarkerExpiration
            if let Some(days) = rule.del_marker_expiration_days {
                data.push_str("<DelMarkerExpiration><Days>");
                data.push_str(&days.to_string());
                data.push_str("</Days></DelMarkerExpiration>");
            }

            // AllVersionsExpiration
            if let Some(days) = rule.all_versions_expiration_days {
                data.push_str("<AllVersionsExpiration><Days>");
                data.push_str(&days.to_string());
                data.push_str("</Days>");

                if let Some(delete_marker) = rule.all_versions_expiration_delete_marker {
                    if delete_marker {
                        data.push_str("<DeleteMarker>true</DeleteMarker>");
                    }
                }

                data.push_str("</AllVersionsExpiration>");
            }

            // NoncurrentVersionExpiration
            if let Some(days) = rule.noncurrent_version_expiration_noncurrent_days {
                data.push_str("<NoncurrentVersionExpiration><NoncurrentDays>");
                data.push_str(&days.to_string());
                data.push_str("</NoncurrentDays>");

                if let Some(versions) = rule.noncurrent_version_expiration_newer_versions {
                    data.push_str("<NewerNoncurrentVersions>");
                    data.push_str(&versions.to_string());
                    data.push_str("</NewerNoncurrentVersions>");
                }

                data.push_str("</NoncurrentVersionExpiration>");
            }

            // NoncurrentVersionTransition
            let has_noncurrent_transition =
                rule.noncurrent_version_transition_noncurrent_days.is_some()
                    || rule.noncurrent_version_transition_storage_class.is_some()
                    || rule.noncurrent_version_transition_newer_versions.is_some();

            if has_noncurrent_transition {
                data.push_str("<NoncurrentVersionTransition>");

                if let Some(days) = rule.noncurrent_version_transition_noncurrent_days {
                    data.push_str("<NoncurrentDays>");
                    data.push_str(&days.to_string());
                    data.push_str("</NoncurrentDays>");
                }

                if let Some(storage_class) = &rule.noncurrent_version_transition_storage_class {
                    data.push_str("<StorageClass>");
                    data.push_str(storage_class);
                    data.push_str("</StorageClass>");
                }

                if let Some(versions) = rule.noncurrent_version_transition_newer_versions {
                    data.push_str("<NewerNoncurrentVersions>");
                    data.push_str(&versions.to_string());
                    data.push_str("</NewerNoncurrentVersions>");
                }

                data.push_str("</NoncurrentVersionTransition>");
            }

            // Transition
            let has_transition = rule.transition_date.is_some()
                || rule.transition_days.is_some()
                || rule.transition_storage_class.is_some();

            if has_transition {
                data.push_str("<Transition>");

                if let Some(date) = rule.transition_date {
                    data.push_str("<Date>");
                    data.push_str(&to_iso8601utc(date));
                    data.push_str("</Date>");
                }

                if let Some(days) = rule.transition_days {
                    data.push_str("<Days>");
                    data.push_str(&days.to_string());
                    data.push_str("</Days>");
                }

                if let Some(storage_class) = &rule.transition_storage_class {
                    data.push_str("<StorageClass>");
                    data.push_str(storage_class);
                    data.push_str("</StorageClass>");
                }

                data.push_str("</Transition>");
            }

            data.push_str("</Rule>");
        }

        data.push_str("</LifecycleConfiguration>");
        data
    }
}

#[derive(PartialEq, Clone, Debug, Default)]
pub struct LifecycleRule {
    // Common
    pub id: String,
    pub status: bool,
    pub filter: Filter,

    // Expiration
    pub expiration_days: Option<u32>,
    pub expiration_date: Option<chrono::DateTime<chrono::Utc>>,
    pub expiration_expired_object_delete_marker: Option<bool>,
    pub expiration_expired_object_all_versions: Option<bool>,

    // DelMarkerExpiration
    pub del_marker_expiration_days: Option<u32>,

    // AllVersionsExpiration
    pub all_versions_expiration_days: Option<u32>,
    pub all_versions_expiration_delete_marker: Option<bool>,

    // Transition
    pub transition_days: Option<u32>,
    pub transition_date: Option<chrono::DateTime<chrono::Utc>>,
    pub transition_storage_class: Option<String>,

    // NoncurrentVersionExpiration
    pub noncurrent_version_expiration_noncurrent_days: Option<u32>,
    pub noncurrent_version_expiration_newer_versions: Option<u32>,

    // NoncurrentVersionTransition
    pub noncurrent_version_transition_noncurrent_days: Option<u32>,
    pub noncurrent_version_transition_storage_class: Option<String>,
    pub noncurrent_version_transition_newer_versions: Option<u32>,

    // AbortIncompleteMultipartUpload
    pub abort_incomplete_multipart_upload_days_after_initiation: Option<u32>,
}

impl LifecycleRule {
    pub fn from_xml(rule_elem: &Element) -> Result<Self, Error> {
        let mut rule = LifecycleRule::default();

        // Parse ID
        if let Some(id_elem) = rule_elem.get_child("ID") {
            if let Some(id_text) = id_elem.get_text() {
                rule.id = id_text.to_string();
            }
        }

        // Parse Status
        if let Some(status_elem) = rule_elem.get_child("Status") {
            if let Some(status_text) = status_elem.get_text() {
                rule.status = status_text == "Enabled";
            }
        } else {
            return Err(Error::XmlError("Missing <Status> element".to_string()));
        }

        // Parse Filter
        if let Some(filter_elem) = rule_elem.get_child("Filter") {
            rule.filter = Filter::from_xml(filter_elem)?;
        }

        // Parse AbortIncompleteMultipartUpload
        if let Some(abort_elem) = rule_elem.get_child("AbortIncompleteMultipartUpload") {
            if let Some(days_elem) = abort_elem.get_child("DaysAfterInitiation") {
                if let Some(days_text) = days_elem.get_text() {
                    rule.abort_incomplete_multipart_upload_days_after_initiation =
                        Some(days_text.parse().map_err(|_| {
                            Error::XmlError("Invalid DaysAfterInitiation value".to_string())
                        })?);
                }
            }
        }

        // Parse Expiration
        if let Some(expiration_elem) = rule_elem.get_child("Expiration") {
            // Date
            if let Some(date_elem) = expiration_elem.get_child("Date") {
                if let Some(date_text) = date_elem.get_text() {
                    // Assume a function that parses ISO8601 to DateTime<Utc>
                    rule.expiration_date = Some(parse_iso8601(&date_text)?);
                }
            }

            // Days
            if let Some(days_elem) = expiration_elem.get_child("Days") {
                if let Some(days_text) = days_elem.get_text() {
                    rule.expiration_days = Some(days_text.parse().map_err(|_| {
                        Error::XmlError("Invalid Expiration Days value".to_string())
                    })?);
                }
            }

            // ExpiredObjectDeleteMarker
            if let Some(delete_marker_elem) = expiration_elem.get_child("ExpiredObjectDeleteMarker")
            {
                if let Some(delete_marker_text) = delete_marker_elem.get_text() {
                    rule.expiration_expired_object_delete_marker =
                        Some(delete_marker_text == "true");
                }
            }

            // ExpiredObjectAllVersions
            if let Some(all_versions_elem) = expiration_elem.get_child("ExpiredObjectAllVersions") {
                if let Some(all_versions_text) = all_versions_elem.get_text() {
                    rule.expiration_expired_object_all_versions = Some(all_versions_text == "true");
                }
            }
        }

        // Parse DelMarkerExpiration
        if let Some(del_marker_elem) = rule_elem.get_child("DelMarkerExpiration") {
            if let Some(days_elem) = del_marker_elem.get_child("Days") {
                if let Some(days_text) = days_elem.get_text() {
                    rule.del_marker_expiration_days = Some(days_text.parse().map_err(|_| {
                        Error::XmlError("Invalid DelMarkerExpiration Days value".to_string())
                    })?);
                }
            }
        }

        // Parse AllVersionsExpiration
        if let Some(all_versions_elem) = rule_elem.get_child("AllVersionsExpiration") {
            if let Some(days_elem) = all_versions_elem.get_child("Days") {
                if let Some(days_text) = days_elem.get_text() {
                    rule.all_versions_expiration_days = Some(days_text.parse().map_err(|_| {
                        Error::XmlError("Invalid AllVersionsExpiration Days value".to_string())
                    })?);
                }
            }

            if let Some(delete_marker_elem) = all_versions_elem.get_child("DeleteMarker") {
                if let Some(delete_marker_text) = delete_marker_elem.get_text() {
                    rule.all_versions_expiration_delete_marker = Some(delete_marker_text == "true");
                }
            }
        }

        // Parse NoncurrentVersionExpiration
        if let Some(noncurrent_exp_elem) = rule_elem.get_child("NoncurrentVersionExpiration") {
            if let Some(days_elem) = noncurrent_exp_elem.get_child("NoncurrentDays") {
                if let Some(days_text) = days_elem.get_text() {
                    rule.noncurrent_version_expiration_noncurrent_days =
                        Some(days_text.parse().map_err(|_| {
                            Error::XmlError(
                                "Invalid NoncurrentVersionExpiration NoncurrentDays value"
                                    .to_string(),
                            )
                        })?);
                }
            }

            if let Some(versions_elem) = noncurrent_exp_elem.get_child("NewerNoncurrentVersions") {
                if let Some(versions_text) = versions_elem.get_text() {
                    rule.noncurrent_version_expiration_newer_versions =
                        Some(versions_text.parse().map_err(|_| {
                            Error::XmlError("Invalid NewerNoncurrentVersions value".to_string())
                        })?);
                }
            }
        }

        // Parse NoncurrentVersionTransition
        if let Some(noncurrent_trans_elem) = rule_elem.get_child("NoncurrentVersionTransition") {
            if let Some(days_elem) = noncurrent_trans_elem.get_child("NoncurrentDays") {
                if let Some(days_text) = days_elem.get_text() {
                    rule.noncurrent_version_transition_noncurrent_days =
                        Some(days_text.parse().map_err(|_| {
                            Error::XmlError(
                                "Invalid NoncurrentVersionTransition NoncurrentDays value"
                                    .to_string(),
                            )
                        })?);
                }
            }

            if let Some(storage_elem) = noncurrent_trans_elem.get_child("StorageClass") {
                if let Some(storage_text) = storage_elem.get_text() {
                    rule.noncurrent_version_transition_storage_class =
                        Some(storage_text.to_string());
                }
            }

            if let Some(versions_elem) = noncurrent_trans_elem.get_child("NewerNoncurrentVersions")
            {
                if let Some(versions_text) = versions_elem.get_text() {
                    rule.noncurrent_version_transition_newer_versions =
                        Some(versions_text.parse().map_err(|_| {
                            Error::XmlError("Invalid NewerNoncurrentVersions value".to_string())
                        })?);
                }
            }
        }

        // Parse Transition
        if let Some(transition_elem) = rule_elem.get_child("Transition") {
            // Date
            if let Some(date_elem) = transition_elem.get_child("Date") {
                if let Some(date_text) = date_elem.get_text() {
                    rule.transition_date = Some(parse_iso8601(&date_text)?);
                }
            }

            // Days
            if let Some(days_elem) = transition_elem.get_child("Days") {
                if let Some(days_text) = days_elem.get_text() {
                    rule.transition_days = Some(days_text.parse().map_err(|_| {
                        Error::XmlError("Invalid Transition Days value".to_string())
                    })?);
                }
            }

            // StorageClass
            if let Some(storage_elem) = transition_elem.get_child("StorageClass") {
                if let Some(storage_text) = storage_elem.get_text() {
                    rule.transition_storage_class = Some(storage_text.to_string());
                }
            }
        }

        Ok(rule)
    }

    pub fn validate(&self) -> Result<(), Error> {
        // Basic validation requirements

        // Ensure ID is present
        if self.id.is_empty() {
            return Err(Error::XmlError("Rule ID cannot be empty".to_string()));
        }

        // Validate storage classes in transitions
        if let Some(storage_class) = &self.transition_storage_class {
            if storage_class.is_empty() {
                return Err(Error::XmlError(
                    "Transition StorageClass cannot be empty".to_string(),
                ));
            }
        }

        if let Some(storage_class) = &self.noncurrent_version_transition_storage_class {
            if storage_class.is_empty() {
                return Err(Error::XmlError(
                    "NoncurrentVersionTransition StorageClass cannot be empty".to_string(),
                ));
            }
        }

        // Check that expiration has either days or date, not both
        if self.expiration_days.is_some() && self.expiration_date.is_some() {
            return Err(Error::XmlError(
                "Expiration cannot specify both Days and Date".to_string(),
            ));
        }

        // Check that transition has either days or date, not both
        if self.transition_days.is_some() && self.transition_date.is_some() {
            return Err(Error::XmlError(
                "Transition cannot specify both Days and Date".to_string(),
            ));
        }

        Ok(())
    }
}

// Helper function to parse ISO8601 dates
fn parse_iso8601(date_str: &str) -> Result<chrono::DateTime<chrono::Utc>, Error> {
    chrono::DateTime::parse_from_rfc3339(date_str)
        .map(|dt| dt.with_timezone(&chrono::Utc))
        .map_err(|_| Error::XmlError(format!("Invalid date format: {}", date_str)))
}