/// <summary>
        /// Filters gallery templates based on the passed options.
        /// </summary>
        /// <param name="options">The filter options</param>
        /// <returns>The filtered list</returns>
        public virtual List<PSGalleryItem> FilterGalleryTemplates(FilterGalleryTemplatesOptions options)
        {
            List<string> filterStrings = new List<string>();
            ItemListParameters parameters = null;
            List<GalleryItem> result = new List<GalleryItem>();

            if (!string.IsNullOrEmpty(options.Identity))
            {
                result.Add(GalleryClient.Items.Get(options.Identity).Item);
            }
            else
            {
                result.AddRange(QueryGalleryTemplates(options, filterStrings, parameters));
            }

            if (!options.AllVersions && result.Count > 1)
            {
                // Take only the most recent version
                GalleryItem mostRecentTemplate = MostRecentTemplate(result);
                if (mostRecentTemplate != null)
                {
                    return new List<PSGalleryItem>() { mostRecentTemplate.ToPSGalleryItem() };
                }
            }

            return result.Select(i => i.ToPSGalleryItem()).ToList();
        }
示例#2
0
        /// <summary>
        /// Filters gallery templates based on the passed options.
        /// </summary>
        /// <param name="options">The filter options</param>
        /// <returns>The filtered list</returns>
        public virtual List <PSGalleryItem> FilterGalleryTemplates(FilterGalleryTemplatesOptions options)
        {
            List <string>      filterStrings = new List <string>();
            ItemListParameters parameters    = null;
            List <GalleryItem> result        = new List <GalleryItem>();

            if (!string.IsNullOrEmpty(options.Identity))
            {
                result.Add(GalleryClient.Items.Get(options.Identity).Item);
            }
            else
            {
                result.AddRange(QueryGalleryTemplates(options, filterStrings, parameters));
            }

            if (!options.AllVersions && result.Count > 1)
            {
                // Take only the most recent version
                GalleryItem mostRecentTemplate = MostRecentTemplate(result);
                if (mostRecentTemplate != null)
                {
                    return(new List <PSGalleryItem>()
                    {
                        mostRecentTemplate.ToPSGalleryItem()
                    });
                }
            }

            return(result.Select(i => i.ToPSGalleryItem()).ToList());
        }
        public override void ExecuteCmdlet()
        {
            FilterGalleryTemplatesOptions options = new FilterGalleryTemplatesOptions()
            {
                Category = Category,
                Identity = Identity,
                Publisher = Publisher
            };

            List<PSGalleryItem> galleryItems = GalleryTemplatesClient.FilterGalleryTemplates(options);

            if (galleryItems != null)
            {
                if (galleryItems.Count == 1 && !string.IsNullOrEmpty(Identity))
                {
                    WriteObject(galleryItems[0]);
                }
                else
                {
                    List<PSObject> output = new List<PSObject>();
                    galleryItems.Where(gi => !gi.Identity.EndsWith("-placeholder"))
                    .OrderBy(gi => gi.Identity)
                    .ToList()
                    .ForEach(gi => output.Add(base.ConstructPSObject(
                        null,
                        "Publisher", gi.Publisher,
                        "Identity", gi.Identity)));
                    WriteObject(output, true);
                }
            }
        }
示例#4
0
        /// <summary>
        /// Filters gallery templates based on the passed options.
        /// </summary>
        /// <param name="options">The filter options</param>
        /// <returns>The filtered list</returns>
        public virtual List <PSGalleryItem> FilterGalleryTemplates(FilterGalleryTemplatesOptions options)
        {
            List <string>      filterStrings = new List <string>();
            ItemListParameters parameters    = null;
            List <GalleryItem> result        = new List <GalleryItem>();

            if (!string.IsNullOrEmpty(options.Identity))
            {
                result.Add(GalleryClient.Items.Get(options.Identity).Item);
            }
            else
            {
                result.AddRange(QueryGalleryTemplates(options, filterStrings, parameters));
            }

            if (!options.AllVersions && result.Count > 1)
            {
                if (!string.IsNullOrEmpty(options.Publisher) && string.IsNullOrEmpty(options.ApplicationName) && string.IsNullOrEmpty(options.Identity))
                {
                    // we return a list of the most recent templates, for each name.
                    List <GalleryItem>   latest        = new List <GalleryItem>();
                    IEnumerable <string> distinctNames = result.Select(g => g.Name).Distinct();
                    foreach (var name in distinctNames)
                    {
                        List <GalleryItem> galleryItems   = result.Where(x => x.Name.Equals(name)).ToList();
                        GalleryItem        recentTemplate = this.MostRecentTemplate(galleryItems);
                        if (recentTemplate != null)
                        {
                            latest.Add(recentTemplate);
                        }
                    }

                    return(latest.Select(i => i.ToPSGalleryItem()).ToList());
                }

                // Take only the most recent version
                GalleryItem mostRecentTemplate = MostRecentTemplate(result);
                if (mostRecentTemplate != null)
                {
                    return(new List <PSGalleryItem>()
                    {
                        mostRecentTemplate.ToPSGalleryItem()
                    });
                }
            }

            return(result.Select(i => i.ToPSGalleryItem()).ToList());
        }
        /// <summary>
        /// Filters gallery templates based on the passed options.
        /// </summary>
        /// <param name="options">The filter options</param>
        /// <returns>The filtered list</returns>
        public virtual List<PSGalleryItem> FilterGalleryTemplates(FilterGalleryTemplatesOptions options)
        {
            List<string> filterStrings = new List<string>();
            ItemListParameters parameters = null;
            List<GalleryItem> result = new List<GalleryItem>();

            if (!string.IsNullOrEmpty(options.Identity))
            {
                result.Add(GalleryClient.Items.Get(options.Identity).Item);
            }
            else
            {
                result.AddRange(QueryGalleryTemplates(options, filterStrings, parameters));
            }

            return result.Select(i => i.ToPSGalleryItem()).ToList();
        }
        /// <summary>
        /// Filters gallery templates based on the passed options.
        /// </summary>
        /// <param name="options">The filter options</param>
        /// <returns>The filtered list</returns>
        public virtual List <PSGalleryItem> FilterGalleryTemplates(FilterGalleryTemplatesOptions options)
        {
            List <string>      filterStrings = new List <string>();
            ItemListParameters parameters    = null;
            List <GalleryItem> result        = new List <GalleryItem>();

            if (!string.IsNullOrEmpty(options.Identity))
            {
                result.Add(GalleryClient.Items.Get(options.Identity).Item);
            }
            else
            {
                result.AddRange(QueryGalleryTemplates(options, filterStrings, parameters));
            }

            return(result.Select(i => i.ToPSGalleryItem()).ToList());
        }
        /// <summary>
        /// Filters gallery templates based on the passed options.
        /// </summary>
        /// <param name="options">The filter options</param>
        /// <returns>The filtered list</returns>
        public virtual List<PSGalleryItem> FilterGalleryTemplates(FilterGalleryTemplatesOptions options)
        {
            List<string> filterStrings = new List<string>();
            ItemListParameters parameters = null;
            List<GalleryItem> result = new List<GalleryItem>();

            if (!string.IsNullOrEmpty(options.Identity))
            {
                result.Add(GalleryClient.Items.Get(options.Identity).Item);
            }
            else
            {
                result.AddRange(QueryGalleryTemplates(options, filterStrings, parameters));
            }

            if (!options.AllVersions && result.Count > 1)
            {
                if (!string.IsNullOrEmpty(options.Publisher) && string.IsNullOrEmpty(options.ApplicationName) && string.IsNullOrEmpty(options.Identity))
                {
                    // we return a list of the most recent templates, for each name.
                    List<GalleryItem> latest = new List<GalleryItem>();
                    IEnumerable<string> distinctNames = result.Select(g => g.Name).Distinct();
                    foreach (var name in distinctNames)
                    {
                        List<GalleryItem> galleryItems = result.Where(x => x.Name.Equals(name)).ToList();
                        GalleryItem recentTemplate = this.MostRecentTemplate(galleryItems);
                        if (recentTemplate != null)
                        {
                            latest.Add(recentTemplate);
                        }
                    }

                    return latest.Select(i => i.ToPSGalleryItem()).ToList();
                }

                // Take only the most recent version
                GalleryItem mostRecentTemplate = MostRecentTemplate(result);
                if (mostRecentTemplate != null)
                {
                    return new List<PSGalleryItem>() { mostRecentTemplate.ToPSGalleryItem() };
                }
            }

            return result.Select(i => i.ToPSGalleryItem()).ToList();
        }
        public override void ExecuteCmdlet()
        {
            WriteWarning("This cmdlet is being deprecated and will be removed in a future release.");
            FilterGalleryTemplatesOptions options = new FilterGalleryTemplatesOptions()
            {
                Category = Category,
                Identity = Identity,
                Publisher = Publisher,
                ApplicationName = ApplicationName,
                AllVersions = AllVersions
            };

            if (Category == null && Identity == null && Publisher == null && ApplicationName == null)
            {
                // if there are no filter parameters, return everything
                options.AllVersions = true;
            }

            List<PSGalleryItem> galleryItems = GalleryTemplatesClient.FilterGalleryTemplates(options);

            if (galleryItems != null)
            {
                if (galleryItems.Count == 1 && !string.IsNullOrEmpty(Identity))
                {
                    WriteObject(galleryItems[0]);
                }
                else
                {
                    List<PSObject> output = new List<PSObject>();
                    galleryItems.Where(gi => !gi.Identity.EndsWith("-placeholder"))
                    .OrderBy(gi => gi.Identity)
                    .ToList()
                    .ForEach(gi => output.Add(base.ConstructPSObject(
                        null,
                        "Publisher", gi.Publisher,
                        "Identity", gi.Identity)));
                    WriteObject(output, true);
                }
            }
        }
示例#9
0
        private List <GalleryItem> QueryGalleryTemplates(FilterGalleryTemplatesOptions options, List <string> filterStrings, ItemListParameters parameters)
        {
            if (!string.IsNullOrEmpty(options.Publisher))
            {
                filterStrings.Add(FilterString.Generate <ItemListFilter>(f => f.Publisher == options.Publisher));
            }

            if (!string.IsNullOrEmpty(options.Category))
            {
                filterStrings.Add(FilterString.Generate <ItemListFilter>(f => f.CategoryIds.Contains(options.Category)));
            }

            if (filterStrings.Count > 0)
            {
                parameters = new ItemListParameters()
                {
                    Filter = string.Join(" and ", filterStrings)
                };
            }

            List <GalleryItem> galleryItems = GalleryClient.Items.List(parameters).Items.ToList();

            if (!string.IsNullOrEmpty(options.ApplicationName))
            {
                List <GalleryItem> result = new List <GalleryItem>();
                string             wildcardApplicationName = Regex.Escape(options.ApplicationName).Replace(@"\*", ".*").Replace(@"\?", ".");
                Regex regex = new Regex(wildcardApplicationName, RegexOptions.IgnoreCase);
                foreach (var galleryItem in galleryItems)
                {
                    if (regex.IsMatch(galleryItem.Name))
                    {
                        result.Add(galleryItem);
                    }
                }

                return(result);
            }

            return(galleryItems);
        }
        private List <GalleryItem> QueryGalleryTemplates(FilterGalleryTemplatesOptions options, List <string> filterStrings, ItemListParameters parameters)
        {
            if (!string.IsNullOrEmpty(options.Publisher))
            {
                filterStrings.Add(FilterString.Generate <ItemListFilter>(f => f.Publisher == options.Publisher));
            }

            if (!string.IsNullOrEmpty(options.Category))
            {
                filterStrings.Add(FilterString.Generate <ItemListFilter>(f => f.CategoryIds.Contains(options.Category)));
            }

            if (filterStrings.Count > 0)
            {
                parameters = new ItemListParameters()
                {
                    Filter = string.Join(" and ", filterStrings)
                };
            }

            return(GalleryClient.Items.List(parameters).Items.ToList());
        }
        public void GetsGalleryTemplates()
        {
            FilterGalleryTemplatesOptions options = new FilterGalleryTemplatesOptions()
            {
                Category = "category",
                Identity = "hobba",
                Publisher = "Microsoft"
            };
            FilterGalleryTemplatesOptions actual = new FilterGalleryTemplatesOptions();
            List<PSGalleryItem> result = new List<PSGalleryItem>()
            {
                new PSGalleryItem()
                {
                    Publisher = "Microsoft",
                    Identity = "T1"
                },
                new PSGalleryItem()
                {
                    Publisher = "Microsoft",
                    Identity = "T2"
                },
            };
            galleryTemplatesClientMock.Setup(f => f.FilterGalleryTemplates(It.IsAny<FilterGalleryTemplatesOptions>()))
                .Returns(result)
                .Callback((FilterGalleryTemplatesOptions o) => actual = o);

            cmdlet.Category = options.Category;
            cmdlet.Identity = options.Identity;
            cmdlet.Publisher = options.Publisher;

            cmdlet.ExecuteCmdlet();

            Assert.Equal(2, result.Count);
            Assert.True(result.All(g => g.Publisher == "Microsoft"));

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<List<PSObject>>(), true), Times.Once());
        }
        public void FiltersGalleryTemplatesLatestVersion()
        {
            string filterString = FilterString.Generate<ItemListFilter>(f => f.Publisher == "Microsoft");
            ItemListParameters actual = new ItemListParameters();
            galleryClientMock.Setup(f => f.Items.ListAsync(It.IsAny<ItemListParameters>(), new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ItemListResult
                {
                    Items = new List<GalleryItem>()
                    {
                        new GalleryItem()
                        {
                            Name = "Template0",
                            Publisher = "Microsoft",
                            Version = "0.0.0.0"
                        },
                        new GalleryItem()
                        {
                            Name = "Template1",
                            Publisher = "Microsoft",
                            Version = "0.0.0.1"
                        },
                        new GalleryItem()
                        {
                            Name = "Template2",
                            Publisher = "Microsoft",
                            Version = "0.0.0.2"
                        }
                    }
                }))
                .Callback((ItemListParameters p, CancellationToken c) => actual = p);

            FilterGalleryTemplatesOptions options = new FilterGalleryTemplatesOptions()
            {
                Publisher = "Microsoft"
            };

            List<PSGalleryItem> result = galleryTemplatesClient.FilterGalleryTemplates(options);

            Assert.Equal(1, result.Count);
            Assert.Equal("Template2", result[0].Name);
        }
        public void FiltersGalleryTemplatesUsingComplexQuery()
        {
            string filterString = "Publisher eq 'Microsoft' and CategoryIds/any(c: c eq 'awesome')";
            ItemListParameters actual = new ItemListParameters();
            galleryClientMock.Setup(f => f.Items.ListAsync(It.IsAny<ItemListParameters>(), new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ItemListResult
                {
                    Items = new List<GalleryItem>()
                    {
                        new GalleryItem()
                        {
                            Name = "Template1",
                            Publisher = "Microsoft"
                        },
                        new GalleryItem()
                        {
                            Name = "Template2",
                            Publisher = "Microsoft"
                        }
                    }
                }))
                .Callback((ItemListParameters p, CancellationToken c) => actual = p);

            FilterGalleryTemplatesOptions options = new FilterGalleryTemplatesOptions()
            {
                Publisher = "Microsoft",
                Category = "awesome",
                AllVersions = true
            };

            List<PSGalleryItem> result = galleryTemplatesClient.FilterGalleryTemplates(options);

            Assert.Equal(2, result.Count);
            Assert.Equal(filterString, actual.Filter);
        }
        private List<GalleryItem> QueryGalleryTemplates(FilterGalleryTemplatesOptions options, List<string> filterStrings, ItemListParameters parameters)
        {
            if (!string.IsNullOrEmpty(options.Publisher))
            {
                filterStrings.Add(FilterString.Generate<ItemListFilter>(f => f.Publisher == options.Publisher));
            }

            if (!string.IsNullOrEmpty(options.Category))
            {
                filterStrings.Add(FilterString.Generate<ItemListFilter>(f => f.CategoryIds.Contains(options.Category)));
            }

            if (filterStrings.Count > 0)
            {
                parameters = new ItemListParameters() { Filter = string.Join(" and ", filterStrings) };
            }

            List<GalleryItem> galleryItems = GalleryClient.Items.List(parameters).Items.ToList();
            if (!string.IsNullOrEmpty(options.ApplicationName))
            {
                List<GalleryItem> result = new List<GalleryItem>();
                string wildcardApplicationName = Regex.Escape(options.ApplicationName).Replace(@"\*", ".*").Replace(@"\?", ".");
                Regex regex = new Regex(wildcardApplicationName, RegexOptions.IgnoreCase);
                foreach (var galleryItem in galleryItems)
                {
                    if (regex.IsMatch(galleryItem.Name))
                    {
                        result.Add(galleryItem);
                    }
                }

                return result;
            }

            return galleryItems;
        }
        private List<GalleryItem> QueryGalleryTemplates(FilterGalleryTemplatesOptions options, List<string> filterStrings, ItemListParameters parameters)
        {
            if (!string.IsNullOrEmpty(options.Publisher))
            {
                filterStrings.Add(FilterString.Generate<ItemListFilter>(f => f.Publisher == options.Publisher));
            }

            if (!string.IsNullOrEmpty(options.Category))
            {
                filterStrings.Add(FilterString.Generate<ItemListFilter>(f => f.CategoryIds.Contains(options.Category)));
            }

            if (filterStrings.Count > 0)
            {
                parameters = new ItemListParameters() { Filter = string.Join(" and ", filterStrings) };
            }

            return GalleryClient.Items.List(parameters).Items.ToList();
        }