/// <summary> /// Lists protected items protected by the recovery services vault according to the provider data /// </summary> /// <returns>List of protected items</returns> public List <ItemBase> ListProtectedItems() { string vaultName = (string)ProviderData[VaultParams.VaultName]; string resourceGroupName = (string)ProviderData[VaultParams.ResourceGroupName]; ContainerBase container = (ContainerBase)ProviderData[ItemParams.Container]; string name = (string)ProviderData[ItemParams.ItemName]; ItemProtectionStatus protectionStatus = (ItemProtectionStatus)ProviderData[ItemParams.ProtectionStatus]; ItemProtectionState status = (ItemProtectionState)ProviderData[ItemParams.ProtectionState]; CmdletModel.WorkloadType workloadType = (CmdletModel.WorkloadType)ProviderData[ItemParams.WorkloadType]; PolicyBase policy = (PolicyBase)ProviderData[PolicyParams.ProtectionPolicy]; ODataQuery <ProtectedItemQueryObject> queryParams = policy != null ? new ODataQuery <ProtectedItemQueryObject>( q => q.BackupManagementType == ServiceClientModel.BackupManagementType.AzureSql && q.ItemType == DataSourceType.AzureSqlDb && q.PolicyName == policy.Name) : new ODataQuery <ProtectedItemQueryObject>( q => q.BackupManagementType == ServiceClientModel.BackupManagementType.AzureSql && q.ItemType == DataSourceType.AzureSqlDb); List <ProtectedItemResource> protectedItems = new List <ProtectedItemResource>(); string skipToken = null; var listResponse = ServiceClientAdapter.ListProtectedItem( queryParams, skipToken, vaultName: vaultName, resourceGroupName: resourceGroupName); protectedItems.AddRange(listResponse); // 1. Filter by container if (container != null) { protectedItems = protectedItems.Where(protectedItem => { Dictionary <UriEnums, string> dictionary = HelperUtils.ParseUri(protectedItem.Id); string containerUri = HelperUtils.GetContainerUri(dictionary, protectedItem.Id); return(containerUri.Contains(container.Name)); }).ToList(); } List <ProtectedItemResource> protectedItemGetResponses = new List <ProtectedItemResource>(); // 2. Filter by item's friendly name if (!string.IsNullOrEmpty(name)) { protectedItems = protectedItems.Where(protectedItem => { Dictionary <UriEnums, string> dictionary = HelperUtils.ParseUri(protectedItem.Id); string protectedItemUri = HelperUtils.GetProtectedItemUri(dictionary, protectedItem.Id); return(protectedItemUri.ToLower().Contains(name.ToLower())); }).ToList(); ODataQuery <GetProtectedItemQueryObject> getItemQueryParams = new ODataQuery <GetProtectedItemQueryObject>(q => q.Expand == extendedInfo); for (int i = 0; i < protectedItems.Count; i++) { Dictionary <UriEnums, string> dictionary = HelperUtils.ParseUri(protectedItems[i].Id); string containerUri = HelperUtils.GetContainerUri(dictionary, protectedItems[i].Id); string protectedItemUri = HelperUtils.GetProtectedItemUri(dictionary, protectedItems[i].Id); var getResponse = ServiceClientAdapter.GetProtectedItem( containerUri, protectedItemUri, getItemQueryParams, vaultName: vaultName, resourceGroupName: resourceGroupName); protectedItemGetResponses.Add(getResponse.Body); } } List <ItemBase> itemModels = ConversionHelpers.GetItemModelList(protectedItems); if (!string.IsNullOrEmpty(name)) { for (int i = 0; i < itemModels.Count; i++) { AzureSqlProtectedItem azureSqlProtectedItem = (AzureSqlProtectedItem)protectedItemGetResponses[i].Properties; AzureSqlItemExtendedInfo extendedInfo = new AzureSqlItemExtendedInfo(); var hydraExtendedInfo = azureSqlProtectedItem.ExtendedInfo; if (hydraExtendedInfo.OldestRecoveryPoint.HasValue) { extendedInfo.OldestRecoveryPoint = hydraExtendedInfo.OldestRecoveryPoint; } extendedInfo.PolicyState = hydraExtendedInfo.PolicyState; extendedInfo.RecoveryPointCount = hydraExtendedInfo.RecoveryPointCount; ((AzureSqlItem)itemModels[i]).ExtendedInfo = extendedInfo; } } // 3. Filter by item's Protection Status if (protectionStatus != 0) { throw new Exception( string.Format( Resources.ProtectionStatusNotAllowedForAzureSqlItem, protectionStatus.ToString())); } // 4. Filter by item's Protection State if (status != 0) { if (status != ItemProtectionState.Protected) { throw new Exception( string.Format( Resources.ProtectionStateInvalidForAzureSqlItem, status.ToString())); } itemModels = itemModels.Where(itemModel => { return(((AzureSqlItem)itemModel).ProtectionState == status.ToString()); }).ToList(); } // 5. Filter by workload type if (workloadType != 0) { itemModels = itemModels.Where(itemModel => { return(itemModel.WorkloadType == workloadType); }).ToList(); } return(itemModels); }
/// <summary> /// Lists protected items protected by the recovery services vault according to the provider data /// </summary> /// <returns>List of protected items</returns> public List <ItemBase> ListProtectedItems() { ContainerBase container = (ContainerBase)this.ProviderData[ItemParams.Container]; string name = (string)this.ProviderData[ItemParams.AzureVMName]; ItemProtectionStatus protectionStatus = (ItemProtectionStatus)this.ProviderData[ItemParams.ProtectionStatus]; ItemProtectionState status = (ItemProtectionState)this.ProviderData[ItemParams.ProtectionState]; Models.WorkloadType workloadType = (Models.WorkloadType) this.ProviderData[ItemParams.WorkloadType]; ProtectedItemListQueryParam queryParams = new ProtectedItemListQueryParam(); queryParams.DatasourceType = ServiceClientModel.WorkloadType.AzureSqlDb.ToString(); queryParams.BackupManagementType = ServiceClientModel.BackupManagementType.AzureSql.ToString(); List <ProtectedItemResource> protectedItems = new List <ProtectedItemResource>(); string skipToken = null; PaginationRequest paginationRequest = null; do { var listResponse = ServiceClientAdapter.ListProtectedItem(queryParams, paginationRequest); protectedItems.AddRange(listResponse.ItemList.Value); ServiceClientHelpers.GetSkipTokenFromNextLink( listResponse.ItemList.NextLink, out skipToken); if (skipToken != null) { paginationRequest = new PaginationRequest(); paginationRequest.SkipToken = skipToken; } } while (skipToken != null); // 1. Filter by container if (container != null) { protectedItems = protectedItems.Where(protectedItem => { Dictionary <UriEnums, string> dictionary = HelperUtils.ParseUri(protectedItem.Id); string containerUri = HelperUtils.GetContainerUri(dictionary, protectedItem.Id); return(containerUri.Contains(container.Name)); }).ToList(); } List <ProtectedItemResponse> protectedItemGetResponses = new List <ProtectedItemResponse>(); // 2. Filter by item's friendly name if (!string.IsNullOrEmpty(name)) { protectedItems = protectedItems.Where(protectedItem => { Dictionary <UriEnums, string> dictionary = HelperUtils.ParseUri(protectedItem.Id); string protectedItemUri = HelperUtils.GetProtectedItemUri(dictionary, protectedItem.Id); return(protectedItemUri.ToLower().Contains(name.ToLower())); }).ToList(); GetProtectedItemQueryParam getItemQueryParams = new GetProtectedItemQueryParam(); getItemQueryParams.Expand = extendedInfo; for (int i = 0; i < protectedItems.Count; i++) { Dictionary <UriEnums, string> dictionary = HelperUtils.ParseUri(protectedItems[i].Id); string containerUri = HelperUtils.GetContainerUri(dictionary, protectedItems[i].Id); string protectedItemUri = HelperUtils.GetProtectedItemUri(dictionary, protectedItems[i].Id); var getResponse = ServiceClientAdapter.GetProtectedItem( containerUri, protectedItemUri, getItemQueryParams); protectedItemGetResponses.Add(getResponse); } } List <ItemBase> itemModels = ConversionHelpers.GetItemModelList(protectedItems); if (!string.IsNullOrEmpty(name)) { for (int i = 0; i < itemModels.Count; i++) { AzureSqlProtectedItem azureSqlProtectedItem = (AzureSqlProtectedItem)protectedItemGetResponses[i].Item.Properties; AzureSqlItemExtendedInfo extendedInfo = new AzureSqlItemExtendedInfo(); var hydraExtendedInfo = azureSqlProtectedItem.ExtendedInfo; if (hydraExtendedInfo.OldestRecoveryPoint.HasValue) { extendedInfo.OldestRecoveryPoint = hydraExtendedInfo.OldestRecoveryPoint; } extendedInfo.PolicyState = hydraExtendedInfo.PolicyState; extendedInfo.RecoveryPointCount = hydraExtendedInfo.RecoveryPointCount; ((AzureSqlItem)itemModels[i]).ExtendedInfo = extendedInfo; } } // 3. Filter by item's Protection Status if (protectionStatus != 0) { throw new Exception( string.Format( Resources.ProtectionStatusNotAllowedForAzureSqlItem, protectionStatus.ToString())); } // 4. Filter by item's Protection State if (status != 0) { if (status != ItemProtectionState.Protected) { throw new Exception( string.Format( Resources.ProtectionStateInvalidForAzureSqlItem, status.ToString())); } itemModels = itemModels.Where(itemModel => { return(((AzureSqlItem)itemModel).ProtectionState == status.ToString()); }).ToList(); } // 5. Filter by workload type if (workloadType != 0) { itemModels = itemModels.Where(itemModel => { return(itemModel.WorkloadType == workloadType); }).ToList(); } return(itemModels); }