public static MappingOperationResponse PairCloudToAzure( this SiteRecoveryManagementClient client, Fabric primaryFabric, ProtectionContainer primaryContainer, Policy policy, string armResourceName) { CreateProtectionContainerMappingInputProperties pairingProps = new CreateProtectionContainerMappingInputProperties() { PolicyId = policy.Id, TargetProtectionContainerId = "Microsoft Azure", ProviderSpecificInput = new ReplicationProviderContainerMappingInput() }; CreateProtectionContainerMappingInput pairingInput = new CreateProtectionContainerMappingInput() { Properties = pairingProps }; return(client.ProtectionContainerMapping.ConfigureProtection( primaryFabric.Name, primaryContainer.Name, armResourceName, pairingInput, GetRequestHeaders()) as MappingOperationResponse); }
/// <summary> /// Write Protection Container. /// </summary> /// <param name="protectionContainer">Protection Container</param> private void WriteProtectionContainer( ProtectionContainer protectionContainer) { var availablePolicies = new List <ASRPolicy>(); var asrProtectionContainerMappings = new List <ASRProtectionContainerMapping>(); var protectionContainerMappingListResponse = this.RecoveryServicesClient .GetAzureSiteRecoveryProtectionContainerMapping( Utilities.GetValueFromArmId( protectionContainer.Id, ARMResourceTypeConstants.ReplicationFabrics), protectionContainer.Name); asrProtectionContainerMappings = protectionContainerMappingListResponse .Select(pcm => new ASRProtectionContainerMapping(pcm)) .ToList(); foreach (var protectionContainerMapping in protectionContainerMappingListResponse) { var policyResponse = this.RecoveryServicesClient.GetAzureSiteRecoveryPolicy( Utilities.GetValueFromArmId( protectionContainerMapping.Properties.PolicyId, ARMResourceTypeConstants.ReplicationPolicies)); availablePolicies.Add(new ASRPolicy(policyResponse)); } this.WriteObject( new ASRProtectionContainer( protectionContainer, availablePolicies.Distinct() .ToList(), asrProtectionContainerMappings)); }
public static List <ProtectableItem> GetUnprotectedItems( this SiteRecoveryManagementClient client, Fabric primaryFabric, ProtectionContainer container) { List <ProtectableItem> protectableItemList = new List <ProtectableItem>(); ProtectableItemListResponse protectableItemListResponse = client.ProtectableItem.List( primaryFabric.Name, container.Name, "Unprotected", null, "1000", GetRequestHeaders()); protectableItemList.AddRange(protectableItemListResponse.ProtectableItems); while (protectableItemListResponse.NextLink != null) { protectableItemListResponse = client.ProtectableItem.ListNext( protectableItemListResponse.NextLink, GetRequestHeaders()); protectableItemList.AddRange(protectableItemListResponse.ProtectableItems); } return(protectableItemList); }
/// <summary> /// Initializes a new instance of the <see cref="ASRProtectionContainer" /> class with /// required parameters. /// </summary> /// <param name="pc">Protection container object</param> public ASRProtectionContainer(ProtectionContainer pc) { this.ID = pc.ID; this.Name = pc.Name; this.ConfigurationStatus = pc.ConfigurationStatus; this.Role = pc.Role; this.ServerId = pc.ServerId; this.FabricObjectId = pc.FabricObjectId; }
public static LongRunningOperationResponse TestFailover( this SiteRecoveryManagementClient client, Fabric primaryFabric, ProtectionContainer protectionContainer, ReplicationProtectedItem protectedItem) { TestFailoverInput tfoInput = new TestFailoverInput() { Properties = new TestFailoverInputProperties() { ProviderSpecificDetails = new ProviderSpecificFailoverInput() } }; if (protectedItem.Properties.ProviderSpecificDetails.InstanceType == "HyperVReplicaAzure") { HyperVReplicaAzureFailoverProviderInput hvrAFOInput = new HyperVReplicaAzureFailoverProviderInput() { VaultLocation = "West US", }; string networkId = (protectedItem.Properties.ProviderSpecificDetails as HyperVReplicaAzureReplicationDetails) .SelectedRecoveryAzureNetworkId; TestFailoverInputProperties tfoProp = new TestFailoverInputProperties() { ProviderSpecificDetails = hvrAFOInput, NetworkType = string.IsNullOrEmpty(networkId) ? null : "VmNetworkAsInput", NetworkId = networkId }; tfoInput.Properties = tfoProp; } DateTime startTFO = DateTime.UtcNow; var tfoOp = client.ReplicationProtectedItem.TestFailover( primaryFabric.Name, protectionContainer.Name, protectedItem.Name, tfoInput, GetRequestHeaders()); var jobs = MonitoringHelper.GetJobId(MonitoringHelper.TestFailoverJobName, startTFO, client, GetRequestHeaders()); ResumeJobParamsProperties resProp = new ResumeJobParamsProperties() { Comments = "Res TFO" }; ResumeJobParams resParam = new ResumeJobParams() { Properties = resProp }; return(client.Jobs.Resume(jobs.Name, resParam, GetRequestHeaders())); }
public void PurgeProtectionForVMwareVM() { using (UndoContext context = UndoContext.Current) { context.Start(); var client = GetSiteRecoveryClient(CustomHttpHandler); var fabrics = client.Fabrics.List(RequestHeaders); Fabric selectedFabric = null; ProtectionContainer selectedContainer = null; foreach (var fabric in fabrics.Fabrics) { if (fabric.Properties.CustomDetails.InstanceType.Contains("VMware")) { selectedFabric = fabric; } } var containers = client.ProtectionContainer.List(selectedFabric.Name, RequestHeaders); foreach (var container in containers.ProtectionContainers) { if (container.Properties.ProtectedItemCount > 0 && container.Properties.Role.Equals("Primary")) { selectedContainer = container; break; } } if (selectedContainer != null) { var protectedItem = client.ReplicationProtectedItem.List( selectedFabric.Name, selectedContainer.Name, RequestHeaders).ReplicationProtectedItems[0]; var response = client.ReplicationProtectedItem.PurgeProtection( selectedFabric.Name, selectedContainer.Name, protectedItem.Name, RequestHeaders); Assert.NotNull(response); Assert.Equal(OperationStatus.Succeeded, response.Status); } else { throw new System.Exception("No protected item found"); } } }
/// <summary> /// Initializes a new instance of the <see cref="ASRProtectionContainer" /> class with /// required parameters. /// </summary> /// <param name="pc">Protection container object</param> public ASRProtectionContainer(ProtectionContainer pc, List<ASRPolicy> availablePolicies) { this.AvailablePolicies = availablePolicies; this.ID = pc.Id; this.Name = pc.Name; this.FriendlyName = pc.Properties.FriendlyName; this.FabricFriendlyName = pc.Properties.FabricFriendlyName; this.Role = pc.Properties.Role; this.FabricType = pc.Properties.FabricType; this.Type = pc.Type; }
public static ReplicationProtectedItemOperationResponse CommitFailover( this SiteRecoveryManagementClient client, Fabric primaryFabric, ProtectionContainer protectionContainer, ReplicationProtectedItem protectedItem) { return(client.ReplicationProtectedItem.CommitFailover( primaryFabric.Name, protectionContainer.Name, protectedItem.Name, GetRequestHeaders()) as ReplicationProtectedItemOperationResponse); }
public static LongRunningOperationResponse DisableDR( this SiteRecoveryManagementClient client, Fabric primaryFabric, ProtectionContainer protectionContainer, string armResourceName) { return(client.ReplicationProtectedItem.DisableProtection( primaryFabric.Name, protectionContainer.Name, armResourceName, new DisableProtectionInput(), GetRequestHeaders())); }
/// <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)); }
public static ReplicationProtectedItemOperationResponse PlannedFailover( this SiteRecoveryManagementClient client, Fabric primaryFabric, ProtectionContainer protectionContainer, ReplicationProtectedItem protectedItem) { PlannedFailoverInputProperties plannedFailoverProp = new PlannedFailoverInputProperties(); if (protectedItem.Properties.ProviderSpecificDetails.InstanceType == "HyperVReplicaAzure") { if (protectedItem.Properties.ActiveLocation == "Recovery") { HyperVReplicaAzureFailbackProviderInput hvrAFBInput = new HyperVReplicaAzureFailbackProviderInput() { RecoveryVmCreationOption = "NoAction", DataSyncOption = "ForSyncronization" }; plannedFailoverProp.ProviderSpecificDetails = hvrAFBInput; } else { HyperVReplicaAzureFailoverProviderInput hvrAFOInput = new HyperVReplicaAzureFailoverProviderInput() { VaultLocation = "West US", }; plannedFailoverProp.ProviderSpecificDetails = hvrAFOInput; } } PlannedFailoverInput pfoInput = new PlannedFailoverInput() { Properties = plannedFailoverProp }; return(client.ReplicationProtectedItem.PlannedFailover( primaryFabric.Name, protectionContainer.Name, protectedItem.Name, pfoInput, GetRequestHeaders()) as ReplicationProtectedItemOperationResponse); }
public static LongRunningOperationResponse UnpairClouds( this SiteRecoveryManagementClient client, Fabric primaryFabric, ProtectionContainer primaryContainer, string armResourceName) { ProtectionContainerMapping mapping = client.ProtectionContainerMapping.Get( primaryFabric.Name, primaryContainer.Name, armResourceName, GetRequestHeaders()).ProtectionContainerMapping; return(client.ProtectionContainerMapping.UnconfigureProtection( primaryFabric.Name, primaryContainer.Name, armResourceName, new RemoveProtectionContainerMappingInput(), GetRequestHeaders())); }
public void PurgeCloudPair() { using (UndoContext context = UndoContext.Current) { context.Start(); var client = GetSiteRecoveryClient(CustomHttpHandler); Fabric selectedFabric = null; var fabrics = client.Fabrics.List(RequestHeaders); ProtectionContainer selectedContainer = null; foreach (var fabric in fabrics.Fabrics) { if (fabric.Properties.CustomDetails.InstanceType.Contains("VMM")) { selectedFabric = fabric; } } var containers = client.ProtectionContainer.List(selectedFabric.Name, RequestHeaders); foreach (var container in containers.ProtectionContainers) { if (container.Properties.ProtectedItemCount == 0 && container.Properties.Role.Equals("Primary")) { selectedContainer = container; break; } } var pairs = client.ProtectionContainerMapping.List(selectedFabric.Name, selectedContainer.Name, RequestHeaders); var purgePairing = client.ProtectionContainerMapping.PurgeProtection( selectedFabric.Name, selectedContainer.Name, pairs.ProtectionContainerMappings[0].Name, RequestHeaders); } }
public static ReplicationProtectedItemOperationResponse UnplannedFailover( this SiteRecoveryManagementClient client, Fabric primaryFabric, ProtectionContainer protectionContainer, ReplicationProtectedItem protectedItem) { UnplannedFailoverInput ufoInput = new UnplannedFailoverInput() { Properties = new UnplannedFailoverInputProperties() { ProviderSpecificDetails = new ProviderSpecificFailoverInput() } }; if (protectedItem.Properties.ProviderSpecificDetails.InstanceType == "HyperVReplicaAzure") { HyperVReplicaAzureFailoverProviderInput hvrAFOInput = new HyperVReplicaAzureFailoverProviderInput() { VaultLocation = "West US", }; UnplannedFailoverInputProperties ufoProp = new UnplannedFailoverInputProperties() { ProviderSpecificDetails = new ProviderSpecificFailoverInput(), SourceSiteOperations = "NotRequired" }; ufoInput.Properties = ufoProp; } return(client.ReplicationProtectedItem.UnplannedFailover( primaryFabric.Name, protectionContainer.Name, protectedItem.Name, ufoInput, GetRequestHeaders()) as ReplicationProtectedItemOperationResponse); }
public static ReplicationProtectedItemOperationResponse ReverseReplication( this SiteRecoveryManagementClient client, Fabric primaryFabric, ProtectionContainer protectionContainer, ReplicationProtectedItem protectedItem) { if (protectedItem.Properties.ProviderSpecificDetails.InstanceType == "HyperVReplicaAzure") { ProtectableItem protectableItem = client.ProtectableItem.Get( primaryFabric.Name, protectionContainer.Name, protectedItem.Properties.ProtectableItemId.Substring( protectedItem.Properties.ProtectableItemId.LastIndexOf("/") + 1), GetRequestHeaders()).ProtectableItem; string vhdId = (protectableItem.Properties.CustomDetails as HyperVVirtualMachineDetails) .DiskDetailsList[0].VhdId; DiskDetails osDisk = (protectableItem.Properties.CustomDetails as HyperVVirtualMachineDetails) .DiskDetailsList .FirstOrDefault(item => item.VhdType == "OperatingSystem"); if (osDisk != null) { vhdId = osDisk.VhdId; } string storageAccount = (protectedItem.Properties.ProviderSpecificDetails as HyperVReplicaAzureReplicationDetails) .RecoveryAzureStorageAccount; HyperVReplicaAzureReprotectInput hvrARRInput = new HyperVReplicaAzureReprotectInput() { HvHostVmId = (protectableItem.Properties.CustomDetails as HyperVVirtualMachineDetails).SourceItemId, OSType = "Windows", VHDId = vhdId, VmName = protectableItem.Properties.FriendlyName, StorageAccountId = storageAccount, }; ReverseReplicationInputProperties rrProp = new ReverseReplicationInputProperties() { FailoverDirection = "", ProviderSpecificDetails = hvrARRInput }; ReverseReplicationInput rrInput = new ReverseReplicationInput() { Properties = rrProp }; return(client.ReplicationProtectedItem.Reprotect( primaryFabric.Name, protectionContainer.Name, protectedItem.Name, rrInput, GetRequestHeaders()) as ReplicationProtectedItemOperationResponse); } else if (protectedItem.Properties.ProviderSpecificDetails.InstanceType == "HyperVReplica2012" || protectedItem.Properties.ProviderSpecificDetails.InstanceType == "HyperVReplica2012R2") { ReverseReplicationInputProperties rrProp = new ReverseReplicationInputProperties() { ProviderSpecificDetails = new ReverseReplicationProviderSpecificInput() }; ReverseReplicationInput rrInput = new ReverseReplicationInput() { Properties = rrProp }; return(client.ReplicationProtectedItem.Reprotect( primaryFabric.Name, protectionContainer.Name, protectedItem.Name, rrInput, GetRequestHeaders()) as ReplicationProtectedItemOperationResponse); } else { throw new NotImplementedException(); } }
/// <summary> /// Get the protected container by Id. /// </summary> /// <param name='protectionContainerId'> /// Required. Protection Container ID. /// </param> /// <param name='customRequestHeaders'> /// Optional. Request header parameters. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The response model for the Protection Container object. /// </returns> public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.SiteRecovery.Models.ProtectionContainerResponse> GetAsync(string protectionContainerId, CustomRequestHeaders customRequestHeaders, CancellationToken cancellationToken) { // Validate if (protectionContainerId == null) { throw new ArgumentNullException("protectionContainerId"); } // Tracing bool shouldTrace = CloudContext.Configuration.Tracing.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = Tracing.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("protectionContainerId", protectionContainerId); tracingParameters.Add("customRequestHeaders", customRequestHeaders); Tracing.Enter(invocationId, this, "GetAsync", tracingParameters); } // Construct URL string url = (this.Client.Credentials.SubscriptionId != null ? this.Client.Credentials.SubscriptionId.Trim() : "") + "/cloudservices/" + this.Client.CloudServiceName.Trim() + "/resources/WAHyperVRecoveryManager/~/HyperVRecoveryManagerVault/" + this.Client.ResourceName.Trim() + "/ProtectionContainers/" + protectionContainerId.Trim() + "?"; url = url + "api-version=2014-10-27"; string baseUrl = this.Client.BaseUri.AbsoluteUri; // Trim '/' character from the end of baseUrl and beginning of url. if (baseUrl[baseUrl.Length - 1] == '/') { baseUrl = baseUrl.Substring(0, baseUrl.Length - 1); } if (url[0] == '/') { url = url.Substring(1); } url = baseUrl + "/" + url; url = url.Replace(" ", "%20"); // Create HTTP transport objects HttpRequestMessage httpRequest = null; try { httpRequest = new HttpRequestMessage(); httpRequest.Method = HttpMethod.Get; httpRequest.RequestUri = new Uri(url); // Set Headers httpRequest.Headers.Add("Accept", "application/xml"); httpRequest.Headers.Add("x-ms-client-request-id", customRequestHeaders.ClientRequestId); httpRequest.Headers.Add("x-ms-version", "2013-03-01"); // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Send Request HttpResponseMessage httpResponse = null; try { if (shouldTrace) { Tracing.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { Tracing.ReceiveResponse(invocationId, httpResponse); } HttpStatusCode statusCode = httpResponse.StatusCode; if (statusCode != HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); if (shouldTrace) { Tracing.Error(invocationId, ex); } throw ex; } // Create Result ProtectionContainerResponse result = null; // Deserialize Response cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new ProtectionContainerResponse(); XDocument responseDoc = XDocument.Parse(responseContent); XElement serviceResourceElement = responseDoc.Element(XName.Get("ServiceResource", "http://schemas.microsoft.com/windowsazure")); if (serviceResourceElement != null) { ProtectionContainer serviceResourceInstance = new ProtectionContainer(); result.ProtectionContainer = serviceResourceInstance; XElement serverIdElement = serviceResourceElement.Element(XName.Get("ServerId", "http://schemas.microsoft.com/windowsazure")); if (serverIdElement != null) { string serverIdInstance = serverIdElement.Value; serviceResourceInstance.ServerId = serverIdInstance; } XElement fabricObjectIdElement = serviceResourceElement.Element(XName.Get("FabricObjectId", "http://schemas.microsoft.com/windowsazure")); if (fabricObjectIdElement != null) { string fabricObjectIdInstance = fabricObjectIdElement.Value; serviceResourceInstance.FabricObjectId = fabricObjectIdInstance; } XElement configurationStatusElement = serviceResourceElement.Element(XName.Get("ConfigurationStatus", "http://schemas.microsoft.com/windowsazure")); if (configurationStatusElement != null) { string configurationStatusInstance = configurationStatusElement.Value; serviceResourceInstance.ConfigurationStatus = configurationStatusInstance; } XElement pairedToElement = serviceResourceElement.Element(XName.Get("PairedTo", "http://schemas.microsoft.com/windowsazure")); if (pairedToElement != null) { string pairedToInstance = pairedToElement.Value; serviceResourceInstance.PairedTo = pairedToInstance; } XElement roleElement = serviceResourceElement.Element(XName.Get("Role", "http://schemas.microsoft.com/windowsazure")); if (roleElement != null) { string roleInstance = roleElement.Value; serviceResourceInstance.Role = roleInstance; } XElement nameElement = serviceResourceElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure")); if (nameElement != null) { string nameInstance = nameElement.Value; serviceResourceInstance.Name = nameInstance; } XElement idElement = serviceResourceElement.Element(XName.Get("ID", "http://schemas.microsoft.com/windowsazure")); if (idElement != null) { string idInstance = idElement.Value; serviceResourceInstance.ID = idInstance; } } result.StatusCode = statusCode; if (httpResponse.Headers.Contains("x-ms-request-id")) { result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); } if (shouldTrace) { Tracing.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }
/// <summary> /// Get the protected container by Id. /// </summary> /// <param name='protectionContainerId'> /// Required. Protection Container ID. /// </param> /// <param name='customRequestHeaders'> /// Optional. Request header parameters. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// The response model for the Protection Container object. /// </returns> public async Task <ProtectionContainerResponse> GetAsync(string protectionContainerId, CustomRequestHeaders customRequestHeaders, CancellationToken cancellationToken) { // Validate if (protectionContainerId == null) { throw new ArgumentNullException("protectionContainerId"); } // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("protectionContainerId", protectionContainerId); tracingParameters.Add("customRequestHeaders", customRequestHeaders); TracingAdapter.Enter(invocationId, this, "GetAsync", tracingParameters); } // Construct URL string url = ""; if (this.Client.Credentials.SubscriptionId != null) { url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId); } url = url + "/cloudservices/"; url = url + Uri.EscapeDataString(this.Client.CloudServiceName); url = url + "/resources/"; url = url + "WAHyperVRecoveryManager"; url = url + "/~/"; url = url + "HyperVRecoveryManagerVault"; url = url + "/"; url = url + Uri.EscapeDataString(this.Client.ResourceName); url = url + "/ProtectionContainers/"; url = url + Uri.EscapeDataString(protectionContainerId); List <string> queryParameters = new List <string>(); queryParameters.Add("api-version=2015-04-10"); if (queryParameters.Count > 0) { url = url + "?" + string.Join("&", queryParameters); } string baseUrl = this.Client.BaseUri.AbsoluteUri; // Trim '/' character from the end of baseUrl and beginning of url. if (baseUrl[baseUrl.Length - 1] == '/') { baseUrl = baseUrl.Substring(0, baseUrl.Length - 1); } if (url[0] == '/') { url = url.Substring(1); } url = baseUrl + "/" + url; url = url.Replace(" ", "%20"); // Create HTTP transport objects HttpRequestMessage httpRequest = null; try { httpRequest = new HttpRequestMessage(); httpRequest.Method = HttpMethod.Get; httpRequest.RequestUri = new Uri(url); // Set Headers httpRequest.Headers.Add("Accept", "application/xml"); httpRequest.Headers.Add("x-ms-client-request-id", customRequestHeaders.ClientRequestId); httpRequest.Headers.Add("x-ms-version", "2013-03-01"); // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Send Request HttpResponseMessage httpResponse = null; try { if (shouldTrace) { TracingAdapter.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { TracingAdapter.ReceiveResponse(invocationId, httpResponse); } HttpStatusCode statusCode = httpResponse.StatusCode; if (statusCode != HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } // Create Result ProtectionContainerResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new ProtectionContainerResponse(); XDocument responseDoc = XDocument.Parse(responseContent); XElement protectionContainerElement = responseDoc.Element(XName.Get("ProtectionContainer", "http://schemas.microsoft.com/windowsazure")); if (protectionContainerElement != null) { ProtectionContainer protectionContainerInstance = new ProtectionContainer(); result.ProtectionContainer = protectionContainerInstance; XElement serverIdElement = protectionContainerElement.Element(XName.Get("ServerId", "http://schemas.microsoft.com/windowsazure")); if (serverIdElement != null) { string serverIdInstance = serverIdElement.Value; protectionContainerInstance.ServerId = serverIdInstance; } XElement fabricObjectIdElement = protectionContainerElement.Element(XName.Get("FabricObjectId", "http://schemas.microsoft.com/windowsazure")); if (fabricObjectIdElement != null) { string fabricObjectIdInstance = fabricObjectIdElement.Value; protectionContainerInstance.FabricObjectId = fabricObjectIdInstance; } XElement roleElement = protectionContainerElement.Element(XName.Get("Role", "http://schemas.microsoft.com/windowsazure")); if (roleElement != null) { string roleInstance = roleElement.Value; protectionContainerInstance.Role = roleInstance; } XElement fabricTypeElement = protectionContainerElement.Element(XName.Get("FabricType", "http://schemas.microsoft.com/windowsazure")); if (fabricTypeElement != null) { string fabricTypeInstance = fabricTypeElement.Value; protectionContainerInstance.FabricType = fabricTypeInstance; } XElement typeElement = protectionContainerElement.Element(XName.Get("Type", "http://schemas.microsoft.com/windowsazure")); if (typeElement != null) { string typeInstance = typeElement.Value; protectionContainerInstance.Type = typeInstance; } XElement availableProtectionProfilesSequenceElement = protectionContainerElement.Element(XName.Get("AvailableProtectionProfiles", "http://schemas.microsoft.com/windowsazure")); if (availableProtectionProfilesSequenceElement != null) { foreach (XElement availableProtectionProfilesElement in availableProtectionProfilesSequenceElement.Elements(XName.Get("ProtectionProfile", "http://schemas.microsoft.com/windowsazure"))) { ProtectionProfile protectionProfileInstance = new ProtectionProfile(); protectionContainerInstance.AvailableProtectionProfiles.Add(protectionProfileInstance); XElement protectedEntityCountElement = availableProtectionProfilesElement.Element(XName.Get("ProtectedEntityCount", "http://schemas.microsoft.com/windowsazure")); if (protectedEntityCountElement != null) { int protectedEntityCountInstance = int.Parse(protectedEntityCountElement.Value, CultureInfo.InvariantCulture); protectionProfileInstance.ProtectedEntityCount = protectedEntityCountInstance; } XElement replicationProviderElement = availableProtectionProfilesElement.Element(XName.Get("ReplicationProvider", "http://schemas.microsoft.com/windowsazure")); if (replicationProviderElement != null) { string replicationProviderInstance = replicationProviderElement.Value; protectionProfileInstance.ReplicationProvider = replicationProviderInstance; } XElement replicationProviderSettingElement = availableProtectionProfilesElement.Element(XName.Get("ReplicationProviderSetting", "http://schemas.microsoft.com/windowsazure")); if (replicationProviderSettingElement != null) { string replicationProviderSettingInstance = replicationProviderSettingElement.Value; protectionProfileInstance.ReplicationProviderSetting = replicationProviderSettingInstance; } XElement canDissociateElement = availableProtectionProfilesElement.Element(XName.Get("CanDissociate", "http://schemas.microsoft.com/windowsazure")); if (canDissociateElement != null) { bool canDissociateInstance = bool.Parse(canDissociateElement.Value); protectionProfileInstance.CanDissociate = canDissociateInstance; } XElement associationDetailSequenceElement = availableProtectionProfilesElement.Element(XName.Get("AssociationDetail", "http://schemas.microsoft.com/windowsazure")); if (associationDetailSequenceElement != null) { foreach (XElement associationDetailElement in associationDetailSequenceElement.Elements(XName.Get("ProtectionProfileAssociationDetails", "http://schemas.microsoft.com/windowsazure"))) { ProtectionProfileAssociationDetails protectionProfileAssociationDetailsInstance = new ProtectionProfileAssociationDetails(); protectionProfileInstance.AssociationDetail.Add(protectionProfileAssociationDetailsInstance); XElement primaryProtectionContainerIdElement = associationDetailElement.Element(XName.Get("PrimaryProtectionContainerId", "http://schemas.microsoft.com/windowsazure")); if (primaryProtectionContainerIdElement != null) { string primaryProtectionContainerIdInstance = primaryProtectionContainerIdElement.Value; protectionProfileAssociationDetailsInstance.PrimaryProtectionContainerId = primaryProtectionContainerIdInstance; } XElement recoveryProtectionContainerIdElement = associationDetailElement.Element(XName.Get("RecoveryProtectionContainerId", "http://schemas.microsoft.com/windowsazure")); if (recoveryProtectionContainerIdElement != null) { string recoveryProtectionContainerIdInstance = recoveryProtectionContainerIdElement.Value; protectionProfileAssociationDetailsInstance.RecoveryProtectionContainerId = recoveryProtectionContainerIdInstance; } XElement associationStatusElement = associationDetailElement.Element(XName.Get("AssociationStatus", "http://schemas.microsoft.com/windowsazure")); if (associationStatusElement != null) { string associationStatusInstance = associationStatusElement.Value; protectionProfileAssociationDetailsInstance.AssociationStatus = associationStatusInstance; } } } XElement nameElement = availableProtectionProfilesElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure")); if (nameElement != null) { string nameInstance = nameElement.Value; protectionProfileInstance.Name = nameInstance; } XElement idElement = availableProtectionProfilesElement.Element(XName.Get("ID", "http://schemas.microsoft.com/windowsazure")); if (idElement != null) { string idInstance = idElement.Value; protectionProfileInstance.ID = idInstance; } } } XElement nameElement2 = protectionContainerElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure")); if (nameElement2 != null) { string nameInstance2 = nameElement2.Value; protectionContainerInstance.Name = nameInstance2; } XElement idElement2 = protectionContainerElement.Element(XName.Get("ID", "http://schemas.microsoft.com/windowsazure")); if (idElement2 != null) { string idInstance2 = idElement2.Value; protectionContainerInstance.ID = idInstance2; } } } result.StatusCode = statusCode; if (httpResponse.Headers.Contains("x-ms-request-id")) { result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); } if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }
public static ReplicationProtectedItemOperationResponse EnableDR( this SiteRecoveryManagementClient client, Fabric primaryFabric, ProtectionContainer protectionContainer, Policy policy, ProtectableItem protectableItem, string armResourceName) { if (policy.Properties.ProviderSpecificDetails.InstanceType == "HyperVReplicaAzure") { string vhdId = (protectableItem.Properties.CustomDetails as HyperVVirtualMachineDetails) .DiskDetailsList[0].VhdId; DiskDetails osDisk = (protectableItem.Properties.CustomDetails as HyperVVirtualMachineDetails).DiskDetailsList .FirstOrDefault(item => item.VhdType == "OperatingSystem"); if (osDisk != null) { vhdId = osDisk.VhdId; } HyperVReplicaAzureEnableProtectionInput hvrAEnableDRInput = new HyperVReplicaAzureEnableProtectionInput() { HvHostVmId = (protectableItem.Properties.CustomDetails as HyperVVirtualMachineDetails).SourceItemId, OSType = "Windows", VhdId = vhdId, VmName = protectableItem.Properties.FriendlyName, TargetStorageAccountId = (policy.Properties.ProviderSpecificDetails as HyperVReplicaAzurePolicyDetails) .ActiveStorageAccountId, }; EnableProtectionInputProperties enableDRProp = new EnableProtectionInputProperties() { PolicyId = policy.Id, ProtectableItemId = protectableItem.Id, ProviderSpecificDetails = hvrAEnableDRInput }; EnableProtectionInput enableDRInput = new EnableProtectionInput() { Properties = enableDRProp }; return(client.ReplicationProtectedItem.EnableProtection( primaryFabric.Name, protectionContainer.Name, armResourceName, enableDRInput, GetRequestHeaders()) as ReplicationProtectedItemOperationResponse); } else if (policy.Properties.ProviderSpecificDetails.InstanceType == "HyperVReplica2012" || policy.Properties.ProviderSpecificDetails.InstanceType == "HyperVReplica2012R2") { var enableDRProp = new EnableProtectionInputProperties() { PolicyId = policy.Id, ProtectableItemId = protectableItem.Id, ProviderSpecificDetails = new EnableProtectionProviderSpecificInput() }; EnableProtectionInput enableInput = new EnableProtectionInput() { Properties = enableDRProp }; return(client.ReplicationProtectedItem.EnableProtection( primaryFabric.Name, protectionContainer.Name, armResourceName, enableInput, GetRequestHeaders()) as ReplicationProtectedItemOperationResponse); } else { throw new NotImplementedException(); } }
public void ApplyRecoveryPoint() { using (UndoContext context = UndoContext.Current) { context.Start(); var client = GetSiteRecoveryClient(CustomHttpHandler); var fabrics = client.Fabrics.List(RequestHeaders); Fabric selectedFabric = null; ProtectionContainer selectedContainer = null; foreach (var fabric in fabrics.Fabrics) { if (fabric.Properties.CustomDetails.InstanceType.Contains("VMM")) { selectedFabric = fabric; break; } } var containers = client.ProtectionContainer.List(selectedFabric.Name, RequestHeaders); foreach (var container in containers.ProtectionContainers) { if (container.Properties.ProtectedItemCount > 0 && container.Properties.Role.Equals("Primary")) { selectedContainer = container; break; } } string fabricId = selectedFabric.Name; string containerId = selectedContainer.Name; if (selectedContainer != null) { var pgs = client.ReplicationProtectedItem.List(fabricId, containerId, RequestHeaders); var rps = client.RecoveryPoint.List(fabricId, containerId, pgs.ReplicationProtectedItems[0].Name, RequestHeaders); ApplyRecoveryPointInputProperties applyRpProp = new ApplyRecoveryPointInputProperties() { RecoveryPointId = rps.RecoveryPoints[rps.RecoveryPoints.Count - 2].Id, ProviderSpecificDetails = new HyperVReplicaAzureApplyRecoveryPointInput() { VaultLocation = "SoutheastAsia" } }; ApplyRecoveryPointInput applyRpInput = new ApplyRecoveryPointInput() { Properties = applyRpProp }; var applyRpResp = client.ReplicationProtectedItem.ApplyRecoveryPoint( fabricId, containerId, pgs.ReplicationProtectedItems[0].Name, applyRpInput, RequestHeaders); } else { throw new System.Exception("Container not found."); } } }
/// <summary> /// Write Protection Container. /// </summary> /// <param name="protectionContainer">Protection Container</param> private void WriteProtectionContainer(ProtectionContainer protectionContainer) { this.WriteObject(new ASRProtectionContainer(protectionContainer)); }
public void EndToEndB2ASingleVM() { using (UndoContext context = UndoContext.Current) { context.Start(); var client = GetSiteRecoveryClient(CustomHttpHandler); bool createPolicy = true; bool pairClouds = true; bool enableDR = true; bool pfo = true; bool commit = true; bool tfo = true; bool pfoReverse = true; bool commitReverse = true; bool reprotect = true; bool disableDR = true; bool unpair = true; bool removePolicy = true; // Process Variables string fabricName = string.Empty; string recCldName = "Microsoft Azure"; string priCldName = string.Empty; string policyName = "Hydra-EndToEndB2ASingleVM-" + (new Random()).Next(); string mappingName = "Mapping-EndToEndB2ASingleVM-" + (new Random()).Next(); string enableDRName = string.Empty; string protectedItemName = "PE" + (new Random()).Next(); // Data Variables Fabric selectedFabric = null; ProtectionContainer primaryCloud = null; Policy selectedPolicy = null; ProtectableItem protectableItem = null; ReplicationProtectedItem protectedItem = null; // Fetch HyperV if (string.IsNullOrEmpty(fabricName)) { var fabrics = client.Fabrics.List(RequestHeaders); foreach (var fabric in fabrics.Fabrics) { if (fabric.Properties.CustomDetails.InstanceType.Contains("HyperV")) { selectedFabric = fabric; fabricName = selectedFabric.Name; } } } else { selectedFabric = client.Fabrics.Get(fabricName, RequestHeaders).Fabric; } // Fetch Cloud primaryCloud = client.ProtectionContainer.List(selectedFabric.Name, RequestHeaders).ProtectionContainers[0]; priCldName = primaryCloud.Name; if (createPolicy) { HyperVReplicaAzurePolicyInput hvrAPolicy = new HyperVReplicaAzurePolicyInput() { ApplicationConsistentSnapshotFrequencyInHours = 0, Encryption = "Disable", OnlineIrStartTime = null, RecoveryPointHistoryDuration = 0, ReplicationInterval = 30, StorageAccounts = new List <string>() { "/subscriptions/19b823e2-d1f3-4805-93d7-401c5d8230d5/resourceGroups/Default-Storage-WestUS/providers/Microsoft.ClassicStorage/storageAccounts/bvtmapped2storacc" } }; CreatePolicyInputProperties createInputProp = new CreatePolicyInputProperties() { ProviderSpecificInput = hvrAPolicy }; CreatePolicyInput policyInput = new CreatePolicyInput() { Properties = createInputProp }; selectedPolicy = (client.Policies.Create(policyName, policyInput, RequestHeaders) as CreatePolicyOperationResponse).Policy; } else { selectedPolicy = client.Policies.Get(policyName, RequestHeaders).Policy; } if (pairClouds) { CreateProtectionContainerMappingInputProperties pairingProps = new CreateProtectionContainerMappingInputProperties() { PolicyId = selectedPolicy.Id, TargetProtectionContainerId = recCldName, ProviderSpecificInput = new ReplicationProviderContainerMappingInput() }; CreateProtectionContainerMappingInput pairingInput = new CreateProtectionContainerMappingInput() { Properties = pairingProps }; var pairingResponse = client.ProtectionContainerMapping.ConfigureProtection( selectedFabric.Name, primaryCloud.Name, mappingName, pairingInput, RequestHeaders); } if (enableDR) { if (string.IsNullOrEmpty(enableDRName)) { protectableItem = client.ProtectableItem.List(selectedFabric.Name, primaryCloud.Name, "Unprotected", RequestHeaders).ProtectableItems[0]; enableDRName = protectableItem.Name; } else { protectableItem = client.ProtectableItem.Get(selectedFabric.Name, primaryCloud.Name, enableDRName, RequestHeaders).ProtectableItem; } HyperVReplicaAzureEnableProtectionInput hvrAEnableDRInput = new HyperVReplicaAzureEnableProtectionInput() { HvHostVmId = (protectableItem.Properties.CustomDetails as HyperVVirtualMachineDetails).SourceItemId, OSType = "Windows", VhdId = (protectableItem.Properties.CustomDetails as HyperVVirtualMachineDetails).DiskDetailsList[0].VhdId, VmName = protectableItem.Properties.FriendlyName, TargetStorageAccountId = "/subscriptions/19b823e2-d1f3-4805-93d7-401c5d8230d5/resourceGroups/Default-Storage-WestUS/providers/Microsoft.ClassicStorage/storageAccounts/bvtmapped2storacc", }; EnableProtectionInputProperties enableDRProp = new EnableProtectionInputProperties() { PolicyId = selectedPolicy.Id, ProtectableItemId = protectableItem.Id, ProviderSpecificDetails = hvrAEnableDRInput }; EnableProtectionInput enableDRInput = new EnableProtectionInput() { Properties = enableDRProp }; DateTime enablStartTime = DateTime.UtcNow; protectedItem = ( client.ReplicationProtectedItem.EnableProtection( selectedFabric.Name, primaryCloud.Name, protectedItemName, enableDRInput, RequestHeaders) as ReplicationProtectedItemOperationResponse).ReplicationProtectedItem; MonitoringHelper.MonitorJobs(MonitoringHelper.AzureIrJobName, enablStartTime, client, RequestHeaders); } if (pfo || commit || tfo || pfoReverse || commitReverse || reprotect || disableDR) { protectableItem = client.ProtectableItem.Get(selectedFabric.Name, primaryCloud.Name, enableDRName, RequestHeaders).ProtectableItem; protectedItem = client.ReplicationProtectedItem.Get(selectedFabric.Name, primaryCloud.Name, protectedItemName, RequestHeaders).ReplicationProtectedItem; // Create Input for Operations ///////////////////////////// PFO ///////////////////////////////////// HyperVReplicaAzureFailoverProviderInput hvrAFOInput = new HyperVReplicaAzureFailoverProviderInput() { VaultLocation = "West US", }; PlannedFailoverInputProperties plannedFailoverProp = new PlannedFailoverInputProperties() { FailoverDirection = "", ProviderSpecificDetails = hvrAFOInput }; PlannedFailoverInput plannedFailoverInput = new PlannedFailoverInput() { Properties = plannedFailoverProp }; HyperVReplicaAzureFailbackProviderInput hvrAFBInput = new HyperVReplicaAzureFailbackProviderInput() { RecoveryVmCreationOption = "NoAction", DataSyncOption = "ForSyncronization" }; PlannedFailoverInputProperties plannedFailbackProp = new PlannedFailoverInputProperties() { FailoverDirection = "", ProviderSpecificDetails = hvrAFBInput }; PlannedFailoverInput plannedFailbackInput = new PlannedFailoverInput() { Properties = plannedFailbackProp }; ////////////////////////////// Reprotect ////////////////////////////////////// HyperVReplicaAzureReprotectInput hvrARRInput = new HyperVReplicaAzureReprotectInput() { HvHostVmId = (protectableItem.Properties.CustomDetails as HyperVVirtualMachineDetails).SourceItemId, OSType = "Windows", VHDId = (protectableItem.Properties.CustomDetails as HyperVVirtualMachineDetails).DiskDetailsList[0].VhdId, VmName = protectableItem.Properties.FriendlyName, StorageAccountId = "/subscriptions/19b823e2-d1f3-4805-93d7-401c5d8230d5/resourceGroups/Default-Storage-WestUS/providers/Microsoft.ClassicStorage/storageAccounts/bvtmapped2storacc", }; ReverseReplicationInputProperties rrProp = new ReverseReplicationInputProperties() { FailoverDirection = "", ProviderSpecificDetails = hvrARRInput }; ReverseReplicationInput rrInput = new ReverseReplicationInput() { Properties = rrProp }; ////////////////////////////////// UFO ///////////////////////////////////////// UnplannedFailoverInputProperties ufoProp = new UnplannedFailoverInputProperties() { ProviderSpecificDetails = hvrAFOInput, SourceSiteOperations = "NotRequired" }; UnplannedFailoverInput ufoInput = new UnplannedFailoverInput() { Properties = ufoProp }; /////////////////////////////////// TFO ///////////////////////////////////////////// TestFailoverInputProperties tfoProp = new TestFailoverInputProperties() { ProviderSpecificDetails = hvrAFOInput }; TestFailoverInput tfoInput = new TestFailoverInput() { Properties = tfoProp }; ////////////////////////////////////////////////////////////////////////////////////////// if (pfo) { var plannedfailover = client.ReplicationProtectedItem.PlannedFailover(selectedFabric.Name, primaryCloud.Name, protectedItem.Name, plannedFailoverInput, RequestHeaders); } if (commit) { var commitFailover = client.ReplicationProtectedItem.CommitFailover(selectedFabric.Name, primaryCloud.Name, protectedItem.Name, RequestHeaders); } if (pfoReverse) { //var unplannedFailoverReverse = client.ReplicationProtectedItem.UnplannedFailover(selectedFabric.Name, priCld, replicationProtectedItems.ReplicationProtectedItems[0].Name, ufoInput, RequestHeaders); var plannedFailoverReverse = client.ReplicationProtectedItem.PlannedFailover(selectedFabric.Name, primaryCloud.Name, protectedItem.Name, plannedFailbackInput, RequestHeaders); } if (commitReverse) { var commitFailoverReverse = client.ReplicationProtectedItem.CommitFailover(selectedFabric.Name, primaryCloud.Name, protectedItem.Name, RequestHeaders); } if (reprotect) { var reprotectStartTime = DateTime.UtcNow; var rrReverseOp = client.ReplicationProtectedItem.Reprotect(selectedFabric.Name, primaryCloud.Name, protectedItem.Name, rrInput, RequestHeaders); MonitoringHelper.MonitorJobs(MonitoringHelper.AzureIrJobName, reprotectStartTime, client, RequestHeaders); } if (tfo) { DateTime startTFO = DateTime.UtcNow; var tfoOp = client.ReplicationProtectedItem.TestFailover(selectedFabric.Name, primaryCloud.Name, protectedItem.Name, tfoInput, RequestHeaders); var jobs = MonitoringHelper.GetJobId(MonitoringHelper.TestFailoverJobName, startTFO, client, RequestHeaders); ResumeJobParamsProperties resProp = new ResumeJobParamsProperties() { Comments = "Res TFO" }; ResumeJobParams resParam = new ResumeJobParams() { Properties = resProp }; var resJob = client.Jobs.Resume(jobs.Name, resParam, RequestHeaders); } if (disableDR) { var disableDROperation = client.ReplicationProtectedItem.DisableProtection(selectedFabric.Name, primaryCloud.Name, protectedItem.Name, new DisableProtectionInput(), RequestHeaders); } if (unpair) { var unpairClouds = client.ProtectionContainerMapping.UnconfigureProtection( selectedFabric.Name, primaryCloud.Name, mappingName, new RemoveProtectionContainerMappingInput(), RequestHeaders); } } if (removePolicy) { var policyDeletion = client.Policies.Delete(selectedPolicy.Name, RequestHeaders); } } }