| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using Azure.Core; |
| | 6 | |
|
| | 7 | | namespace Azure.Messaging.ServiceBus.Management |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Represents the options that can be specified for the creation of a topic. |
| | 11 | | /// </summary> |
| | 12 | | public class CreateTopicOptions : IEquatable<CreateTopicOptions> |
| | 13 | | { |
| 48 | 14 | | private TimeSpan _duplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(1); |
| | 15 | | private string _name; |
| 48 | 16 | | private TimeSpan _defaultMessageTimeToLive = TimeSpan.MaxValue; |
| 48 | 17 | | private TimeSpan _autoDeleteOnIdle = TimeSpan.MaxValue; |
| | 18 | | private string _userMetadata = null; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Initializes a new instance of <see cref="CreateTopicOptions"/> with the specified relative name. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="name">Name of the topic relative to the namespace base address.</param> |
| 40 | 24 | | public CreateTopicOptions(string name) |
| | 25 | | { |
| 40 | 26 | | Name = name; |
| 40 | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Initializes a new instance of <see cref="CreateTopicOptions"/> based on the |
| | 31 | | /// specified <see cref="TopicProperties"/> instance. This is useful for creating a new topic based |
| | 32 | | /// on the properties of an existing topic. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="topic">Existing topic to create options from.</param> |
| 8 | 35 | | public CreateTopicOptions(TopicProperties topic) |
| | 36 | | { |
| 8 | 37 | | Name = topic.Name; |
| 8 | 38 | | MaxSizeInMegabytes = topic.MaxSizeInMegabytes; |
| 8 | 39 | | RequiresDuplicateDetection = topic.RequiresDuplicateDetection; |
| 8 | 40 | | DefaultMessageTimeToLive = topic.DefaultMessageTimeToLive; |
| 8 | 41 | | AutoDeleteOnIdle = topic.AutoDeleteOnIdle; |
| 8 | 42 | | DuplicateDetectionHistoryTimeWindow = topic.DuplicateDetectionHistoryTimeWindow; |
| 8 | 43 | | EnableBatchedOperations = topic.EnableBatchedOperations; |
| 8 | 44 | | AuthorizationRules = topic.AuthorizationRules.Clone(); |
| 8 | 45 | | Status = topic.Status; |
| 8 | 46 | | EnablePartitioning = topic.EnablePartitioning; |
| 8 | 47 | | if (topic.UserMetadata != null) |
| | 48 | | { |
| 4 | 49 | | UserMetadata = topic.UserMetadata; |
| | 50 | | } |
| 8 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// The default time to live value for the messages. This is the duration after which the message expires, |
| | 55 | | /// starting from when the message is sent to Service Bus. </summary> |
| | 56 | | /// <remarks> |
| | 57 | | /// This is the default value used when <see cref="ServiceBusMessage.TimeToLive"/> is not set on a |
| | 58 | | /// message itself. Messages older than their TimeToLive value will expire and no longer be retained |
| | 59 | | /// in the message store. |
| | 60 | | /// Subscribers will be unable to receive expired messages. |
| | 61 | | /// Default value is <see cref="TimeSpan.MaxValue"/>. |
| | 62 | | /// </remarks> |
| | 63 | | public TimeSpan DefaultMessageTimeToLive |
| | 64 | | { |
| 56 | 65 | | get => _defaultMessageTimeToLive; |
| | 66 | | set |
| | 67 | | { |
| 12 | 68 | | Argument.AssertInRange( |
| 12 | 69 | | value, |
| 12 | 70 | | ManagementClientConstants.MinimumAllowedTimeToLive, |
| 12 | 71 | | ManagementClientConstants.MaximumAllowedTimeToLive, |
| 12 | 72 | | nameof(DefaultMessageTimeToLive)); |
| | 73 | |
|
| 12 | 74 | | _defaultMessageTimeToLive = value; |
| 12 | 75 | | } |
| | 76 | | } |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// The <see cref="TimeSpan"/> idle interval after which the topic is automatically deleted. |
| | 80 | | /// </summary> |
| | 81 | | /// <remarks>The minimum duration is 5 minutes. Default value is <see cref="TimeSpan.MaxValue"/>.</remarks> |
| | 82 | | public TimeSpan AutoDeleteOnIdle |
| | 83 | | { |
| 56 | 84 | | get => _autoDeleteOnIdle; |
| | 85 | | set |
| | 86 | | { |
| 12 | 87 | | Argument.AssertAtLeast( |
| 12 | 88 | | value, |
| 12 | 89 | | ManagementClientConstants.MinimumAllowedAutoDeleteOnIdle, |
| 12 | 90 | | nameof(AutoDeleteOnIdle)); |
| | 91 | |
|
| 12 | 92 | | _autoDeleteOnIdle = value; |
| 12 | 93 | | } |
| | 94 | | } |
| | 95 | |
|
| | 96 | | /// <summary> |
| | 97 | | /// The maximum size of the topic in megabytes, which is the size of memory allocated for the topic. |
| | 98 | | /// </summary> |
| | 99 | | /// <remarks>Default value is 1024.</remarks> |
| 116 | 100 | | public long MaxSizeInMegabytes { get; set; } = 1024; |
| | 101 | |
|
| | 102 | | /// <summary> |
| | 103 | | /// This value indicates if the topic requires guard against duplicate messages. If true, duplicate messages hav |
| | 104 | | /// <see cref="ServiceBusMessage.MessageId"/> and sent to topic within duration of <see cref="DuplicateDetection |
| | 105 | | /// will be discarded. |
| | 106 | | /// </summary> |
| | 107 | | /// <remarks>Defaults to false.</remarks> |
| 76 | 108 | | public bool RequiresDuplicateDetection { get; set; } = false; |
| | 109 | |
|
| | 110 | | /// <summary> |
| | 111 | | /// The <see cref="TimeSpan"/> duration of duplicate detection history that is maintained by the service. |
| | 112 | | /// </summary> |
| | 113 | | /// <remarks> |
| | 114 | | /// The default value is 1 minute. Max value is 7 days and minimum is 20 seconds. |
| | 115 | | /// </remarks> |
| | 116 | | public TimeSpan DuplicateDetectionHistoryTimeWindow |
| | 117 | | { |
| 48 | 118 | | get => _duplicateDetectionHistoryTimeWindow; |
| | 119 | | set |
| | 120 | | { |
| 12 | 121 | | Argument.AssertInRange( |
| 12 | 122 | | value, |
| 12 | 123 | | ManagementClientConstants.MinimumDuplicateDetectionHistoryTimeWindow, |
| 12 | 124 | | ManagementClientConstants.MaximumDuplicateDetectionHistoryTimeWindow, |
| 12 | 125 | | nameof(DuplicateDetectionHistoryTimeWindow)); |
| | 126 | |
|
| 12 | 127 | | _duplicateDetectionHistoryTimeWindow = value; |
| 12 | 128 | | } |
| | 129 | | } |
| | 130 | |
|
| | 131 | | /// <summary> |
| | 132 | | /// Name of the topic relative to the namespace base address. |
| | 133 | | /// </summary> |
| | 134 | | /// <remarks>Max length is 260 chars. Cannot start or end with a slash. |
| | 135 | | /// Cannot have restricted characters: '@','?','#','*'</remarks> |
| | 136 | | public string Name |
| | 137 | | { |
| 64 | 138 | | get => _name; |
| | 139 | | set |
| | 140 | | { |
| 48 | 141 | | EntityNameFormatter.CheckValidTopicName(value, nameof(Name)); |
| 48 | 142 | | _name = value; |
| 48 | 143 | | } |
| | 144 | | } |
| | 145 | |
|
| | 146 | | /// <summary> |
| | 147 | | /// The <see cref="AuthorizationRules"/> on the topic to control user access at entity level. |
| | 148 | | /// </summary> |
| 148 | 149 | | public AuthorizationRules AuthorizationRules { get; internal set; } = new AuthorizationRules(); |
| | 150 | |
|
| | 151 | | /// <summary> |
| | 152 | | /// The current status of the topic (Enabled / Disabled). |
| | 153 | | /// </summary> |
| | 154 | | /// <remarks>When an entity is disabled, that entity cannot send or receive messages.</remarks> |
| 112 | 155 | | public EntityStatus Status { get; set; } = EntityStatus.Active; |
| | 156 | |
|
| | 157 | | /// <summary> |
| | 158 | | /// Indicates whether the topic is to be partitioned across multiple message brokers. |
| | 159 | | /// </summary> |
| | 160 | | /// <remarks>Defaults to false.</remarks> |
| 28 | 161 | | public bool EnablePartitioning { get; set; } = false; |
| | 162 | |
|
| | 163 | | /// <summary> |
| | 164 | | /// Defines whether ordering needs to be maintained. If true, messages sent to topic will be |
| | 165 | | /// forwarded to the subscription in order. |
| | 166 | | /// </summary> |
| | 167 | | /// <remarks>Defaults to false.</remarks> |
| 0 | 168 | | public bool SupportOrdering { get; set; } = false; |
| | 169 | |
|
| | 170 | | /// <summary> |
| | 171 | | /// Indicates whether server-side batched operations are enabled. |
| | 172 | | /// </summary> |
| | 173 | | /// <remarks>Defaults to true.</remarks> |
| 156 | 174 | | public bool EnableBatchedOperations { get; set; } = true; |
| | 175 | |
|
| | 176 | | /// <summary> |
| | 177 | | /// Custom metdata that user can associate with the topic. |
| | 178 | | /// </summary> |
| | 179 | | /// <remarks>Cannot be null. Max length is 1024 chars.</remarks> |
| | 180 | | public string UserMetadata |
| | 181 | | { |
| 44 | 182 | | get => _userMetadata; |
| | 183 | | set |
| | 184 | | { |
| 8 | 185 | | Argument.AssertNotNull(value, nameof(UserMetadata)); |
| 8 | 186 | | Argument.AssertNotTooLong( |
| 8 | 187 | | value, |
| 8 | 188 | | ManagementClientConstants.MaxUserMetadataLength, |
| 8 | 189 | | nameof(UserMetadata)); |
| | 190 | |
|
| 8 | 191 | | _userMetadata = value; |
| 8 | 192 | | } |
| | 193 | | } |
| | 194 | |
|
| | 195 | | /// <summary> |
| | 196 | | /// Returns a hash code for this instance. |
| | 197 | | /// </summary> |
| | 198 | | public override int GetHashCode() |
| | 199 | | { |
| 0 | 200 | | return Name?.GetHashCode() ?? base.GetHashCode(); |
| | 201 | | } |
| | 202 | |
|
| | 203 | | /// <summary>Determines whether the specified object is equal to the current object.</summary> |
| | 204 | | public override bool Equals(object obj) |
| | 205 | | { |
| 0 | 206 | | var other = obj as TopicProperties; |
| 0 | 207 | | return Equals(other); |
| | 208 | | } |
| | 209 | |
|
| | 210 | | /// <summary>Determines whether the specified object is equal to the current object.</summary> |
| | 211 | | public bool Equals(CreateTopicOptions other) |
| | 212 | | { |
| 8 | 213 | | if (other is CreateTopicOptions otherOptions |
| 8 | 214 | | && Name.Equals(otherOptions.Name, StringComparison.OrdinalIgnoreCase) |
| 8 | 215 | | && AutoDeleteOnIdle.Equals(otherOptions.AutoDeleteOnIdle) |
| 8 | 216 | | && DefaultMessageTimeToLive.Equals(otherOptions.DefaultMessageTimeToLive) |
| 8 | 217 | | && (!RequiresDuplicateDetection || DuplicateDetectionHistoryTimeWindow.Equals(otherOptions.DuplicateDete |
| 8 | 218 | | && EnableBatchedOperations == otherOptions.EnableBatchedOperations |
| 8 | 219 | | && EnablePartitioning == otherOptions.EnablePartitioning |
| 8 | 220 | | && MaxSizeInMegabytes == otherOptions.MaxSizeInMegabytes |
| 8 | 221 | | && RequiresDuplicateDetection.Equals(otherOptions.RequiresDuplicateDetection) |
| 8 | 222 | | && Status.Equals(otherOptions.Status) |
| 8 | 223 | | && string.Equals(_userMetadata, otherOptions._userMetadata, StringComparison.OrdinalIgnoreCase) |
| 8 | 224 | | && (AuthorizationRules != null && otherOptions.AuthorizationRules != null |
| 8 | 225 | | || AuthorizationRules == null && otherOptions.AuthorizationRules == null) |
| 8 | 226 | | && (AuthorizationRules == null || AuthorizationRules.Equals(otherOptions.AuthorizationRules))) |
| | 227 | | { |
| 8 | 228 | | return true; |
| | 229 | | } |
| | 230 | |
|
| 0 | 231 | | return false; |
| | 232 | | } |
| | 233 | |
|
| | 234 | | /// <summary></summary> |
| | 235 | | public static bool operator ==(CreateTopicOptions left, CreateTopicOptions right) |
| | 236 | | { |
| 0 | 237 | | if (ReferenceEquals(left, right)) |
| | 238 | | { |
| 0 | 239 | | return true; |
| | 240 | | } |
| | 241 | |
|
| 0 | 242 | | if (ReferenceEquals(left, null) || ReferenceEquals(right, null)) |
| | 243 | | { |
| 0 | 244 | | return false; |
| | 245 | | } |
| | 246 | |
|
| 0 | 247 | | return left.Equals(right); |
| | 248 | | } |
| | 249 | |
|
| | 250 | | /// <summary></summary> |
| | 251 | | public static bool operator !=(CreateTopicOptions left, CreateTopicOptions right) |
| | 252 | | { |
| 0 | 253 | | return !(left == right); |
| | 254 | | } |
| | 255 | | } |
| | 256 | | } |