| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Xml.Linq; |
| | 6 | | using Azure.Messaging.ServiceBus.Diagnostics; |
| | 7 | |
|
| | 8 | | namespace Azure.Messaging.ServiceBus.Management |
| | 9 | | { |
| | 10 | | internal static class RuleActionExtensions |
| | 11 | | { |
| | 12 | | internal static RuleAction ParseFromXElement(XElement xElement) |
| | 13 | | { |
| 48 | 14 | | var attribute = xElement.Attribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace)); |
| 48 | 15 | | if (attribute == null) |
| | 16 | | { |
| 0 | 17 | | return null; |
| | 18 | | } |
| | 19 | |
|
| 48 | 20 | | switch (attribute.Value) |
| | 21 | | { |
| | 22 | | case "SqlRuleAction": |
| 12 | 23 | | return ParseFromXElementSqlRuleAction(xElement); |
| | 24 | |
|
| | 25 | | case "EmptyRuleAction": |
| 36 | 26 | | return null; |
| | 27 | |
|
| | 28 | | default: |
| 0 | 29 | | ServiceBusEventSource.Log.ManagementSerializationException( |
| 0 | 30 | | $"{nameof(RuleActionExtensions)}_{nameof(ParseFromXElement)}", |
| 0 | 31 | | xElement.ToString()); |
| 0 | 32 | | return null; |
| | 33 | | } |
| | 34 | | } |
| | 35 | |
|
| | 36 | | private static RuleAction ParseFromXElementSqlRuleAction(XElement xElement) |
| | 37 | | { |
| 12 | 38 | | var expression = xElement.Element(XName.Get("SqlExpression", ManagementClientConstants.ServiceBusNamespace)) |
| 12 | 39 | | if (string.IsNullOrWhiteSpace(expression)) |
| | 40 | | { |
| 0 | 41 | | return null; |
| | 42 | | } |
| | 43 | |
|
| 12 | 44 | | var action = new SqlRuleAction(expression); |
| | 45 | |
|
| 12 | 46 | | var parameters = xElement.Element(XName.Get("Parameters", ManagementClientConstants.ServiceBusNamespace)); |
| 12 | 47 | | if (parameters != null && parameters.HasElements) |
| | 48 | | { |
| 0 | 49 | | foreach (var param in parameters.Elements(XName.Get("KeyValueOfstringanyType", ManagementClientConstants |
| | 50 | | { |
| 0 | 51 | | var key = param.Element(XName.Get("Key", ManagementClientConstants.ServiceBusNamespace))?.Value; |
| 0 | 52 | | var value = XmlObjectConvertor.ParseValueObject(param.Element(XName.Get("Value", ManagementClientCon |
| 0 | 53 | | action.Parameters.Add(key, value); |
| | 54 | | } |
| | 55 | | } |
| 12 | 56 | | return action; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | public static XElement Serialize(this RuleAction action) |
| | 60 | | { |
| 4 | 61 | | if (action is SqlRuleAction sqlRuleAction) |
| | 62 | | { |
| 4 | 63 | | XElement parameterElement = new XElement( |
| 4 | 64 | | XName.Get( |
| 4 | 65 | | "Parameters", |
| 4 | 66 | | ManagementClientConstants.ServiceBusNamespace)); |
| 0 | 67 | | foreach (var param in sqlRuleAction.Parameters) |
| | 68 | | { |
| 0 | 69 | | parameterElement.Add( |
| 0 | 70 | | new XElement(XName.Get("KeyValueOfstringanyType", ManagementClientConstants.ServiceBusNamespace) |
| 0 | 71 | | new XElement(XName.Get("Key", ManagementClientConstants.ServiceBusNamespace), param.Key), |
| 0 | 72 | | XmlObjectConvertor.SerializeObject(param.Value))); |
| | 73 | | } |
| | 74 | |
|
| 4 | 75 | | return new XElement( |
| 4 | 76 | | XName.Get("Action", ManagementClientConstants.ServiceBusNamespace), |
| 4 | 77 | | new XAttribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace), nameof(S |
| 4 | 78 | | new XElement(XName.Get("SqlExpression", ManagementClientConstants.ServiceBusNamespace), sqlRuleA |
| 4 | 79 | | parameterElement); |
| | 80 | | } |
| | 81 | | else |
| | 82 | | { |
| 0 | 83 | | throw new NotImplementedException($"Rule action of type {action.GetType().Name} is not implemented"); |
| | 84 | | } |
| | 85 | | } |
| | 86 | | } |
| | 87 | | } |