/// <summary>
 /// Creates and Associates Azure Site Recovery Protection Profile.
 /// </summary>
 /// <param name="createAndAssociateProtectionProfileInput">Protection Profile Input</param>
 /// <returns>Job response</returns>
 public JobResponse StartCreateAndAssociateAzureSiteRecoveryProtectionProfileJob(
     CreateAndAssociateProtectionProfileInput createAndAssociateProtectionProfileInput)
 {
     return this.GetSiteRecoveryClient().ProtectionProfile.CreateAndAssociate(
         createAndAssociateProtectionProfileInput,
         this.GetRequestHeaders());
 }
 /// <summary>
 /// Enable Protection for the given protection entity.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.SiteRecovery.IProtectionProfileOperations.
 /// </param>
 /// <param name='input'>
 /// Required. input.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// The response model for the Job details object.
 /// </returns>
 public static JobResponse CreateAndAssociate(this IProtectionProfileOperations operations, CreateAndAssociateProtectionProfileInput input, CustomRequestHeaders customRequestHeaders)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IProtectionProfileOperations)s).CreateAndAssociateAsync(input, customRequestHeaders);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        public void CreateAndAssociateE2A(string provider)
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                var client = GetSiteRecoveryClient(CustomHttpHandler);
                var requestHeaders = RequestHeaders;
                requestHeaders.AgentAuthenticationHeader = GenerateAgentAuthenticationHeader(requestHeaders.ClientRequestId);

                JobQueryParameter jqp = new JobQueryParameter();
                var responseRP = client.ProtectionProfile.List(RequestHeaders);

                string serializedHyperVReplicaAzureProfileManagementInput = null;
                foreach (var profile in responseRP.ProtectionProfiles)
                {
                    if (profile.AssociationDetail[0].AssociationStatus == "Paired")
                    {
                        // Instead of creating new set of values. Picking the values from already paired cloud.
                        var obj =
                        DataContractUtils<HyperVReplicaAzureProtectionProfileDetails>.Deserialize(
                        profile.ReplicationProviderSetting);

                        var settings = new HyperVReplicaAzureProtectionProfileInput();
                        settings.AppConsistencyFreq = obj.AppConsistencyFreq;
                        settings.IsEncryptionEnabled = obj.IsEncryptionEnabled;
                        settings.OnlineIrStartTime = obj.OnlineIrStartTime;
                        settings.RecoveryPointHistoryDuration = obj.RecoveryPointHistoryDuration;
                        settings.ReplicationInterval = obj.ReplicationInterval;
                        settings.StorageAccounts = new List<CustomerStorageAccount>();
                        var storageAccount = new CustomerStorageAccount();
                        storageAccount.StorageAccountName = obj.ActiveStorageAccount.StorageAccountName;
                        storageAccount.SubscriptionId = obj.ActiveStorageAccount.SubscriptionId;
                        settings.StorageAccounts.Add(storageAccount);

                        serializedHyperVReplicaAzureProfileManagementInput =
                            DataContractUtils<HyperVReplicaAzureProtectionProfileInput>.Serialize(settings);
                    }
                }

                var responsePC = client.ProtectionContainer.List(RequestHeaders);

                foreach (var pc in responsePC.ProtectionContainers)
                {
                    if (string.IsNullOrWhiteSpace(pc.Role))
                    {
                        var input = new CreateAndAssociateProtectionProfileInput();

                        input.ProtectionProfileInput = new CreateProtectionProfileInput();
                        input.ProtectionProfileInput.Name = "PP1";
                        input.ProtectionProfileInput.ReplicationProvider = "HyperVReplicaAzure";
                        input.ProtectionProfileInput.ReplicationProviderSettings =
                            serializedHyperVReplicaAzureProfileManagementInput;

                        input.AssociationInput = new ProtectionProfileAssociationInput();
                        input.AssociationInput.PrimaryProtectionContainerId = pc.ID;
                        input.AssociationInput.RecoveryProtectionContainerId = AzureProtectionContainerId;

                        client.ProtectionProfile.CreateAndAssociate(input, requestHeaders);
                    }
                }

                //Assert.NotNull(response.);
                //Assert.NotNull(response.RecoveryPlan.ID);
                //Assert.NotNull(response.RecoveryPlan.Name);
                //Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            }
        }
        public void CreateAndAssociateE2E()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                var client = GetSiteRecoveryClient(CustomHttpHandler);
                var requestHeaders = RequestHeaders;
                requestHeaders.AgentAuthenticationHeader = GenerateAgentAuthenticationHeader(requestHeaders.ClientRequestId);

                string serializedHyperVReplicaAzureProfileManagementInput = null;
               
                var settings = new HyperVReplicaProtectionProfileInput();
                settings.AllowedAuthenticationType = 1;
                settings.AllowReplicaDeletion = false;
                settings.ApplicationConsistentSnapshotFrequencyInHours = 0;
                settings.CompressionEnabled = true;
                settings.OfflineReplicationExportPath = null;
                settings.OfflineReplicationImportPath = null;
                settings.OnlineReplicationMethod = true;
                settings.OnlineReplicationStartTime = null;
                settings.RecoveryPoints = 1;
                settings.ReplicationFrequencyInSeconds = 300;
                settings.ReplicationPort = 8083;

                serializedHyperVReplicaAzureProfileManagementInput =
                    DataContractUtils<HyperVReplicaProtectionProfileInput>.Serialize(settings);

                var responsePC = client.ProtectionContainer.List(RequestHeaders);

                string primaryPCId = null;
                string recoveryPCId = null;
                foreach (var pc in responsePC.ProtectionContainers)
                {
                    if (string.IsNullOrWhiteSpace(pc.Role))
                    {
                        if (primaryPCId == null)
                        {
                            primaryPCId = pc.ID;
                            continue;
                        }

                        if (recoveryPCId == null)
                        {
                            recoveryPCId = pc.ID;
                            break;
                        }
                    }
                }

                var input = new CreateAndAssociateProtectionProfileInput();

                input.ProtectionProfileInput = new CreateProtectionProfileInput();
                input.ProtectionProfileInput.Name = "E2E_Profile";
                input.ProtectionProfileInput.ReplicationProvider = "HyperVReplica";
                input.ProtectionProfileInput.ReplicationProviderSettings =
                    serializedHyperVReplicaAzureProfileManagementInput;

                input.AssociationInput = new ProtectionProfileAssociationInput();
                input.AssociationInput.PrimaryProtectionContainerId = primaryPCId;
                input.AssociationInput.RecoveryProtectionContainerId = recoveryPCId;

                JobResponse response = client.ProtectionProfile.CreateAndAssociate(input, requestHeaders);

                Assert.NotNull(response.Job);
                Assert.NotNull(response.Job.ID);
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            }
        }
        public void DissociateAndDelete()
        {
            using (UndoContext context = UndoContext.Current)
            {
                JobResponse response = null;
                context.Start();
                var client = GetSiteRecoveryClient(CustomHttpHandler);
                var requestHeaders = RequestHeaders;
                requestHeaders.AgentAuthenticationHeader = GenerateAgentAuthenticationHeader(requestHeaders.ClientRequestId);

                JobQueryParameter jqp = new JobQueryParameter();
                var responseRP = client.ProtectionProfile.List(RequestHeaders);

                foreach (var profile in responseRP.ProtectionProfiles)
                {
                    foreach (var associationDetail in profile.AssociationDetail)
                    {
                        if (associationDetail.AssociationStatus == "Paired")
                        {
                            var input = new CreateAndAssociateProtectionProfileInput();
                            input.AssociationInput.PrimaryProtectionContainerId = associationDetail.PrimaryProtectionContainerId;
                            input.AssociationInput.RecoveryProtectionContainerId = associationDetail.RecoveryProtectionContainerId;

                            response = client.ProtectionProfile.DissociateAndDelete(
                                profile.ID,
                                input,
                                requestHeaders);
                            break;
                        }
                    }
                }

                Assert.NotNull(response);
                Assert.NotNull(response.Job.ID);
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            }
        }
        /// <summary>
        /// Associates protection profile with one enterprise based and an Azure protection container
        /// </summary>
        private void EnterpriseToAzureAssociation()
        {
            if (string.Compare(
                this.ProtectionProfile.ReplicationProvider,
                Constants.HyperVReplicaAzure,
                StringComparison.OrdinalIgnoreCase) != 0)
            {
                throw new InvalidOperationException(
                    string.Format(
                    Properties.Resources.IncorrectReplicationProvider,
                    this.ProtectionProfile.ReplicationProvider));
            }

            HyperVReplicaAzureProtectionProfileInput hyperVReplicaAzureProtectionProfileInput
                = new HyperVReplicaAzureProtectionProfileInput()
                {
                    ApplicationConsistentSnapshotFrequencyInHours = this.ProtectionProfile.HyperVReplicaAzureProviderSettingsObject.ApplicationConsistentSnapshotFrequencyInHours,
                    ReplicationInterval = this.ProtectionProfile.HyperVReplicaAzureProviderSettingsObject.ReplicationFrequencyInSeconds,
                    OnlineReplicationStartTime = this.ProtectionProfile.HyperVReplicaAzureProviderSettingsObject.ReplicationStartTime,
                    RecoveryPointHistoryDuration = this.ProtectionProfile.HyperVReplicaAzureProviderSettingsObject.RecoveryPoints,
                    EncryptionEnabled = this.ProtectionProfile.HyperVReplicaAzureProviderSettingsObject.EncryptStoredData
                };

            var storageAccount = new CustomerStorageAccount();
            storageAccount.StorageAccountName = this.ProtectionProfile.HyperVReplicaAzureProviderSettingsObject.RecoveryAzureStorageAccountName;
            storageAccount.SubscriptionId = this.ProtectionProfile.HyperVReplicaAzureProviderSettingsObject.RecoveryAzureSubscription;

            hyperVReplicaAzureProtectionProfileInput.StorageAccounts = new System.Collections.Generic.List<CustomerStorageAccount>();
            hyperVReplicaAzureProtectionProfileInput.StorageAccounts.Add(storageAccount);

            CreateProtectionProfileInput createProtectionProfileInput =
                new CreateProtectionProfileInput(
                    string.IsNullOrEmpty(this.ProtectionProfile.Name) ? this.PrimaryProtectionContainer.Name : this.ProtectionProfile.Name,
                    this.ProtectionProfile.ReplicationProvider,
                    DataContractUtils<HyperVReplicaAzureProtectionProfileInput>.Serialize(hyperVReplicaAzureProtectionProfileInput));

            ProtectionProfileAssociationInput protectionProfileAssociationInput =
                new ProtectionProfileAssociationInput(
                    this.PrimaryProtectionContainer.ID,
                    Constants.AzureContainer);

            CreateAndAssociateProtectionProfileInput createAndAssociateProtectionProfileInput =
                new CreateAndAssociateProtectionProfileInput(
                    createProtectionProfileInput,
                    protectionProfileAssociationInput);

            this.jobResponse = RecoveryServicesClient.StartCreateAndAssociateAzureSiteRecoveryProtectionProfileJob(
                createAndAssociateProtectionProfileInput);

            this.WriteJob(this.jobResponse.Job);
        }
        /// <summary>
        /// Associates protection profile with enterprise based protection containers
        /// </summary>
        private void EnterpriseToEnterpriseAssociation()
        {
            if (string.Compare(
                this.ProtectionProfile.ReplicationProvider,
                Constants.HyperVReplica,
                StringComparison.OrdinalIgnoreCase) != 0)
            {
                throw new InvalidOperationException(
                    string.Format(
                    Properties.Resources.IncorrectReplicationProvider,
                    this.ProtectionProfile.ReplicationProvider));
            }

            HyperVReplicaProtectionProfileInput hyperVReplicaProtectionProfileInput
                    = new HyperVReplicaProtectionProfileInput()
                    {
                        ApplicationConsistentSnapshotFrequencyInHours = this.ProtectionProfile.HyperVReplicaProviderSettingsObject.ApplicationConsistentSnapshotFrequencyInHours,
                        ReplicationFrequencyInSeconds = this.ProtectionProfile.HyperVReplicaProviderSettingsObject.ReplicationFrequencyInSeconds,
                        OnlineReplicationStartTime = this.ProtectionProfile.HyperVReplicaProviderSettingsObject.ReplicationStartTime,
                        CompressionEnabled = this.ProtectionProfile.HyperVReplicaProviderSettingsObject.CompressionEnabled,
                        OnlineReplicationMethod = (string.Compare(this.ProtectionProfile.HyperVReplicaProviderSettingsObject.ReplicationMethod, Constants.OnlineReplicationMethod, StringComparison.OrdinalIgnoreCase) == 0) ? true : false,
                        RecoveryPoints = this.ProtectionProfile.HyperVReplicaProviderSettingsObject.RecoveryPoints,
                        ReplicationPort = this.ProtectionProfile.HyperVReplicaProviderSettingsObject.ReplicationPort,
                        AllowReplicaDeletion = this.ProtectionProfile.HyperVReplicaProviderSettingsObject.AllowReplicaDeletion,
                        AllowedAuthenticationType = (ushort)((string.Compare(this.ProtectionProfile.HyperVReplicaProviderSettingsObject.Authentication, Constants.AuthenticationTypeKerberos, StringComparison.OrdinalIgnoreCase) == 0) ? 1 : 2),
                    };

            CreateProtectionProfileInput createProtectionProfileInput =
                new CreateProtectionProfileInput(
                    //// Name of the protection profile as the name of the protection container if not given
                    string.IsNullOrEmpty(this.ProtectionProfile.Name) ? this.PrimaryProtectionContainer.Name : this.ProtectionProfile.Name,
                    this.ProtectionProfile.ReplicationProvider,
                    DataContractUtils<HyperVReplicaProtectionProfileInput>.Serialize(hyperVReplicaProtectionProfileInput));

            ProtectionProfileAssociationInput protectionProfileAssociationInput =
                new ProtectionProfileAssociationInput(
                    this.PrimaryProtectionContainer.ID,
                    this.RecoveryProtectionContainer.ID);

            CreateAndAssociateProtectionProfileInput createAndAssociateProtectionProfileInput =
                new CreateAndAssociateProtectionProfileInput(
                    createProtectionProfileInput,
                    protectionProfileAssociationInput);

            this.jobResponse = RecoveryServicesClient.StartCreateAndAssociateAzureSiteRecoveryProtectionProfileJob(
                createAndAssociateProtectionProfileInput);

            this.WriteJob(this.jobResponse.Job);
        }
 /// <summary>
 /// Enable Protection for the given protection entity.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.SiteRecovery.IProtectionProfileOperations.
 /// </param>
 /// <param name='input'>
 /// Required. input.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Optional. Request header parameters.
 /// </param>
 /// <returns>
 /// The response model for the Job details object.
 /// </returns>
 public static Task<JobResponse> CreateAndAssociateAsync(this IProtectionProfileOperations operations, CreateAndAssociateProtectionProfileInput input, CustomRequestHeaders customRequestHeaders)
 {
     return operations.CreateAndAssociateAsync(input, customRequestHeaders, CancellationToken.None);
 }