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
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C)
# 2020 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Request/response of PutBucketReplication and GetBucketReplication APIs."""

from __future__ import absolute_import

from abc import ABCMeta

from .commonconfig import DISABLED, BaseRule, check_status
from .xml import Element, SubElement, find, findall, findtext


class Status:
    """Status."""
    __metaclass__ = ABCMeta

    def __init__(self, status):
        check_status(status)
        self._status = status

    @property
    def status(self):
        """Get status."""
        return self._status

    @classmethod
    def fromxml(cls, element):
        """Create new object with values from XML element."""
        element = find(element, cls.__name__)
        return cls(findtext(element, "Status", True))

    def toxml(self, element):
        """Convert to XML."""
        element = SubElement(element, self.__class__.__name__)
        SubElement(element, "Status", self._status)
        return element


class SseKmsEncryptedObjects(Status):
    """SSE KMS encrypted objects."""


class SourceSelectionCriteria:
    """Source selection criteria."""

    def __init__(self, sse_kms_encrypted_objects=None):
        self._sse_kms_encrypted_objects = sse_kms_encrypted_objects

    @property
    def sse_kms_encrypted_objects(self):
        """Get SSE KMS encrypted objects."""
        return self._sse_kms_encrypted_objects

    @classmethod
    def fromxml(cls, element):
        """Create new object with values from XML element."""
        element = find(element, "SourceSelectionCriteria")
        return cls(
            None if find(element, "SseKmsEncryptedObjects") is None
            else SseKmsEncryptedObjects.fromxml(element)
        )

    def toxml(self, element):
        """Convert to XML."""
        element = SubElement(element, "SourceSelectionCriteria")
        if self._sse_kms_encrypted_objects:
            self._sse_kms_encrypted_objects.toxml(element)
        return element


class ExistingObjectReplication(Status):
    """Existing object replication."""


class DeleteMarkerReplication(Status):
    """Delete marker replication."""

    def __init__(self, status=DISABLED):
        super().__init__(status)


class ReplicationTimeValue:
    """Replication time value."""
    __metaclass__ = ABCMeta

    def __init__(self, minutes=15):
        self._minutes = minutes

    @property
    def minutes(self):
        """Get minutes."""
        return self._minutes

    @classmethod
    def fromxml(cls, element):
        """Create new object with values from XML element."""
        element = find(element, cls.__name__)
        minutes = findtext(element, "Minutes")
        if minutes is not None:
            minutes = int(minutes)
        return cls(minutes)

    def toxml(self, element):
        """Convert to XML."""
        element = SubElement(element, self.__class__.__name__)
        if self._minutes is not None:
            SubElement(element, "Minutes", str(self._minutes))
        return element


class Time(ReplicationTimeValue):
    """Time."""


class ReplicationTime:
    """Replication time."""

    def __init__(self, time, status):
        if not time:
            raise ValueError("time must be provided")
        check_status(status)
        self._time = time
        self._status = status

    @property
    def time(self):
        """Get time value."""
        return self._time

    @property
    def status(self):
        """Get status."""
        return self._status

    @classmethod
    def fromxml(cls, element):
        """Create new object with values from XML element."""
        element = find(element, "ReplicationTime")
        time = Time.fromxml(element)
        status = findtext(element, "Status", True)
        return cls(time, status)

    def toxml(self, element):
        """Convert to XML."""
        element = SubElement(element, "ReplicationTime")
        self._time.toxml(element)
        SubElement(element, "Status", self._status)
        return element


class EventThreshold(ReplicationTimeValue):
    """Event threshold."""


class Metrics:
    """Metrics."""

    def __init__(self, event_threshold, status):
        if not event_threshold:
            raise ValueError("event threshold must be provided")
        check_status(status)
        self._event_threshold = event_threshold
        self._status = status

    @property
    def event_threshold(self):
        """Get event threshold."""
        return self._event_threshold

    @property
    def status(self):
        """Get status."""
        return self._status

    @classmethod
    def fromxml(cls, element):
        """Create new object with values from XML element."""
        element = find(element, "Metrics")
        event_threshold = EventThreshold.fromxml(element)
        status = findtext(element, "Status", True)
        return cls(event_threshold, status)

    def toxml(self, element):
        """Convert to XML."""
        element = SubElement(element, "Metrics")
        self._event_threshold.toxml(element)
        SubElement(element, "Status", self._status)
        return element


class EncryptionConfig:
    """Encryption configuration."""

    def __init__(self, replica_kms_key_id=None):
        self._replica_kms_key_id = replica_kms_key_id

    @property
    def replica_kms_key_id(self):
        """Get replica KMS key ID."""
        return self._replica_kms_key_id

    @classmethod
    def fromxml(cls, element):
        """Create new object with values from XML element."""
        element = find(element, "EncryptionConfiguration")
        return cls(findtext(element, "ReplicaKmsKeyID"))

    def toxml(self, element):
        """Convert to XML."""
        element = SubElement(element, "EncryptionConfiguration")
        SubElement(element, "ReplicaKmsKeyID", self._replica_kms_key_id)
        return element


class AccessControlTranslation:
    """Access control translation."""

    def __init__(self, owner="Destination"):
        if not owner:
            raise ValueError("owner must be provided")
        self._owner = owner

    @property
    def owner(self):
        """Get owner."""
        return self._owner

    @classmethod
    def fromxml(cls, element):
        """Create new object with values from XML element."""
        element = find(element, "AccessControlTranslation")
        return cls(findtext(element, "Owner"))

    def toxml(self, element):
        """Convert to XML."""
        element = SubElement(element, "AccessControlTranslation")
        SubElement(element, "Owner", self._owner)
        return element


class Destination:
    """Replication destination."""

    def __init__(self, bucket_arn,
                 access_control_translation=None, account=None,
                 encryption_config=None, metrics=None,
                 replication_time=None, storage_class=None):
        if not bucket_arn:
            raise ValueError("bucket ARN must be provided")
        self._bucket_arn = bucket_arn
        self._access_control_translation = access_control_translation
        self._account = account
        self._encryption_config = encryption_config
        self._metrics = metrics
        self._replication_time = replication_time
        self._storage_class = storage_class

    @property
    def bucket_arn(self):
        """Get bucket ARN."""
        return self._bucket_arn

    @property
    def access_control_translation(self):
        """Get access control translation. """
        return self._access_control_translation

    @property
    def account(self):
        """Get account."""
        return self._account

    @property
    def encryption_config(self):
        """Get encryption configuration."""
        return self._encryption_config

    @property
    def metrics(self):
        """Get metrics."""
        return self._metrics

    @property
    def replication_time(self):
        """Get replication time."""
        return self._replication_time

    @property
    def storage_class(self):
        """Get storage class."""
        return self._storage_class

    @classmethod
    def fromxml(cls, element):
        """Create new object with values from XML element."""
        element = find(element, "Destination")
        access_control_translation = (
            None if find(element, "AccessControlTranslation") is None
            else AccessControlTranslation.fromxml(element)
        )
        account = findtext(element, "Account")
        bucket_arn = findtext(element, "Bucket", True)
        encryption_config = (
            None if find(element, "EncryptionConfiguration") is None
            else EncryptionConfig.fromxml(element)
        )
        metrics = (
            None if find(element, "Metrics") is None
            else Metrics.fromxml(element)
        )
        replication_time = (
            None if find(element, "ReplicationTime") is None
            else ReplicationTime.fromxml(element)
        )
        storage_class = findtext(element, "StorageClass")
        return cls(bucket_arn, access_control_translation, account,
                   encryption_config, metrics, replication_time, storage_class)

    def toxml(self, element):
        """Convert to XML."""
        element = SubElement(element, "Destination")
        if self._access_control_translation:
            self._access_control_translation.toxml(element)
        if self._account is not None:
            SubElement(element, "Account", self._account)
        SubElement(element, "Bucket", self._bucket_arn)
        if self._encryption_config:
            self._encryption_config.toxml(element)
        if self._metrics:
            self._metrics.toxml(element)
        if self._replication_time:
            self._replication_time.toxml(element)
        if self._storage_class:
            SubElement(element, "StorageClass", self._storage_class)
        return element


class Rule(BaseRule):
    """Replication rule. """

    def __init__(self, destination, status,
                 delete_marker_replication=None,
                 existing_object_replication=None,
                 rule_filter=None, rule_id=None, prefix=None,
                 priority=None, source_selection_criteria=None):
        if not destination:
            raise ValueError("destination must be provided")

        check_status(status)

        super().__init__(rule_filter, rule_id)

        self._destination = destination
        self._status = status
        if rule_filter and not delete_marker_replication:
            delete_marker_replication = DeleteMarkerReplication()
        self._delete_marker_replication = delete_marker_replication
        self._existing_object_replication = existing_object_replication
        self._prefix = prefix
        self._priority = priority
        self._source_selection_criteria = source_selection_criteria

    @property
    def destination(self):
        """Get destination."""
        return self._destination

    @property
    def status(self):
        """Get status."""
        return self._status

    @property
    def delete_marker_replication(self):
        """Get delete marker replication."""
        return self._delete_marker_replication

    @property
    def existing_object_replication(self):
        """Get existing object replication."""
        return self._existing_object_replication

    @property
    def prefix(self):
        """Get prefix."""
        return self._prefix

    @property
    def priority(self):
        """Get priority."""
        return self._priority

    @property
    def source_selection_criteria(self):
        """Get source selection criteria."""
        return self._source_selection_criteria

    @classmethod
    def fromxml(cls, element):
        """Create new object with values from XML element."""
        delete_marker_replication = (
            None if find(element, "DeleteMarkerReplication") is None
            else DeleteMarkerReplication.fromxml(element)
        )
        destination = Destination.fromxml(element)
        existing_object_replication = (
            None if find(element, "ExistingObjectReplication") is None
            else ExistingObjectReplication.fromxml(element)
        )
        rule_filter, rule_id = cls.parsexml(element)
        prefix = findtext(element, "Prefix")
        priority = findtext(element, "Priority")
        if priority:
            priority = int(priority)
        source_selection_criteria = (
            None if find(element, "SourceSelectionCriteria") is None
            else SourceSelectionCriteria.fromxml(element)
        )
        status = findtext(element, "Status", True)

        return cls(destination, status, delete_marker_replication,
                   existing_object_replication, rule_filter,
                   rule_id, prefix, priority, source_selection_criteria)

    def toxml(self, element):
        """Convert to XML."""
        element = SubElement(element, "Rule")
        if self._delete_marker_replication:
            self._delete_marker_replication.toxml(element)
        self._destination.toxml(element)
        if self._existing_object_replication:
            self._existing_object_replication.toxml(element)
        super().toxml(element)
        if self._prefix is not None:
            SubElement(element, "Prefix", self._prefix)
        if self._priority is not None:
            SubElement(element, "Priority", str(self._priority))
        if self._source_selection_criteria:
            self._source_selection_criteria.toxml(element)
        SubElement(element, "Status", self._status)
        return element


class ReplicationConfig:
    """Replication configuration."""

    def __init__(self, role, rules):
        if not role:
            raise ValueError("role must be provided")
        if not rules:
            raise ValueError("rules must be provided")
        if len(rules) > 1000:
            raise ValueError("more than 1000 rules are not supported")
        self._role = role
        self._rules = rules

    @property
    def role(self):
        """Get role."""
        return self._role

    @property
    def rules(self):
        """Get rules."""
        return self._rules

    @classmethod
    def fromxml(cls, element):
        """Create new object with values from XML element."""
        role = findtext(element, "Role", True)
        elements = findall(element, "Rule")
        rules = []
        for tag in elements:
            rules.append(Rule.fromxml(tag))
        return cls(role, rules)

    def toxml(self, element):
        """Convert to XML."""
        element = Element("ReplicationConfiguration")
        SubElement(element, "Role", self._role)
        for rule in self._rules:
            rule.toxml(element)
        return element