| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Globalization; |
| | 6 | | using System.Xml.Linq; |
| | 7 | |
|
| | 8 | | namespace Azure.Messaging.ServiceBus.Management |
| | 9 | | { |
| | 10 | | internal class NamespacePropertiesExtensions |
| | 11 | | { |
| | 12 | | public static NamespaceProperties ParseFromContent(string xml) |
| | 13 | | { |
| | 14 | | try |
| | 15 | | { |
| 4 | 16 | | var xDoc = XElement.Parse(xml); |
| 4 | 17 | | if (!xDoc.IsEmpty) |
| | 18 | | { |
| 4 | 19 | | if (xDoc.Name.LocalName == "entry") |
| | 20 | | { |
| 4 | 21 | | return ParseFromEntryElement(xDoc); |
| | 22 | | } |
| | 23 | | } |
| 0 | 24 | | } |
| 0 | 25 | | catch (Exception ex) when (!(ex is ServiceBusException)) |
| | 26 | | { |
| 0 | 27 | | throw new ServiceBusException(false, ex.Message); |
| | 28 | | } |
| | 29 | |
|
| 0 | 30 | | throw new ServiceBusException(false, "Unknown error."); |
| 4 | 31 | | } |
| | 32 | |
|
| | 33 | | private static NamespaceProperties ParseFromEntryElement(XElement xEntry) |
| | 34 | | { |
| 4 | 35 | | var nsInfo = new NamespaceProperties(); |
| | 36 | |
|
| 4 | 37 | | var nsInfoXml = xEntry.Element(XName.Get("content", ManagementClientConstants.AtomNamespace))? |
| 4 | 38 | | .Element(XName.Get("NamespaceInfo", ManagementClientConstants.ServiceBusNamespace)); |
| | 39 | |
|
| 4 | 40 | | if (nsInfoXml == null) |
| | 41 | | { |
| 0 | 42 | | throw new ServiceBusException(true, "Unknown error."); |
| | 43 | | } |
| | 44 | |
|
| 56 | 45 | | foreach (var element in nsInfoXml.Elements()) |
| | 46 | | { |
| 24 | 47 | | switch (element.Name.LocalName) |
| | 48 | | { |
| | 49 | | case "CreatedTime": |
| 4 | 50 | | nsInfo.CreatedTime = DateTimeOffset.Parse(element.Value, CultureInfo.InvariantCulture); |
| 4 | 51 | | break; |
| | 52 | | case "ModifiedTime": |
| 4 | 53 | | nsInfo.ModifiedTime = DateTimeOffset.Parse(element.Value, CultureInfo.InvariantCulture); |
| 4 | 54 | | break; |
| | 55 | | case "Name": |
| 4 | 56 | | nsInfo.Name = element.Value; |
| 4 | 57 | | break; |
| | 58 | | case "Alias": |
| 0 | 59 | | nsInfo.Alias = element.Value; |
| 0 | 60 | | break; |
| | 61 | | case "MessagingUnits": |
| 4 | 62 | | _ = int.TryParse(element.Value, out var units); |
| 4 | 63 | | nsInfo.MessagingUnits = units; |
| 4 | 64 | | break; |
| | 65 | | case "NamespaceType": |
| 4 | 66 | | nsInfo.NamespaceType = element.Value; |
| 4 | 67 | | break; |
| | 68 | | case "MessagingSKU": |
| 4 | 69 | | nsInfo.MessagingSku = element.Value; |
| | 70 | | break; |
| | 71 | | } |
| | 72 | | } |
| | 73 | |
|
| 4 | 74 | | return nsInfo; |
| | 75 | | } |
| | 76 | | } |
| | 77 | | } |