| | 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 set of options that can be specified for the creation of a rule. |
| | 11 | | /// </summary> |
| | 12 | | public sealed class CreateRuleOptions : IEquatable<CreateRuleOptions> |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Gets the name of the default rule on the subscription. |
| | 16 | | /// </summary> |
| | 17 | | /// <remarks> |
| | 18 | | /// Whenever a new subscription is created, a default rule is always added. |
| | 19 | | /// The default rule is a <see cref="TrueRuleFilter"/> which will enable all messages in the topic to reach subs |
| | 20 | | /// </remarks> |
| | 21 | | public const string DefaultRuleName = "$Default"; |
| | 22 | | private RuleFilter _filter; |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Initializes a new instance of the <see cref="CreateRuleOptions" /> class with default values. |
| | 26 | | /// </summary> |
| | 27 | | public CreateRuleOptions() |
| 36 | 28 | | : this(DefaultRuleName, TrueRuleFilter.Default) |
| | 29 | | { |
| 36 | 30 | | } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Initializes a new instance of the <see cref="CreateRuleOptions" /> class with the specified name. |
| | 34 | | /// </summary> |
| | 35 | | public CreateRuleOptions(string name) |
| 0 | 36 | | : this(name, TrueRuleFilter.Default) |
| | 37 | | { |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Initializes a new instance of the <see cref="CreateRuleOptions" /> class with the specified name and filter |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="name"></param> |
| | 44 | | /// <param name="filter">The filter expression used to match messages.</param> |
| 44 | 45 | | public CreateRuleOptions(string name, RuleFilter filter) |
| | 46 | | { |
| 44 | 47 | | Argument.AssertNotNull(filter, nameof(filter)); |
| 44 | 48 | | Filter = filter; |
| 44 | 49 | | Name = name; |
| 44 | 50 | | } |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Initializes a new instance of <see cref="CreateRuleOptions"/> based on the |
| | 54 | | /// specified <see cref="RuleProperties"/> instance. This is useful for creating a new rule based |
| | 55 | | /// on the properties of an existing rule. |
| | 56 | | /// </summary> |
| | 57 | | /// <param name="rule">Existing rule to create options from.</param> |
| 24 | 58 | | public CreateRuleOptions(RuleProperties rule) |
| | 59 | | { |
| 24 | 60 | | Filter = rule.Filter?.Clone(); |
| 24 | 61 | | Action = rule.Action?.Clone(); |
| 24 | 62 | | Name = rule.Name; |
| 24 | 63 | | } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Gets or sets the filter expression used to match messages. |
| | 67 | | /// </summary> |
| | 68 | | /// <value>The filter expression used to match messages.</value> |
| | 69 | | /// <exception cref="System.ArgumentNullException">null (Nothing in Visual Basic) is assigned.</exception> |
| | 70 | | public RuleFilter Filter |
| | 71 | | { |
| 116 | 72 | | get => _filter; |
| | 73 | |
|
| | 74 | | set |
| | 75 | | { |
| 80 | 76 | | Argument.AssertNotNull(value, nameof(Filter)); |
| 80 | 77 | | _filter = value; |
| 80 | 78 | | } |
| | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Gets or sets the action to perform if the message satisfies the filtering expression. |
| | 83 | | /// </summary> |
| | 84 | | /// <value>The action to perform if the message satisfies the filtering expression.</value> |
| 116 | 85 | | public RuleAction Action { get; set; } |
| | 86 | |
|
| | 87 | | /// <summary> |
| | 88 | | /// Gets or sets the name of the rule. |
| | 89 | | /// </summary> |
| | 90 | | /// <value>Returns a <see cref="System.String" /> Representing the name of the rule.</value> |
| | 91 | | /// <remarks>Max allowed length of rule name is 50 chars.</remarks> |
| 180 | 92 | | public string Name { get; set; } |
| | 93 | |
|
| | 94 | | // TODO: Implement for AMQP |
| | 95 | | internal DateTime CreatedAt |
| | 96 | | { |
| 0 | 97 | | get; set; |
| | 98 | | } |
| | 99 | |
|
| | 100 | | /// <inheritdoc/> |
| | 101 | | public override int GetHashCode() |
| | 102 | | { |
| 0 | 103 | | int hash = 13; |
| | 104 | | unchecked |
| | 105 | | { |
| 0 | 106 | | hash = (hash * 7) + _filter?.GetHashCode() ?? 0; |
| 0 | 107 | | hash = (hash * 7) + Action?.GetHashCode() ?? 0; |
| | 108 | | } |
| 0 | 109 | | return hash; |
| | 110 | | } |
| | 111 | |
|
| | 112 | | /// <inheritdoc/> |
| | 113 | | public override bool Equals(object obj) |
| | 114 | | { |
| 0 | 115 | | var other = obj as CreateRuleOptions; |
| 0 | 116 | | return Equals(other); |
| | 117 | | } |
| | 118 | |
|
| | 119 | | /// <inheritdoc/> |
| | 120 | | public bool Equals(CreateRuleOptions other) |
| | 121 | | { |
| 24 | 122 | | if (other is CreateRuleOptions otherOptions |
| 24 | 123 | | && string.Equals(Name, otherOptions.Name, StringComparison.OrdinalIgnoreCase) |
| 24 | 124 | | && (Filter == null || Filter.Equals(otherOptions.Filter)) |
| 24 | 125 | | && (Action == null || Action.Equals(otherOptions.Action))) |
| | 126 | | { |
| 24 | 127 | | return true; |
| | 128 | | } |
| | 129 | |
|
| 0 | 130 | | return false; |
| | 131 | | } |
| | 132 | |
|
| | 133 | | /// <summary> |
| | 134 | | /// |
| | 135 | | /// </summary> |
| | 136 | | /// <param name="left"></param> |
| | 137 | | /// <param name="right"></param> |
| | 138 | | /// <returns></returns> |
| | 139 | | public static bool operator ==(CreateRuleOptions left, CreateRuleOptions right) |
| | 140 | | { |
| 0 | 141 | | if (ReferenceEquals(left, right)) |
| | 142 | | { |
| 0 | 143 | | return true; |
| | 144 | | } |
| | 145 | |
|
| 0 | 146 | | if (ReferenceEquals(left, null) || ReferenceEquals(right, null)) |
| | 147 | | { |
| 0 | 148 | | return false; |
| | 149 | | } |
| | 150 | |
|
| 0 | 151 | | return left.Equals(right); |
| | 152 | | } |
| | 153 | |
|
| | 154 | | /// <summary> |
| | 155 | | /// |
| | 156 | | /// </summary> |
| | 157 | | /// <param name="left"></param> |
| | 158 | | /// <param name="right"></param> |
| | 159 | | /// <returns></returns> |
| | 160 | | public static bool operator !=(CreateRuleOptions left, CreateRuleOptions right) |
| | 161 | | { |
| 0 | 162 | | return !(left == right); |
| | 163 | | } |
| | 164 | | } |
| | 165 | | } |