/// <summary> /// Write Protection Entities /// </summary> /// <param name="protectableItems">List of protectable items</param> internal List <T> FetchProtectionEntitiesData <T>(IList <ProtectableItem> protectableItems, string protectionContainerId, string protectionContainerName) { List <ASRProtectionEntity> asrProtectionEntityList = new List <ASRProtectionEntity>(); Dictionary <string, Policy> policyCache = new Dictionary <string, Policy>(); Dictionary <string, ReplicationProtectedItem> protectedItemCache = new Dictionary <string, ReplicationProtectedItem>(); // Check even if an single item is protected then we will get all the protecteditems & policies. if (protectableItems.Select(p => 0 == string.Compare(p.Properties.ProtectionStatus, "protected", StringComparison.OrdinalIgnoreCase)) != null) { // Get all the protected items for the container. ReplicationProtectedItemListResponse ReplicationProtectedItemListResponse = this.GetAzureSiteRecoveryReplicationProtectedItem( Utilities.GetValueFromArmId(protectionContainerId, ARMResourceTypeConstants.ReplicationFabrics), protectionContainerName); // Fill all protected items in dictionary for quick access. foreach (ReplicationProtectedItem protectedItem in ReplicationProtectedItemListResponse.ReplicationProtectedItems) { protectedItemCache.Add(protectedItem.Name.ToLower(), protectedItem); } // Get all policies and fill up the dictionary once for quick access. PolicyListResponse policyListResponse = this.GetAzureSiteRecoveryPolicy(); foreach (Policy policy in policyListResponse.Policies) { policyCache.Add(policy.Name.ToLower(), policy); } } List <T> entities = new List <T>(); // Fill up powershell entity with all the data. foreach (ProtectableItem protectableItem in protectableItems) { if (0 == string.Compare(protectableItem.Properties.ProtectionStatus, "protected", StringComparison.OrdinalIgnoreCase)) { string protectedItemName = Utilities.GetValueFromArmId( protectableItem.Properties.ReplicationProtectedItemId, ARMResourceTypeConstants.ReplicationProtectedItems).ToLower(); ReplicationProtectedItem protectedItem = protectedItemCache[protectedItemName]; string policyName = Utilities.GetValueFromArmId(protectedItem.Properties.PolicyID, ARMResourceTypeConstants.ReplicationPolicies).ToLower(); Policy asrPolicy = policyCache[policyName]; if (typeof(T) == typeof(ASRVirtualMachine)) { entities.Add((T)Convert.ChangeType(new ASRVirtualMachine(protectableItem, protectedItem, asrPolicy), typeof(T))); } else { entities.Add((T)Convert.ChangeType(new ASRProtectionEntity(protectableItem, protectedItem, asrPolicy), typeof(T))); } } else { if (typeof(T) == typeof(ASRVirtualMachine)) { entities.Add((T)Convert.ChangeType(new ASRVirtualMachine(protectableItem), typeof(T))); } else { entities.Add((T)Convert.ChangeType(new ASRProtectionEntity(protectableItem), typeof(T))); } } } asrProtectionEntityList.Sort((x, y) => x.FriendlyName.CompareTo(y.FriendlyName)); return(entities); }