versioningconfig.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 PutBucketVersioning and GetBucketVersioning APIs."""
  17. from __future__ import absolute_import, annotations
  18. from dataclasses import dataclass
  19. from typing import List, Optional, Type, TypeVar, Union, cast
  20. from xml.etree import ElementTree as ET
  21. from .commonconfig import DISABLED, ENABLED
  22. from .xml import Element, SubElement, findall, findtext
  23. OFF = "Off"
  24. SUSPENDED = "Suspended"
  25. A = TypeVar("A", bound="VersioningConfig")
  26. @dataclass(frozen=True)
  27. class VersioningConfig:
  28. """Versioning configuration."""
  29. status: Optional[str] = None
  30. mfa_delete: Optional[str] = None
  31. excluded_prefixes: Optional[list[str]] = None
  32. exclude_folders: bool = False
  33. def __post_init__(self):
  34. if self.status is not None and self.status not in [ENABLED, SUSPENDED]:
  35. raise ValueError(f"status must be {ENABLED} or {SUSPENDED}")
  36. if (
  37. self.mfa_delete is not None and
  38. self.mfa_delete not in [ENABLED, DISABLED]
  39. ):
  40. raise ValueError(f"MFA delete must be {ENABLED} or {DISABLED}")
  41. @property
  42. def status_string(self) -> str:
  43. """Convert status to status string. """
  44. return OFF if self.status is None else self.status
  45. @classmethod
  46. def fromxml(cls: Type[A], element: ET.Element) -> A:
  47. """Create new object with values from XML element."""
  48. status = findtext(element, "Status")
  49. mfa_delete = findtext(element, "MFADelete")
  50. excluded_prefixes = [
  51. prefix.text
  52. for prefix in findall(
  53. element,
  54. "ExcludedPrefixes/Prefix",
  55. )
  56. ]
  57. exclude_folders = findtext(element, "ExcludeFolders") == "true"
  58. return cls(
  59. status=status,
  60. mfa_delete=mfa_delete,
  61. excluded_prefixes=cast(Union[List[str], None], excluded_prefixes),
  62. exclude_folders=exclude_folders,
  63. )
  64. def toxml(self, element: Optional[ET.Element]) -> ET.Element:
  65. """Convert to XML."""
  66. element = Element("VersioningConfiguration")
  67. if self.status:
  68. SubElement(element, "Status", self.status)
  69. if self.mfa_delete:
  70. SubElement(element, "MFADelete", self.mfa_delete)
  71. for prefix in self.excluded_prefixes or []:
  72. SubElement(
  73. SubElement(element, "ExcludedPrefixes"),
  74. "Prefix",
  75. prefix,
  76. )
  77. if self.exclude_folders:
  78. SubElement(element, "ExcludeFolders", "true")
  79. return element