| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using Azure.Core; |
| | 7 | |
|
| | 8 | | namespace Azure.Messaging.ServiceBus.Management |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Represents the static properties of the subscription. |
| | 12 | | /// </summary> |
| | 13 | | public class SubscriptionProperties : IEquatable<SubscriptionProperties> |
| | 14 | | { |
| | 15 | | private string _topicName, _subscriptionName; |
| 96 | 16 | | private TimeSpan _lockDuration = TimeSpan.FromSeconds(60); |
| 96 | 17 | | private TimeSpan _defaultMessageTimeToLive = TimeSpan.MaxValue; |
| 96 | 18 | | private TimeSpan _autoDeleteOnIdle = TimeSpan.MaxValue; |
| 96 | 19 | | private int _maxDeliveryCount = 10; |
| | 20 | | private string _forwardTo = null; |
| | 21 | | private string _forwardDeadLetteredMessagesTo = null; |
| | 22 | | private string _userMetadata = null; |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Initializes a new instance of SubscriptionDescription class with the specified name and topic name. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="topicName">Name of the topic relative to the namespace base address.</param> |
| | 28 | | /// <param name="subscriptionName">Name of the subscription.</param> |
| 68 | 29 | | public SubscriptionProperties(string topicName, string subscriptionName) |
| | 30 | | { |
| 68 | 31 | | TopicName = topicName; |
| 68 | 32 | | SubscriptionName = subscriptionName; |
| 68 | 33 | | } |
| | 34 | |
|
| 28 | 35 | | internal SubscriptionProperties(CreateSubscriptionOptions options) |
| | 36 | | { |
| 28 | 37 | | TopicName = options.TopicName; |
| 28 | 38 | | SubscriptionName = options.SubscriptionName; |
| 28 | 39 | | LockDuration = options.LockDuration; |
| 28 | 40 | | RequiresSession = options.RequiresSession; |
| 28 | 41 | | DefaultMessageTimeToLive = options.DefaultMessageTimeToLive; |
| 28 | 42 | | AutoDeleteOnIdle = options.AutoDeleteOnIdle; |
| 28 | 43 | | DeadLetteringOnMessageExpiration = options.DeadLetteringOnMessageExpiration; |
| 28 | 44 | | EnableDeadLetteringOnFilterEvaluationExceptions = options.EnableDeadLetteringOnFilterEvaluationExceptions; |
| 28 | 45 | | MaxDeliveryCount = options.MaxDeliveryCount; |
| 28 | 46 | | EnableBatchedOperations = options.EnableBatchedOperations; |
| 28 | 47 | | Status = options.Status; |
| 28 | 48 | | ForwardTo = options.ForwardTo; |
| 28 | 49 | | ForwardDeadLetteredMessagesTo = options.ForwardDeadLetteredMessagesTo; |
| 28 | 50 | | if (options.UserMetadata != null) |
| | 51 | | { |
| 4 | 52 | | UserMetadata = options.UserMetadata; |
| | 53 | | } |
| 28 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Duration of a peek lock receive. i.e., the amount of time that the message is locked by a given receiver so |
| | 58 | | /// no other receiver receives the same message. |
| | 59 | | /// </summary> |
| | 60 | | /// <remarks>Max value is 5 minutes. Default value is 60 seconds.</remarks> |
| | 61 | | public TimeSpan LockDuration |
| | 62 | | { |
| 52 | 63 | | get => _lockDuration; |
| | 64 | | set |
| | 65 | | { |
| 72 | 66 | | Argument.AssertPositive(value, nameof(LockDuration)); |
| 72 | 67 | | _lockDuration = value; |
| 72 | 68 | | } |
| | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// This indicates whether the subscription supports the concept of session. Sessionful-messages follow FIFO ord |
| | 73 | | /// </summary> |
| | 74 | | /// <remarks> |
| | 75 | | /// If true, the receiver can only receive messages using <see cref="ServiceBusSessionProcessor"/>. |
| | 76 | | /// Defaults to false. |
| | 77 | | /// </remarks> |
| 124 | 78 | | public bool RequiresSession { get; set; } = false; |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// The default time to live value for the messages. This is the duration after which the message expires, start |
| | 82 | | /// the message is sent to Service Bus. </summary> |
| | 83 | | /// <remarks> |
| | 84 | | /// This is the default value used when <see cref="ServiceBusMessage.TimeToLive"/> is not set on a |
| | 85 | | /// message itself. Messages older than their TimeToLive value will expire and no longer be retained in the mes |
| | 86 | | /// Subscribers will be unable to receive expired messages. |
| | 87 | | /// Default value is <see cref="TimeSpan.MaxValue"/>. |
| | 88 | | /// </remarks> |
| | 89 | | public TimeSpan DefaultMessageTimeToLive |
| | 90 | | { |
| 60 | 91 | | get => _defaultMessageTimeToLive; |
| | 92 | | set |
| | 93 | | { |
| 76 | 94 | | Argument.AssertInRange( |
| 76 | 95 | | value, |
| 76 | 96 | | ManagementClientConstants.MinimumAllowedTimeToLive, |
| 76 | 97 | | ManagementClientConstants.MaximumAllowedTimeToLive, |
| 76 | 98 | | nameof(DefaultMessageTimeToLive)); |
| | 99 | |
|
| 76 | 100 | | _defaultMessageTimeToLive = value; |
| 76 | 101 | | } |
| | 102 | | } |
| | 103 | |
|
| | 104 | | /// <summary> |
| | 105 | | /// The <see cref="TimeSpan"/> idle interval after which the subscription is automatically deleted. |
| | 106 | | /// </summary> |
| | 107 | | /// <remarks>The minimum duration is 5 minutes. Default value is <see cref="TimeSpan.MaxValue"/>.</remarks> |
| | 108 | | public TimeSpan AutoDeleteOnIdle |
| | 109 | | { |
| 60 | 110 | | get => _autoDeleteOnIdle; |
| | 111 | | set |
| | 112 | | { |
| 72 | 113 | | Argument.AssertAtLeast( |
| 72 | 114 | | value, |
| 72 | 115 | | ManagementClientConstants.MinimumAllowedAutoDeleteOnIdle, |
| 72 | 116 | | nameof(AutoDeleteOnIdle)); |
| | 117 | |
|
| 72 | 118 | | _autoDeleteOnIdle = value; |
| 72 | 119 | | } |
| | 120 | | } |
| | 121 | |
|
| | 122 | | /// <summary> |
| | 123 | | /// Indicates whether this subscription has dead letter support when a message expires. |
| | 124 | | /// </summary> |
| | 125 | | /// <remarks>If true, the expired messages are moved to dead-letter sub-queue. Default value is false.</remarks> |
| 124 | 126 | | public bool DeadLetteringOnMessageExpiration { get; set; } = false; |
| | 127 | |
|
| | 128 | | /// <summary> |
| | 129 | | /// indicates whether messages need to be forwarded to dead-letter sub queue when subscription rule evaluation f |
| | 130 | | /// </summary> |
| | 131 | | /// <remarks>Defaults to true.</remarks> |
| 220 | 132 | | public bool EnableDeadLetteringOnFilterEvaluationExceptions { get; set; } = true; |
| | 133 | |
|
| | 134 | | /// <summary> |
| | 135 | | /// Name of the topic under which subscription exists. |
| | 136 | | /// </summary> |
| | 137 | | /// <remarks>Value cannot be null or empty. Value cannot exceed 260 chars. Cannot start or end with a slash. |
| | 138 | | /// Cannot have restricted characters: '@','?','#','*'</remarks> |
| | 139 | | public string TopicName |
| | 140 | | { |
| 88 | 141 | | get => _topicName; |
| | 142 | | set |
| | 143 | | { |
| 96 | 144 | | EntityNameFormatter.CheckValidTopicName(value, nameof(TopicName)); |
| 96 | 145 | | _topicName = value; |
| 96 | 146 | | } |
| | 147 | | } |
| | 148 | |
|
| | 149 | | /// <summary> |
| | 150 | | /// Name of the subscription. |
| | 151 | | /// </summary> |
| | 152 | | /// <remarks>Value cannot be null or empty. Value cannot exceed 50 chars. |
| | 153 | | /// Cannot have restricted characters: '@','?','#','*','/','\'</remarks> |
| | 154 | | public string SubscriptionName |
| | 155 | | { |
| 56 | 156 | | get => _subscriptionName; |
| | 157 | | set |
| | 158 | | { |
| 96 | 159 | | EntityNameFormatter.CheckValidSubscriptionName(value, nameof(SubscriptionName)); |
| 96 | 160 | | _subscriptionName = value; |
| 96 | 161 | | } |
| | 162 | | } |
| | 163 | |
|
| | 164 | | /// <summary> |
| | 165 | | /// The maximum delivery count of a message before it is dead-lettered. |
| | 166 | | /// </summary> |
| | 167 | | /// <remarks>The delivery count is increased when a message is received in <see cref="ReceiveMode.PeekLock"/> mo |
| | 168 | | /// and didn't complete the message before the message lock expired. |
| | 169 | | /// Default value is 10. Minimum value is 1.</remarks> |
| | 170 | | public int MaxDeliveryCount |
| | 171 | | { |
| 52 | 172 | | get => _maxDeliveryCount; |
| | 173 | | set |
| | 174 | | { |
| 76 | 175 | | Argument.AssertAtLeast( |
| 76 | 176 | | value, |
| 76 | 177 | | ManagementClientConstants.MinAllowedMaxDeliveryCount, |
| 76 | 178 | | nameof(AutoDeleteOnIdle)); |
| | 179 | |
|
| 76 | 180 | | _maxDeliveryCount = value; |
| 76 | 181 | | } |
| | 182 | | } |
| | 183 | |
|
| | 184 | | /// <summary> |
| | 185 | | /// The current status of the subscription (Enabled / Disabled). |
| | 186 | | /// </summary> |
| | 187 | | /// <remarks>When an entity is disabled, that entity cannot send or receive messages.</remarks> |
| 220 | 188 | | public EntityStatus Status { get; set; } = EntityStatus.Active; |
| | 189 | |
|
| | 190 | | /// <summary> |
| | 191 | | /// The name of the recipient entity to which all the messages sent to the subscription are forwarded to. |
| | 192 | | /// </summary> |
| | 193 | | /// <remarks>If set, user cannot manually receive messages from this subscription. The destination entity |
| | 194 | | /// must be an already existing entity.</remarks> |
| | 195 | | public string ForwardTo |
| | 196 | | { |
| 130 | 197 | | get => _forwardTo; |
| | 198 | | set |
| | 199 | | { |
| 38 | 200 | | if (string.IsNullOrWhiteSpace(value)) |
| | 201 | | { |
| 28 | 202 | | _forwardTo = value; |
| 28 | 203 | | return; |
| | 204 | | } |
| | 205 | |
|
| 10 | 206 | | EntityNameFormatter.CheckValidQueueName(value, nameof(ForwardTo)); |
| 6 | 207 | | if (_topicName.Equals(value, StringComparison.CurrentCultureIgnoreCase)) |
| | 208 | | { |
| 0 | 209 | | throw new InvalidOperationException("Entity cannot have auto-forwarding policy to itself"); |
| | 210 | | } |
| | 211 | |
|
| 6 | 212 | | _forwardTo = value; |
| 6 | 213 | | } |
| | 214 | | } |
| | 215 | |
|
| | 216 | | /// <summary> |
| | 217 | | /// The name of the recipient entity to which all the dead-lettered messages of this subscription are forwarded |
| | 218 | | /// </summary> |
| | 219 | | /// <remarks>If set, user cannot manually receive dead-lettered messages from this subscription. The destination |
| | 220 | | /// entity must already exist.</remarks> |
| | 221 | | public string ForwardDeadLetteredMessagesTo |
| | 222 | | { |
| 130 | 223 | | get => _forwardDeadLetteredMessagesTo; |
| | 224 | | set |
| | 225 | | { |
| 38 | 226 | | if (string.IsNullOrWhiteSpace(value)) |
| | 227 | | { |
| 28 | 228 | | _forwardDeadLetteredMessagesTo = value; |
| 28 | 229 | | return; |
| | 230 | | } |
| | 231 | |
|
| 10 | 232 | | EntityNameFormatter.CheckValidQueueName(value, nameof(ForwardDeadLetteredMessagesTo)); |
| 6 | 233 | | if (_topicName.Equals(value, StringComparison.CurrentCultureIgnoreCase)) |
| | 234 | | { |
| 0 | 235 | | throw new InvalidOperationException("Entity cannot have auto-forwarding policy to itself"); |
| | 236 | | } |
| | 237 | |
|
| 6 | 238 | | _forwardDeadLetteredMessagesTo = value; |
| 6 | 239 | | } |
| | 240 | | } |
| | 241 | |
|
| | 242 | | /// <summary> |
| | 243 | | /// Indicates whether server-side batched operations are enabled. |
| | 244 | | /// </summary> |
| | 245 | | /// <remarks>Defaults to true.</remarks> |
| 220 | 246 | | public bool EnableBatchedOperations { get; set; } = true; |
| | 247 | |
|
| | 248 | | /// <summary> |
| | 249 | | /// Custom metdata that user can associate with the description. |
| | 250 | | /// </summary> |
| | 251 | | /// <remarks>Cannot be null. Max length is 1024 chars.</remarks> |
| | 252 | | public string UserMetadata |
| | 253 | | { |
| 60 | 254 | | get => _userMetadata; |
| | 255 | | set |
| | 256 | | { |
| 24 | 257 | | Argument.AssertNotNull(value, nameof(UserMetadata)); |
| 24 | 258 | | Argument.AssertNotTooLong( |
| 24 | 259 | | value, |
| 24 | 260 | | ManagementClientConstants.MaxUserMetadataLength, |
| 24 | 261 | | nameof(UserMetadata)); |
| | 262 | |
|
| 24 | 263 | | _userMetadata = value; |
| 24 | 264 | | } |
| | 265 | | } |
| | 266 | |
|
| | 267 | | /// <summary> |
| | 268 | | /// List of properties that were retrieved using GetSubscription but are not understood by this version of clien |
| | 269 | | /// The list will be sent back when an already retrieved SubscriptionDescription will be used in UpdateSubscript |
| | 270 | | /// </summary> |
| 172 | 271 | | internal List<object> UnknownProperties { get; set; } |
| | 272 | |
|
| 92 | 273 | | internal RuleProperties Rule { get; set; } |
| | 274 | |
|
| | 275 | | /// <summary> |
| | 276 | | /// Returns a hash code for this instance. |
| | 277 | | /// </summary> |
| | 278 | | public override int GetHashCode() |
| | 279 | | { |
| 0 | 280 | | int hash = 7; |
| | 281 | | unchecked |
| | 282 | | { |
| 0 | 283 | | hash = (hash * 7) + TopicName?.GetHashCode() ?? 0; |
| 0 | 284 | | hash = (hash * 7) + SubscriptionName?.GetHashCode() ?? 0; |
| | 285 | | } |
| | 286 | |
|
| 0 | 287 | | return hash; |
| | 288 | | } |
| | 289 | |
|
| | 290 | | /// <summary>Determines whether the specified object is equal to the current object.</summary> |
| | 291 | | public override bool Equals(object obj) |
| | 292 | | { |
| 0 | 293 | | var other = obj as SubscriptionProperties; |
| 0 | 294 | | return Equals(other); |
| | 295 | | } |
| | 296 | |
|
| | 297 | | /// <summary>Determines whether the specified object is equal to the current object.</summary> |
| | 298 | | public bool Equals(SubscriptionProperties other) |
| | 299 | | { |
| 4 | 300 | | if (other is SubscriptionProperties otherDescription |
| 4 | 301 | | && SubscriptionName.Equals(otherDescription.SubscriptionName, StringComparison.OrdinalIgnoreCase) |
| 4 | 302 | | && TopicName.Equals(otherDescription.TopicName, StringComparison.OrdinalIgnoreCase) |
| 4 | 303 | | && AutoDeleteOnIdle.Equals(otherDescription.AutoDeleteOnIdle) |
| 4 | 304 | | && DefaultMessageTimeToLive.Equals(otherDescription.DefaultMessageTimeToLive) |
| 4 | 305 | | && EnableBatchedOperations == otherDescription.EnableBatchedOperations |
| 4 | 306 | | && DeadLetteringOnMessageExpiration == otherDescription.DeadLetteringOnMessageExpiration |
| 4 | 307 | | && EnableDeadLetteringOnFilterEvaluationExceptions == otherDescription.EnableDeadLetteringOnFilterEvalua |
| 4 | 308 | | && string.Equals(ForwardDeadLetteredMessagesTo, otherDescription.ForwardDeadLetteredMessagesTo, StringCo |
| 4 | 309 | | && string.Equals(ForwardTo, otherDescription.ForwardTo, StringComparison.OrdinalIgnoreCase) |
| 4 | 310 | | && LockDuration.Equals(otherDescription.LockDuration) |
| 4 | 311 | | && MaxDeliveryCount == otherDescription.MaxDeliveryCount |
| 4 | 312 | | && RequiresSession.Equals(otherDescription.RequiresSession) |
| 4 | 313 | | && Status.Equals(otherDescription.Status) |
| 4 | 314 | | && string.Equals(_userMetadata, otherDescription._userMetadata, StringComparison.OrdinalIgnoreCase)) |
| | 315 | | { |
| 4 | 316 | | return true; |
| | 317 | | } |
| | 318 | |
|
| 0 | 319 | | return false; |
| | 320 | | } |
| | 321 | |
|
| | 322 | | /// <summary> |
| | 323 | | /// |
| | 324 | | /// </summary> |
| | 325 | | /// <param name="left"></param> |
| | 326 | | /// <param name="right"></param> |
| | 327 | | /// <returns></returns> |
| | 328 | | public static bool operator ==(SubscriptionProperties left, SubscriptionProperties right) |
| | 329 | | { |
| 0 | 330 | | if (ReferenceEquals(left, right)) |
| | 331 | | { |
| 0 | 332 | | return true; |
| | 333 | | } |
| | 334 | |
|
| 0 | 335 | | if (left is null || right is null) |
| | 336 | | { |
| 0 | 337 | | return false; |
| | 338 | | } |
| | 339 | |
|
| 0 | 340 | | return left.Equals(right); |
| | 341 | | } |
| | 342 | |
|
| | 343 | | /// <summary> |
| | 344 | | /// |
| | 345 | | /// </summary> |
| | 346 | | /// <param name="left"></param> |
| | 347 | | /// <param name="right"></param> |
| | 348 | | /// <returns></returns> |
| | 349 | | public static bool operator !=(SubscriptionProperties left, SubscriptionProperties right) |
| | 350 | | { |
| 0 | 351 | | return !(left == right); |
| | 352 | | } |
| | 353 | | } |
| | 354 | | } |