|
28 | 28 | ServiceBusSender,
|
29 | 29 | )
|
30 | 30 | from azure.servicebus.management import (
|
| 31 | + AuthorizationRule, |
31 | 32 | CorrelationRuleFilter,
|
32 | 33 | QueueProperties,
|
33 | 34 | ServiceBusAdministrationClient,
|
@@ -194,6 +195,93 @@ def delete_queue(self, queue_name: str) -> None:
|
194 | 195 | with self.get_conn() as service_mgmt_conn:
|
195 | 196 | service_mgmt_conn.delete_queue(queue_name)
|
196 | 197 |
|
| 198 | + def create_topic( |
| 199 | + self, |
| 200 | + topic_name: str, |
| 201 | + azure_service_bus_conn_id: str = "azure_service_bus_default", |
| 202 | + default_message_time_to_live: datetime.timedelta | str | None = None, |
| 203 | + max_size_in_megabytes: int | None = None, |
| 204 | + requires_duplicate_detection: bool | None = None, |
| 205 | + duplicate_detection_history_time_window: datetime.timedelta | str | None = None, |
| 206 | + enable_batched_operations: bool | None = None, |
| 207 | + size_in_bytes: int | None = None, |
| 208 | + filtering_messages_before_publishing: bool | None = None, |
| 209 | + authorization_rules: list[AuthorizationRule] | None = None, |
| 210 | + support_ordering: bool | None = None, |
| 211 | + auto_delete_on_idle: datetime.timedelta | str | None = None, |
| 212 | + enable_partitioning: bool | None = None, |
| 213 | + enable_express: bool | None = None, |
| 214 | + user_metadata: str | None = None, |
| 215 | + max_message_size_in_kilobytes: int | None = None, |
| 216 | + ) -> str: |
| 217 | + """ |
| 218 | + Create a topic by connecting to service Bus Admin client. |
| 219 | +
|
| 220 | + :param topic_name: Name of the topic. |
| 221 | + :param default_message_time_to_live: ISO 8601 default message time span to live value. This is |
| 222 | + the duration after which the message expires, starting from when the message is sent to Service |
| 223 | + Bus. This is the default value used when TimeToLive is not set on a message itself. |
| 224 | + Input value of either type ~datetime.timedelta or string in ISO 8601 duration format |
| 225 | + like "PT300S" is accepted. |
| 226 | + :param max_size_in_megabytes: The maximum size of the topic in megabytes, which is the size of |
| 227 | + memory allocated for the topic. |
| 228 | + :param requires_duplicate_detection: A value indicating if this topic requires duplicate |
| 229 | + detection. |
| 230 | + :param duplicate_detection_history_time_window: ISO 8601 time span structure that defines the |
| 231 | + duration of the duplicate detection history. The default value is 10 minutes. |
| 232 | + Input value of either type ~datetime.timedelta or string in ISO 8601 duration format |
| 233 | + like "PT300S" is accepted. |
| 234 | + :param enable_batched_operations: Value that indicates whether server-side batched operations |
| 235 | + are enabled. |
| 236 | + :param size_in_bytes: The size of the topic, in bytes. |
| 237 | + :param filtering_messages_before_publishing: Filter messages before publishing. |
| 238 | + :param authorization_rules: List of Authorization rules for resource. |
| 239 | + :param support_ordering: A value that indicates whether the topic supports ordering. |
| 240 | + :param auto_delete_on_idle: ISO 8601 time span idle interval after which the topic is |
| 241 | + automatically deleted. The minimum duration is 5 minutes. |
| 242 | + Input value of either type ~datetime.timedelta or string in ISO 8601 duration format |
| 243 | + like "PT300S" is accepted. |
| 244 | + :param enable_partitioning: A value that indicates whether the topic is to be partitioned |
| 245 | + across multiple message brokers. |
| 246 | + :param enable_express: A value that indicates whether Express Entities are enabled. An express |
| 247 | + queue holds a message in memory temporarily before writing it to persistent storage. |
| 248 | + :param user_metadata: Metadata associated with the topic. |
| 249 | + :param max_message_size_in_kilobytes: The maximum size in kilobytes of message payload that |
| 250 | + can be accepted by the queue. This feature is only available when using a Premium namespace |
| 251 | + and Service Bus API version "2021-05" or higher. |
| 252 | + The minimum allowed value is 1024 while the maximum allowed value is 102400. Default value is 1024. |
| 253 | + """ |
| 254 | + if topic_name is None: |
| 255 | + raise TypeError("Topic name cannot be None.") |
| 256 | + |
| 257 | + with self.get_conn() as service_mgmt_conn: |
| 258 | + try: |
| 259 | + topic_properties = service_mgmt_conn.get_topic(topic_name) |
| 260 | + except ResourceNotFoundError: |
| 261 | + topic_properties = None |
| 262 | + if topic_properties and topic_properties.name == topic_name: |
| 263 | + self.log.info("Topic name already exists") |
| 264 | + return topic_properties.name |
| 265 | + topic = service_mgmt_conn.create_topic( |
| 266 | + topic_name=topic_name, |
| 267 | + default_message_time_to_live=default_message_time_to_live, |
| 268 | + max_size_in_megabytes=max_size_in_megabytes, |
| 269 | + requires_duplicate_detection=requires_duplicate_detection, |
| 270 | + duplicate_detection_history_time_window=duplicate_detection_history_time_window, |
| 271 | + enable_batched_operations=enable_batched_operations, |
| 272 | + size_in_bytes=size_in_bytes, |
| 273 | + filtering_messages_before_publishing=filtering_messages_before_publishing, |
| 274 | + authorization_rules=authorization_rules, |
| 275 | + support_ordering=support_ordering, |
| 276 | + auto_delete_on_idle=auto_delete_on_idle, |
| 277 | + enable_partitioning=enable_partitioning, |
| 278 | + enable_express=enable_express, |
| 279 | + user_metadata=user_metadata, |
| 280 | + max_message_size_in_kilobytes=max_message_size_in_kilobytes, |
| 281 | + ) |
| 282 | + self.log.info("Created Topic %s", topic.name) |
| 283 | + return topic.name |
| 284 | + |
197 | 285 | def create_subscription(
|
198 | 286 | self,
|
199 | 287 | topic_name: str,
|
|
0 commit comments