/// <summary>
 /// Update Azure Site Recovery Policy.
 /// </summary>
 /// <param name="UpdatePolicyInput">Policy Input</param>
 /// <param name="policyName">Policy Name</param>
 /// <returns>Long operation response</returns>
 public LongRunningOperationResponse UpdatePolicy(string policyName, UpdatePolicyInput input)
 {
     return this.GetSiteRecoveryClient().Policies.BeginUpdating(
         input,
         policyName,
         this.GetRequestHeaders());
 }
 /// <summary>
 /// Update Policy.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.SiteRecovery.IPolicyOperations.
 /// </param>
 /// <param name='input'>
 /// Required. input.
 /// </param>
 /// <param name='policyName'>
 /// Required. Policy name.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// A standard service response for long running operations.
 /// </returns>
 public static Task<LongRunningOperationResponse> UpdateAsync(this IPolicyOperations operations, UpdatePolicyInput input, string policyName, CustomRequestHeaders customRequestHeaders)
 {
     return operations.UpdateAsync(input, policyName, customRequestHeaders, CancellationToken.None);
 }
 /// <summary>
 /// Update Policy.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.SiteRecovery.IPolicyOperations.
 /// </param>
 /// <param name='input'>
 /// Required. input.
 /// </param>
 /// <param name='policyName'>
 /// Required. Policy name.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// A standard service response for long running operations.
 /// </returns>
 public static LongRunningOperationResponse Update(this IPolicyOperations operations, UpdatePolicyInput input, string policyName, CustomRequestHeaders customRequestHeaders)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IPolicyOperations)s).UpdateAsync(input, policyName, customRequestHeaders);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        /// <summary>
        /// Creates an E2A Policy Object
        /// </summary>
        private void EnterpriseToAzurePolicyObject()
        {
            if (string.Compare(this.Policy.ReplicationProvider, Constants.HyperVReplicaAzure, StringComparison.OrdinalIgnoreCase) != 0)
            {
                throw new InvalidOperationException(
                    string.Format(
                    Properties.Resources.IncorrectReplicationProvider,
                    this.Policy.ReplicationProvider));
            }

            var replicationProviderSettings = this.Policy.ReplicationProviderSettings as ASRHyperVReplicaAzurePolicyDetails;

            this.replicationFrequencyInSeconds = this.MyInvocation.BoundParameters.ContainsKey(Utilities.GetMemberName(() => this.ReplicationFrequencyInSeconds)) ?
                PSRecoveryServicesClient.ConvertReplicationFrequencyToUshort(this.ReplicationFrequencyInSeconds) :
                (ushort)replicationProviderSettings.ReplicationFrequencyInSeconds;
            this.recoveryPoints = this.MyInvocation.BoundParameters.ContainsKey(Utilities.GetMemberName(() => this.RecoveryPoints)) ?
                this.RecoveryPoints :
                replicationProviderSettings.RecoveryPoints;
            this.applicationConsistentSnapshotFrequencyInHours = this.MyInvocation.BoundParameters.ContainsKey(Utilities.GetMemberName(() => this.ApplicationConsistentSnapshotFrequencyInHours)) ?
                this.ApplicationConsistentSnapshotFrequencyInHours :
                replicationProviderSettings.ApplicationConsistentSnapshotFrequencyInHours;           
            this.replicationStartTime = this.MyInvocation.BoundParameters.ContainsKey(Utilities.GetMemberName(() => this.ReplicationStartTime)) ?
                this.replicationStartTime :
                replicationProviderSettings.OnlineReplicationStartTime;
            this.recoveryAzureStorageAccountId = this.MyInvocation.BoundParameters.ContainsKey(Utilities.GetMemberName(() => this.RecoveryAzureStorageAccountId)) ?
                this.RecoveryAzureStorageAccountId :
                replicationProviderSettings.ActiveStorageAccountId;
            this.encryption = this.MyInvocation.BoundParameters.ContainsKey(Utilities.GetMemberName(() => this.Encryption)) ?
                this.Encryption :
                ((string.Compare(replicationProviderSettings.Encryption, "Disabled", StringComparison.OrdinalIgnoreCase) == 0) ? Constants.Disable : Constants.Enable);

            var hyperVReplicaAzurePolicyInput = new HyperVReplicaAzurePolicyInput()
            {
                ApplicationConsistentSnapshotFrequencyInHours = this.applicationConsistentSnapshotFrequencyInHours,
                Encryption = this.encryption,
                OnlineIrStartTime = this.replicationStartTime,
                RecoveryPointHistoryDuration = this.recoveryPoints,
                ReplicationInterval = this.replicationFrequencyInSeconds
            };

            hyperVReplicaAzurePolicyInput.StorageAccounts =
                   new System.Collections.Generic.List<string>();

            if (RecoveryAzureStorageAccountId != null)
            {
                string storageAccount = this.recoveryAzureStorageAccountId;
                hyperVReplicaAzurePolicyInput.StorageAccounts.Add(storageAccount);
            }

            var updatePolicyProperties = new UpdatePolicyProperties()
            {
                ReplicationProviderSettings = hyperVReplicaAzurePolicyInput
            };

            var updatePolicyInput = new UpdatePolicyInput()
            {
                Properties = updatePolicyProperties
            };

            LongRunningOperationResponse response =
                RecoveryServicesClient.UpdatePolicy(this.Policy.Name, updatePolicyInput);

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

            WriteObject(new ASRJob(jobResponse.Job));
        }
        /// <summary>
        /// Creates an E2E Policy object
        /// </summary>
        private void EnterpriseToEnterprisePolicyObject()
        {
            if (string.Compare(this.Policy.ReplicationProvider, Constants.HyperVReplica2012, StringComparison.OrdinalIgnoreCase) != 0 && string.Compare(this.Policy.ReplicationProvider, Constants.HyperVReplica2012R2, StringComparison.OrdinalIgnoreCase) != 0)
            {
                throw new InvalidOperationException(
                    string.Format(
                    Properties.Resources.IncorrectReplicationProvider,
                    this.Policy.ReplicationProvider));
            }

            var replicationProviderSettings = this.Policy.ReplicationProviderSettings as ASRHyperVReplicaPolicyDetails;

            this.replicationMethod = this.MyInvocation.BoundParameters.ContainsKey(Utilities.GetMemberName(() => this.ReplicationMethod)) ?
                ((string.Compare(this.ReplicationMethod, Constants.OnlineReplicationMethod, StringComparison.OrdinalIgnoreCase) == 0) ? "OverNetwork" : "Offline") :
                ((string.Compare(replicationProviderSettings.InitialReplicationMethod, Constants.OnlineReplicationMethod, StringComparison.OrdinalIgnoreCase) == 0) ? "OverNetwork" : "Offline");
            this.replicationFrequencyInSeconds = this.MyInvocation.BoundParameters.ContainsKey(Utilities.GetMemberName(() => this.ReplicationFrequencyInSeconds)) ? 
                PSRecoveryServicesClient.ConvertReplicationFrequencyToUshort(this.ReplicationFrequencyInSeconds) :
                replicationProviderSettings.ReplicationFrequencyInSeconds;
            this.recoveryPoints = this.MyInvocation.BoundParameters.ContainsKey(Utilities.GetMemberName(() => this.RecoveryPoints)) ?
                this.RecoveryPoints :
                replicationProviderSettings.RecoveryPoints;
            this.applicationConsistentSnapshotFrequencyInHours = this.MyInvocation.BoundParameters.ContainsKey(Utilities.GetMemberName(() => this.ApplicationConsistentSnapshotFrequencyInHours)) ?
                this.ApplicationConsistentSnapshotFrequencyInHours :
                replicationProviderSettings.ApplicationConsistentSnapshotFrequencyInHours;
            this.compression = this.MyInvocation.BoundParameters.ContainsKey(Utilities.GetMemberName(() => this.Compression)) ?
                this.Compression :
                replicationProviderSettings.Compression;
            this.replicationPort = this.MyInvocation.BoundParameters.ContainsKey(Utilities.GetMemberName(() => this.ReplicationPort)) ?
                this.ReplicationPort :
                replicationProviderSettings.ReplicationPort;
            this.authentication = this.MyInvocation.BoundParameters.ContainsKey(Utilities.GetMemberName(() => this.Authentication)) ?
                ((ushort)((string.Compare(this.Authentication, Constants.AuthenticationTypeKerberos, StringComparison.OrdinalIgnoreCase) == 0) ? 1 : 2)) :
                ((ushort)((string.Compare(replicationProviderSettings.AllowedAuthenticationType, Constants.AuthenticationTypeKerberos, StringComparison.OrdinalIgnoreCase) == 0) ? 1 : 2));
            this.replicationStartTime = this.MyInvocation.BoundParameters.ContainsKey(Utilities.GetMemberName(() => this.ReplicationStartTime)) ?
                this.replicationStartTime :
                replicationProviderSettings.OnlineReplicationStartTime;
            this.replicaDeletion = this.MyInvocation.BoundParameters.ContainsKey(Utilities.GetMemberName(() => this.ReplicaDeletion)) ?
                this.ReplicaDeletion :
                replicationProviderSettings.ReplicaDeletionOption;
                
            var updatePolicyProperties = new UpdatePolicyProperties();

            if (string.Compare(this.Policy.ReplicationProvider, Constants.HyperVReplica2012, StringComparison.OrdinalIgnoreCase) == 0)
            {
                updatePolicyProperties.ReplicationProviderSettings = new HyperVReplica2012PolicyInput()
                {
                    AllowedAuthenticationType = this.authentication,
                    ApplicationConsistentSnapshotFrequencyInHours = this.applicationConsistentSnapshotFrequencyInHours,
                    Compression = this.compression,
                    InitialReplicationMethod = this.replicationMethod,
                    OnlineReplicationStartTime = this.replicationStartTime,
                    RecoveryPoints = this.recoveryPoints,
                    ReplicaDeletion = this.replicaDeletion,
                    ReplicationPort = this.replicationPort
                };

            }
            else
            {
                updatePolicyProperties.ReplicationProviderSettings = new HyperVReplica2012R2PolicyInput()
                {
                    AllowedAuthenticationType = this.authentication,
                    ApplicationConsistentSnapshotFrequencyInHours = this.applicationConsistentSnapshotFrequencyInHours,
                    Compression = this.compression,
                    InitialReplicationMethod = this.replicationMethod,
                    OnlineReplicationStartTime = this.replicationStartTime,
                    RecoveryPoints = this.recoveryPoints,
                    ReplicaDeletion = this.replicaDeletion,                    
                    ReplicationPort = this.replicationPort,
                    ReplicationFrequencyInSeconds = replicationFrequencyInSeconds
                };
            }

            var updatePolicyInput = new UpdatePolicyInput()
            {
                Properties = updatePolicyProperties
            };

            LongRunningOperationResponse responseBlue =
                RecoveryServicesClient.UpdatePolicy(this.Policy.Name, updatePolicyInput);

            JobResponse jobResponseBlue =
                RecoveryServicesClient
                .GetAzureSiteRecoveryJobDetails(PSRecoveryServicesClient.GetJobIdFromReponseLocation(responseBlue.Location));

            WriteObject(new ASRJob(jobResponseBlue.Job));
        }