notificationconfig.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. """
  17. Request/response of PutBucketNotificationConfiguration and
  18. GetBucketNotiicationConfiguration APIs.
  19. """
  20. from __future__ import absolute_import, annotations
  21. from abc import ABC
  22. from dataclasses import dataclass, field
  23. from typing import Optional, Type, TypeVar, cast
  24. from xml.etree import ElementTree as ET
  25. from .xml import Element, SubElement, find, findall, findtext
  26. A = TypeVar("A", bound="FilterRule")
  27. @dataclass(frozen=True)
  28. class FilterRule(ABC):
  29. """Filter rule."""
  30. name: str
  31. value: str
  32. @classmethod
  33. def fromxml(cls: Type[A], element: ET.Element) -> A:
  34. """Create new object with values from XML element."""
  35. name = cast(str, findtext(element, "Name", True))
  36. value = cast(str, findtext(element, "Value", True))
  37. return cls(name, value)
  38. def toxml(self, element: Optional[ET.Element]) -> ET.Element:
  39. """Convert to XML."""
  40. if element is None:
  41. raise ValueError("element must be provided")
  42. element = SubElement(element, "FilterRule")
  43. SubElement(element, "Name", self.name)
  44. SubElement(element, "Value", self.value)
  45. return element
  46. @dataclass(frozen=True)
  47. class PrefixFilterRule(FilterRule):
  48. """Prefix filter rule."""
  49. def __init__(self, value: str):
  50. super().__init__(name="prefix", value=value)
  51. @dataclass(frozen=True)
  52. class SuffixFilterRule(FilterRule):
  53. """Suffix filter rule."""
  54. def __init__(self, value: str):
  55. super().__init__(name="suffix", value=value)
  56. @dataclass(frozen=True)
  57. class CommonConfig(ABC):
  58. """Common for cloud-function/queue/topic configuration."""
  59. events: list[str]
  60. config_id: Optional[str] = None
  61. prefix_filter_rule: Optional[PrefixFilterRule] = None
  62. suffix_filter_rule: Optional[SuffixFilterRule] = None
  63. def __post_init__(self):
  64. if not self.events:
  65. raise ValueError("events must be provided")
  66. @staticmethod
  67. def parsexml(
  68. element: ET.Element,
  69. ) -> tuple[
  70. list[str],
  71. Optional[str],
  72. Optional[PrefixFilterRule],
  73. Optional[SuffixFilterRule],
  74. ]:
  75. """Parse XML."""
  76. elements = findall(element, "Event")
  77. events = []
  78. for tag in elements:
  79. if tag.text is None:
  80. raise ValueError("missing value in XML tag 'Event'")
  81. events.append(tag.text)
  82. config_id = findtext(element, "Id")
  83. elem = find(element, "Filter")
  84. if elem is None:
  85. return events, config_id, None, None
  86. prefix_filter_rule = None
  87. suffix_filter_rule = None
  88. elem = cast(ET.Element, find(elem, "S3Key", True))
  89. elements = findall(elem, "FilterRule")
  90. for tag in elements:
  91. filter_rule = FilterRule.fromxml(tag)
  92. if filter_rule.name == "prefix":
  93. prefix_filter_rule = PrefixFilterRule(filter_rule.value)
  94. else:
  95. suffix_filter_rule = SuffixFilterRule(filter_rule.value)
  96. return events, config_id, prefix_filter_rule, suffix_filter_rule
  97. def toxml(self, element: Optional[ET.Element]) -> ET.Element:
  98. """Convert to XML."""
  99. if element is None:
  100. raise ValueError("element must be provided")
  101. for event in self.events:
  102. SubElement(element, "Event", event)
  103. if self.config_id is not None:
  104. SubElement(element, "Id", self.config_id)
  105. if self.prefix_filter_rule or self.suffix_filter_rule:
  106. rule = SubElement(element, "Filter")
  107. rule = SubElement(rule, "S3Key")
  108. if self.prefix_filter_rule:
  109. self.prefix_filter_rule.toxml(rule)
  110. if self.suffix_filter_rule:
  111. self.suffix_filter_rule.toxml(rule)
  112. return element
  113. B = TypeVar("B", bound="CloudFuncConfig")
  114. @dataclass(frozen=True)
  115. class CloudFuncConfig(CommonConfig):
  116. """Cloud function configuration."""
  117. cloud_func: Optional[str] = None
  118. def __post_init__(self):
  119. if not self.cloud_func:
  120. raise ValueError("cloud function must be provided")
  121. @classmethod
  122. def fromxml(cls: Type[B], element: ET.Element) -> B:
  123. """Create new object with values from XML element."""
  124. cloud_func = cast(str, findtext(element, "CloudFunction", True))
  125. (events, config_id, prefix_filter_rule,
  126. suffix_filter_rule) = cls.parsexml(element)
  127. return cls(
  128. cloud_func=cloud_func,
  129. events=events,
  130. config_id=config_id,
  131. prefix_filter_rule=prefix_filter_rule,
  132. suffix_filter_rule=suffix_filter_rule,
  133. )
  134. def toxml(self, element: Optional[ET.Element]) -> ET.Element:
  135. """Convert to XML."""
  136. if element is None:
  137. raise ValueError("element must be provided")
  138. element = SubElement(element, "CloudFunctionConfiguration")
  139. SubElement(element, "CloudFunction", self.cloud_func)
  140. super().toxml(element)
  141. return element
  142. C = TypeVar("C", bound="QueueConfig")
  143. @dataclass(frozen=True)
  144. class QueueConfig(CommonConfig):
  145. """Queue configuration."""
  146. queue: Optional[str] = None
  147. def __post_init__(self):
  148. if not self.queue:
  149. raise ValueError("queue must be provided")
  150. @classmethod
  151. def fromxml(cls: Type[C], element: ET.Element) -> C:
  152. """Create new object with values from XML element."""
  153. queue = cast(str, findtext(element, "Queue", True))
  154. (events, config_id, prefix_filter_rule,
  155. suffix_filter_rule) = cls.parsexml(element)
  156. return cls(
  157. queue=queue,
  158. events=events,
  159. config_id=config_id,
  160. prefix_filter_rule=prefix_filter_rule,
  161. suffix_filter_rule=suffix_filter_rule,
  162. )
  163. def toxml(self, element: Optional[ET.Element]) -> ET.Element:
  164. """Convert to XML."""
  165. if element is None:
  166. raise ValueError("element must be provided")
  167. element = SubElement(element, "QueueConfiguration")
  168. SubElement(element, "Queue", self.queue)
  169. super().toxml(element)
  170. return element
  171. D = TypeVar("D", bound="TopicConfig")
  172. @dataclass(frozen=True)
  173. class TopicConfig(CommonConfig):
  174. """Get topic configuration."""
  175. topic: Optional[str] = None
  176. def __post_init__(self):
  177. if not self.topic:
  178. raise ValueError("topic must be provided")
  179. @classmethod
  180. def fromxml(cls: Type[D], element: ET.Element) -> D:
  181. """Create new object with values from XML element."""
  182. topic = cast(str, findtext(element, "Topic", True))
  183. (events, config_id, prefix_filter_rule,
  184. suffix_filter_rule) = cls.parsexml(element)
  185. return cls(
  186. topic=topic,
  187. events=events,
  188. config_id=config_id,
  189. prefix_filter_rule=prefix_filter_rule,
  190. suffix_filter_rule=suffix_filter_rule,
  191. )
  192. def toxml(self, element: Optional[ET.Element]) -> ET.Element:
  193. """Convert to XML."""
  194. if element is None:
  195. raise ValueError("element must be provided")
  196. element = SubElement(element, "TopicConfiguration")
  197. SubElement(element, "Topic", self.topic)
  198. super().toxml(element)
  199. return element
  200. E = TypeVar("E", bound="NotificationConfig")
  201. @dataclass(frozen=True)
  202. class NotificationConfig:
  203. """Notification configuration."""
  204. cloud_func_config_list: list[CloudFuncConfig] = field(default_factory=list)
  205. queue_config_list: list[QueueConfig] = field(default_factory=list)
  206. topic_config_list: list[TopicConfig] = field(default_factory=list)
  207. @classmethod
  208. def fromxml(cls: Type[E], element: ET.Element) -> E:
  209. """Create new object with values from XML element."""
  210. elements = findall(element, "CloudFunctionConfiguration")
  211. cloud_func_config_list = []
  212. for tag in elements:
  213. cloud_func_config_list.append(CloudFuncConfig.fromxml(tag))
  214. elements = findall(element, "QueueConfiguration")
  215. queue_config_list = []
  216. for tag in elements:
  217. queue_config_list.append(QueueConfig.fromxml(tag))
  218. elements = findall(element, "TopicConfiguration")
  219. topic_config_list = []
  220. for tag in elements:
  221. topic_config_list.append(TopicConfig.fromxml(tag))
  222. return cls(
  223. cloud_func_config_list, queue_config_list, topic_config_list,
  224. )
  225. def toxml(self, element: Optional[ET.Element]) -> ET.Element:
  226. """Convert to XML."""
  227. element = Element("NotificationConfiguration")
  228. for cloud_func_config in self.cloud_func_config_list:
  229. cloud_func_config.toxml(element)
  230. for queue_config in self.queue_config_list:
  231. queue_config.toxml(element)
  232. for config in self.topic_config_list:
  233. config.toxml(element)
  234. return element