/// <summary>
 /// Starts Azure Site Recovery Planned failover.
 /// </summary>
 /// <param name="recoveryPlanName">Recovery Plan Name</param>
 /// <param name="input">Recovery Plan Planned Failover Input</param>
 /// <returns>Job response</returns>
 public LongRunningOperationResponse StartAzureSiteRecoveryPlannedFailover(string recoveryPlanName, RecoveryPlanPlannedFailoverInput input)
 {
     return this.GetSiteRecoveryClient().RecoveryPlan.BeginPlannedFailover(
         recoveryPlanName,
         input,
         this.GetRequestHeaders());
 }
        /// <summary>
        /// Starts RP Planned failover.
        /// </summary>
        private void StartRpPlannedFailover()
        {
            // Refresh RP Object
            var rp = RecoveryServicesClient.GetAzureSiteRecoveryRecoveryPlan(this.RecoveryPlan.Name);

            var recoveryPlanPlannedFailoverInputProperties = new RecoveryPlanPlannedFailoverInputProperties()
            {
                FailoverDirection = this.Direction,
                ProviderSpecificDetails = new List<RecoveryPlanProviderSpecificFailoverInput>()
            };

            foreach (string replicationProvider in rp.RecoveryPlan.Properties.ReplicationProviders)
            {
                if (0 == string.Compare(
                    replicationProvider,
                    Constants.HyperVReplicaAzure,
                    StringComparison.OrdinalIgnoreCase))
                {
                    if (this.Direction == Constants.PrimaryToRecovery)
                    {
                        var recoveryPlanHyperVReplicaAzureFailoverInput = new RecoveryPlanHyperVReplicaAzureFailoverInput()
                        {
                            InstanceType = replicationProvider,
                            PrimaryKekCertificatePfx = primaryKekCertpfx,
                            SecondaryKekCertificatePfx = secondaryKekCertpfx,
                            VaultLocation = this.GetCurrentVaultLocation()
                        };
                        recoveryPlanPlannedFailoverInputProperties.ProviderSpecificDetails.Add(recoveryPlanHyperVReplicaAzureFailoverInput);
                    }
                    else
                    {
                        var recoveryPlanHyperVReplicaAzureFailbackInput = new RecoveryPlanHyperVReplicaAzureFailbackInput()
                        {
                            InstanceType = replicationProvider + "Failback",
                            DataSyncOption = this.Optimize == Constants.ForDowntime ? Constants.ForDowntime : Constants.ForSynchronization,
                            RecoveryVmCreationOption = "CreateVmIfNotFound" //CreateVmIfNotFound | NoAction
                        };
                        recoveryPlanPlannedFailoverInputProperties.ProviderSpecificDetails.Add(recoveryPlanHyperVReplicaAzureFailbackInput);
                    }
                }
            }

            var recoveryPlanPlannedFailoverInput = new RecoveryPlanPlannedFailoverInput()
            {
                Properties = recoveryPlanPlannedFailoverInputProperties
            };

            LongRunningOperationResponse response = RecoveryServicesClient.StartAzureSiteRecoveryPlannedFailover(
                this.RecoveryPlan.Name,
                recoveryPlanPlannedFailoverInput);

            JobResponse jobResponse =
                RecoveryServicesClient
                .GetAzureSiteRecoveryJobDetails(PSRecoveryServicesClient.GetJobIdFromReponseLocation(response.Location));

            WriteObject(new ASRJob(jobResponse.Job));
        }
        public void RecoveryPlan_ValidateE2E()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();

                var client = GetSiteRecoveryClient(CustomHttpHandler);
                var fabrics = client.Fabrics.List(RequestHeaders).Fabrics.ToList();
                var containers =
                    client.ProtectionContainer.List(
                        fabrics.First().Name,
                        RequestHeaders)
                    .ProtectionContainers
                    .ToList();
                var vms = 
                    client.ReplicationProtectedItem.List(
                        fabrics.First().Name,
                        containers.First().Name,
                        RequestHeaders)
                    .ReplicationProtectedItems
                    .ToList();

                // Create one recovery plan.
                var rpName = "Test-" + Guid.NewGuid().ToString();
                var input = GetE2EInput(fabrics.First().Id, vms.First().Id);
                client.RecoveryPlan.Create(rpName, input, RequestHeaders);

                // Test failover.
                var tfoInput = new RecoveryPlanTestFailoverInput()
                {
                    Properties = new RecoveryPlanTestFailoverInputProperties()
                    {
                        FailoverDirection = "PrimaryToRecovery",
                        NetworkType = "NoNetworkAttachAsInput"
                    }
                };
                client.RecoveryPlan.TestFailover(rpName, tfoInput, RequestHeaders);

                // Planned failover.
                var pfoInput = new RecoveryPlanPlannedFailoverInput()
                {
                    Properties = new RecoveryPlanPlannedFailoverInputProperties()
                    {
                        FailoverDirection = "PrimaryToRecovery"
                    }
                };
                client.RecoveryPlan.PlannedFailover(rpName, pfoInput, RequestHeaders);

                // Commit failover.
                client.RecoveryPlan.CommitFailover(rpName, RequestHeaders);

                // Reverse replicate.
                client.RecoveryPlan.Reprotect(rpName, RequestHeaders);

                // Unplanned failover.
                var ufoInput = new RecoveryPlanUnplannedFailoverInput()
                {
                    Properties = new RecoveryPlanUnplannedFailoverInputProperties()
                    {
                        FailoverDirection = "RecoveryToPrimary",
                        SourceSiteOperations = "NotRequired"
                    }
                };
                client.RecoveryPlan.UnplannedFailover(rpName, ufoInput, RequestHeaders);

                // Reverse replicate.
                client.RecoveryPlan.Reprotect(rpName, RequestHeaders);
            }
        }
        public void RecoveryPlan_ValidateE2A()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();

                var client = GetSiteRecoveryClient(CustomHttpHandler);
                var fabrics = client.Fabrics.List(RequestHeaders).Fabrics.ToList();
                var containers =
                    client.ProtectionContainer.List(
                        fabrics.First().Name,
                        RequestHeaders)
                    .ProtectionContainers
                    .ToList();
                var vms = 
                    client.ReplicationProtectedItem.List(
                        fabrics.First().Name,
                        containers.First().Name,
                        RequestHeaders)
                    .ReplicationProtectedItems
                    .ToList();

                // Create one recovery plan.
                var rpName = "Test-" + Guid.NewGuid().ToString();
                var input = GetE2AInput(fabrics.First().Id, vms.First().Id);
                client.RecoveryPlan.Create(rpName, input, RequestHeaders);

                // Test failover.
                var tfoInput = new RecoveryPlanTestFailoverInput()
                {
                    Properties = new RecoveryPlanTestFailoverInputProperties()
                    {
                        FailoverDirection = "PrimaryToRecovery",
                        NetworkType = "NoNetworkAttachAsInput",
                        ProviderSpecificDetails = new List<RecoveryPlanProviderSpecificFailoverInput>()
                        {
                            new RecoveryPlanHyperVReplicaAzureFailoverInput()
                            {
                                VaultLocation = "Southeast Asia"
                            }
                        }
                    }
                };
                client.RecoveryPlan.TestFailover(rpName, tfoInput, RequestHeaders);

                // Planned failover.
                var pfoInput = new RecoveryPlanPlannedFailoverInput()
                {
                    Properties = new RecoveryPlanPlannedFailoverInputProperties()
                    {
                        FailoverDirection = "PrimaryToRecovery",
                        ProviderSpecificDetails = new List<RecoveryPlanProviderSpecificFailoverInput>()
                        {
                            new RecoveryPlanHyperVReplicaAzureFailoverInput()
                            {
                                VaultLocation = "Southeast Asia"
                            }
                        }
                    }
                };
                client.RecoveryPlan.PlannedFailover(rpName, pfoInput, RequestHeaders);

                // Commit failover.
                client.RecoveryPlan.CommitFailover(rpName, RequestHeaders);

                // Planned failback.
                var fbInput = new RecoveryPlanPlannedFailoverInput()
                {
                    Properties = new RecoveryPlanPlannedFailoverInputProperties()
                    {
                        FailoverDirection = "RecoveryToPrimary",
                        ProviderSpecificDetails = new List<RecoveryPlanProviderSpecificFailoverInput>()
                        {
                            new RecoveryPlanHyperVReplicaAzureFailbackInput()
                            {
                                DataSyncOption = "ForSyncronization",
                                RecoveryVmCreationOption = "NoAction"
                            }
                        }
                    }
                };
                client.RecoveryPlan.PlannedFailover(rpName, fbInput, RequestHeaders);

                // Commit failover.
                client.RecoveryPlan.CommitFailover(rpName, RequestHeaders);

                // Reverse replicate.
                client.RecoveryPlan.Reprotect(rpName, RequestHeaders);

                // Unplanned failover.
                var ufoInput = new RecoveryPlanUnplannedFailoverInput()
                {
                    Properties = new RecoveryPlanUnplannedFailoverInputProperties()
                    {
                        FailoverDirection = "PrimaryToRecovery",
                        SourceSiteOperations = "NotRequired",
                        ProviderSpecificDetails = new List<RecoveryPlanProviderSpecificFailoverInput>()
                        {
                            new RecoveryPlanHyperVReplicaAzureFailoverInput()
                            {
                                VaultLocation = "Southeast Asia"
                            }
                        }
                    }
                };
                client.RecoveryPlan.UnplannedFailover(rpName, ufoInput, RequestHeaders);
            }
        }
 /// <summary>
 /// Planned failover of the recovery plan.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.SiteRecovery.IRecoveryPlanOperations.
 /// </param>
 /// <param name='recoveryPlanName'>
 /// Required. Recovery plan name.
 /// </param>
 /// <param name='input'>
 /// Required. Recovery plan planned failover input.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// A standard service response for long running operations.
 /// </returns>
 public static Task<LongRunningOperationResponse> BeginPlannedFailoverAsync(this IRecoveryPlanOperations operations, string recoveryPlanName, RecoveryPlanPlannedFailoverInput input, CustomRequestHeaders customRequestHeaders)
 {
     return operations.BeginPlannedFailoverAsync(recoveryPlanName, input, customRequestHeaders, CancellationToken.None);
 }
 /// <summary>
 /// Planned failover of the recovery plan.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.SiteRecovery.IRecoveryPlanOperations.
 /// </param>
 /// <param name='recoveryPlanName'>
 /// Required. Recovery plan name.
 /// </param>
 /// <param name='input'>
 /// Required. Recovery plan planned failover input.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// A standard service response for long running operations.
 /// </returns>
 public static LongRunningOperationResponse BeginPlannedFailover(this IRecoveryPlanOperations operations, string recoveryPlanName, RecoveryPlanPlannedFailoverInput input, CustomRequestHeaders customRequestHeaders)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IRecoveryPlanOperations)s).BeginPlannedFailoverAsync(recoveryPlanName, input, customRequestHeaders);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }