/// <summary> /// Queries by Friendly name. /// </summary> private void GetByFriendlyName() { PolicyListResponse profileListResponse = RecoveryServicesClient.GetAzureSiteRecoveryPolicy(); bool found = false; foreach (Policy policy in profileListResponse.Policies) { if (0 == string.Compare(this.FriendlyName, policy.Properties.FriendlyName, true)) { var policyByName = RecoveryServicesClient.GetAzureSiteRecoveryPolicy(policy.Name).Policy; this.WritePolicy(policyByName); found = true; } } if (!found) { throw new InvalidOperationException( string.Format( Properties.Resources.PolicyNotFound, this.FriendlyName, PSRecoveryServicesClient.asrVaultCreds.ResourceName)); } }
/// <summary> /// Queries all / by default. /// </summary> private void GetAll() { PolicyListResponse policyListResponse = RecoveryServicesClient.GetAzureSiteRecoveryPolicy(); this.WritePolicies(policyListResponse.Policies); }
/// <summary> /// Writes Protection Containers. /// </summary> /// <param name="protectionContainers">List of Protection Containers</param> private void WriteProtectionContainers(IList <ProtectionContainer> protectionContainers) { List <ASRProtectionContainer> asrProtectionContainers = new List <ASRProtectionContainer>(); Dictionary <string, ASRPolicy> policyCache = new Dictionary <string, ASRPolicy>(); foreach (ProtectionContainer protectionContainer in protectionContainers) { List <ASRPolicy> availablePolicies = new List <ASRPolicy>(); // Check if container is paired then fetch policy details. if (0 == string.Compare(protectionContainer.Properties.PairingStatus, "paired", StringComparison.OrdinalIgnoreCase)) { // Get all Protection Container Mappings for specific container to find out the policies attached to container. ProtectionContainerMappingListResponse protectionContainerMappingListResponse = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainerMapping( Utilities.GetValueFromArmId(protectionContainer.Id, ARMResourceTypeConstants.ReplicationFabrics), protectionContainer.Name); // TODO: This call can be made parallel to speed up processing if required later. foreach (ProtectionContainerMapping protectionContainerMapping in protectionContainerMappingListResponse.ProtectionContainerMappings) { string policyName = Utilities.GetValueFromArmId(protectionContainerMapping.Properties.PolicyId, ARMResourceTypeConstants.ReplicationPolicies).ToLower(); ASRPolicy asrPolicy = null; if (policyCache.ContainsKey(policyName)) { asrPolicy = policyCache[policyName]; } else { // Get all policies and fill up the dictionary once. PolicyListResponse policyListResponse = RecoveryServicesClient.GetAzureSiteRecoveryPolicy(); foreach (Policy policy in policyListResponse.Policies) { asrPolicy = new ASRPolicy(policy); try { policyCache.Add(asrPolicy.Name.ToLower(), asrPolicy); } catch (ArgumentException) { // In case of item already exist eat the exception. } } // Get the policy from dictionary now. asrPolicy = policyCache[policyName]; } availablePolicies.Add(asrPolicy); } } asrProtectionContainers.Add(new ASRProtectionContainer(protectionContainer, availablePolicies)); } asrProtectionContainers.Sort((x, y) => x.FriendlyName.CompareTo(y.FriendlyName)); this.WriteObject(asrProtectionContainers, true); }
/// <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); }