public async Task <BackupServerVersion> CheckServerVersionAndConnectivity(string dpmServer, string userName, string password)
        {
            Collection <PSObject> returnData = await this.RunScriptAsync(PowerShellCommandFactory.GetCheckDpmVersionAndConnectivityScript(dpmServer), dpmServer, userName, password);

            if (returnData != null && returnData.Count > 0)
            {
                BackupServerVersion version = new BackupServerVersion()
                {
                    Major    = (int)returnData[0].Members["Major"].Value,
                    Minor    = (int)returnData[0].Members["Minor"].Value,
                    Build    = (int)returnData[0].Members["Build"].Value,
                    Revision = (int)returnData[0].Members["Revision"].Value
                };
                return(version);
            }
            else
            {
                throw new IndexOutOfRangeException("No version returned!");
            }
        }
Example #2
0
        public async Task <BackupServer> CreateBackupServer(BackupServer server)
        {
            try
            {
                ServerGroup group = await this.configStorageClient.SelectServerGroupByIdAsync <ServerGroup>(server.GroupId, ServerGroupMapping.CreateServerGroup);

                if (group == null)
                {
                    string exceptionMessage = string.Format(CultureInfo.CurrentCulture, ErrorMessages.CreateBackupServerFailedGroupNotFound, server.ServerName);
                    VmBackupLog.Current.WriteErrorMessage("CreateBackupServer", VmBackupEventId.ServerGroupNotFound, null, server.GroupId);
                    throw Utility.CreateHttpResponseException(exceptionMessage, HttpStatusCode.BadRequest);
                }

                BackupServerVersion version = await this.dpmServerClient.CheckServerVersionAndConnectivity(server.ServerName, server.UserName, server.Password);

                if (version.Major < 4 || (version.Major == 4 && version.Minor < 2))
                {
                    string exceptionMessage = string.Format(CultureInfo.CurrentCulture, ErrorMessages.CreateBackupServerFailedBadVersionNumber, server.ServerName, version.VersionString);
                    VmBackupLog.Current.WriteErrorMessage("CreateBackupServer", VmBackupEventId.DpmVersionTooLow, null, server.ServerName, version.VersionString);
                    throw Utility.CreateHttpResponseException(exceptionMessage, HttpStatusCode.BadRequest);
                }

                if (group.AzureBackupEnabled)
                {
                    await this.dpmServerClient.CheckAzureSubscription(server.ServerName, server.UserName, server.Password);
                }

                byte[]       encryptionResult = encryptor.EncryptData(server.Password);
                BackupServer createdServer    = await this.configStorageClient.InsertBackupServerAsync <BackupServer>(Guid.NewGuid(), server.ServerName, server.UserName, encryptionResult, server.GroupId, BackupServerMapping.CreateBackupServerWithoutPassword);

                return(createdServer);
            }
            catch (PSRemotingTransportException e)
            {
                string exceptionMessage = string.Format(CultureInfo.CurrentCulture, ErrorMessages.CannotConnectToServer, server.ServerName);
                VmBackupLog.Current.WriteErrorMessage("CreateBackupServer", VmBackupEventId.CannnotConnectToServer, e, server.ServerName);
                throw Utility.CreateHttpResponseException(exceptionMessage, HttpStatusCode.BadRequest);
            }
            catch (RuntimeException e)
            {
                string    exceptionMessage;
                const int AzureSubscriptionNotFound  = 10001;
                const int AzureSubscriptionNotActive = 10002;

                int  messageNumber;
                bool parseSucceeded = int.TryParse(e.ErrorRecord.FullyQualifiedErrorId, out messageNumber);

                if (parseSucceeded && AzureSubscriptionNotFound == messageNumber)
                {
                    exceptionMessage = string.Format(CultureInfo.CurrentCulture, ErrorMessages.CreateBackupServerFailedAzureSubscriptionNotFound, server.ServerName);
                    VmBackupLog.Current.WriteErrorMessage("CreateBackupServer", VmBackupEventId.AzureSubscriptionNotFound, null, server.GroupId, server.ServerName);
                    throw Utility.CreateHttpResponseException(exceptionMessage, HttpStatusCode.BadRequest);
                }
                else if (parseSucceeded && AzureSubscriptionNotActive == messageNumber)
                {
                    exceptionMessage = string.Format(CultureInfo.CurrentCulture, ErrorMessages.CreateBackupServerFailedAzureSubscriptionNotReady, server.ServerName);
                    VmBackupLog.Current.WriteErrorMessage("CreateBackupServer", VmBackupEventId.AzureSubscriptionNotReady, null, server.GroupId, server.ServerName);
                    throw Utility.CreateHttpResponseException(exceptionMessage, HttpStatusCode.BadRequest);
                }
                else
                {
                    VmBackupLog.Current.WriteErrorMessage("CreateBackupServer", VmBackupEventId.UnexpectedAdminException, e);
                    throw;
                }
            }
            catch (SqlException e)
            {
                string    exceptionMessage;
                const int GroupNotFound         = 56001;
                const int ServerNameAlreadyUsed = 56002;

                int  messageNumber;
                bool parseSucceeded = int.TryParse(e.Message, out messageNumber);

                if (parseSucceeded && GroupNotFound == messageNumber)
                {
                    exceptionMessage = string.Format(CultureInfo.CurrentCulture, ErrorMessages.CreateBackupServerFailedGroupNotFound, server.ServerName);
                    VmBackupLog.Current.WriteErrorMessage("CreateBackupServer", VmBackupEventId.ServerGroupNotFound, null, server.GroupId);
                    throw Utility.CreateHttpResponseException(exceptionMessage, HttpStatusCode.BadRequest);
                }
                else if (parseSucceeded && ServerNameAlreadyUsed == messageNumber)
                {
                    exceptionMessage = string.Format(CultureInfo.CurrentCulture, ErrorMessages.CreateBackupServerFailedNameInUse, server.ServerName);
                    VmBackupLog.Current.WriteErrorMessage("CreateBackupServer", VmBackupEventId.ServerNameAlreadyInUse, null, server.ServerName);
                    throw Utility.CreateHttpResponseException(exceptionMessage, HttpStatusCode.Conflict);
                }
                else
                {
                    VmBackupLog.Current.WriteErrorMessage("CreateBackupServer", VmBackupEventId.UnexpectedAdminException, e);
                    throw;
                }
            }
            catch (Exception e)
            {
                VmBackupLog.Current.WriteErrorMessage("CreateBackupServer", VmBackupEventId.UnexpectedAdminException, e);
                throw;
            }
        }