retention.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # -*- coding: utf-8 -*-
  2. # MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C)
  3. # 2020 MinIO, Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Request/response of PutObjectRetention and GetObjectRetention APIs."""
  17. from __future__ import absolute_import, annotations
  18. from dataclasses import dataclass
  19. from datetime import datetime
  20. from typing import Optional, Type, TypeVar, cast
  21. from xml.etree import ElementTree as ET
  22. from .commonconfig import COMPLIANCE, GOVERNANCE
  23. from .time import from_iso8601utc, to_iso8601utc
  24. from .xml import Element, SubElement, findtext
  25. A = TypeVar("A", bound="Retention")
  26. @dataclass(frozen=True)
  27. class Retention:
  28. """Retention configuration."""
  29. mode: str
  30. retain_until_date: datetime
  31. def __post_init__(self):
  32. if self.mode not in [GOVERNANCE, COMPLIANCE]:
  33. raise ValueError(f"mode must be {GOVERNANCE} or {COMPLIANCE}")
  34. if not isinstance(self.retain_until_date, datetime):
  35. raise ValueError(
  36. "retain until date must be datetime type",
  37. )
  38. @classmethod
  39. def fromxml(cls: Type[A], element: ET.Element) -> A:
  40. """Create new object with values from XML element."""
  41. mode = cast(str, findtext(element, "Mode", True))
  42. retain_until_date = cast(
  43. datetime,
  44. from_iso8601utc(
  45. cast(str, findtext(element, "RetainUntilDate", True)),
  46. ),
  47. )
  48. return cls(mode=mode, retain_until_date=retain_until_date)
  49. def toxml(self, element: Optional[ET.Element]) -> ET.Element:
  50. """Convert to XML."""
  51. element = Element("Retention")
  52. SubElement(element, "Mode", self.mode)
  53. SubElement(
  54. element,
  55. "RetainUntilDate",
  56. to_iso8601utc(self.retain_until_date),
  57. )
  58. return element