Ejemplo n.º 1
0
        /// <summary>
        /// Queries by name.
        /// </summary>
        private void GetByName()
        {
            ProtectionEntityListResponse protectionEntityListResponse =
                RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity(
                    this.ProtectionContainerId);

            bool found = false;

            foreach (ProtectionEntity pe in protectionEntityListResponse.ProtectionEntities)
            {
                if (0 == string.Compare(this.Name, pe.Name, true))
                {
                    this.WriteProtectionEntity(pe);
                    found = true;
                }
            }

            if (!found)
            {
                throw new InvalidOperationException(
                          string.Format(
                              Properties.Resources.ProtectionEntityNotFound,
                              this.Name,
                              this.ProtectionContainerId));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Set PE protection.
        /// </summary>
        private void SetPEReprotect()
        {
            if (this.ProtectionEntity == null)
            {
                ProtectionEntityResponse protectionEntityResponse =
                    RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity(
                        this.ProtectionContainerId,
                        this.ProtectionEntityId);
                this.ProtectionEntity = new ASRProtectionEntity(protectionEntityResponse.ProtectionEntity);
            }

            // Until RR is done active location remains same from where FO was initiated.
            if ((this.Direction == PSRecoveryServicesClient.PrimaryToRecovery &&
                 this.ProtectionEntity.ActiveLocation != PSRecoveryServicesClient.RecoveryLocation) ||
                (this.Direction == PSRecoveryServicesClient.RecoveryToPrimary &&
                 this.ProtectionEntity.ActiveLocation != PSRecoveryServicesClient.PrimaryLocation))
            {
                throw new ArgumentException("Parameter value is not correct.", "Direction");
            }

            this.jobResponse = RecoveryServicesClient.StartAzureSiteRecoveryReprotection(
                this.ProtectionContainerId,
                this.ProtectionEntityId);

            this.WriteJob(this.jobResponse.Job);

            if (this.WaitForCompletion.IsPresent)
            {
                this.WaitForJobCompletion(this.jobResponse.Job.ID);
            }
        }
        /// <summary>
        /// Set PE protection.
        /// </summary>
        private void SetPEReprotect()
        {
            var request = new ReprotectRequest();

            if (this.ProtectionEntity == null)
            {
                var pe = RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity(
                    this.protectionContainerId,
                    this.protectionEntityId);
                this.ProtectionEntity = new ASRProtectionEntity(pe.ProtectionEntity);

                this.ValidateUsageById(
                    this.ProtectionEntity.ReplicationProvider,
                    this.protectionEntityId);
            }

            request.ReplicationProviderSettings = string.Empty;

            request.ReplicationProvider = this.ProtectionEntity.ReplicationProvider;
            request.FailoverDirection   = this.Direction;

            LongRunningOperationResponse response = RecoveryServicesClient.StartAzureSiteRecoveryReprotection(
                this.protectionContainerId,
                this.protectionEntityId,
                request);

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

            WriteObject(new ASRJob(jobResponse.Job));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Queries all.
        /// </summary>
        private void GetAll()
        {
            ProtectionEntityListResponse protectionEntityListResponse =
                RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity(
                    this.ProtectionContainerId);

            this.WriteProtectionEntities(protectionEntityListResponse.ProtectionEntities);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Queries by Id.
        /// </summary>
        private void GetById()
        {
            ProtectionEntityResponse protectionEntityResponse =
                RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity(
                    this.ProtectionContainerId,
                    this.Id);

            this.WriteProtectionEntity(protectionEntityResponse.ProtectionEntity);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Starts PE Unplanned failover.
        /// </summary>
        private void StartPEUnplannedFailover()
        {
            var request = new UnplannedFailoverRequest();

            request.FailoverDirection    = this.Direction;
            request.SourceSiteOperations = this.PerformSourceSideActions ? Constants.Required : Constants.NotRequired;
            LongRunningOperationResponse response =
                RecoveryServicesClient.StartAzureSiteRecoveryUnplannedFailover(
                    this.protectionContainerId,
                    this.protectionEntityId,
                    request);

            if (string.IsNullOrEmpty(this.ProtectionEntity.ReplicationProvider))
            {
                // fetch the latest PE object
                // As get PE by name is failing before protection, get all & filter.
                // Once after we fix get pe by name, change the logic to use the same.
                ProtectionEntityListResponse protectionEntityListResponse =
                    RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity(
                        this.ProtectionEntity.ProtectionContainerId);

                foreach (ProtectionEntity pe in protectionEntityListResponse.ProtectionEntities)
                {
                    if (0 == string.Compare(this.ProtectionEntity.FriendlyName, pe.Properties.FriendlyName, true))
                    {
                        this.ProtectionEntity = new ASRProtectionEntity(pe);
                        break;
                    }
                }
            }

            request.ReplicationProvider         = this.ProtectionEntity.ReplicationProvider;
            request.ReplicationProviderSettings = new FailoverReplicationProviderSpecificInput();

            if (0 == string.Compare(
                    this.ProtectionEntity.ReplicationProvider,
                    Constants.HyperVReplicaAzure,
                    StringComparison.OrdinalIgnoreCase))
            {
                if (this.Direction == Constants.PrimaryToRecovery)
                {
                    AzureFailoverInput failoverInput = new AzureFailoverInput()
                    {
                        PrimaryKekCertificatePfx   = string.Empty,
                        SecondaryKekCertificatePfx = string.Empty,
                        VaultLocation = this.GetCurrentValutLocation()
                    };
                    request.ReplicationProviderSettings = failoverInput;
                }
            }

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

            WriteObject(new ASRJob(jobResponse.Job));
        }
        /// <summary>
        /// Set PE protection.
        /// </summary>
        private void SetPEReprotect()
        {
            if ((this.Direction == Constants.PrimaryToRecovery &&
                 this.ProtectionEntity.ActiveLocation == Constants.RecoveryLocation) ||
                (this.Direction == Constants.RecoveryToPrimary &&
                 this.ProtectionEntity.ActiveLocation == Constants.PrimaryLocation))
            {
                throw new ArgumentException("Parameter value is not correct.", "Direction");
            }

            var request = new ReprotectRequest();

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

                this.ValidateUsageById(
                    this.ProtectionEntity.ReplicationProvider,
                    this.ProtectionEntityId);
            }

            request.ReplicationProviderSettings = string.Empty;

            if (this.ProtectionEntity.ReplicationProvider == Constants.HyperVReplicaAzure)
            {
                if (this.Direction == Constants.PrimaryToRecovery)
                {
                    var blob = new AzureReProtectionInput();
                    blob.HvHostVmId = this.ProtectionEntity.FabricObjectId;
                    blob.VmName     = this.ProtectionEntity.Name;

                    blob.OSType = this.ProtectionEntity.OS;
                    blob.VHDId  = this.ProtectionEntity.OSDiskId;

                    request.ReplicationProviderSettings = DataContractUtils.Serialize <AzureReProtectionInput>(blob);
                }
            }

            request.ReplicationProvider = this.ProtectionEntity.ReplicationProvider;
            request.FailoverDirection   = this.Direction;

            this.jobResponse = RecoveryServicesClient.StartAzureSiteRecoveryReprotection(
                this.ProtectionContainerId,
                this.ProtectionEntityId,
                request);

            this.WriteJob(this.jobResponse.Job);

            if (this.WaitForCompletion.IsPresent)
            {
                this.WaitForJobCompletion(this.jobResponse.Job.ID);
            }
        }
        /// <summary>
        /// Starts PE Planned failover.
        /// </summary>
        private void StartPEPlannedFailover()
        {
            var request = new PlannedFailoverRequest();

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

                this.ValidateUsageById(this.ProtectionEntity.ReplicationProvider, Constants.ProtectionEntityId);
            }

            if (this.ProtectionEntity.ReplicationProvider == Constants.HyperVReplicaAzure)
            {
                if (this.Direction == Constants.PrimaryToRecovery)
                {
                    var blob = new AzureFailoverInput();
                    blob.VaultLocation = this.GetCurrentValutLocation();
                    request.ReplicationProviderSettings = DataContractUtils.Serialize <AzureFailoverInput>(blob);
                }
                else
                {
                    var blob = new AzureFailbackInput();
                    blob.CreateRecoveryVmIfDoesntExist = false;
                    blob.SkipDataSync = this.Optimize == Constants.ForDowntime ? true : false;
                    request.ReplicationProviderSettings = DataContractUtils.Serialize <AzureFailbackInput>(blob);
                }
            }
            else
            {
                request.ReplicationProviderSettings = string.Empty;
            }

            request.ReplicationProvider = this.ProtectionEntity.ReplicationProvider;
            request.FailoverDirection   = this.Direction;

            this.jobResponse =
                RecoveryServicesClient.StartAzureSiteRecoveryPlannedFailover(
                    this.ProtectionContainerId,
                    this.ProtectionEntityId,
                    request);
            this.WriteJob(this.jobResponse.Job);

            if (this.WaitForCompletion.IsPresent)
            {
                this.WaitForJobCompletion(this.jobResponse.Job.ID);
            }
        }
        /// <summary>
        /// Starts PE Test failover.
        /// </summary>
        private void StartPETestFailover()
        {
            var request = new TestFailoverRequest();

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

                this.ValidateUsageById(
                    this.ProtectionEntity.ReplicationProvider,
                    Constants.ProtectionEntityId);
            }

            request.ReplicationProviderSettings = string.Empty;

            if (this.ProtectionEntity.ReplicationProvider == Constants.HyperVReplicaAzure)
            {
                if (this.Direction == Constants.PrimaryToRecovery)
                {
                    var blob = new AzureFailoverInput();
                    blob.VaultLocation = this.GetCurrentValutLocation();
                    request.ReplicationProviderSettings = DataContractUtils.Serialize <AzureFailoverInput>(blob);
                }
            }

            request.ReplicationProvider = this.ProtectionEntity.ReplicationProvider;
            request.FailoverDirection   = this.Direction;

            request.NetworkID   = this.networkId;
            request.NetworkType = this.networkType;

            this.jobResponse =
                RecoveryServicesClient.StartAzureSiteRecoveryTestFailover(
                    this.ProtectionContainerId,
                    this.ProtectionEntityId,
                    request);
            this.WriteJob(this.jobResponse.Job);

            if (this.WaitForCompletion.IsPresent)
            {
                this.WaitForJobCompletion(this.jobResponse.Job.ID);
            }
        }
        /// <summary>
        /// Start PE Commit.
        /// </summary>
        private void SetPECommit()
        {
            var request = new CommitFailoverRequest();

            if (this.ProtectionEntity == null)
            {
                var pe = RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity(
                    this.protectionContainerId,
                    this.protectionEntityId);
                this.ProtectionEntity = new ASRProtectionEntity(pe.ProtectionEntity);

                this.ValidateUsageById(
                    this.ProtectionEntity.ReplicationProvider,
                    Constants.ProtectionEntityId);
            }

            request.ReplicationProvider         = this.ProtectionEntity.ReplicationProvider;
            request.ReplicationProviderSettings = string.Empty;
            request.FailoverDirection           = this.Direction;

            //if (this.ProtectionEntity.ActiveLocation == Constants.PrimaryLocation)
            //{
            //    request.FailoverDirection = Constants.RecoveryToPrimary;
            //}
            //else
            //{
            //    request.FailoverDirection = Constants.PrimaryToRecovery;
            //}

            LongRunningOperationResponse response = RecoveryServicesClient.StartAzureSiteRecoveryCommitFailover(
                this.protectionContainerId,
                this.protectionEntityId,
                request);

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

            WriteObject(new ASRJob(jobResponse.Job));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Start PE Commit.
        /// </summary>
        private void SetPECommit()
        {
            var request = new CommitFailoverRequest();

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

                this.ValidateUsageById(
                    this.ProtectionEntity.ReplicationProvider,
                    Constants.ProtectionEntityId);
            }

            request.ReplicationProvider         = this.ProtectionEntity.ReplicationProvider;
            request.ReplicationProviderSettings = string.Empty;

            if (this.ProtectionEntity.ActiveLocation == Constants.PrimaryLocation)
            {
                request.FailoverDirection = Constants.RecoveryToPrimary;
            }
            else
            {
                request.FailoverDirection = Constants.PrimaryToRecovery;
            }

            this.jobResponse = RecoveryServicesClient.StartAzureSiteRecoveryCommitFailover(
                this.ProtectionContainerId,
                this.ProtectionEntityId,
                request);

            this.WriteJob(this.jobResponse.Job);

            if (this.WaitForCompletion.IsPresent)
            {
                this.WaitForJobCompletion(this.jobResponse.Job.ID);
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Start PE Commit.
        /// </summary>
        private void SetPECommit()
        {
            var request = new CommitFailoverRequest();

            request.FailoverDirection = this.Direction;

            if (string.IsNullOrEmpty(this.ProtectionEntity.ReplicationProvider))
            {
                // fetch the latest PE object
                // As get PE by name is failing before protection, get all & filter.
                // Once after we fix get pe by name, change the logic to use the same.
                ProtectionEntityListResponse protectionEntityListResponse =
                    RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity(
                        this.ProtectionEntity.ProtectionContainerId);

                foreach (ProtectionEntity pe in protectionEntityListResponse.ProtectionEntities)
                {
                    if (0 == string.Compare(this.ProtectionEntity.FriendlyName, pe.Properties.FriendlyName, true))
                    {
                        this.ProtectionEntity = new ASRProtectionEntity(pe);
                        break;
                    }
                }
            }

            request.ReplicationProvider         = this.ProtectionEntity.ReplicationProvider;
            request.ReplicationProviderSettings = new FailoverReplicationProviderSpecificInput();

            LongRunningOperationResponse response = RecoveryServicesClient.StartAzureSiteRecoveryCommitFailover(
                this.protectionContainerId,
                this.protectionEntityId,
                request);

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

            WriteObject(new ASRJob(jobResponse.Job));
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Queries by Name.
        /// </summary>
        private void GetByName()
        {
            // Commenting below code as Get PE by Name is faiing for somereason in service.
            // Taking alternate route

            //ProtectionEntityResponse protectionEntityResponse =
            //    RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity(
            //    this.ProtectionContainer.Name,
            //    this.Name);

            //this.WriteProtectionEntity(protectionEntityResponse.ProtectionEntity);

            ProtectionEntityListResponse protectionEntityListResponse =
                RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity(
                    this.ProtectionContainer.Name);

            bool found = false;

            foreach (ProtectionEntity pe in protectionEntityListResponse.ProtectionEntities)
            {
                if (0 == string.Compare(this.Name, pe.Name, true))
                {
                    this.WriteProtectionEntity(pe);
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                throw new InvalidOperationException(
                          string.Format(
                              Properties.Resources.ProtectionEntityNotFound,
                              this.Name,
                              this.ProtectionContainer.FriendlyName));
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Starts PE Test failover.
        /// </summary>
        private void StartPETestFailover()
        {
            var request = new TestFailoverRequest();

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

                /* this.ValidateUsageById(
                 *  this.ProtectionEntity.ReplicationProvider,
                 *  Constants.ProtectionEntityId); */
            }

            request.ReplicationProviderSettings = string.Empty;

            request.ReplicationProvider = this.ProtectionEntity.ReplicationProvider;
            request.FailoverDirection   = this.Direction;

            request.NetworkID   = this.networkId;
            request.NetworkType = this.networkType;

            LongRunningOperationResponse response =
                RecoveryServicesClient.StartAzureSiteRecoveryTestFailover(
                    this.ProtectionEntity.ProtectionContainerId,
                    this.ProtectionEntity.Name,
                    request);

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

            WriteObject(new ASRJob(jobResponse.Job));
        }
Ejemplo n.º 15
0
        /// <summary>
        /// ProcessRecord of the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            this.targetNameOrId = this.ProtectionEntity.FriendlyName;
            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)
                    {
                        EnableProtectionInput input = new EnableProtectionInput();

                        // Considering no PP input for E2E, if needed, change below logic
                        // for E2E, consider the profile created PS & a profile created in service
                        if (this.ProtectionProfile != null &&
                            0 == string.Compare(
                                this.ProtectionProfile.ReplicationProvider,
                                Constants.HyperVReplicaAzure,
                                StringComparison.OrdinalIgnoreCase))
                        {
                            input.ProtectionProfileId = this.ProtectionProfile.Name;
                            AzureEnableProtectionInput providerSettings = new AzureEnableProtectionInput();
                            providerSettings.HvHostVmId = this.ProtectionEntity.FabricObjectId;
                            providerSettings.VmName     = this.ProtectionEntity.FriendlyName;

                            // Id disk details are missing in input PE object, get the latest PE.
                            // As get PE by name is failing before protection, get all & filter.
                            // Once after we fix get pe by name, change the logic to use the same.
                            if (string.IsNullOrEmpty(this.ProtectionEntity.OS))
                            {
                                // Just checked for OS to see whether the disk details got filled up or not
                                ProtectionEntityListResponse protectionEntityListResponse =
                                    RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity(
                                        this.ProtectionEntity.ProtectionContainerId);

                                foreach (ProtectionEntity pe in protectionEntityListResponse.ProtectionEntities)
                                {
                                    if (0 == string.Compare(this.ProtectionEntity.FriendlyName, pe.Properties.FriendlyName, true))
                                    {
                                        this.ProtectionEntity = new ASRProtectionEntity(pe);
                                        break;
                                    }
                                }
                            }
                            providerSettings.OSType = this.ProtectionEntity.OS;

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

                            input.ProviderSettings = providerSettings;
                        }

                        this.response =
                            RecoveryServicesClient.EnableProtection(
                                this.ProtectionEntity.ProtectionContainerId,
                                this.ProtectionEntity.Name,
                                input);
                    }
                    else
                    {
                        DisableProtectionInput input = new DisableProtectionInput();

                        this.response =
                            RecoveryServicesClient.DisableProtection(
                                this.ProtectionEntity.ProtectionContainerId,
                                this.ProtectionEntity.Name,
                                input);
                    }

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

                    WriteObject(new ASRJob(jobResponse.Job));

                    if (this.WaitForCompletion.IsPresent)
                    {
                        this.WaitForJobCompletion(this.jobResponse.Job.Name);
                    }
                }
                catch (Exception exception)
                {
                    this.HandleException(exception);
                }
            });
        }
        /// <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);
                }
            });
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Set PE protection.
        /// </summary>
        private void SetPEReprotect()
        {
            var request = new ReprotectRequest();

            request.FailoverDirection = this.Direction;

            if (string.IsNullOrEmpty(this.ProtectionEntity.ReplicationProvider))
            {
                // fetch the latest PE object
                // As get PE by name is failing before protection, get all & filter.
                // Once after we fix get pe by name, change the logic to use the same.
                ProtectionEntityListResponse protectionEntityListResponse =
                    RecoveryServicesClient.GetAzureSiteRecoveryProtectionEntity(
                        this.ProtectionEntity.ProtectionContainerId);

                foreach (ProtectionEntity pe in protectionEntityListResponse.ProtectionEntities)
                {
                    if (0 == string.Compare(this.ProtectionEntity.FriendlyName, pe.Properties.FriendlyName, true))
                    {
                        this.ProtectionEntity = new ASRProtectionEntity(pe);
                        break;
                    }
                }
            }

            request.ReplicationProvider         = this.ProtectionEntity.ReplicationProvider;
            request.ReplicationProviderSettings = new FailoverReplicationProviderSpecificInput();

            if ((this.Direction == Constants.PrimaryToRecovery &&
                 this.ProtectionEntity.ActiveLocation == Constants.RecoveryLocation) ||
                (this.Direction == Constants.RecoveryToPrimary &&
                 this.ProtectionEntity.ActiveLocation == Constants.PrimaryLocation))
            {
                throw new ArgumentException("Parameter value is not correct.", "Direction");
            }

            if (0 == string.Compare(
                    this.ProtectionEntity.ReplicationProvider,
                    Constants.HyperVReplicaAzure,
                    StringComparison.OrdinalIgnoreCase))
            {
                if (this.Direction == Constants.PrimaryToRecovery)
                {
                    AzureReprotectInput reprotectInput = new AzureReprotectInput()
                    {
                        HvHostVmId = this.ProtectionEntity.FabricObjectId,
                        VmName     = this.ProtectionEntity.Name,
                        OSType     = this.ProtectionEntity.OS,
                        VHDId      = this.ProtectionEntity.OSDiskId
                    };

                    request.ReplicationProviderSettings = reprotectInput;
                }
            }

            LongRunningOperationResponse response = RecoveryServicesClient.StartAzureSiteRecoveryReprotection(
                this.protectionContainerId,
                this.protectionEntityId,
                request);

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

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