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