/// <summary>
 /// Get the list of all container based on the given query filter
 /// string.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.BackupServices.IContainerOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required.
 /// </param>
 /// <param name='resourceName'>
 /// Required.
 /// </param>
 /// <param name='parameters'>
 /// Optional. Container query parameters.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// The definition of a CSMContainerListOperationResponse.
 /// </returns>
 public static CSMContainerListOperationResponse List(this IContainerOperations operations, string resourceGroupName, string resourceName, ContainerQueryParameters parameters, CustomRequestHeaders customRequestHeaders)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IContainerOperations)s).ListAsync(resourceGroupName, resourceName, parameters, customRequestHeaders);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        void ListContainersByFriendlyNameAndStatusReturnsValidResponse()
        {
            using (UndoContext undoContext = UndoContext.Current)
            {
                undoContext.Start();

                BackupServicesManagementClient client = GetServiceClient<BackupServicesManagementClient>();

                string containerId = ConfigurationManager.AppSettings["BMSContainerId"];
                string friendlyName = ConfigurationManager.AppSettings["BMSContainerFriendlyName"];
                string name = ConfigurationManager.AppSettings["BMSContainerName"];
                string containerType = ConfigurationManager.AppSettings["BMSContainerType"];
                string containerStatus = ConfigurationManager.AppSettings["BMSContainerStatus"];
                string containerHealthStatus = ConfigurationManager.AppSettings["BMSContainerHealthStatus"];
                string containerParentId = ConfigurationManager.AppSettings["BMSContainerParentId"];

                ContainerQueryParameters parameters = new ContainerQueryParameters();
                parameters.ContainerType = containerType;
                parameters.FriendlyName = friendlyName;
                parameters.Status = containerStatus;

                CSMContainerListOperationResponse response = client.Container.List(BackupServicesTestsBase.ResourceGroupName, BackupServicesTestsBase.ResourceName, parameters, GetCustomRequestHeaders());

                // Response Validation
                Assert.NotNull(response);
                Assert.True(response.StatusCode == HttpStatusCode.OK, "Status code should be OK");
                Assert.NotNull(response.CSMContainerListResponse);

                // Basic Validation
                Assert.True(response.CSMContainerListResponse.Value.Any(container =>
                {
                    return container.Id == containerId &&
                           string.Equals(container.Name, name, StringComparison.OrdinalIgnoreCase) &&
                           container.Properties != null &&
                           container.Properties.ContainerType == containerType &&
                           string.Equals(container.Properties.FriendlyName, friendlyName, StringComparison.OrdinalIgnoreCase) &&
                           container.Properties.HealthStatus == containerHealthStatus &&
                           container.Properties.ParentContainerId == containerParentId &&
                           container.Properties.Status == containerStatus;
                }), "Obtained container list doesn't contain the input container");
            }
        }
 /// <summary>
 /// Get the list of all container based on the given query filter
 /// string.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.BackupServices.IContainerOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required.
 /// </param>
 /// <param name='resourceName'>
 /// Required.
 /// </param>
 /// <param name='parameters'>
 /// Optional. Container query parameters.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// The definition of a CSMContainerListOperationResponse.
 /// </returns>
 public static Task<CSMContainerListOperationResponse> ListAsync(this IContainerOperations operations, string resourceGroupName, string resourceName, ContainerQueryParameters parameters, CustomRequestHeaders customRequestHeaders)
 {
     return operations.ListAsync(resourceGroupName, resourceName, parameters, customRequestHeaders, CancellationToken.None);
 }
 /// <summary>
 /// Gets all IaaSVM containers in the vault by friendly name
 /// </summary>
 /// <param name="parameters"></param>
 /// <returns></returns>
 public IEnumerable<CSMContainerResponse> ListContainers(string resourceGroupName, string resourceName, ContainerQueryParameters parameters)
 {
     var listResponse = AzureBackupClient.Container.ListAsync(resourceGroupName, resourceName, parameters, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
     return listResponse.CSMContainerListResponse.Value;
 }
        private bool IsDiscoveryNeeded(string vmName, string rgName, out CSMContainerResponse container)
        {
            bool isDiscoveryNeed = false;
            ContainerQueryParameters parameters = new ContainerQueryParameters()
            {
                ContainerType = ManagedContainerType.IaasVM.ToString(),
                FriendlyName = vmName,      
                Status = AzureBackupContainerRegistrationStatus.NotRegistered.ToString(),
            };

            //First check if container is discovered or not            
            var containers = AzureBackupClient.ListContainers(Vault.ResourceGroupName, Vault.Name, parameters);
            WriteDebug(String.Format(Resources.ContainerCountFromService, containers.Count()));
            if (containers.Count() == 0)
            {
                //Container is not discover
                WriteDebug(Resources.ContainerNotDiscovered);
                container = null;
                isDiscoveryNeed = true;
            }

            else
            {
                //We can have multiple container with same friendly name. 
                container = containers.Where(c => ContainerHelpers.GetRGNameFromId(c.Properties.ParentContainerId).Equals(rgName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); 
                if (container == null)
                {
                    //Container is not in list of registered container
                    WriteDebug(String.Format(Resources.DesiredContainerNotFound));
                    isDiscoveryNeed = true;
                }
            }
            return isDiscoveryNeed;
        }
        private List<AzureRMBackupContainer> GetManagedContainers(string resourceGroupName, string resourceName)
        {
            List<AzureRMBackupContainer> managedContainers = new List<AzureRMBackupContainer>();

            ContainerQueryParameters parameters = new ContainerQueryParameters();
            parameters.ContainerType = ManagedContainerType.IaasVM.ToString();
            parameters.FriendlyName = Name;
            if (Status != 0)
            {
                parameters.Status = Status.ToString();
            }

            List<CSMContainerResponse> containers = new List<CSMContainerResponse>();
            containers.AddRange(AzureBackupClient.ListContainers(resourceGroupName, resourceName, parameters));
            WriteDebug(string.Format(Resources.FetchedContainer , containers.Count()));

            // When resource group name is specified, remove all containers whose resource group name
            // doesn't match the given resource group name
            if (!string.IsNullOrEmpty(ManagedResourceGroupName))
            {
                containers.RemoveAll(container =>
                {
                    string rgName = ContainerHelpers.GetRGNameFromId(container.Properties.ParentContainerId);
                    return rgName != ManagedResourceGroupName;
                });
                WriteDebug(string.Format(Resources.ContainerCountAfterFilter, containers.Count));
            }

            // TODO: Container friendly name is not captures in Container response
            // BUG: Friendly name was previously assigned to ResourceName (vault name)
            managedContainers.AddRange(containers.ConvertAll(container =>
            {
                return new AzureRMBackupContainer(Vault, container);
            }));

            return managedContainers;
        }