/// <summary> /// Helper to configure cloud /// </summary> private void Dissociate(string targetProtectionContainerId) { RemoveProtectionContainerMappingInputProperties inputProperties = new RemoveProtectionContainerMappingInputProperties() { ProviderSpecificInput = new ReplicationProviderContainerUnmappingInput() }; RemoveProtectionContainerMappingInput input = new RemoveProtectionContainerMappingInput() { Properties = inputProperties }; ProtectionContainerMappingListResponse protectionContainerMappingListResponse = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainerMapping(Utilities.GetValueFromArmId(PrimaryProtectionContainer.ID, ARMResourceTypeConstants.ReplicationFabrics), PrimaryProtectionContainer.Name); ProtectionContainerMapping protectionContainerMapping = protectionContainerMappingListResponse.ProtectionContainerMappings.SingleOrDefault(t => (t.Properties.PolicyId.CompareTo(this.Policy.ID) == 0 && t.Properties.TargetProtectionContainerId.CompareTo(targetProtectionContainerId) == 0)); if (protectionContainerMapping == null) { throw new Exception("Cloud is not paired"); } LongRunningOperationResponse response = RecoveryServicesClient.UnConfigureProtection(Utilities.GetValueFromArmId(this.PrimaryProtectionContainer.ID, ARMResourceTypeConstants.ReplicationFabrics), this.PrimaryProtectionContainer.Name, protectionContainerMapping.Name, input); JobResponse jobResponse = RecoveryServicesClient .GetAzureSiteRecoveryJobDetails(PSRecoveryServicesClient.GetJobIdFromReponseLocation(response.Location)); this.WriteObject(new ASRJob(jobResponse.Job)); }
/// <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> /// Queries all Protection Container Mappings for a given Protection Container /// </summary> private void GetAll() { ProtectionContainerMappingListResponse protectionContainerMappingListResponse = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainerMapping( Utilities.GetValueFromArmId(this.ProtectionContainer.ID, ARMResourceTypeConstants.ReplicationFabrics), this.ProtectionContainer.Name); WriteProtectionContainerMappings(protectionContainerMappingListResponse.ProtectionContainerMappings); }
/// <summary> /// Write Protection Container. /// </summary> /// <param name="protectionContainer">Protection Container</param> private void WriteProtectionContainer(ProtectionContainer protectionContainer) { List <ASRPolicy> availablePolicies = new List <ASRPolicy>(); ProtectionContainerMappingListResponse protectionContainerMappingListResponse = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainerMapping(Utilities.GetValueFromArmId(protectionContainer.Id, ARMResourceTypeConstants.ReplicationFabrics), protectionContainer.Name); foreach (ProtectionContainerMapping protectionContainerMapping in protectionContainerMappingListResponse.ProtectionContainerMappings) { PolicyResponse policyResponse = RecoveryServicesClient.GetAzureSiteRecoveryPolicy(Utilities.GetValueFromArmId(protectionContainerMapping.Properties.PolicyId, ARMResourceTypeConstants.ReplicationPolicies)); availablePolicies.Add(new ASRPolicy(policyResponse.Policy)); } this.WriteObject(new ASRProtectionContainer(protectionContainer, availablePolicies)); }