From e14828e0d408c84e420d2eb2d0f363690f3c28e1 Mon Sep 17 00:00:00 2001 From: Barbara Valdez <34872381+barbaravaldez@users.noreply.github.com> Date: Fri, 7 Jul 2023 13:03:25 -0700 Subject: [PATCH] Send min and max values for server properties (#2130) --- .../ObjectTypes/Server/ServerHandler.cs | 64 +++++++++---------- .../ObjectTypes/Server/ServerInfo.cs | 11 +++- .../{ServerData.cs => ServerPrototype.cs} | 41 +++++++----- .../ObjectManagement/ServerHandlerTests.cs | 17 +++-- 4 files changed, 75 insertions(+), 58 deletions(-) rename src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/Server/{ServerData.cs => ServerPrototype.cs} (95%) diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/Server/ServerHandler.cs b/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/Server/ServerHandler.cs index 7105f09c..dc691d25 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/Server/ServerHandler.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/Server/ServerHandler.cs @@ -68,43 +68,43 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement } return Task.FromResult(new InitializeViewResult { ViewInfo = this.serverViewInfo, Context = context }); } - } - - public override Task Save(ServerViewContext context, ServerInfo obj) - { - UpdateServerProperties(context.Parameters, obj); - return Task.CompletedTask; - } - - public override Task Script(ServerViewContext context, ServerInfo obj) - { - throw new NotSupportedException("ServerHandler does not support Script method"); - } - - private void UpdateServerProperties(InitializeViewRequestParams viewParams, ServerInfo serverInfo) - { - if (viewParams != null) - { - ConnectionInfo connInfo = this.GetConnectionInfo(viewParams.ConnectionUri); - CDataContainer dataContainer = CDataContainer.CreateDataContainer(connInfo); - - ServerPrototype prototype = new ServerPrototype(dataContainer); - prototype.ApplyInfoToPrototype(serverInfo); - ConfigureServer(dataContainer, ConfigAction.Update, RunType.RunNow, prototype); } - } - private void ConfigureServer(CDataContainer dataContainer, ConfigAction configAction, RunType runType, ServerPrototype prototype) - { - using (var actions = new ServerActions(dataContainer, prototype, configAction)) + public override Task Save(ServerViewContext context, ServerInfo obj) { - var executionHandler = new ExecutonHandler(actions); - executionHandler.RunNow(runType, this); - if (executionHandler.ExecutionResult == ExecutionMode.Failure) + UpdateServerProperties(context.Parameters, obj); + return Task.CompletedTask; + } + + public override Task Script(ServerViewContext context, ServerInfo obj) + { + throw new NotSupportedException("ServerHandler does not support Script method"); + } + + private void UpdateServerProperties(InitializeViewRequestParams viewParams, ServerInfo serverInfo) + { + if (viewParams != null) { - throw executionHandler.ExecutionFailureException; + ConnectionInfo connInfo = this.GetConnectionInfo(viewParams.ConnectionUri); + CDataContainer dataContainer = CDataContainer.CreateDataContainer(connInfo); + + ServerPrototype prototype = new ServerPrototype(dataContainer); + prototype.ApplyInfoToPrototype(serverInfo); + ConfigureServer(dataContainer, ConfigAction.Update, RunType.RunNow, prototype); + } + } + + private void ConfigureServer(CDataContainer dataContainer, ConfigAction configAction, RunType runType, ServerPrototype prototype) + { + using (var actions = new ServerActions(dataContainer, prototype, configAction)) + { + var executionHandler = new ExecutonHandler(actions); + executionHandler.RunNow(runType, this); + if (executionHandler.ExecutionResult == ExecutionMode.Failure) + { + throw executionHandler.ExecutionFailureException; + } } } } -} } \ No newline at end of file diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/Server/ServerInfo.cs b/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/Server/ServerInfo.cs index b806a2dc..a900fd7a 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/Server/ServerInfo.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/Server/ServerInfo.cs @@ -28,7 +28,14 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement public string? ServiceTier { get; set; } public int? StorageSpaceUsageInMB { get; set; } public string Version { get; set; } - public int MaxServerMemory { get; set; } - public int MinServerMemory { get; set; } + public NumericServerProperty MaxServerMemory { get; set; } + public NumericServerProperty MinServerMemory { get; set; } + } + + public class NumericServerProperty + { + public int MaximumValue { get; set; } + public int MinimumValue { get; set; } + public int Value { get; set; } } } \ No newline at end of file diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/Server/ServerData.cs b/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/Server/ServerPrototype.cs similarity index 95% rename from src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/Server/ServerData.cs rename to src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/Server/ServerPrototype.cs index cc1334e3..760401a7 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/Server/ServerData.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/Server/ServerPrototype.cs @@ -25,7 +25,6 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement private ServerConnection sqlConnection; private ServerConfigService configService; - private ServerPrototypeData currentState; private ServerPrototypeData originalState; @@ -264,7 +263,8 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement } } - public int MaxServerMemory + + public NumericServerProperty MaxServerMemory { get { @@ -276,7 +276,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement } } - public int MinServerMemory + public NumericServerProperty MinServerMemory { get { @@ -334,19 +334,18 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement public bool UpdateMemoryValues(Microsoft.SqlServer.Management.Smo.Server server) { bool changesMade = false; - - if (this.currentState.MinMemory != this.originalState.MinMemory) + if (this.currentState.MinMemory.Value != this.originalState.MinMemory.Value) { changesMade = true; ConfigProperty serverConfig = this.configService.GetServerSmoConfig(server, this.configService.MinServerMemoryPropertyNumber); - serverConfig.ConfigValue = this.currentState.MinMemory; + serverConfig.ConfigValue = this.currentState.MinMemory.Value; } - if (this.currentState.MaxMemory != this.originalState.MaxMemory) + if (this.currentState.MaxMemory.Value != this.originalState.MaxMemory.Value) { changesMade = true; ConfigProperty serverConfig = this.configService.GetServerSmoConfig(server, this.configService.MaxServerMemoryPropertyNumber); - serverConfig.ConfigValue = this.currentState.MaxMemory; + serverConfig.ConfigValue = this.currentState.MaxMemory.Value; } return changesMade; } @@ -404,9 +403,9 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement private string serviceTier = String.Empty; private int reservedStorageSizeMB = 0; private int storageSpaceUsageInMB = 0; - private int minMemory = 0; - private int maxMemory = 0; + private NumericServerProperty minMemory; + private NumericServerProperty maxMemory; private bool initialized = false; private Server server; private CDataContainer context; @@ -821,7 +820,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement } - public int MinMemory + public NumericServerProperty MinMemory { get { @@ -836,7 +835,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement set { if (this.initialized) - { + { Logger.Error(SR.PropertyNotInitialized("MinMemory")); } @@ -844,7 +843,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement } } - public int MaxMemory + public NumericServerProperty MaxMemory { get { @@ -907,6 +906,8 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement this.isYukonOrLater = (this.server.Information.Version.Major >= 9); this.serverMaxMemoryProperty = this.configService.GetServerSmoConfig(server, this.configService.MaxServerMemoryPropertyNumber); this.serverMinMemoryProperty = this.configService.GetServerSmoConfig(server, this.configService.MinServerMemoryPropertyNumber); + this.minMemory = new NumericServerProperty(); + this.maxMemory = new NumericServerProperty(); LoadData(); } @@ -963,8 +964,18 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement this.reservedStorageSizeMB = server.ReservedStorageSizeMB; this.serviceTier = server.ServiceTier; this.storageSpaceUsageInMB = server.UsedStorageSizeMB; - this.maxMemory = serverMaxMemoryProperty.ConfigValue; - this.minMemory = serverMinMemoryProperty.ConfigValue; + LoadMemoryProperties(); + } + + private void LoadMemoryProperties() + { + this.maxMemory.Value = serverMaxMemoryProperty.ConfigValue; + this.maxMemory.MaximumValue = serverMaxMemoryProperty.Maximum; + this.maxMemory.MinimumValue = serverMaxMemoryProperty.Minimum; + + this.minMemory.Value = serverMinMemoryProperty.ConfigValue; + this.minMemory.MaximumValue = serverMinMemoryProperty.Maximum; + this.minMemory.MinimumValue = serverMinMemoryProperty.Minimum; } } } diff --git a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/ObjectManagement/ServerHandlerTests.cs b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/ObjectManagement/ServerHandlerTests.cs index da4df194..1684fe12 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/ObjectManagement/ServerHandlerTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/ObjectManagement/ServerHandlerTests.cs @@ -14,7 +14,6 @@ using NUnit.Framework; using Microsoft.SqlTools.ServiceLayer.Test.Common; using Server = Microsoft.SqlServer.Management.Smo.Server; -using Microsoft.SqlTools.ServiceLayer.ServerConfigurations; namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.ObjectManagement { @@ -61,7 +60,6 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.ObjectManagement { var server = new Server(new ServerConnection(sqlConn)); var serverHandler = new ServerHandler(ConnectionService.Instance); - var serverConfig = new ServerConfigService(); var requestParams = ObjectManagementTestUtils.GetInitializeViewRequestParams(connectionResult.ConnectionInfo.OwnerUri, "master", true, SqlObjectType.Server, "", ""); var result = (ServerInfo)(await serverHandler.InitializeObjectView(requestParams)).ViewInfo.ObjectInfo; @@ -90,17 +88,18 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.ObjectManagement }; // Change memory settings - serverInfo.MinServerMemory = 10; - serverInfo.MaxServerMemory = 500; + serverInfo.MinServerMemory.Value = 10; + serverInfo.MaxServerMemory.Value = 500; - Assert.AreNotEqual(result.MinServerMemory, serverInfo.MinServerMemory, "Server property should not be equal after update"); - Assert.AreNotEqual(result.MaxServerMemory, serverInfo.MaxServerMemory, "Server property should not be equal after update"); + Assert.That(result.MinServerMemory.Value, Is.Not.EqualTo(serverInfo.MinServerMemory.Value), "Server property should not be equal after update"); + Assert.That(result.MaxServerMemory.Value, Is.Not.EqualTo(serverInfo.MaxServerMemory.Value), "Server property should not be equal after update"); await ObjectManagementTestUtils.SaveObject(requestParams, serverInfo); result = (ServerInfo)(await serverHandler.InitializeObjectView(requestParams)).ViewInfo.ObjectInfo; - Assert.IsNotNull(result); - Assert.AreEqual(result.MinServerMemory, serverInfo.MinServerMemory, "Server property should not be different after update"); - Assert.AreEqual(result.MaxServerMemory, serverInfo.MaxServerMemory, "Server property should not be different after update"); + + Assert.That(result, Is.Not.Null); + Assert.That(result.MinServerMemory.Value, Is.EqualTo(serverInfo.MinServerMemory.Value), "Server property should be equal after update"); + Assert.That(result.MaxServerMemory.Value, Is.EqualTo(serverInfo.MaxServerMemory.Value), "Server property should be equal after update"); } } }