/// <summary>
 /// Creates a 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. Create recovery plan input.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// A standard service response for long running operations.
 /// </returns>
 public static LongRunningOperationResponse BeginCreating(this IRecoveryPlanOperations operations, string recoveryPlanName, CreateRecoveryPlanInput input, CustomRequestHeaders customRequestHeaders)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IRecoveryPlanOperations)s).BeginCreatingAsync(recoveryPlanName, input, customRequestHeaders);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
 /// <summary>
 /// Starts Creating Recovery Plan.
 /// </summary>
 /// <param name="recoveryPlanName">Recovery Plan Name</param>
 /// <param name="input">Create Recovery Plan Input</param>
 /// <returns>Job response</returns>
 public LongRunningOperationResponse CreateAzureSiteRecoveryRecoveryPlan(string recoveryPlanName, CreateRecoveryPlanInput input)
 {
     return this.GetSiteRecoveryClient().RecoveryPlan.BeginCreating(recoveryPlanName,
         input,
         this.GetRequestHeaders());
 }
        private static CreateRecoveryPlanInput GetCreateInput2(string fabricId, string vmId)
        {
            CreateRecoveryPlanInput input = new CreateRecoveryPlanInput();
            input.Properties = new CreateRecoveryPlanInputProperties();
            input.Properties.PrimaryFabricId = fabricId;
            input.Properties.RecoveryFabricId = fabricId;
            input.Properties.Groups = new List<RecoveryPlanGroup>();
            input.Properties.Groups.Add(new RecoveryPlanGroup()
            {
                GroupType = "Shutdown",
                ReplicationProtectedItems = new List<RecoveryPlanProtectedItem>()
                {
                    new RecoveryPlanProtectedItem()
                    {
                        Id = vmId
                    }
                },
                StartGroupActions = new List<RecoveryPlanAction>()
                {
                    new RecoveryPlanAction()
                    {
                        ActionName = "S2",
                        FailoverTypes = new List<string>() { "PlannedFailover" },
                        FailoverDirections = new List<string>() { "PrimaryToRecovery" },
                        CustomDetails = new RecoveryPlanScriptActionDetails()
                        {
                            FabricLocation = "Primary",
                            Path = "path2",
                            Timeout = null
                        }
                    },
                    new RecoveryPlanAction()
                    {
                        ActionName = "M2",
                        FailoverTypes = new List<string>() { "UnplannedFailover" },
                        FailoverDirections = new List<string>() { "RecoveryToPrimary" },
                        CustomDetails = new RecoveryPlanManualActionDetails()
                        {
                            Description = "desc2"
                        }
                    }
                },
                EndGroupActions = new List<RecoveryPlanAction>()
                {
                }
            });

            return input;
        }
        private static CreateRecoveryPlanInput GetE2EInput(string fabricId, string vmId)
        {
            CreateRecoveryPlanInput input = new CreateRecoveryPlanInput();
            input.Properties = new CreateRecoveryPlanInputProperties();
            input.Properties.PrimaryFabricId = fabricId;
            input.Properties.RecoveryFabricId = fabricId;
            input.Properties.Groups = new List<RecoveryPlanGroup>();
            input.Properties.Groups.Add(new RecoveryPlanGroup()
            {
                GroupType = "Boot",
                ReplicationProtectedItems = new List<RecoveryPlanProtectedItem>()
                        {
                            new RecoveryPlanProtectedItem()
                            {
                                Id = vmId
                            }
                        }
            });

            return input;
        }
        private static CreateRecoveryPlanInput GetE2AInput(string fabricId, string vmId)
        {
            CreateRecoveryPlanInput input = new CreateRecoveryPlanInput();
            input.Properties = new CreateRecoveryPlanInputProperties();
            input.Properties.PrimaryFabricId = fabricId;
            input.Properties.RecoveryFabricId = "microsoft azure";
            input.Properties.FailoverDeploymentModel = "Classic";
            input.Properties.Groups = new List<RecoveryPlanGroup>();
            input.Properties.Groups.Add(new RecoveryPlanGroup()
            {
                GroupType = "Boot",
                ReplicationProtectedItems = new List<RecoveryPlanProtectedItem>()
                        {
                            new RecoveryPlanProtectedItem()
                            {
                                Id = vmId
                            }
                        }
            });

            return input;
        }
 /// <summary>
 /// Creates a 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. Create recovery plan input.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// A standard service response for long running operations.
 /// </returns>
 public static Task<LongRunningOperationResponse> BeginCreatingAsync(this IRecoveryPlanOperations operations, string recoveryPlanName, CreateRecoveryPlanInput input, CustomRequestHeaders customRequestHeaders)
 {
     return operations.BeginCreatingAsync(recoveryPlanName, input, customRequestHeaders, CancellationToken.None);
 }
        /// <summary>
        /// Create Replication Plan: Utility call
        /// </summary>
        private void CreateRecoveryPlan(string recoveryPlanName, CreateRecoveryPlanInput createRecoveryPlanInput)
        {
            LongRunningOperationResponse response =
                RecoveryServicesClient.CreateAzureSiteRecoveryRecoveryPlan(recoveryPlanName, createRecoveryPlanInput);

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

            WriteObject(new ASRJob(jobResponse.Job));
        }
        /// <summary>
        /// Create Recovery Plan: By Service object
        /// </summary>
        private void CreateRecoveryPlan(RecoveryPlan recoveryPlan)
        {
            CreateRecoveryPlanInputProperties createRecoveryPlanInputProperties = new CreateRecoveryPlanInputProperties()
            {
                FailoverDeploymentModel = recoveryPlan.Properties.FailoverDeploymentModel,
                Groups = recoveryPlan.Properties.Groups,
                PrimaryFabricId = recoveryPlan.Properties.PrimaryFabricId,
                RecoveryFabricId = recoveryPlan.Properties.RecoveryFabricId
            };

            CreateRecoveryPlanInput createRecoveryPlanInput = new CreateRecoveryPlanInput()
            {
                Properties = createRecoveryPlanInputProperties
            };

            CreateRecoveryPlan(recoveryPlan.Name, createRecoveryPlanInput);
        }
        /// <summary>
        /// Creates Replication Plan
        /// </summary>
        private void CreateRecoveryPlan()
        {
            CreateRecoveryPlanInputProperties createRecoveryPlanInputProperties = new CreateRecoveryPlanInputProperties()
            {
                FailoverDeploymentModel = failoverDeploymentModel,
                Groups = new List<RecoveryPlanGroup>(),
                PrimaryFabricId = this.primaryserver,
                RecoveryFabricId = this.recoveryserver
            };

            RecoveryPlanGroup recoveryPlanGroup = new RecoveryPlanGroup()
            {
                GroupType = Constants.Boot,
                ReplicationProtectedItems = new List<RecoveryPlanProtectedItem>(),
                StartGroupActions = new List<RecoveryPlanAction>(),
                EndGroupActions = new List<RecoveryPlanAction>()
            };

            foreach (ASRReplicationProtectedItem rpi in ReplicationProtectedItem)
            {
                string fabricName = Utilities.GetValueFromArmId(rpi.ID, ARMResourceTypeConstants.ReplicationFabrics);

                ReplicationProtectedItemResponse replicationProtectedItemResponse =
                            RecoveryServicesClient.GetAzureSiteRecoveryReplicationProtectedItem(fabricName,
                            Utilities.GetValueFromArmId(rpi.ID, ARMResourceTypeConstants.ReplicationProtectionContainers), 
                            rpi.Name);

                string VmId = null;

                if (replicationProtectedItemResponse.ReplicationProtectedItem.Properties.ProviderSpecificDetails.GetType() == typeof(HyperVReplicaAzureReplicationDetails))
                {
                    VmId = ((HyperVReplicaAzureReplicationDetails)replicationProtectedItemResponse.ReplicationProtectedItem.Properties.ProviderSpecificDetails).VmId;
                }
                else if (replicationProtectedItemResponse.ReplicationProtectedItem.Properties.ProviderSpecificDetails.GetType() == typeof(HyperVReplica2012ReplicationDetails))
                {
                    VmId = ((HyperVReplica2012ReplicationDetails)replicationProtectedItemResponse.ReplicationProtectedItem.Properties.ProviderSpecificDetails).VmId;
                }

                RecoveryPlanProtectedItem recoveryPlanProtectedItem = new RecoveryPlanProtectedItem();
                recoveryPlanProtectedItem.Id = replicationProtectedItemResponse.ReplicationProtectedItem.Id;
                recoveryPlanProtectedItem.VirtualMachineId = VmId;
                recoveryPlanGroup.ReplicationProtectedItems.Add(recoveryPlanProtectedItem);
            }

            createRecoveryPlanInputProperties.Groups.Add(recoveryPlanGroup);

            CreateRecoveryPlanInput createRecoveryPlanInput = new CreateRecoveryPlanInput()
            {
                Properties = createRecoveryPlanInputProperties
            };

            CreateRecoveryPlan(this.Name, createRecoveryPlanInput);
        }