/// <summary> /// ProcessRecord of the command. /// </summary> public override void ExecuteSiteRecoveryCmdlet() { base.ExecuteSiteRecoveryCmdlet(); var policy = new ASRPolicy(this.RecoveryServicesClient.GetAzureSiteRecoveryPolicy(this.InputObject.Name)); if (!policy.ReplicationProvider.Equals(this.InputObject.ReplicationProvider, StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException( string.Format( Resources.IncorrectReplicationProvider, this.InputObject.ReplicationProvider)); } if (this.ShouldProcess( this.InputObject.FriendlyName, VerbsData.Update)) { switch (this.ParameterSetName) { case ASRParameterSets.EnterpriseToEnterprise: this.EnterpriseToEnterprisePolicyObject(); break; case ASRParameterSets.HyperVToAzure: this.HyperVToAzurePolicyObject(); break; case ASRParameterSets.VMwareToAzure: this.UpdateV2APolicyObject(); break; case ASRParameterSets.AzureToVMware: this.UpdateV2VPolicyObject(); break; case ASRParameterSets.AzureToAzure: this.UpdateA2APolicy(); break; case ASRParameterSets.ReplicateVMwareToAzure: this.UpdateInMageRcmPolicy(); break; case ASRParameterSets.Default: DefaultUpdatePolicy(); break; } } }
/// <summary> /// Writes Protection Containers. /// </summary> /// <param name="protectionContainers">List of Protection Containers</param> private void WriteProtectionContainers( IList <ProtectionContainer> protectionContainers) { var asrProtectionContainers = new List <ASRProtectionContainer>(); var policyCache = new Dictionary <string, ASRPolicy>(); foreach (var protectionContainer in protectionContainers) { var availablePolicies = new List <ASRPolicy>(); var 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. var protectionContainerMappingListResponse = this.RecoveryServicesClient .GetAzureSiteRecoveryProtectionContainerMapping( Utilities.GetValueFromArmId( protectionContainer.Id, ARMResourceTypeConstants.ReplicationFabrics), protectionContainer.Name); asrProtectionContainerMappings = protectionContainerMappingListResponse .Select(pcm => new ASRProtectionContainerMapping(pcm)) .ToList(); // TODO: This call can be made parallel to speed up processing if required later. foreach (var protectionContainerMapping in protectionContainerMappingListResponse) { var 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. var policyListResponse = this.RecoveryServicesClient.GetAzureSiteRecoveryPolicy(); foreach (var policy in policyListResponse) { 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); }