/// <summary>
 /// Sets protection on Protection entity.
 /// </summary>
 /// <param name="protectionContainerId">Protection Container ID</param>
 /// <param name="virtualMachineId">Virtual Machine ID</param>
 /// <param name="input">Enable protection input.</param>
 /// <returns>Job response</returns>
 public JobResponse EnableProtection(
     string protectionContainerId,
     string virtualMachineId,
     EnableProtectionInput input)
 {
     return this.GetSiteRecoveryClient().ProtectionEntity.EnableProtection(
         protectionContainerId,
         virtualMachineId,
         input,
         this.GetRequestHeaders());
 }
 /// <summary>
 /// Enable Protection for the given protection entity.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.SiteRecovery.IProtectionEntityOperations.
 /// </param>
 /// <param name='protectionContainerId'>
 /// Required. Parent Protection Container ID.
 /// </param>
 /// <param name='protectionEntityId'>
 /// Required. Protection entity ID.
 /// </param>
 /// <param name='input'>
 /// Optional. Protection entity ID.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// The response model for the Job details object.
 /// </returns>
 public static Task<JobResponse> EnableProtectionAsync(this IProtectionEntityOperations operations, string protectionContainerId, string protectionEntityId, EnableProtectionInput input, CustomRequestHeaders customRequestHeaders)
 {
     return operations.EnableProtectionAsync(protectionContainerId, protectionEntityId, input, customRequestHeaders, CancellationToken.None);
 }
 /// <summary>
 /// Enable Protection for the given protection entity.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.SiteRecovery.IProtectionEntityOperations.
 /// </param>
 /// <param name='protectionContainerId'>
 /// Required. Parent Protection Container ID.
 /// </param>
 /// <param name='protectionEntityId'>
 /// Required. Protection entity ID.
 /// </param>
 /// <param name='input'>
 /// Optional. Protection entity ID.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// The response model for the Job details object.
 /// </returns>
 public static JobResponse EnableProtection(this IProtectionEntityOperations operations, string protectionContainerId, string protectionEntityId, EnableProtectionInput input, CustomRequestHeaders customRequestHeaders)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IProtectionEntityOperations)s).EnableProtectionAsync(protectionContainerId, protectionEntityId, input, customRequestHeaders);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        /// <summary>
        /// ProcessRecord of the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            switch (this.ParameterSetName)
            {
                case ASRParameterSets.ByPEObject:
                    this.Id = this.ProtectionEntity.ID;
                    this.ProtectionContainerId = this.ProtectionEntity.ProtectionContainerId;
                    this.targetNameOrId = this.ProtectionEntity.Name;
                    this.alreadyEnabled = this.ProtectionEntity.Protected;

                    break;
                case ASRParameterSets.ByIDs:
                    this.targetNameOrId = this.Id;
                    ProtectionEntityResponse protectionEntityResponse =
                        RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity(
                        this.ProtectionContainerId,
                        this.Id);
                    this.alreadyEnabled = protectionEntityResponse.ProtectionEntity.Protected;
                    this.targetNameOrId = protectionEntityResponse.ProtectionEntity.Name;

                    break;
            }

            if (this.alreadyEnabled &&
                this.Protection.Equals(Constants.EnableProtection, StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException(
                    string.Format(                   
                        Properties.Resources.ProtectionEntityAlreadyEnabled,
                        this.targetNameOrId));
            }
            else if (!this.alreadyEnabled &&
                this.Protection.Equals(Constants.DisableProtection, StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException(
                    Properties.Resources.ProtectionEntityAlreadyDisabled,
                    this.targetNameOrId);
            }

            this.ConfirmAction(
                this.Force.IsPresent || 0 != string.CompareOrdinal(this.Protection, Constants.DisableProtection),
                string.Format(Properties.Resources.DisableProtectionWarning, this.targetNameOrId),
                string.Format(Properties.Resources.DisableProtectionWhatIfMessage, this.Protection),
                this.targetNameOrId,
                () =>
                    {
                        try
                        {
                            if (this.Protection == Constants.EnableProtection)
                            {
                                string profileId = string.Empty;
                                var input = new EnableProtectionInput();

                                if (this.ProtectionEntity == null)
                                {
                                    var pe = RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity(
                                        this.ProtectionContainerId,
                                        this.Id);
                                    this.ProtectionEntity = new ASRProtectionEntity(pe.ProtectionEntity);
                                }

                                // Get the replciation provider from profile object otherwise assume its E2E.
                                // Let the call go without profileId set.
                                string replicationProvider = null;

                                if (this.ProtectionProfile != null)
                                {
                                    profileId = this.ProtectionProfile.ID;
                                    replicationProvider = this.ProtectionProfile.ReplicationProvider;
                                }
                                else
                                {
                                    string pcId = this.ProtectionContainerId ?? this.ProtectionEntity.ProtectionContainerId;
                                    var pc = RecoveryServicesClient.GetAzureSiteRecoveryProtectionContainer(
                                        pcId);

                                    // PC will have all profiles associated with same replciation providers only.
                                    replicationProvider =
                                        pc.ProtectionContainer.AvailableProtectionProfiles.Count < 1 ?
                                        null :
                                        pc.ProtectionContainer.AvailableProtectionProfiles[0].ReplicationProvider;

                                    if (replicationProvider != Constants.HyperVReplica)
                                    {
                                        throw new Exception("Please provide the protection profile object. It can be chosen from available protection profiles of the protection container.");
                                    }
                                }

                                if (this.ParameterSetName == ASRParameterSets.ByIDs)
                                {
                                    this.ValidateUsageById(replicationProvider, "Id");
                                }

                                if (replicationProvider == Constants.HyperVReplicaAzure)
                                {
                                    input.ProtectionProfileId = this.ProtectionProfile.ID;
                                    AzureEnableProtectionInput azureInput = new AzureEnableProtectionInput();
                                    azureInput.HvHostVmId = this.ProtectionEntity.FabricObjectId;
                                    azureInput.VmName = this.ProtectionEntity.Name;

                                    azureInput.OSType = this.OS;
                                    if (string.IsNullOrWhiteSpace(this.OS))
                                    {
                                        azureInput.OSType = this.ProtectionEntity.OS;
                                    }

                                    if (string.IsNullOrWhiteSpace(this.OSDiskName))
                                    {
                                        azureInput.VHDId = this.ProtectionEntity.OSDiskId;
                                    }
                                    else
                                    {
                                        foreach (var disk in this.ProtectionEntity.Disks)
                                        {
                                            if (disk.Name == this.OSDiskName)
                                            {
                                                azureInput.VHDId = disk.Id;
                                                break;
                                            }
                                        }
                                    }

                                    input.ReplicationProviderInput = DataContractUtils.Serialize<AzureEnableProtectionInput>(azureInput);
                                }
                                else if (string.IsNullOrWhiteSpace(profileId))
                                {
                                    input = null;
                                }
                                else
                                {
                                    input.ReplicationProviderInput = string.Empty;
                                    input.ProtectionProfileId = profileId;
                                }

                                this.jobResponse =
                                    RecoveryServicesClient.EnableProtection(
                                    this.ProtectionContainerId,
                                    this.Id,
                                    input);
                            }
                            else
                            {
                                this.jobResponse =
                                    RecoveryServicesClient.DisbleProtection(
                                    this.ProtectionContainerId,
                                    this.Id);
                            }

                            this.WriteJob(this.jobResponse.Job);

                            if (this.WaitForCompletion.IsPresent)
                            {
                                this.WaitForJobCompletion(this.jobResponse.Job.ID);
                            }
                        }
                        catch (Exception exception)
                        {
                            this.HandleException(exception);
                        }
                    });
        }