mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-13 17:23:02 -05:00
Get server general properties (#2117)
* add server handler * get server general properties
This commit is contained in:
@@ -40,6 +40,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
|
|||||||
this.objectTypeHandlers.Add(new DatabaseRoleHandler(ConnectionService.Instance));
|
this.objectTypeHandlers.Add(new DatabaseRoleHandler(ConnectionService.Instance));
|
||||||
this.objectTypeHandlers.Add(new ServerRoleHandler(ConnectionService.Instance));
|
this.objectTypeHandlers.Add(new ServerRoleHandler(ConnectionService.Instance));
|
||||||
this.objectTypeHandlers.Add(new DatabaseHandler(ConnectionService.Instance));
|
this.objectTypeHandlers.Add(new DatabaseHandler(ConnectionService.Instance));
|
||||||
|
this.objectTypeHandlers.Add(new ServerHandler(ConnectionService.Instance));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -135,12 +135,12 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
|
|||||||
DateCreated = smoDatabase.CreateDate.ToString(),
|
DateCreated = smoDatabase.CreateDate.ToString(),
|
||||||
LastDatabaseBackup = smoDatabase.LastBackupDate == DateTime.MinValue ? SR.databaseBackupDate_None : smoDatabase.LastBackupDate.ToString(),
|
LastDatabaseBackup = smoDatabase.LastBackupDate == DateTime.MinValue ? SR.databaseBackupDate_None : smoDatabase.LastBackupDate.ToString(),
|
||||||
LastDatabaseLogBackup = smoDatabase.LastLogBackupDate == DateTime.MinValue ? SR.databaseBackupDate_None : smoDatabase.LastLogBackupDate.ToString(),
|
LastDatabaseLogBackup = smoDatabase.LastLogBackupDate == DateTime.MinValue ? SR.databaseBackupDate_None : smoDatabase.LastLogBackupDate.ToString(),
|
||||||
MemoryAllocatedToMemoryOptimizedObjectsInMb = DatabaseUtils.ConvertKbtoMb(smoDatabase.MemoryAllocatedToMemoryOptimizedObjectsInKB),
|
MemoryAllocatedToMemoryOptimizedObjectsInMb = ByteConverter.ConvertKbtoMb(smoDatabase.MemoryAllocatedToMemoryOptimizedObjectsInKB),
|
||||||
MemoryUsedByMemoryOptimizedObjectsInMb = DatabaseUtils.ConvertKbtoMb(smoDatabase.MemoryUsedByMemoryOptimizedObjectsInKB),
|
MemoryUsedByMemoryOptimizedObjectsInMb = ByteConverter.ConvertKbtoMb(smoDatabase.MemoryUsedByMemoryOptimizedObjectsInKB),
|
||||||
NumberOfUsers = smoDatabase.Users.Count,
|
NumberOfUsers = smoDatabase.Users.Count,
|
||||||
Owner = smoDatabase.Owner,
|
Owner = smoDatabase.Owner,
|
||||||
SizeInMb = smoDatabase.Size,
|
SizeInMb = smoDatabase.Size,
|
||||||
SpaceAvailableInMb = DatabaseUtils.ConvertKbtoMb(smoDatabase.SpaceAvailable),
|
SpaceAvailableInMb = ByteConverter.ConvertKbtoMb(smoDatabase.SpaceAvailable),
|
||||||
Status = smoDatabase.Status.ToString()
|
Status = smoDatabase.Status.ToString()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,95 @@
|
|||||||
|
//
|
||||||
|
// 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.Threading.Tasks;
|
||||||
|
using Microsoft.SqlServer.Management.Common;
|
||||||
|
using Microsoft.SqlServer.Management.Smo;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.ObjectManagement.Contracts;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.ObjectManagement.ObjectTypes.Server;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.ServerConfigurations;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Server object type handler
|
||||||
|
/// </summary>
|
||||||
|
public class ServerHandler : ObjectTypeHandler<ServerInfo, ServerViewContext>
|
||||||
|
{
|
||||||
|
private ServerViewInfo serverViewInfo = new ServerViewInfo();
|
||||||
|
private ServerConfigService configService = new ServerConfigService();
|
||||||
|
private Server server = null;
|
||||||
|
|
||||||
|
public ServerHandler(ConnectionService connectionService) : base(connectionService)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool CanHandleType(SqlObjectType objectType)
|
||||||
|
{
|
||||||
|
return objectType == SqlObjectType.Server;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Task<InitializeViewResult> InitializeObjectView(InitializeViewRequestParams requestParams)
|
||||||
|
{
|
||||||
|
ConnectionInfo connInfo = this.GetConnectionInfo(requestParams.ConnectionUri);
|
||||||
|
ServerConnection serverConnection = ConnectionService.OpenServerConnection(connInfo, ObjectManagementService.ApplicationName);
|
||||||
|
|
||||||
|
using (var context = new ServerViewContext(requestParams, serverConnection))
|
||||||
|
{
|
||||||
|
this.server = new Server(context.Connection);
|
||||||
|
if (this.server != null)
|
||||||
|
{
|
||||||
|
this.serverViewInfo.ObjectInfo = new ServerInfo()
|
||||||
|
{
|
||||||
|
Name = server.Name,
|
||||||
|
HardwareGeneration = server.HardwareGeneration,
|
||||||
|
Language = server.Language,
|
||||||
|
MemoryInMB = server.PhysicalMemory,
|
||||||
|
OperatingSystem = server.HostDistribution,
|
||||||
|
Platform = server.HostPlatform,
|
||||||
|
Processors = server.Processors,
|
||||||
|
IsClustered = server.IsClustered,
|
||||||
|
IsHadrEnabled = server.IsHadrEnabled,
|
||||||
|
IsPolyBaseInstalled = server.IsPolyBaseInstalled,
|
||||||
|
IsXTPSupported = server.IsXTPSupported,
|
||||||
|
Product = server.Product,
|
||||||
|
ReservedStorageSizeMB = server.ReservedStorageSizeMB,
|
||||||
|
RootDirectory = server.RootDirectory,
|
||||||
|
ServerCollation = server.Collation,
|
||||||
|
ServiceTier = server.ServiceTier,
|
||||||
|
StorageSpaceUsageInGB = (int)ByteConverter.ConvertMbtoGb(server.UsedStorageSizeMB),
|
||||||
|
Version = server.Version.ToString(),
|
||||||
|
MinServerMemory = GetServerMinMemory(),
|
||||||
|
MaxServerMemory = GetServerMaxMemory()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return Task.FromResult(new InitializeViewResult { ViewInfo = this.serverViewInfo, Context = context });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Task Save(ServerViewContext context, ServerInfo serverInfo)
|
||||||
|
{
|
||||||
|
throw new NotSupportedException("ServerHandler does not support Save method");
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Task<string> Script(ServerViewContext context, ServerInfo obj)
|
||||||
|
{
|
||||||
|
throw new NotSupportedException("ServerHandler does not support Script method");
|
||||||
|
}
|
||||||
|
|
||||||
|
private int GetServerMaxMemory()
|
||||||
|
{
|
||||||
|
return configService.GetServerSmoConfig(server, configService.MaxServerMemoryPropertyNumber).ConfigValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int GetServerMinMemory()
|
||||||
|
{
|
||||||
|
return configService.GetServerSmoConfig(server, configService.MinServerMemoryPropertyNumber).ConfigValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A class for storing various properties needed for Saving & Scripting a server
|
||||||
|
/// </summary>
|
||||||
|
public class ServerInfo : SqlObject
|
||||||
|
{
|
||||||
|
public string? HardwareGeneration { get; set; }
|
||||||
|
public string Language { get; set; }
|
||||||
|
public int MemoryInMB { get; set; }
|
||||||
|
public string OperatingSystem { get; set; }
|
||||||
|
public string Platform { get; set; }
|
||||||
|
public int Processors { get; set; }
|
||||||
|
public bool IsClustered { get; set; }
|
||||||
|
public bool IsHadrEnabled { get; set; }
|
||||||
|
public bool IsPolyBaseInstalled { get; set; }
|
||||||
|
public bool? IsXTPSupported { get; set; }
|
||||||
|
public string Product { get; set; }
|
||||||
|
public int? ReservedStorageSizeMB { get; set; }
|
||||||
|
public string RootDirectory { get; set; }
|
||||||
|
public string ServerCollation { get; set; }
|
||||||
|
public string? ServiceTier { get; set; }
|
||||||
|
public int? StorageSpaceUsageInGB { get; set; }
|
||||||
|
public string Version { get; set; }
|
||||||
|
public int MaxServerMemory { get; set; }
|
||||||
|
public int MinServerMemory { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
using Microsoft.SqlServer.Management.Common;
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
|
||||||
|
{
|
||||||
|
public class ServerViewContext : SqlObjectViewContext
|
||||||
|
{
|
||||||
|
public ServerConnection Connection { get; }
|
||||||
|
public ServerViewContext(Contracts.InitializeViewRequestParams parameters, ServerConnection connection) : base(parameters)
|
||||||
|
{
|
||||||
|
this.Connection = connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Dispose()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement.ObjectTypes.Server
|
||||||
|
{
|
||||||
|
public class ServerViewInfo : SqlObjectViewInfo
|
||||||
|
{
|
||||||
|
public ServerViewInfo() { }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,6 +31,8 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement
|
|||||||
[EnumMember(Value = "View")]
|
[EnumMember(Value = "View")]
|
||||||
View,
|
View,
|
||||||
[EnumMember(Value = "Database")]
|
[EnumMember(Value = "Database")]
|
||||||
Database
|
Database,
|
||||||
|
[EnumMember(Value = "Server")]
|
||||||
|
Server
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -23,6 +23,8 @@ namespace Microsoft.SqlTools.ServiceLayer.ServerConfigurations
|
|||||||
{
|
{
|
||||||
private ConnectionService connectionService = null;
|
private ConnectionService connectionService = null;
|
||||||
private static readonly Lazy<ServerConfigService> instance = new Lazy<ServerConfigService>(() => new ServerConfigService());
|
private static readonly Lazy<ServerConfigService> instance = new Lazy<ServerConfigService>(() => new ServerConfigService());
|
||||||
|
public readonly int MaxServerMemoryPropertyNumber = 1544;
|
||||||
|
public readonly int MinServerMemoryPropertyNumber = 1543;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the singleton instance object
|
/// Gets the singleton instance object
|
||||||
@@ -175,7 +177,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ServerConfigurations
|
|||||||
public void UpdateConfig(ServerConnection serverConnection, int configNumber, int configValue)
|
public void UpdateConfig(ServerConnection serverConnection, int configNumber, int configValue)
|
||||||
{
|
{
|
||||||
Server server = new Server(serverConnection);
|
Server server = new Server(serverConnection);
|
||||||
ConfigProperty serverConfig = GetSmoConfig(server, configNumber);
|
ConfigProperty serverConfig = GetServerSmoConfig(server, configNumber);
|
||||||
|
|
||||||
if (serverConfig != null)
|
if (serverConfig != null)
|
||||||
{
|
{
|
||||||
@@ -203,7 +205,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ServerConfigurations
|
|||||||
private ServerConfigProperty GetConfig(ServerConnection serverConnection, int configNumber)
|
private ServerConfigProperty GetConfig(ServerConnection serverConnection, int configNumber)
|
||||||
{
|
{
|
||||||
Server server = new Server(serverConnection);
|
Server server = new Server(serverConnection);
|
||||||
ConfigProperty serverConfig = GetSmoConfig(server, configNumber);
|
ConfigProperty serverConfig = GetServerSmoConfig(server, configNumber);
|
||||||
return serverConfig != null ? ServerConfigProperty.ToServerConfigProperty(serverConfig) : null;
|
return serverConfig != null ? ServerConfigProperty.ToServerConfigProperty(serverConfig) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -218,7 +220,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ServerConfigurations
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ConfigProperty GetSmoConfig(Server server, int configNumber)
|
public ConfigProperty GetServerSmoConfig(Server server, int configNumber)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
32
src/Microsoft.SqlTools.ServiceLayer/Utility/ByteConverter.cs
Normal file
32
src/Microsoft.SqlTools.ServiceLayer/Utility/ByteConverter.cs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.Utility
|
||||||
|
{
|
||||||
|
public static class ByteConverter
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Converts value in KBs to MBs
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="valueInKb">value in kilo bytes</param>
|
||||||
|
/// <returns>Returns as double type</returns>
|
||||||
|
public static double ConvertKbtoMb(double valueInKb)
|
||||||
|
{
|
||||||
|
return (Math.Round(valueInKb / 1024, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts value in MBs to GBs
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="valueInMb">value in mega bytes</param>
|
||||||
|
/// <returns>Returns as double type</returns>
|
||||||
|
public static double ConvertMbtoGb(double valueInMb)
|
||||||
|
{
|
||||||
|
return (Math.Round(valueInMb / 1024, 2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -345,15 +345,5 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
|
|||||||
return new string(nameChars);
|
return new string(nameChars);
|
||||||
}
|
}
|
||||||
private static readonly HashSet<char> illegalFilenameCharacters = new HashSet<char>(new char[] { '\\', '/', ':', '*', '?', '"', '<', '>', '|' });
|
private static readonly HashSet<char> illegalFilenameCharacters = new HashSet<char>(new char[] { '\\', '/', ':', '*', '?', '"', '<', '>', '|' });
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Converts value in KBs to MBs
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="valueInKb">value in kilo bytes</param>
|
|
||||||
/// <returns>Returns as double type</returns>
|
|
||||||
public static double ConvertKbtoMb(double valueInKb)
|
|
||||||
{
|
|
||||||
return (Math.Round(valueInKb / 1024, 2));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Data.SqlClient;
|
||||||
|
using Microsoft.SqlServer.Management.Common;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.ObjectManagement;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||||
|
using Microsoft.SqlServer.Management.Smo;
|
||||||
|
|
||||||
|
using NUnit.Framework;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.ObjectManagement
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Tests for the Login management component
|
||||||
|
/// </summary>
|
||||||
|
public class ServerHandlerTests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Test GetServerProperties for Sql Server
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public async Task GetServerProperties()
|
||||||
|
{
|
||||||
|
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", serverType: TestServerType.OnPrem);
|
||||||
|
using (SqlConnection sqlConn = ConnectionService.OpenSqlConnection(connectionResult.ConnectionInfo))
|
||||||
|
{
|
||||||
|
var server = new Server(new ServerConnection(sqlConn));
|
||||||
|
var serverHandler = new ServerHandler(ConnectionService.Instance);
|
||||||
|
|
||||||
|
var requestParams = ObjectManagementTestUtils.GetInitializeViewRequestParams(connectionResult.ConnectionInfo.OwnerUri, "master", true, SqlObjectType.Server, "", "");
|
||||||
|
var result = await serverHandler.InitializeObjectView(requestParams);
|
||||||
|
Assert.That(result.ViewInfo.ObjectInfo, Is.Not.Null, $"Expected result should not be empty");
|
||||||
|
Assert.That(result.ViewInfo.ObjectInfo.Name, Is.EqualTo(server.Name), $"Server name should not be empty");
|
||||||
|
Assert.That(((ServerInfo)result.ViewInfo.ObjectInfo).Language, Is.Not.Null, $"Server language should not be null");
|
||||||
|
Assert.That(((ServerInfo)result.ViewInfo.ObjectInfo).MemoryInMB, Is.GreaterThan(0), $"Server physical memory should be greater than 0");
|
||||||
|
Assert.That(((ServerInfo)result.ViewInfo.ObjectInfo).Platform, Is.Not.Null, $"Server platform should not be null");
|
||||||
|
Assert.That(((ServerInfo)result.ViewInfo.ObjectInfo).OperatingSystem, Is.Not.Null, $"Server operating system should not be null");
|
||||||
|
Assert.That(((ServerInfo)result.ViewInfo.ObjectInfo).Processors, Is.Not.Null, $"Server processors should not be null");
|
||||||
|
Assert.That(((ServerInfo)result.ViewInfo.ObjectInfo).IsClustered, Is.Not.Null, $"Server isClustered property should not be null");
|
||||||
|
Assert.That(((ServerInfo)result.ViewInfo.ObjectInfo).IsHadrEnabled, Is.Not.Null, $"Server isHadrEnabled property should not be null");
|
||||||
|
Assert.That(((ServerInfo)result.ViewInfo.ObjectInfo).IsPolyBaseInstalled, Is.Not.Null, $"Server isPolyBaseInstalled property should not be null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user