/src/ResourceManager/AzureBatch/Commands.Batch/Accounts/Helpers.cs
C# | 199 lines | 155 code | 24 blank | 20 comment | 44 complexity | 06caba845ec30cd08ab3a73cce740113 MD5 | raw file
- // ----------------------------------------------------------------------------------
- //
- // Copyright Microsoft Corporation
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- // http://www.apache.org/licenses/LICENSE-2.0
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- // ----------------------------------------------------------------------------------
-
- using Microsoft.Azure.Commands.Batch.Models;
- using Microsoft.Azure.Commands.Batch.Properties;
- using Microsoft.Azure.Management.Batch.Models;
- using Microsoft.WindowsAzure.Commands.Utilities.Common;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Rest.Azure;
-
- namespace Microsoft.Azure.Commands.Batch
- {
- internal class Helpers
- {
- // copied from Resources\Commands.Resources
-
- private const string ExcludedTagPrefix = "hidden-related:/";
-
- public static PSTagValuePair Create(Hashtable hashtable)
- {
- if (hashtable == null ||
- !hashtable.ContainsKey("Name"))
- {
- return null;
- }
-
- PSTagValuePair tagValue = new PSTagValuePair();
- tagValue.Name = hashtable["Name"].ToString();
-
- if (hashtable.ContainsKey("Value"))
- {
- tagValue.Value = hashtable["Value"].ToString();
- }
-
- return tagValue;
- }
-
- public static Dictionary<string, string> CreateTagDictionary(Hashtable[] hashtableArray, bool validate)
- {
- Dictionary<string, string> tagDictionary = null;
- if (hashtableArray != null && hashtableArray.Length > 0)
- {
- tagDictionary = new Dictionary<string, string>();
- foreach (var tag in hashtableArray)
- {
- var tagValuePair = Create(tag);
- if (tagValuePair != null)
- {
- if (tagValuePair.Value != null)
- {
- tagDictionary[tagValuePair.Name] = tagValuePair.Value;
- }
- else
- {
- tagDictionary[tagValuePair.Name] = "";
- }
- }
- }
- }
-
- if (validate)
- {
- if (hashtableArray != null && hashtableArray.Length > 0 && hashtableArray[0].Count > 0 &&
- (tagDictionary == null || tagDictionary.Count == 0))
- {
- throw new ArgumentException(Resources.InvalidTagFormat);
- }
- if (hashtableArray != null && hashtableArray.Length > 0 && hashtableArray[0].Count > 0 &&
- (tagDictionary == null || hashtableArray.Length != tagDictionary.Count))
- {
- throw new ArgumentException(Resources.InvalidTagFormatNotUniqueName);
- }
- }
-
- return tagDictionary;
- }
-
- public static Hashtable[] CreateTagHashtable(IDictionary<string, string> dictionary)
- {
- List<Hashtable> tagHashtable = new List<Hashtable>();
- if (dictionary != null)
- {
- foreach (string key in dictionary.Keys)
- {
- tagHashtable.Add(new Hashtable
- {
- {"Name", key},
- {"Value", dictionary[key]}
- });
- }
- }
-
- return tagHashtable.ToArray();
- }
-
- public static string FormatTagsTable(Hashtable[] tags)
- {
- if (tags == null)
- {
- return null;
- }
-
- Hashtable emptyHashtable = new Hashtable
- {
- {"Name", string.Empty},
- {"Value", string.Empty}
- };
- StringBuilder resourcesTable = new StringBuilder();
-
- if (tags.Length > 0)
- {
- int maxNameLength = Math.Max("Name".Length, tags.Where(ht => ht.ContainsKey("Name")).DefaultIfEmpty(emptyHashtable).Max(ht => ht["Name"].ToString().Length));
- int maxValueLength = Math.Max("Value".Length, tags.Where(ht => ht.ContainsKey("Value")).DefaultIfEmpty(emptyHashtable).Max(ht => ht["Value"].ToString().Length));
-
- string rowFormat = "{0, -" + maxNameLength + "} {1, -" + maxValueLength + "}\r\n";
- resourcesTable.AppendLine();
- resourcesTable.AppendFormat(rowFormat, "Name", "Value");
- resourcesTable.AppendFormat(rowFormat,
- GeneralUtilities.GenerateSeparator(maxNameLength, "="),
- GeneralUtilities.GenerateSeparator(maxValueLength, "="));
-
- foreach (Hashtable tag in tags)
- {
- PSTagValuePair tagValuePair = Helpers.Create(tag);
- if (tagValuePair != null)
- {
- if (tagValuePair.Name.StartsWith(ExcludedTagPrefix))
- {
- continue;
- }
-
- if (tagValuePair.Value == null)
- {
- tagValuePair.Value = string.Empty;
- }
- resourcesTable.AppendFormat(rowFormat, tagValuePair.Name, tagValuePair.Value);
- }
- }
- }
-
- return resourcesTable.ToString();
- }
-
- /// <summary>
- /// Filters the subscription's account with the given tag.
- /// </summary>
- /// <param name="account">The account to filter on.</param>
- /// <param name="tag">The tag to filter on.</param>
- /// <returns>Whether or not the account's tags match with the given tag</returns>
- public static bool MatchesTag(AccountResource account, Hashtable tag)
- {
- if (tag != null && tag.Count >= 1)
- {
- PSTagValuePair tagValuePair = Helpers.Create(tag);
- if (tagValuePair == null)
- {
- throw new ArgumentException(Resources.InvalidTagFormat);
- }
-
- if (string.IsNullOrEmpty(tagValuePair.Value))
- {
- return ContainsTagWithName(account.Tags, tagValuePair.Name);
- }
- else
- {
- return ContainsTagWithName(account.Tags, tagValuePair.Name) &&
- account.Tags[tagValuePair.Name] == tagValuePair.Value;
- }
- }
-
- return true;
- }
-
- public static bool ContainsTagWithName(IDictionary<string, string> tags, string value)
- {
- if (tags == null)
- {
- return false;
- }
-
- return tags.Keys.Contains(value, StringComparer.OrdinalIgnoreCase);
- }
- }
- }