ossify 0.4.0

A modern, easy-to-use, and reqwest-powered Rust SDK for Alibaba Cloud Object Storage Service (OSS)
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
//! Replication rule types shared by `PutBucketReplication`, `GetBucketReplication`
//! and `GetBucketReplicationProgress`.
//!
//! See <https://www.alibabacloud.com/help/en/oss/developer-reference/putbucketreplication>
//! for the authoritative element reference.

use serde::{Deserialize, Deserializer, Serialize};
use serde_with::skip_serializing_none;

/// Replication action(s) propagated to the destination bucket.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum ReplicationAction {
    /// PUT / DELETE / ABORT operations are all replicated.
    #[default]
    #[serde(rename = "ALL")]
    All,
    /// Only write (PUT) operations are replicated.
    #[serde(rename = "PUT")]
    Put,
}

/// Data transfer link used for cross-region replication.
///
/// This type uses a manual `Deserialize` impl instead of the `#[derive]`
/// version because quick-xml 0.39 treats derived enums specially when they
/// appear as the text content of an element (`<Type>oss_acc</Type>`): it tries
/// to interpret the element *tag* (`Type`) as the variant name rather than the
/// text. The manual impl deserializes to a `String` first and then picks the
/// variant.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, Serialize)]
pub enum TransferType {
    /// Default OSS transfer link.
    #[default]
    #[serde(rename = "internal")]
    Internal,
    /// Transfer acceleration (CRR only).
    #[serde(rename = "oss_acc")]
    OssAcc,
}

impl<'de> Deserialize<'de> for TransferType {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        match s.as_str() {
            "internal" => Ok(TransferType::Internal),
            "oss_acc" => Ok(TransferType::OssAcc),
            other => Err(serde::de::Error::custom(format!(
                "unknown TransferType `{other}`, expected `internal` or `oss_acc`"
            ))),
        }
    }
}

/// Whether a replication rule also replicates historical data.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum HistoricalObjectReplication {
    #[default]
    Enabled,
    Disabled,
}

/// Status of a replication rule itself (returned by GetBucketReplication).
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ReplicationRuleStatus {
    /// OSS is preparing the replication tasks.
    Starting,
    /// Replication is in effect.
    Doing,
    /// The rule has been deleted and OSS is cleaning up.
    Closing,
}

/// Enable/disable flag used by RTC and SseKmsEncryptedObjects. OSS uses
/// both lowercase (`enabled` / `disabled`) on the RTC `<Status>` element
/// and Pascal-case (`Enabled` / `Disabled`) on the SSE-KMS `<Status>`
/// element. Response of GetBucketReplication may also return `enabling`
/// for RTC transitions. We model the union conservatively.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum RtcStatus {
    #[serde(alias = "enabled", alias = "Enabled")]
    #[serde(rename = "enabled")]
    Enabled,
    #[default]
    #[serde(alias = "disabled", alias = "Disabled")]
    #[serde(rename = "disabled")]
    Disabled,
    /// Transitional state surfaced only by GetBucketReplication.
    #[serde(alias = "Enabling")]
    #[serde(rename = "enabling")]
    Enabling,
}

/// `<RTC>` container.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Rtc {
    pub status: RtcStatus,
}

/// `<PrefixSet>` container. OSS wraps the list in a `<Prefix>` repetition.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct PrefixSet {
    #[serde(rename = "Prefix", default, skip_serializing_if = "Vec::is_empty")]
    pub prefixes: Vec<String>,
}

/// `<Destination>` container for a replication rule.
#[skip_serializing_none]
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ReplicationDestination {
    pub bucket: String,
    pub location: String,
    pub transfer_type: Option<TransferType>,
}

/// `<SseKmsEncryptedObjects>` container.
#[skip_serializing_none]
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct SseKmsEncryptedObjects {
    pub status: Option<SseKmsStatus>,
}

/// The subset of `RtcStatus` allowed on SseKmsEncryptedObjects: Pascal-case
/// `Enabled` / `Disabled`.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum SseKmsStatus {
    #[default]
    Enabled,
    Disabled,
}

/// `<SourceSelectionCriteria>` container.
#[skip_serializing_none]
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct SourceSelectionCriteria {
    pub sse_kms_encrypted_objects: Option<SseKmsEncryptedObjects>,
}

/// `<EncryptionConfiguration>` container.
#[skip_serializing_none]
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ReplicationEncryptionConfiguration {
    #[serde(rename = "ReplicaKmsKeyID")]
    pub replica_kms_key_id: Option<String>,
}

/// Tag filtering policy (within `<UserTaggings>`).
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum UserTaggingFilterType {
    #[serde(rename = "AND")]
    And,
    #[serde(rename = "OR")]
    Or,
}

/// Single `<UserTagging>` entry.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct UserTagging {
    pub key: String,
    pub value: String,
}

impl UserTagging {
    pub fn new(key: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            key: key.into(),
            value: value.into(),
        }
    }
}

/// `<UserTaggings>` container.
#[skip_serializing_none]
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct UserTaggings {
    pub filter_type: Option<UserTaggingFilterType>,
    #[serde(rename = "UserTagging", default, skip_serializing_if = "Vec::is_empty")]
    pub user_taggings: Vec<UserTagging>,
}

/// Replication progress information (returned by GetBucketReplicationProgress).
#[skip_serializing_none]
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ReplicationProgressInfo {
    /// Percentage of replicated historical data as a string (e.g. "0.85").
    pub historical_object: Option<String>,
    /// GMT timestamp indicating replication cutoff for new objects.
    pub new_object: Option<String>,
}

/// A single `<Rule>` inside `<ReplicationConfiguration>`.
#[skip_serializing_none]
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ReplicationRule {
    #[serde(rename = "ID")]
    pub id: Option<String>,
    #[serde(rename = "RTC")]
    pub rtc: Option<Rtc>,
    pub prefix_set: Option<PrefixSet>,
    pub action: Option<ReplicationAction>,
    pub destination: Option<ReplicationDestination>,
    pub historical_object_replication: Option<HistoricalObjectReplication>,
    pub sync_role: Option<String>,
    pub source_selection_criteria: Option<SourceSelectionCriteria>,
    pub encryption_configuration: Option<ReplicationEncryptionConfiguration>,
    pub user_taggings: Option<UserTaggings>,
    /// Populated by GetBucketReplication only.
    pub status: Option<ReplicationRuleStatus>,
    /// Populated by GetBucketReplicationProgress only.
    pub progress: Option<ReplicationProgressInfo>,
}

/// Root `<ReplicationConfiguration>` element. Supports both
/// PutBucketReplication (request body) and GetBucketReplication (response
/// body).
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename = "ReplicationConfiguration")]
pub struct ReplicationConfiguration {
    #[serde(rename = "Rule", default, skip_serializing_if = "Vec::is_empty")]
    pub rules: Vec<ReplicationRule>,
}

impl ReplicationConfiguration {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_rules(rules: Vec<ReplicationRule>) -> Self {
        Self { rules }
    }
}

fn unwrap_locations<'de, D>(deserializer: D) -> std::result::Result<Vec<String>, D::Error>
where
    D: Deserializer<'de>,
{
    Vec::<String>::deserialize(deserializer)
}

fn unwrap_transfer_types<'de, D>(deserializer: D) -> std::result::Result<Vec<TransferType>, D::Error>
where
    D: Deserializer<'de>,
{
    #[derive(Deserialize)]
    struct Inner {
        #[serde(rename = "Type", default)]
        ty: Vec<TransferType>,
    }
    Ok(Inner::deserialize(deserializer)?.ty)
}

/// `<LocationTransferType>` entry inside `<LocationTransferTypeConstraint>`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct LocationTransferType {
    pub location: String,
    /// Inner `<TransferTypes><Type>...</Type></TransferTypes>` flattened to a
    /// `Vec<TransferType>`.
    #[serde(default, deserialize_with = "unwrap_transfer_types")]
    pub transfer_types: Vec<TransferType>,
}

/// `<LocationTransferTypeConstraint>` container.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct LocationTransferTypeConstraint {
    #[serde(rename = "LocationTransferType", default)]
    pub location_transfer_types: Vec<LocationTransferType>,
}

/// Response body for `GetBucketReplicationLocation` (XML root
/// `<ReplicationLocation>`).
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ReplicationLocation {
    #[serde(rename = "Location", default, deserialize_with = "unwrap_locations")]
    pub locations: Vec<String>,
    pub location_transfer_type_constraint: Option<LocationTransferTypeConstraint>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_full_rule() {
        let xml = r#"<ReplicationConfiguration>
  <Rule>
    <ID>test_replication_1</ID>
    <PrefixSet>
      <Prefix>source1</Prefix>
      <Prefix>video</Prefix>
    </PrefixSet>
    <UserTaggings>
      <FilterType>OR</FilterType>
      <UserTagging><Key>key1</Key><Value>value1</Value></UserTagging>
      <UserTagging><Key>key2</Key><Value>value2</Value></UserTagging>
    </UserTaggings>
    <Action>PUT</Action>
    <Destination>
      <Bucket>destbucket</Bucket>
      <Location>oss-cn-beijing</Location>
      <TransferType>oss_acc</TransferType>
    </Destination>
    <Status>doing</Status>
    <HistoricalObjectReplication>enabled</HistoricalObjectReplication>
    <SyncRole>aliyunramrole</SyncRole>
    <RTC>
      <Status>enabled</Status>
    </RTC>
  </Rule>
</ReplicationConfiguration>"#;
        let parsed: ReplicationConfiguration = quick_xml::de::from_str(xml).unwrap();
        assert_eq!(parsed.rules.len(), 1);
        let rule = &parsed.rules[0];
        assert_eq!(rule.id.as_deref(), Some("test_replication_1"));
        assert_eq!(rule.action, Some(ReplicationAction::Put));
        let dest = rule.destination.as_ref().unwrap();
        assert_eq!(dest.bucket, "destbucket");
        assert_eq!(dest.transfer_type, Some(TransferType::OssAcc));
        assert_eq!(rule.status, Some(ReplicationRuleStatus::Doing));
        assert_eq!(rule.rtc.as_ref().unwrap().status, RtcStatus::Enabled);
        let prefixes = &rule.prefix_set.as_ref().unwrap().prefixes;
        assert_eq!(prefixes, &vec!["source1".to_string(), "video".to_string()]);
        let tags = rule.user_taggings.as_ref().unwrap();
        assert_eq!(tags.filter_type, Some(UserTaggingFilterType::Or));
        assert_eq!(tags.user_taggings.len(), 2);
    }

    #[test]
    fn serialize_minimal_rule_round_trip() {
        let cfg = ReplicationConfiguration::with_rules(vec![ReplicationRule {
            prefix_set: Some(PrefixSet {
                prefixes: vec!["source1".to_string()],
            }),
            action: Some(ReplicationAction::Put),
            destination: Some(ReplicationDestination {
                bucket: "destbucket".to_string(),
                location: "oss-cn-beijing".to_string(),
                transfer_type: Some(TransferType::OssAcc),
            }),
            historical_object_replication: Some(HistoricalObjectReplication::Enabled),
            sync_role: Some("aliyunramrole".to_string()),
            ..Default::default()
        }]);
        let xml = quick_xml::se::to_string(&cfg).unwrap();
        assert!(xml.contains("<ReplicationConfiguration>"));
        assert!(xml.contains("<Action>PUT</Action>"));
        assert!(xml.contains("<TransferType>oss_acc</TransferType>"));
        let back: ReplicationConfiguration = quick_xml::de::from_str(&xml).unwrap();
        assert_eq!(back, cfg);
    }

    #[test]
    fn parse_replication_location() {
        let xml = r#"<ReplicationLocation>
  <Location>oss-cn-beijing</Location>
  <Location>oss-cn-qingdao</Location>
  <LocationTransferTypeConstraint>
    <LocationTransferType>
      <Location>oss-cn-hongkong</Location>
      <TransferTypes><Type>oss_acc</Type></TransferTypes>
    </LocationTransferType>
  </LocationTransferTypeConstraint>
</ReplicationLocation>"#;
        let parsed: ReplicationLocation = quick_xml::de::from_str(xml).unwrap();
        assert_eq!(
            parsed.locations,
            vec!["oss-cn-beijing".to_string(), "oss-cn-qingdao".to_string()]
        );
        let constraint = parsed.location_transfer_type_constraint.unwrap();
        assert_eq!(constraint.location_transfer_types.len(), 1);
        assert_eq!(constraint.location_transfer_types[0].location, "oss-cn-hongkong");
        assert_eq!(constraint.location_transfer_types[0].transfer_types, vec![TransferType::OssAcc]);
    }

    #[test]
    fn parse_progress() {
        let xml = r#"<ReplicationProgress>
  <Rule>
    <ID>test_replication_1</ID>
    <PrefixSet><Prefix>video</Prefix></PrefixSet>
    <Action>PUT</Action>
    <Destination>
      <Bucket>target</Bucket>
      <Location>oss-cn-beijing</Location>
      <TransferType>oss_acc</TransferType>
    </Destination>
    <Status>doing</Status>
    <HistoricalObjectReplication>enabled</HistoricalObjectReplication>
    <Progress>
      <HistoricalObject>0.85</HistoricalObject>
      <NewObject>2015-09-24T15:28:14.000Z</NewObject>
    </Progress>
  </Rule>
</ReplicationProgress>"#;
        // The wrapper type is identical to ReplicationConfiguration (just a
        // different root name). We reuse the same struct here.
        #[derive(Deserialize)]
        #[serde(rename = "ReplicationProgress", rename_all = "PascalCase")]
        struct ReplicationProgress {
            #[serde(rename = "Rule", default)]
            rules: Vec<ReplicationRule>,
        }
        let parsed: ReplicationProgress = quick_xml::de::from_str(xml).unwrap();
        assert_eq!(parsed.rules.len(), 1);
        let progress = parsed.rules[0].progress.as_ref().unwrap();
        assert_eq!(progress.historical_object.as_deref(), Some("0.85"));
    }
}