public async Task<IEnumerable<DeploymentExtended>> ListDeployments(
                        [Metadata("Resource group name", "The name of resource group to list the deployments for")]string resourceGroupName
            )
        {
            var client = await ResourceUtilities.GetClient().ConfigureAwait(continueOnCapturedContext: false);

            var parameters = new Microsoft.Azure.Management.Resources.Models.DeploymentListParameters();
            var result = await client.Deployments.ListAsync(resourceGroupName, parameters, CancellationToken.None).ConfigureAwait(continueOnCapturedContext: false);

            return result.Deployments;
        }
        public void CancelsActiveDeployment()
        {
            DeploymentListParameters actualParameters = new DeploymentListParameters();
            deploymentsMock.Setup(f => f.ListAsync(
                resourceGroupName,
                It.IsAny<DeploymentListParameters>(),
                new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentListResult
                {
                    Deployments = new List<Deployment>()
                    {
                        new Deployment()
                        {
                            Name = deploymentName + 1,
                            Properties = new DeploymentProperties()
                            {
                                Mode = DeploymentMode.Incremental,
                                TemplateLink = new TemplateLink()
                                {
                                    Uri = new Uri("http://microsoft1.com")
                                },
                                ProvisioningState = ProvisioningState.Succeeded
                            }
                        },
                        new Deployment()
                        {
                            Name = deploymentName + 2,
                            Properties = new DeploymentProperties()
                            {
                                Mode = DeploymentMode.Incremental,
                                TemplateLink = new TemplateLink()
                                {
                                    Uri = new Uri("http://microsoft1.com")
                                },
                                ProvisioningState = ProvisioningState.Failed
                            }
                        },
                        new Deployment()
                        {
                            Name = deploymentName + 3,
                            Properties = new DeploymentProperties()
                            {
                                Mode = DeploymentMode.Incremental,
                                TemplateLink = new TemplateLink()
                                {
                                    Uri = new Uri("http://microsoft1.com")
                                },
                                ProvisioningState = ProvisioningState.Running
                            }
                        }
                    }
                }))
                .Callback((string rgn, DeploymentListParameters p, CancellationToken t) => { actualParameters = p; });

            resourcesClient.CancelDeployment(resourceGroupName, null);

            deploymentsMock.Verify(f => f.CancelAsync(resourceGroupName, deploymentName + 3, new CancellationToken()), Times.Once());
        }
        public void FiltersResourceGroupDeployments()
        {
            FilterResourceGroupDeploymentOptions options = new FilterResourceGroupDeploymentOptions()
            {
                ResourceGroupName = resourceGroupName
            };
            DeploymentListParameters actualParameters = new DeploymentListParameters();
            deploymentsMock.Setup(f => f.ListAsync(
                resourceGroupName,
                It.IsAny<DeploymentListParameters>(),
                new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentListResult
                {
                    Deployments = new List<Deployment>()
                    {
                        new Deployment()
                        {
                            Name = deploymentName + 1,
                            Properties = new DeploymentProperties()
                            {
                                Mode = DeploymentMode.Incremental,
                                CorrelationId = "123",
                                TemplateLink = new TemplateLink()
                                {
                                    Uri = new Uri("http://microsoft1.com")
                                }
                            }
                        }
                    },
                    NextLink = "nextLink"
                }))
                .Callback((string rgn, DeploymentListParameters p, CancellationToken t) => { actualParameters = p; });

            deploymentsMock.Setup(f => f.ListNextAsync(
                "nextLink",
                new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentListResult
                {
                    Deployments = new List<Deployment>()
                    {
                        new Deployment()
                        {
                            Name = deploymentName + 2,
                            Properties = new DeploymentProperties()
                            {
                                Mode = DeploymentMode.Incremental,
                                CorrelationId = "456",
                                TemplateLink = new TemplateLink()
                                {
                                    Uri = new Uri("http://microsoft2.com")
                                }
                            }
                        }
                    }
                }));

            List<PSResourceGroupDeployment> result = resourcesClient.FilterResourceGroupDeployments(options);

            Assert.Equal(2, result.Count);
            Assert.Equal(deploymentName + 1, result[0].DeploymentName);
            Assert.Equal("123", result[0].CorrelationId);
            Assert.Equal(resourceGroupName, result[0].ResourceGroupName);
            Assert.Equal(DeploymentMode.Incremental, result[0].Mode);
            Assert.Equal(new Uri("http://microsoft1.com").ToString(), result[0].TemplateLink.Uri.ToString());

            Assert.Equal(deploymentName + 2, result[1].DeploymentName);
            Assert.Equal(resourceGroupName, result[1].ResourceGroupName);
            Assert.Equal("456", result[1].CorrelationId);
            Assert.Equal(DeploymentMode.Incremental, result[1].Mode);
            Assert.Equal(new Uri("http://microsoft2.com").ToString(), result[1].TemplateLink.Uri.ToString());
        }
        /// <summary>
        /// Filters the resource group deployments
        /// </summary>
        /// <param name="options">The filtering options</param>
        /// <returns>The filtered list of deployments</returns>
        public virtual List<PSResourceGroupDeployment> FilterResourceGroupDeployments(FilterResourceGroupDeploymentOptions options)
        {
            List<PSResourceGroupDeployment> deployments = new List<PSResourceGroupDeployment>();
            string resourceGroup = options.ResourceGroupName;
            string name = options.DeploymentName;
            List<string> excludedProvisioningStates = options.ExcludedProvisioningStates ?? new List<string>();
            List<string> provisioningStates = options.ProvisioningStates ?? new List<string>();

            if (!string.IsNullOrEmpty(resourceGroup) && !string.IsNullOrEmpty(name))
            {
                deployments.Add(ResourceManagementClient.Deployments.Get(resourceGroup, name).ToPSResourceGroupDeployment(options.ResourceGroupName));
            }
            else if (!string.IsNullOrEmpty(resourceGroup))
            {
                DeploymentListParameters parameters = new DeploymentListParameters();

                if (provisioningStates.Count == 1)
                {
                    parameters.ProvisioningState = provisioningStates.First();
                }

                DeploymentListResult result = ResourceManagementClient.Deployments.List(resourceGroup, parameters);

                deployments.AddRange(result.Deployments.Select(d => d.ToPSResourceGroupDeployment(options.ResourceGroupName)));

                while (!string.IsNullOrEmpty(result.NextLink))
                {
                    result = ResourceManagementClient.Deployments.ListNext(result.NextLink);
                    deployments.AddRange(result.Deployments.Select(d => d.ToPSResourceGroupDeployment(options.ResourceGroupName)));
                }
            }

            if (provisioningStates.Count > 1)
            {
                return deployments.Where(d => provisioningStates
                    .Any(s => s.Equals(d.ProvisioningState, StringComparison.OrdinalIgnoreCase))).ToList();
            }
            else if (provisioningStates.Count == 0 && excludedProvisioningStates.Count > 0)
            {
                return deployments.Where(d => excludedProvisioningStates
                    .All(s => !s.Equals(d.ProvisioningState, StringComparison.OrdinalIgnoreCase))).ToList();
            }
            else
            {
                return deployments;
            }
        }
 /// <summary>
 /// Get a list of deployments.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Resources.IDeploymentOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group to filter by. The name is
 /// case insensitive.
 /// </param>
 /// <param name='parameters'>
 /// Optional. Query parameters. If null is passed returns all
 /// deployments.
 /// </param>
 /// <returns>
 /// List of deployments.
 /// </returns>
 public static Task<DeploymentListResult> ListAsync(this IDeploymentOperations operations, string resourceGroupName, DeploymentListParameters parameters)
 {
     return operations.ListAsync(resourceGroupName, parameters, CancellationToken.None);
 }
 /// <summary>
 /// Get a list of deployments.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Resources.IDeploymentOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group to filter by. The name is
 /// case insensitive.
 /// </param>
 /// <param name='parameters'>
 /// Optional. Query parameters. If null is passed returns all
 /// deployments.
 /// </param>
 /// <returns>
 /// List of deployments.
 /// </returns>
 public static DeploymentListResult List(this IDeploymentOperations operations, string resourceGroupName, DeploymentListParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IDeploymentOperations)s).ListAsync(resourceGroupName, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }