Microsoft.Azure.Commands.SiteRecovery.GetAzureRmSiteRecoveryProtectionContainer.WriteProtectionContainers C# (CSharp) Method

WriteProtectionContainers() private method

Writes Protection Containers.
private WriteProtectionContainers ( IList protectionContainers ) : void
protectionContainers IList List of Protection Containers
return void
        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>();
                List<ASRProtectionContainerMapping> asrProtectionContainerMappings = new List<ASRProtectionContainerMapping>();

                // 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);

                    asrProtectionContainerMappings = protectionContainerMappingListResponse.ProtectionContainerMappings.Select(pcm => new ASRProtectionContainerMapping(pcm)).ToList();

                    // 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.Distinct().ToList(), asrProtectionContainerMappings));
            }

            asrProtectionContainers.Sort((x, y) => x.FriendlyName.CompareTo(y.FriendlyName));
            this.WriteObject(asrProtectionContainers, true);
        }