|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.Specialized; |
| 4 | +using System.Globalization; |
| 5 | +using System.Linq; |
| 6 | +using System.Runtime.Serialization; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using System.Web; |
| 10 | + |
| 11 | +namespace Microsoft.Azure.Commands.AzureBackup.Library |
| 12 | +{ |
| 13 | + public class ServiceNamespaces |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Namespace used by data contracts in BMS interface |
| 17 | + /// </summary> |
| 18 | + public const string BMSServiceNamespace = "http://windowscloudbackup.com/BackupManagement/V2014_09"; |
| 19 | + } |
| 20 | + |
| 21 | + [DataContract(Namespace = ServiceNamespaces.BMSServiceNamespace)] |
| 22 | + public class ManagementBaseObject |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + [DataContract(Namespace = ServiceNamespaces.BMSServiceNamespace)] |
| 27 | + [KnownType(typeof(ContainerQueryObject))] |
| 28 | + public class ManagementQueryObject : ManagementBaseObject |
| 29 | + { |
| 30 | + public static string DateTimeFormat = "yyyy-MM-dd hh:mm:ss tt"; |
| 31 | + public ManagementQueryObject() |
| 32 | + { |
| 33 | + } |
| 34 | + |
| 35 | + public virtual NameValueCollection GetNameValueCollection() |
| 36 | + { |
| 37 | + return new NameValueCollection(); |
| 38 | + } |
| 39 | + |
| 40 | + public virtual void Initialize(NameValueCollection filters) |
| 41 | + { |
| 42 | + ValidateCollection(filters); |
| 43 | + } |
| 44 | + |
| 45 | + public virtual List<string> GetSupportedFilters() |
| 46 | + { |
| 47 | + return new List<string>(); |
| 48 | + } |
| 49 | + |
| 50 | + public override string ToString() |
| 51 | + { |
| 52 | + return "ManagementQueryObject"; |
| 53 | + } |
| 54 | + |
| 55 | + private void ValidateCollection(NameValueCollection filters) |
| 56 | + { |
| 57 | + List<string> supportedFilters = GetSupportedFilters(); |
| 58 | + |
| 59 | + if (filters == null) |
| 60 | + { |
| 61 | + throw new ArgumentException("Null collection", "Filters"); |
| 62 | + } |
| 63 | + |
| 64 | + if (filters.Count != filters.AllKeys.Length) |
| 65 | + { |
| 66 | + throw new ArgumentException("Duplicate keys", "Filters"); |
| 67 | + } |
| 68 | + |
| 69 | + if (filters.Count > supportedFilters.Count) |
| 70 | + { |
| 71 | + string errMsg = String.Format("Unsupported filters specified, filter count: {0}", filters.Count); |
| 72 | + throw new ArgumentException(errMsg, "Filters"); |
| 73 | + } |
| 74 | + |
| 75 | + foreach (var filter in filters.AllKeys) |
| 76 | + { |
| 77 | + if (!supportedFilters.Contains(filter)) |
| 78 | + { |
| 79 | + string errMsg = String.Format("Unsupported filters specified, filter: {0}", filter); |
| 80 | + throw new ArgumentException(errMsg, "Filters"); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + //Helper functions that can be used |
| 86 | + //while convertion of different dataTypes |
| 87 | + //Override this to handle new dataTypes if required |
| 88 | + public static string GetString(string str) |
| 89 | + { |
| 90 | + return str; |
| 91 | + } |
| 92 | + |
| 93 | + public static string GetString(DateTime date) |
| 94 | + { |
| 95 | + return date.ToUniversalTime().ToString(DateTimeFormat, CultureInfo.InvariantCulture); |
| 96 | + } |
| 97 | + |
| 98 | + public static string GetString(List<string> strList) |
| 99 | + { |
| 100 | + String[] strtArr = strList.ToArray(); |
| 101 | + return String.Join(",", strtArr); |
| 102 | + } |
| 103 | + |
| 104 | + //Helper funtions to get the DataType format from string |
| 105 | + //Add new dataType conversion if required. |
| 106 | + public static T GetValueFromString<T>(string str) |
| 107 | + { |
| 108 | + if (typeof(T) == typeof(string)) |
| 109 | + { |
| 110 | + return (T)Convert.ChangeType(str, typeof(T)); |
| 111 | + } |
| 112 | + else if (typeof(T) == typeof(List<string>)) |
| 113 | + { |
| 114 | + String[] strArr = str.Split(','); |
| 115 | + List<string> list = new List<string>(strArr.Length); |
| 116 | + foreach (string s in strArr) |
| 117 | + { |
| 118 | + list.Add(s); |
| 119 | + } |
| 120 | + return (T)Convert.ChangeType(list, typeof(T)); |
| 121 | + } |
| 122 | + else if (typeof(T) == typeof(DateTime)) |
| 123 | + { |
| 124 | + DateTime date = DateTime.ParseExact(str, DateTimeFormat, CultureInfo.InvariantCulture); |
| 125 | + return (T)Convert.ChangeType(date, typeof(T)); |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + throw new NotSupportedException(); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + [DataContract(Namespace = ServiceNamespaces.BMSServiceNamespace)] |
| 135 | + public enum ContainerType |
| 136 | + { |
| 137 | + [EnumMember] |
| 138 | + Invalid = 0, |
| 139 | + |
| 140 | + [EnumMember] |
| 141 | + Unknown, |
| 142 | + |
| 143 | + // used by fabric adapter to populate discovered VMs |
| 144 | + [EnumMember] |
| 145 | + IaasVMContainer, |
| 146 | + |
| 147 | + // used by fabric adapter to populate discovered services |
| 148 | + // VMs are child containers of services they belong to |
| 149 | + [EnumMember] |
| 150 | + IaasVMServiceContainer |
| 151 | + } |
| 152 | + |
| 153 | + [DataContract(Namespace = ServiceNamespaces.BMSServiceNamespace)] |
| 154 | + public enum RegistrationStatus |
| 155 | + { |
| 156 | + [EnumMember] |
| 157 | + Invalid = 0, |
| 158 | + |
| 159 | + [EnumMember] |
| 160 | + Unknown, |
| 161 | + |
| 162 | + [EnumMember] |
| 163 | + NotRegistered, |
| 164 | + |
| 165 | + [EnumMember] |
| 166 | + Registered, |
| 167 | + |
| 168 | + [EnumMember] |
| 169 | + Registering, |
| 170 | + } |
| 171 | + |
| 172 | + [DataContract(Namespace = ServiceNamespaces.BMSServiceNamespace)] |
| 173 | + public class ContainerQueryObject : ManagementQueryObject |
| 174 | + { |
| 175 | + public const string ContainerTypeField = "ContainerType"; |
| 176 | + public const string ContainerStatusField = "ContainerStatus"; |
| 177 | + public const string ContainerFriendlyNameField = "FriendlyName"; |
| 178 | + |
| 179 | + [DataMember] |
| 180 | + public string Type { get; set; } |
| 181 | + |
| 182 | + [DataMember] |
| 183 | + public string Status { get; set; } |
| 184 | + |
| 185 | + [DataMember] |
| 186 | + public string FriendlyName { get; set; } |
| 187 | + |
| 188 | + public ContainerQueryObject() |
| 189 | + { |
| 190 | + } |
| 191 | + |
| 192 | + public ContainerQueryObject(string type, string status) |
| 193 | + { |
| 194 | + this.Type = type; |
| 195 | + this.Status = status; |
| 196 | + this.FriendlyName = string.Empty; |
| 197 | + } |
| 198 | + |
| 199 | + public ContainerQueryObject(string type, string status, string name) |
| 200 | + { |
| 201 | + this.Type = type; |
| 202 | + this.Status = status; |
| 203 | + this.FriendlyName = name; |
| 204 | + } |
| 205 | + |
| 206 | + public override List<string> GetSupportedFilters() |
| 207 | + { |
| 208 | + var filterList = base.GetSupportedFilters(); |
| 209 | + filterList.Add(ContainerTypeField); |
| 210 | + filterList.Add(ContainerStatusField); |
| 211 | + filterList.Add(ContainerFriendlyNameField); |
| 212 | + return filterList; |
| 213 | + } |
| 214 | + |
| 215 | + public override NameValueCollection GetNameValueCollection() |
| 216 | + { |
| 217 | + var collection = base.GetNameValueCollection(); |
| 218 | + |
| 219 | + if (!String.IsNullOrEmpty(Type)) |
| 220 | + { |
| 221 | + collection.Add(ContainerTypeField, Type); |
| 222 | + } |
| 223 | + |
| 224 | + if (!String.IsNullOrEmpty(Status)) |
| 225 | + { |
| 226 | + collection.Add(ContainerStatusField, Status); |
| 227 | + } |
| 228 | + |
| 229 | + if (!String.IsNullOrEmpty(FriendlyName)) |
| 230 | + { |
| 231 | + collection.Add(ContainerFriendlyNameField, FriendlyName); |
| 232 | + } |
| 233 | + return collection; |
| 234 | + } |
| 235 | + |
| 236 | + public override void Initialize(NameValueCollection collection) |
| 237 | + { |
| 238 | + base.Initialize(collection); |
| 239 | + |
| 240 | + if (collection[ContainerTypeField] != null) |
| 241 | + { |
| 242 | + SetType(collection[ContainerTypeField]); |
| 243 | + } |
| 244 | + |
| 245 | + if (collection[ContainerStatusField] != null) |
| 246 | + { |
| 247 | + SetStatus(collection[ContainerStatusField]); |
| 248 | + } |
| 249 | + |
| 250 | + if (collection[ContainerFriendlyNameField] != null) |
| 251 | + { |
| 252 | + SetFriendlyName(collection[ContainerFriendlyNameField]); |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + public override string ToString() |
| 257 | + { |
| 258 | + return String.Format("{0} ContainerTypeField: {1}, ContainerStatusField: {2}", base.ToString(), Type, Status); |
| 259 | + } |
| 260 | + |
| 261 | + private void SetType(string type) |
| 262 | + { |
| 263 | + ContainerType containerType; |
| 264 | + if (!Enum.TryParse<ContainerType>(type, out containerType) || containerType == ContainerType.Invalid || containerType == ContainerType.Unknown) |
| 265 | + { |
| 266 | + throw new ArgumentException("Invalid type filter", ContainerTypeField); |
| 267 | + } |
| 268 | + |
| 269 | + Type = type; |
| 270 | + } |
| 271 | + |
| 272 | + private void SetStatus(string status) |
| 273 | + { |
| 274 | + RegistrationStatus contatinerStatus; |
| 275 | + if (!Enum.TryParse<RegistrationStatus>(status, out contatinerStatus) || contatinerStatus == RegistrationStatus.Invalid || contatinerStatus == RegistrationStatus.Unknown) |
| 276 | + { |
| 277 | + throw new ArgumentException("Invalid status filter", ContainerStatusField); |
| 278 | + } |
| 279 | + |
| 280 | + Status = status; |
| 281 | + } |
| 282 | + |
| 283 | + private void SetFriendlyName(string name) |
| 284 | + { |
| 285 | + if (String.IsNullOrEmpty(name)) |
| 286 | + { |
| 287 | + throw new ArgumentException("Name is NullorEmpty", ContainerFriendlyNameField); |
| 288 | + } |
| 289 | + |
| 290 | + FriendlyName = name; |
| 291 | + } |
| 292 | + } |
| 293 | + |
| 294 | + public static class BackupManagementAPIHelper |
| 295 | + { |
| 296 | + public static string GetQueryString(NameValueCollection collection) |
| 297 | + { |
| 298 | + if (collection == null || collection.Count == 0) |
| 299 | + { |
| 300 | + return String.Empty; |
| 301 | + } |
| 302 | + |
| 303 | + var httpValueCollection = HttpUtility.ParseQueryString(String.Empty); |
| 304 | + httpValueCollection.Add(collection); |
| 305 | + |
| 306 | + return "&" + httpValueCollection.ToString(); |
| 307 | + } |
| 308 | + } |
| 309 | +} |
0 commit comments