Ejemplo n.º 1
0
        /// <summary>
        /// Validating vault upgrade prerequisites.
        /// </summary>
        /// <returns>CheckPrerequisites response.</returns>
        private CheckVaultUpgradePrerequisitesResponse ValidateVaultUpgradePrerequisites()
        {
            CheckVaultUpgradePrerequisitesResponse response =
                CheckVaultUpgradePrerequisitesResponse.Succeeded;

            try
            {
                this.RecoveryServicesClient.TestVaultUpgradePrerequistes(
                    this.VaultName,
                    this.Location,
                    this.ResourceType,
                    this.TargetResourceGroupName,
                    this.Profile.Context.Subscription.Id.ToString());
            }
            catch (Exception exception)
            {
                ExceptionDetails details =
                    this.HandleVaultUpgradeException(exception);

                if (details != null)
                {
                    if (!string.IsNullOrEmpty(details.WarningDetails))
                    {
                        this.WriteWarning(details.WarningDetails + Environment.NewLine);
                        response = CheckVaultUpgradePrerequisitesResponse.SucceededWithWarnings;
                    }

                    if (!string.IsNullOrEmpty(details.ErrorDetails))
                    {
                        response = CheckVaultUpgradePrerequisitesResponse.Failed;
                        Exception ex = new InvalidOperationException(
                            string.Format(
                                Properties.Resources.ConfirmVaultUpgradePrereqFailed,
                                Properties.Resources.VaultUpgradeExceptionDetails,
                                details.ErrorDetails));

                        this.WriteVaultUpgradeError(ex);
                    }
                }
                else
                {
                    response = CheckVaultUpgradePrerequisitesResponse.Failed;
                }
            }

            return(response);
        }
        /// <summary>
        /// ProcessRecord of the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            try
            {
                AzureOperationResponse response =
                    this.RecoveryServicesClient.TestVaultUpgradePrerequistes(
                        this.VaultName,
                        this.Location,
                        this.ResourceType,
                        this.TargetResourceGroupName,
                        this.Profile.Context.Subscription.Id.ToString());

                ASRTestVaultUpgradeResponse output = new ASRTestVaultUpgradeResponse()
                {
                    Response = response.StatusCode == HttpStatusCode.OK ?
                               Properties.Resources.CheckPrereqSucceeded :
                               response.StatusCode.ToString()
                };

                this.WriteObject(output, true);
            }
            catch (Exception exception)
            {
                ExceptionDetails details =
                    this.HandleVaultUpgradeException(exception);

                if (details != null)
                {
                    if (!string.IsNullOrEmpty(details.WarningDetails))
                    {
                        this.WriteWarning(details.WarningDetails + Environment.NewLine);
                    }

                    if (!string.IsNullOrEmpty(details.ErrorDetails))
                    {
                        Exception ex = new InvalidOperationException(
                            string.Format(
                                Properties.Resources.ConfirmVaultUpgradePrereqFailed,
                                Properties.Resources.VaultUpgradeExceptionDetails,
                                details.ErrorDetails));
                        this.WriteVaultUpgradeError(ex);
                    }
                }
            }
        }
        /// <summary>
        /// Vault upgrade exception handler.
        /// </summary>
        /// <param name="ex">Exception to handle.</param>
        /// <returns>Object to be returned after categorizing all the errors properly
        /// received as part of vault upgrade operations.</returns>
        public ExceptionDetails HandleVaultUpgradeException(Exception ex)
        {
            ExceptionDetails exceptionDetails = null;
            string clientRequestIdMsg = string.Empty;
            if (this.recoveryServicesClient != null)
            {
                clientRequestIdMsg = string.Format(
                    Properties.Resources.ClientRequestIdMessage,
                    this.recoveryServicesClient.ClientRequestId,
                    Environment.NewLine);
            }

            CloudException cloudException = ex as CloudException;
            if (cloudException != null)
            {
                DataContractSerializer deserializer = null;

                try
                {
                    using (Stream stream = new MemoryStream())
                    {
                        if (cloudException.Error != null && cloudException.Error.OriginalMessage != null)
                        {
                            string message = cloudException.Error.OriginalMessage;
                            byte[] data = System.Text.Encoding.UTF8.GetBytes(message);
                            stream.Write(data, 0, data.Length);
                            stream.Position = 0;

                            if (message.Contains("http://schemas.microsoft.com/wars"))
                            {
                                deserializer = new DataContractSerializer(typeof(StartVaultUpgradeError));
                                StartVaultUpgradeError error = (StartVaultUpgradeError)deserializer.ReadObject(stream);
                                string msg = string.Format(
                                    Properties.Resources.StartVaultUpgradeExceptionDetails,
                                    clientRequestIdMsg,
                                    error.Message.Value);
                                this.WriteVaultUpgradeError(new InvalidOperationException(msg));
                            }
                            else
                            {
                                deserializer = new DataContractSerializer(typeof(VaultUpgradeError));
                                VaultUpgradeError error = (VaultUpgradeError)deserializer.ReadObject(stream);
                                if (error.Details != null)
                                {
                                    return this.recoveryServicesClient.ParseErrorDetails(error.Details, clientRequestIdMsg);
                                }
                                else
                                {
                                    StringBuilder exceptionMessage = new StringBuilder();
                                    exceptionMessage.AppendLine(Properties.Resources.VaultUpgradeExceptionDetails).AppendLine(" ");
                                    exceptionMessage.Append(this.recoveryServicesClient.ParseError(error));
                                    exceptionMessage.AppendLine(clientRequestIdMsg);
                                    this.WriteVaultUpgradeError(
                                        new InvalidOperationException(exceptionMessage.ToString()));
                                }
                            }
                        }
                        else
                        {
                            throw new Exception(
                                string.Format(
                                Properties.Resources.InvalidCloudExceptionErrorMessage,
                                clientRequestIdMsg + ex.Message),
                                ex);
                        }
                    }
                }
                catch (XmlException)
                {
                    throw new XmlException(
                        string.Format(
                        Properties.Resources.InvalidCloudExceptionErrorMessage,
                        cloudException.Message),
                        cloudException);
                }
                catch (SerializationException)
                {
                    throw new SerializationException(
                        string.Format(
                        Properties.Resources.InvalidCloudExceptionErrorMessage,
                        clientRequestIdMsg + cloudException.Message),
                        cloudException);
                }
            }
            else if (ex.Message != null)
            {
                throw new Exception(
                    string.Format(
                    Properties.Resources.InvalidCloudExceptionErrorMessage,
                    clientRequestIdMsg + ex.Message),
                    ex);
            }

            return exceptionDetails;
        }