public void ListProtectableObjectTest()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                string resourceNamespace = ConfigurationManager.AppSettings["ResourceNamespace"];

                var client = GetServiceClient<RecoveryServicesBackupManagementClient>(resourceNamespace);

                ProtectableObjectListQueryParameters queryParams = new ProtectableObjectListQueryParameters();

                PaginationRequest paginationParam = new PaginationRequest();

                ProtectableObjectTestHelper poTestHelper = new ProtectableObjectTestHelper(client);
                ProtectableObjectListResponse response = poTestHelper.ListProtectableObjects(queryParams, paginationParam);

                string protectableObjectName = ConfigurationManager.AppSettings["RsVaultIaasV1ContainerUniqueName"];
                Assert.True(
                    response.ItemList.ProtectableObjects.Any(
                        protectableObject =>
                        {
                            return protectableObject.Properties.GetType() == typeof(AzureIaaSClassicComputeVMProtectableItem) &&
                                    protectableObject.Name == protectableObjectName;
                        }),
                        "Retrieved list of containers doesn't contain AzureIaaSClassicComputeVMProtectable Item");
            }
        }
 /// <summary>
 /// Lists all the protectable objects within your subscription
 /// according to the query filter and the pagination parameters.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.RecoveryServices.Backup.IProtectableObjectOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. Resource group name of your recovery services vault.
 /// </param>
 /// <param name='resourceName'>
 /// Required. Name of your recovery services vault.
 /// </param>
 /// <param name='queryFilter'>
 /// Optional.
 /// </param>
 /// <param name='paginationParams'>
 /// Optional. Pagination parameters for controlling the response.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// List of protectable object resonses as returned by the list
 /// protectable objects API.
 /// </returns>
 public static ProtectableObjectListResponse List(this IProtectableObjectOperations operations, string resourceGroupName, string resourceName, ProtectableObjectListQueryParameters queryFilter, PaginationRequest paginationParams, CustomRequestHeaders customRequestHeaders)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IProtectableObjectOperations)s).ListAsync(resourceGroupName, resourceName, queryFilter, paginationParams, customRequestHeaders);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        public ProtectableObjectListResponse ListProtectableObjects(string rsVaultRgName, string rsVaultName, ProtectableObjectListQueryParameters queryParams, PaginationRequest paginationParams)
        {
            ProtectableObjectListResponse response = Client.ProtectableObjects.List(
                rsVaultRgName, rsVaultName, queryParams, paginationParams, CommonTestHelper.GetCustomRequestHeaders());

            Assert.NotNull(response);
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.NotNull(response.ItemList);
            return response;
        }
        public void ListProtectableObjectTest()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();

                string resourceNamespace = ConfigurationManager.AppSettings["ResourceNamespace"];
                string resourceGroupName = ConfigurationManager.AppSettings["RsVaultRgNameRP"];
                string resourceName = ConfigurationManager.AppSettings["RsVaultNameRP"];
                string location = ConfigurationManager.AppSettings["vaultLocationRP"];
                // TODO: Create VM instead of taking these parameters from config
                string containerUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMContainerUniqueNameRP"];
                string itemUniqueName = ConfigurationManager.AppSettings["RsVaultIaasVMItemUniqueNameRP"];
                string containeType = ConfigurationManager.AppSettings["IaaSVMContainerType"];
                string itemType = ConfigurationManager.AppSettings["IaaSVMItemType"];
                string containerUri = containeType + ";" + containerUniqueName;
                string itemUri = itemType + ";" + itemUniqueName;

                var client = GetServiceClient<RecoveryServicesBackupManagementClient>(resourceNamespace);

                // 1. Create vault
                VaultTestHelpers vaultTestHelper = new VaultTestHelpers(client);
                vaultTestHelper.CreateVault(resourceGroupName, resourceName, location);

                // 2. Get default policy
                PolicyTestHelpers policyTestHelper = new PolicyTestHelpers(client);
                string policyId = policyTestHelper.GetDefaultPolicyId(resourceGroupName, resourceName);

                // 3. Enable protection
                ProtectedItemTestHelpers protectedItemTestHelper = new ProtectedItemTestHelpers(client);
                protectedItemTestHelper.EnableProtection(resourceGroupName, resourceName, policyId, containerUri, itemUri);

                // ACTION: List protectable objects
                ProtectableObjectListQueryParameters queryParams = new ProtectableObjectListQueryParameters();
                queryParams.BackupManagementType = BackupManagementType.AzureIaasVM.ToString();
                ProtectableObjectTestHelper poTestHelper = new ProtectableObjectTestHelper(client);
                ProtectableObjectListResponse response = poTestHelper.ListProtectableObjects(
                    resourceGroupName, resourceName, queryParams, new PaginationRequest());

                // VALIDATION: VM should not be found in the list
                Assert.False(response.ItemList.ProtectableObjects.Any(
                    protectableObject =>
                    {
                        return protectableObject.Properties.GetType().IsSubclassOf(typeof(AzureIaaSVMProtectableItem)) &&
                            protectableObject.Name.Contains(containerUniqueName);
                    }),
                    "Retrieved list of containers doesn't contain AzureIaaSVMProtectableItem Item");
            }
        }
        /// <summary>
        /// Lists protectable items according to the query filter and the pagination params
        /// </summary>
        /// <param name="queryFilter">Query filter</param>
        /// <param name="paginationRequest">Pagination parameters</param>
        /// <returns>List of protectable items</returns>
        public ProtectableObjectListResponse ListProtectableItem(
                ProtectableObjectListQueryParameters queryFilter,
                PaginationRequest paginationRequest = null)
        {
            string resourceName = BmsAdapter.GetResourceName();
            string resourceGroupName = BmsAdapter.GetResourceGroupName();

            return BmsAdapter.Client.ProtectableObjects.ListAsync(
                                     resourceGroupName,
                                     resourceName,
                                     queryFilter,
                                     paginationRequest,
                                     BmsAdapter.GetCustomRequestHeaders(),
                                     BmsAdapter.CmdletCancellationToken).Result;
        }
        private bool IsDiscoveryNeeded(string vmName, string rgName, bool isComputeAzureVM,
            out ProtectableObjectResource protectableObjectResource)
        {
            bool isDiscoveryNeed = true;
            protectableObjectResource = null;
            string vmVersion = string.Empty;
            vmVersion = (isComputeAzureVM) == true ? computeAzureVMVersion : classicComputeAzureVMVersion;
            string virtualMachineId = GetAzureIaasVirtualMachineId(rgName, vmVersion, vmName);

            ProtectableObjectListQueryParameters queryParam = new ProtectableObjectListQueryParameters();
            // --- TBD To be added once bug is fixed in Service Client and service
            //queryParam.ProviderType = ProviderType.AzureIaasVM.ToString();
            //queryParam.FriendlyName = vmName;

            // No need to use skip or top token here as no pagination support of IaaSVM PO.

            //First check if container is discovered or not
            var protectableItemList = ServiceClientAdapter.ListProtectableItem(queryParam).ItemList;

            Logger.Instance.WriteDebug(String.Format(Resources.ContainerCountAfterFilter, 
                protectableItemList.ProtectableObjects.Count()));
            if (protectableItemList.ProtectableObjects.Count() == 0)
            {
                //Container is not discovered
                Logger.Instance.WriteDebug(Resources.ContainerNotDiscovered);
                isDiscoveryNeed = true;
            }
            else
            {
                foreach (var protectableItem in protectableItemList.ProtectableObjects)
                {
                    AzureIaaSVMProtectableItem iaaSVMProtectableItem = 
                        (AzureIaaSVMProtectableItem)protectableItem.Properties;
                    if (iaaSVMProtectableItem != null &&
                        string.Compare(iaaSVMProtectableItem.FriendlyName, vmName, true) == 0
                        && iaaSVMProtectableItem.VirtualMachineId.IndexOf(virtualMachineId,
                        StringComparison.InvariantCultureIgnoreCase) >= 0)
                    {
                        protectableObjectResource = protectableItem;
                        isDiscoveryNeed = false;
                        break;
                    }
                }
            }

            return isDiscoveryNeed;
        }
 /// <summary>
 /// Lists all the protectable objects within your subscription
 /// according to the query filter and the pagination parameters.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.RecoveryServices.Backup.IProtectableObjectOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. Resource group name of your recovery services vault.
 /// </param>
 /// <param name='resourceName'>
 /// Required. Name of your recovery services vault.
 /// </param>
 /// <param name='queryFilter'>
 /// Optional.
 /// </param>
 /// <param name='paginationParams'>
 /// Optional. Pagination parameters for controlling the response.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// List of protectable object resonses as returned by the list
 /// protectable objects API.
 /// </returns>
 public static Task<ProtectableObjectListResponse> ListAsync(this IProtectableObjectOperations operations, string resourceGroupName, string resourceName, ProtectableObjectListQueryParameters queryFilter, PaginationRequest paginationParams, CustomRequestHeaders customRequestHeaders)
 {
     return operations.ListAsync(resourceGroupName, resourceName, queryFilter, paginationParams, customRequestHeaders, CancellationToken.None);
 }