/// <summary>
        /// Filters the subscription's resource groups.
        /// </summary>
        /// <param name="name">The resource group name.</param>
        /// <param name="tag">The resource group tag.</param>
        /// <param name="detailed">Whether the  return is detailed or not.</param>
        /// <param name="location">The resource group location.</param>
        /// <returns>The filtered resource groups</returns>
        public virtual List <PSResourceGroup> FilterResourceGroups(string name, Hashtable tag, bool detailed, string location = null)
        {
            List <PSResourceGroup> result = new List <PSResourceGroup>();

            if (string.IsNullOrEmpty(name))
            {
                var response = ResourceManagementClient.ResourceGroups.List(null);
                List <ResourceGroupExtended> resourceGroups = ResourceManagementClient.ResourceGroups.List(null).ResourceGroups.ToList();

                while (!string.IsNullOrEmpty(response.NextLink))
                {
                    resourceGroups.AddRange(response.ResourceGroups);
                }

                resourceGroups = !string.IsNullOrEmpty(location)
                    ? resourceGroups.Where(resourceGroup => this.NormalizeLetterOrDigitToUpperInvariant(resourceGroup.Location).Equals(this.NormalizeLetterOrDigitToUpperInvariant(location))).ToList()
                    : resourceGroups;

                // TODO: Replace with server side filtering when available
                if (tag != null && tag.Count >= 1)
                {
                    PSTagValuePair tagValuePair = TagsConversionHelper.Create(tag);
                    if (tagValuePair == null)
                    {
                        throw new ArgumentException(ProjectResources.InvalidTagFormat);
                    }
                    if (string.IsNullOrEmpty(tagValuePair.Value))
                    {
                        resourceGroups =
                            resourceGroups.Where(rg => rg.Tags != null &&
                                                 rg.Tags.Keys.Contains(tagValuePair.Name,
                                                                       StringComparer.OrdinalIgnoreCase))
                            .Select(rg => rg).ToList();
                    }
                    else
                    {
                        resourceGroups =
                            resourceGroups.Where(rg => rg.Tags != null && rg.Tags.Keys.Contains(tagValuePair.Name,
                                                                                                StringComparer.OrdinalIgnoreCase))
                            .Where(rg => rg.Tags.Values.Contains(tagValuePair.Value,
                                                                 StringComparer.OrdinalIgnoreCase))
                            .Select(rg => rg).ToList();
                    }
                }
                result.AddRange(resourceGroups.Select(rg => rg.ToPSResourceGroup(this, detailed)));
            }
            else
            {
                try
                {
                    result.Add(ResourceManagementClient.ResourceGroups.Get(name).ResourceGroup.ToPSResourceGroup(this, detailed));
                }
                catch (CloudException)
                {
                    throw new ArgumentException(ProjectResources.ResourceGroupDoesntExists);
                }
            }

            return(result);
        }
        /// <summary>
        /// Get an existing resource or resources.
        /// </summary>
        /// <param name="parameters">The get parameters</param>
        /// <returns>List of resources</returns>
        public virtual List <PSResource> FilterPSResources(BasePSResourceParameters parameters)
        {
            List <PSResource> resources = new List <PSResource>();

            if (!string.IsNullOrEmpty(parameters.Name))
            {
                ResourceIdentity resourceIdentity = parameters.ToResourceIdentity();

                GenericResource getResult;

                try
                {
                    getResult = ResourceManagementClient.Resources.Get(
                        parameters.ResourceGroupName,
                        resourceIdentity.ResourceProviderNamespace,
                        resourceIdentity.ParentResourcePath,
                        resourceIdentity.ResourceType,
                        resourceIdentity.ResourceName,
                        resourceIdentity.ResourceProviderApiVersion);
                }
                catch (CloudException)
                {
                    throw new ArgumentException(ProjectResources.ResourceDoesntExists);
                }
                resources.Add(getResult.ToPSResource(this, false));
            }
            else
            {
                PSTagValuePair tagValuePair = new PSTagValuePair();
                if (parameters.Tag != null && parameters.Tag.Length == 1 && parameters.Tag[0] != null)
                {
                    tagValuePair = TagsConversionHelper.Create(parameters.Tag[0]);
                    if (tagValuePair == null)
                    {
                        throw new ArgumentException(ProjectResources.InvalidTagFormat);
                    }
                }
                var listResult = ResourceManagementClient.Resources.List(
                    new Rest.Azure.OData.ODataQuery <GenericResourceFilter>(
                        ResourceManager.Cmdlets.Components.QueryFilterBuilder.CreateFilter(
                            null,
                            parameters.ResourceGroupName,
                            parameters.ResourceType,
                            null,
                            tagValuePair.Name,
                            tagValuePair.Value,
                            null)));

                if (listResult != null)
                {
                    resources.AddRange(listResult.Select(r => r.ToPSResource(this, false)));
                }
            }
            return(resources);
        }
示例#3
0
        /// <summary>
        /// Get an existing resource or resources.
        /// </summary>
        /// <param name="parameters">The get parameters</param>
        /// <returns>List of resources</returns>
        public virtual List <PSResource> FilterPSResources(BasePSResourceParameters parameters)
        {
            List <PSResource> resources = new List <PSResource>();

            if (!string.IsNullOrEmpty(parameters.ResourceGroupName))
            {
                if (!ResourceManagementClient.ResourceGroups.CheckExistence(parameters.ResourceGroupName).Exists)
                {
                    throw new ArgumentException(ProjectResources.ResourceGroupDoesntExists);
                }
            }

            if (!string.IsNullOrEmpty(parameters.Name))
            {
                ResourceIdentity resourceIdentity = parameters.ToResourceIdentity();

                ResourceGetResult getResult;

                try
                {
                    getResult = ResourceManagementClient.Resources.Get(parameters.ResourceGroupName, resourceIdentity);
                }
                catch (CloudException)
                {
                    throw new ArgumentException(ProjectResources.ResourceDoesntExists);
                }

                resources.Add(getResult.Resource.ToPSResource(this));
            }
            else
            {
                PSTagValuePair tagValuePair = new PSTagValuePair();
                if (parameters.Tag != null && parameters.Tag.Length == 1 && parameters.Tag[0] != null)
                {
                    tagValuePair = TagsConversionHelper.Create(parameters.Tag[0]);
                    if (tagValuePair == null)
                    {
                        throw new ArgumentException(ProjectResources.InvalidTagFormat);
                    }
                }
                ResourceListResult listResult = ResourceManagementClient.Resources.List(new ResourceListParameters
                {
                    ResourceGroupName = parameters.ResourceGroupName,
                    ResourceType      = parameters.ResourceType,
                    TagName           = tagValuePair.Name,
                    TagValue          = tagValuePair.Value
                });

                if (listResult.Resources != null)
                {
                    resources.AddRange(listResult.Resources.Select(r => r.ToPSResource(this)));
                }
            }
            return(resources);
        }
示例#4
0
        /// <summary>
        /// Filters the subscription's resource groups.
        /// </summary>
        /// <param name="name">The resource group name.</param>
        /// <param name="tag">The resource group tag.</param>
        /// <returns>The filtered resource groups</returns>
        public virtual List <PSResourceGroup> FilterResourceGroups(string name, Hashtable tag)
        {
            List <PSResourceGroup> result = new List <PSResourceGroup>();

            if (string.IsNullOrEmpty(name))
            {
                IList <ResourceGroup> resourceGroups = ResourceManagementClient.ResourceGroups.List(null).ResourceGroups;
                // TODO: Replace with server side filtering when available
                if (tag != null && tag.Count >= 1)
                {
                    PSTagValuePair tagValuePair = TagsConversionHelper.Create(tag);
                    if (tagValuePair == null)
                    {
                        throw new ArgumentException(ProjectResources.InvalidTagFormat);
                    }
                    if (string.IsNullOrEmpty(tagValuePair.Value))
                    {
                        resourceGroups =
                            resourceGroups.Where(rg => rg.Tags != null &&
                                                 rg.Tags.Keys.Contains(tagValuePair.Name,
                                                                       StringComparer.OrdinalIgnoreCase))
                            .Select(rg => rg).ToList();
                    }
                    else
                    {
                        resourceGroups =
                            resourceGroups.Where(rg => rg.Tags != null && rg.Tags.Keys.Contains(tagValuePair.Name,
                                                                                                StringComparer.OrdinalIgnoreCase))
                            .Where(rg => rg.Tags.Values.Contains(tagValuePair.Value,
                                                                 StringComparer.OrdinalIgnoreCase))
                            .Select(rg => rg).ToList();
                    }
                }
                result.AddRange(resourceGroups.Select(rg => rg.ToPSResourceGroup(this)));
            }
            else
            {
                try
                {
                    result.Add(ResourceManagementClient.ResourceGroups.Get(name).ResourceGroup.ToPSResourceGroup(this));
                }
                catch (CloudException)
                {
                    throw new ArgumentException(ProjectResources.ResourceGroupDoesntExists);
                }
            }

            return(result);
        }
        public static string ConstructTagsTable(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 = TagsConversionHelper.Create(tag);
                    if (tagValuePair != null)
                    {
                        if (tagValuePair.Name.StartsWith(TagsClient.ExecludedTagPrefix))
                        {
                            continue;
                        }

                        if (tagValuePair.Value == null)
                        {
                            tagValuePair.Value = string.Empty;
                        }
                        resourcesTable.AppendFormat(rowFormat, tagValuePair.Name, tagValuePair.Value);
                    }
                }
            }

            return(resourcesTable.ToString());
        }