PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ResourceManager/AzureBatch/Commands.Batch/Accounts/Helpers.cs

https://gitlab.com/jslee1/azure-powershell
C# | 199 lines | 155 code | 24 blank | 20 comment | 44 complexity | 06caba845ec30cd08ab3a73cce740113 MD5 | raw file
  1. // ----------------------------------------------------------------------------------
  2. //
  3. // Copyright Microsoft Corporation
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // ----------------------------------------------------------------------------------
  14. using Microsoft.Azure.Commands.Batch.Models;
  15. using Microsoft.Azure.Commands.Batch.Properties;
  16. using Microsoft.Azure.Management.Batch.Models;
  17. using Microsoft.WindowsAzure.Commands.Utilities.Common;
  18. using System;
  19. using System.Collections;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using System.Text;
  23. using Microsoft.Rest.Azure;
  24. namespace Microsoft.Azure.Commands.Batch
  25. {
  26. internal class Helpers
  27. {
  28. // copied from Resources\Commands.Resources
  29. private const string ExcludedTagPrefix = "hidden-related:/";
  30. public static PSTagValuePair Create(Hashtable hashtable)
  31. {
  32. if (hashtable == null ||
  33. !hashtable.ContainsKey("Name"))
  34. {
  35. return null;
  36. }
  37. PSTagValuePair tagValue = new PSTagValuePair();
  38. tagValue.Name = hashtable["Name"].ToString();
  39. if (hashtable.ContainsKey("Value"))
  40. {
  41. tagValue.Value = hashtable["Value"].ToString();
  42. }
  43. return tagValue;
  44. }
  45. public static Dictionary<string, string> CreateTagDictionary(Hashtable[] hashtableArray, bool validate)
  46. {
  47. Dictionary<string, string> tagDictionary = null;
  48. if (hashtableArray != null && hashtableArray.Length > 0)
  49. {
  50. tagDictionary = new Dictionary<string, string>();
  51. foreach (var tag in hashtableArray)
  52. {
  53. var tagValuePair = Create(tag);
  54. if (tagValuePair != null)
  55. {
  56. if (tagValuePair.Value != null)
  57. {
  58. tagDictionary[tagValuePair.Name] = tagValuePair.Value;
  59. }
  60. else
  61. {
  62. tagDictionary[tagValuePair.Name] = "";
  63. }
  64. }
  65. }
  66. }
  67. if (validate)
  68. {
  69. if (hashtableArray != null && hashtableArray.Length > 0 && hashtableArray[0].Count > 0 &&
  70. (tagDictionary == null || tagDictionary.Count == 0))
  71. {
  72. throw new ArgumentException(Resources.InvalidTagFormat);
  73. }
  74. if (hashtableArray != null && hashtableArray.Length > 0 && hashtableArray[0].Count > 0 &&
  75. (tagDictionary == null || hashtableArray.Length != tagDictionary.Count))
  76. {
  77. throw new ArgumentException(Resources.InvalidTagFormatNotUniqueName);
  78. }
  79. }
  80. return tagDictionary;
  81. }
  82. public static Hashtable[] CreateTagHashtable(IDictionary<string, string> dictionary)
  83. {
  84. List<Hashtable> tagHashtable = new List<Hashtable>();
  85. if (dictionary != null)
  86. {
  87. foreach (string key in dictionary.Keys)
  88. {
  89. tagHashtable.Add(new Hashtable
  90. {
  91. {"Name", key},
  92. {"Value", dictionary[key]}
  93. });
  94. }
  95. }
  96. return tagHashtable.ToArray();
  97. }
  98. public static string FormatTagsTable(Hashtable[] tags)
  99. {
  100. if (tags == null)
  101. {
  102. return null;
  103. }
  104. Hashtable emptyHashtable = new Hashtable
  105. {
  106. {"Name", string.Empty},
  107. {"Value", string.Empty}
  108. };
  109. StringBuilder resourcesTable = new StringBuilder();
  110. if (tags.Length > 0)
  111. {
  112. int maxNameLength = Math.Max("Name".Length, tags.Where(ht => ht.ContainsKey("Name")).DefaultIfEmpty(emptyHashtable).Max(ht => ht["Name"].ToString().Length));
  113. int maxValueLength = Math.Max("Value".Length, tags.Where(ht => ht.ContainsKey("Value")).DefaultIfEmpty(emptyHashtable).Max(ht => ht["Value"].ToString().Length));
  114. string rowFormat = "{0, -" + maxNameLength + "} {1, -" + maxValueLength + "}\r\n";
  115. resourcesTable.AppendLine();
  116. resourcesTable.AppendFormat(rowFormat, "Name", "Value");
  117. resourcesTable.AppendFormat(rowFormat,
  118. GeneralUtilities.GenerateSeparator(maxNameLength, "="),
  119. GeneralUtilities.GenerateSeparator(maxValueLength, "="));
  120. foreach (Hashtable tag in tags)
  121. {
  122. PSTagValuePair tagValuePair = Helpers.Create(tag);
  123. if (tagValuePair != null)
  124. {
  125. if (tagValuePair.Name.StartsWith(ExcludedTagPrefix))
  126. {
  127. continue;
  128. }
  129. if (tagValuePair.Value == null)
  130. {
  131. tagValuePair.Value = string.Empty;
  132. }
  133. resourcesTable.AppendFormat(rowFormat, tagValuePair.Name, tagValuePair.Value);
  134. }
  135. }
  136. }
  137. return resourcesTable.ToString();
  138. }
  139. /// <summary>
  140. /// Filters the subscription's account with the given tag.
  141. /// </summary>
  142. /// <param name="account">The account to filter on.</param>
  143. /// <param name="tag">The tag to filter on.</param>
  144. /// <returns>Whether or not the account's tags match with the given tag</returns>
  145. public static bool MatchesTag(AccountResource account, Hashtable tag)
  146. {
  147. if (tag != null && tag.Count >= 1)
  148. {
  149. PSTagValuePair tagValuePair = Helpers.Create(tag);
  150. if (tagValuePair == null)
  151. {
  152. throw new ArgumentException(Resources.InvalidTagFormat);
  153. }
  154. if (string.IsNullOrEmpty(tagValuePair.Value))
  155. {
  156. return ContainsTagWithName(account.Tags, tagValuePair.Name);
  157. }
  158. else
  159. {
  160. return ContainsTagWithName(account.Tags, tagValuePair.Name) &&
  161. account.Tags[tagValuePair.Name] == tagValuePair.Value;
  162. }
  163. }
  164. return true;
  165. }
  166. public static bool ContainsTagWithName(IDictionary<string, string> tags, string value)
  167. {
  168. if (tags == null)
  169. {
  170. return false;
  171. }
  172. return tags.Keys.Contains(value, StringComparer.OrdinalIgnoreCase);
  173. }
  174. }
  175. }