mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-17 09:35:37 -05:00
Backup service - add database metatdata request (#358)
* Add backup database metadata json rpc * Backup database metadata changes after sync * Changed request class name to BackupConfigInfo and made other changes according to comments * Addressed PR comments
This commit is contained in:
@@ -2,19 +2,15 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Data.SqlClient;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Admin;
|
||||
using Microsoft.SqlTools.ServiceLayer.Admin.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.DisasterRecovery.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Hosting;
|
||||
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlServer.Management.Smo;
|
||||
using Microsoft.SqlServer.Management.Common;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using Microsoft.SqlTools.ServiceLayer.Common;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
|
||||
{
|
||||
@@ -22,14 +18,14 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
|
||||
{
|
||||
private static readonly Lazy<DisasterRecoveryService> instance = new Lazy<DisasterRecoveryService>(() => new DisasterRecoveryService());
|
||||
private static ConnectionService connectionService = null;
|
||||
private BackupFactory backupFactory;
|
||||
private BackupUtilities backupFactory;
|
||||
|
||||
/// <summary>
|
||||
/// Default, parameterless constructor.
|
||||
/// </summary>
|
||||
internal DisasterRecoveryService()
|
||||
{
|
||||
this.backupFactory = new BackupFactory();
|
||||
this.backupFactory = new BackupUtilities();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -64,27 +60,47 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
|
||||
/// </summary>
|
||||
public void InitializeService(ServiceHost serviceHost)
|
||||
{
|
||||
// Get database info
|
||||
serviceHost.SetRequestHandler(BackupConfigInfoRequest.Type, HandleBackupConfigInfoRequest);
|
||||
// Create backup
|
||||
serviceHost.SetRequestHandler(BackupRequest.Type, HandleBackupRequest);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles a backup request
|
||||
/// </summary>
|
||||
internal static async Task HandleBackupRequest(
|
||||
BackupParams backupParams,
|
||||
RequestContext<BackupResponse> requestContext)
|
||||
{
|
||||
public static async Task HandleBackupConfigInfoRequest(
|
||||
DefaultDatabaseInfoParams optionsParams,
|
||||
RequestContext<BackupConfigInfoResponse> requestContext)
|
||||
{
|
||||
var response = new BackupConfigInfoResponse();
|
||||
ConnectionInfo connInfo;
|
||||
DisasterRecoveryService.ConnectionServiceInstance.TryFindConnection(
|
||||
backupParams.OwnerUri,
|
||||
optionsParams.OwnerUri,
|
||||
out connInfo);
|
||||
CDataContainer dataContainer;
|
||||
|
||||
if (connInfo != null)
|
||||
{
|
||||
CDataContainer dataContainer = GetDataContainer(connInfo);
|
||||
SqlConnection sqlConn = GetSqlConnection(connInfo);
|
||||
if (sqlConn != null)
|
||||
{
|
||||
DisasterRecoveryService.Instance.InitializeBackup(dataContainer, sqlConn);
|
||||
BackupConfigInfo backupConfigInfo = DisasterRecoveryService.Instance.GetDatabaseInfo(sqlConn.Database);
|
||||
backupConfigInfo.DatabaseInfo = AdminService.GetDatabaseInfo(connInfo);
|
||||
response.BackupConfigInfo = backupConfigInfo;
|
||||
}
|
||||
}
|
||||
|
||||
await requestContext.SendResult(response);
|
||||
}
|
||||
|
||||
internal static CDataContainer GetDataContainer(ConnectionInfo connInfo)
|
||||
{
|
||||
CDataContainer dataContainer = null;
|
||||
|
||||
if (connInfo != null)
|
||||
{
|
||||
char[] passwordArray = connInfo.ConnectionDetails.Password.ToCharArray();
|
||||
if (string.Equals(connInfo.ConnectionDetails.AuthenticationType, "SqlLogin", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
fixed (char* passwordPtr = passwordArray)
|
||||
@@ -109,11 +125,32 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
|
||||
null,
|
||||
null);
|
||||
}
|
||||
}
|
||||
|
||||
return dataContainer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles a backup request
|
||||
/// </summary>
|
||||
internal static async Task HandleBackupRequest(
|
||||
BackupParams backupParams,
|
||||
RequestContext<BackupResponse> requestContext)
|
||||
{
|
||||
ConnectionInfo connInfo;
|
||||
DisasterRecoveryService.ConnectionServiceInstance.TryFindConnection(
|
||||
backupParams.OwnerUri,
|
||||
out connInfo);
|
||||
CDataContainer dataContainer;
|
||||
|
||||
if (connInfo != null)
|
||||
{
|
||||
dataContainer = GetDataContainer(connInfo);
|
||||
SqlConnection sqlConn = GetSqlConnection(connInfo);
|
||||
if (sqlConn != null)
|
||||
{
|
||||
DisasterRecoveryService.Instance.InitializeBackup(dataContainer, sqlConn, backupParams.BackupInfo);
|
||||
DisasterRecoveryService.Instance.InitializeBackup(dataContainer, sqlConn);
|
||||
DisasterRecoveryService.Instance.SetBackupInput(backupParams.BackupInfo);
|
||||
DisasterRecoveryService.Instance.PerformBackup();
|
||||
}
|
||||
}
|
||||
@@ -147,15 +184,25 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
|
||||
return null;
|
||||
}
|
||||
|
||||
private void InitializeBackup(CDataContainer dataContainer, SqlConnection sqlConnection, BackupInfo input)
|
||||
private void InitializeBackup(CDataContainer dataContainer, SqlConnection sqlConnection)
|
||||
{
|
||||
this.backupFactory.Initialize(dataContainer, sqlConnection);
|
||||
}
|
||||
|
||||
private void SetBackupInput(BackupInfo input)
|
||||
{
|
||||
this.backupFactory.Initialize(dataContainer, sqlConnection, input);
|
||||
this.backupFactory.SetBackupInput(input);
|
||||
}
|
||||
|
||||
private void PerformBackup()
|
||||
{
|
||||
this.backupFactory.PerformBackup();
|
||||
}
|
||||
}
|
||||
|
||||
private BackupConfigInfo GetDatabaseInfo(string databaseName)
|
||||
{
|
||||
return this.backupFactory.GetBackupConfigInfo(databaseName);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user