| | | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | | 3 | | |
| | | 4 | | namespace Microsoft.Azure.ServiceBus.Management |
| | | 5 | | { |
| | | 6 | | using System; |
| | | 7 | | using System.Xml.Linq; |
| | | 8 | | |
| | | 9 | | internal class NamespaceInfoExtensions |
| | | 10 | | { |
| | | 11 | | public static NamespaceInfo ParseFromContent(string xml) |
| | | 12 | | { |
| | | 13 | | try |
| | | 14 | | { |
| | 0 | 15 | | var xDoc = XElement.Parse(xml); |
| | 0 | 16 | | if (!xDoc.IsEmpty) |
| | | 17 | | { |
| | 0 | 18 | | if (xDoc.Name.LocalName == "entry") |
| | | 19 | | { |
| | 0 | 20 | | return ParseFromEntryElement(xDoc); |
| | | 21 | | } |
| | | 22 | | } |
| | 0 | 23 | | } |
| | 0 | 24 | | catch (Exception ex) when (!(ex is ServiceBusException)) |
| | | 25 | | { |
| | 0 | 26 | | throw new ServiceBusException(false, ex); |
| | | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | throw new ServiceBusException(false, "Unknown error."); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | private static NamespaceInfo ParseFromEntryElement(XElement xEntry) |
| | | 33 | | { |
| | 0 | 34 | | var nsInfo = new NamespaceInfo(); |
| | | 35 | | |
| | 0 | 36 | | var nsInfoXml = xEntry.Element(XName.Get("content", ManagementClientConstants.AtomNamespace))? |
| | 0 | 37 | | .Element(XName.Get("NamespaceInfo", ManagementClientConstants.ServiceBusNamespace)); |
| | | 38 | | |
| | 0 | 39 | | if (nsInfoXml == null) |
| | | 40 | | { |
| | 0 | 41 | | throw new ServiceBusException(true); |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | foreach (var element in nsInfoXml.Elements()) |
| | | 45 | | { |
| | 0 | 46 | | switch (element.Name.LocalName) |
| | | 47 | | { |
| | | 48 | | case "CreatedTime": |
| | 0 | 49 | | nsInfo.CreatedTime = DateTime.Parse(element.Value); |
| | 0 | 50 | | break; |
| | | 51 | | case "ModifiedTime": |
| | 0 | 52 | | nsInfo.ModifiedTime = DateTime.Parse(element.Value); |
| | 0 | 53 | | break; |
| | | 54 | | case "Name": |
| | 0 | 55 | | nsInfo.Name = element.Value; |
| | 0 | 56 | | break; |
| | | 57 | | case "Alias": |
| | 0 | 58 | | nsInfo.Alias = element.Value; |
| | 0 | 59 | | break; |
| | | 60 | | case "MessagingUnits": |
| | 0 | 61 | | int.TryParse(element.Value, out var units); |
| | 0 | 62 | | nsInfo.MessagingUnits = units; |
| | 0 | 63 | | break; |
| | | 64 | | case "NamespaceType": |
| | 0 | 65 | | if (Enum.TryParse<NamespaceType>(element.Value, out var nsType)) |
| | | 66 | | { |
| | 0 | 67 | | nsInfo.NamespaceType = nsType; |
| | | 68 | | } |
| | 0 | 69 | | else if (element.Value == "Messaging") // TODO: workaround till next major as it's a breaking ch |
| | | 70 | | { |
| | 0 | 71 | | nsInfo.NamespaceType = NamespaceType.ServiceBus; |
| | | 72 | | } |
| | | 73 | | else |
| | | 74 | | { |
| | 0 | 75 | | nsInfo.NamespaceType = NamespaceType.Others; |
| | | 76 | | } |
| | 0 | 77 | | break; |
| | | 78 | | case "MessagingSKU": |
| | 0 | 79 | | if (Enum.TryParse<MessagingSku>(element.Value, out var nsSku)) |
| | | 80 | | { |
| | 0 | 81 | | nsInfo.MessagingSku = nsSku; |
| | | 82 | | } |
| | | 83 | | else |
| | | 84 | | { |
| | 0 | 85 | | nsInfo.MessagingSku = MessagingSku.Others; |
| | | 86 | | } |
| | | 87 | | break; |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | return nsInfo; |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | } |