diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs index 64bd854f..6d7980d7 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs @@ -468,6 +468,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent { var result = await ConfigureAgentAlert( parameters.OwnerUri, + parameters.Alert.Name, parameters.Alert, ConfigAction.Create, RunType.RunNow); @@ -486,6 +487,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent { var result = await ConfigureAgentAlert( parameters.OwnerUri, + parameters.OriginalAlertName, parameters.Alert, ConfigAction.Update, RunType.RunNow); @@ -504,6 +506,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent { var result = await ConfigureAgentAlert( parameters.OwnerUri, + parameters.Alert.Name, parameters.Alert, ConfigAction.Drop, RunType.RunNow); @@ -881,6 +884,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent internal async Task> ConfigureAgentAlert( string ownerUri, + string alertName, AgentAlertInfo alert, ConfigAction configAction, RunType runType) @@ -893,11 +897,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent ConnectionServiceInstance.TryFindConnection(ownerUri, out connInfo); CDataContainer dataContainer = CDataContainer.CreateDataContainer(connInfo, databaseExists: true); STParameters param = new STParameters(dataContainer.Document); - param.SetParam("alert", alert.Name); + param.SetParam("alert", alertName); if (alert != null) { - using (AgentAlertActions agentAlert = new AgentAlertActions(dataContainer, alert, configAction)) + using (AgentAlertActions agentAlert = new AgentAlertActions(dataContainer, alertName, alert, configAction)) { var executionHandler = new ExecutonHandler(agentAlert); executionHandler.RunNow(runType, this); diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentAlertInfo.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentAlertInfo.cs index 85e38741..abaf2615 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentAlertInfo.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentAlertInfo.cs @@ -44,11 +44,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent.Contracts public string JobName { get; set; } public string LastOccurrenceDate { get; set; } public string LastResponseDate { get; set; } - public int MessageId { get; set; } + public int? MessageId { get; set; } public string NotificationMessage { get; set; } public int OccurrenceCount { get; set; } public string PerformanceCondition { get; set; } - public int Severity { get; set; } + public int? Severity { get; set; } public string DatabaseName { get; set; } public string CountResetDate { get; set; } public string CategoryName { get; set; } diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentAlertActions.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentAlertActions.cs index cb9343f9..2d29e88e 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentAlertActions.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentAlertActions.cs @@ -11,11 +11,11 @@ using Microsoft.SqlTools.ServiceLayer.Management; namespace Microsoft.SqlTools.ServiceLayer.Agent { - /// - /// AgentAlert class - /// - internal class AgentAlertActions : ManagementActionBase - { + /// + /// AgentAlert class + /// + internal class AgentAlertActions : ManagementActionBase + { /// /// Agent alert info instance /// @@ -23,12 +23,17 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent private ConfigAction configAction; + private string originalAlertName = null; + /// /// Default constructor that will be used to create dialog /// /// - public AgentAlertActions(CDataContainer dataContainer, AgentAlertInfo alertInfo, ConfigAction configAction) + public AgentAlertActions( + CDataContainer dataContainer, string originalAlertName, + AgentAlertInfo alertInfo, ConfigAction configAction) { + this.originalAlertName = originalAlertName; this.alertInfo = alertInfo; this.DataContainer = dataContainer; this.configAction = configAction; @@ -134,6 +139,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent return false; } alert.Alter(); + + if (!string.IsNullOrWhiteSpace(this.alertInfo.Name) && !string.Equals(alert.Name, this.alertInfo.Name)) + { + alert.Rename(this.alertInfo.Name); + } } return true; @@ -160,10 +170,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent throw new ArgumentNullException("alert"); } - if (!string.IsNullOrWhiteSpace(this.DataContainer.ConnectionInfo.DatabaseName)) - { - alert.DatabaseName = this.DataContainer.ConnectionInfo.DatabaseName; - } + alert.DatabaseName = !string.IsNullOrWhiteSpace(this.alertInfo.DatabaseName) + ? this.alertInfo.DatabaseName : string.Empty; if (!string.IsNullOrWhiteSpace(this.alertInfo.CategoryName)) { @@ -174,12 +182,26 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent if (alertInfo.AlertType == Contracts.AlertType.SqlServerEvent) { - alert.Severity = this.alertInfo.Severity; - alert.MessageID = this.alertInfo.MessageId; + if (this.alertInfo.MessageId.HasValue) + { + alert.Severity = 0; + alert.MessageID = this.alertInfo.MessageId.Value; + } + else if (this.alertInfo.Severity.HasValue) + { + alert.Severity = this.alertInfo.Severity.Value; + alert.MessageID = 0; + } + if (!string.IsNullOrWhiteSpace(this.alertInfo.EventDescriptionKeyword)) { alert.EventDescriptionKeyword = this.alertInfo.EventDescriptionKeyword; } + + // clear out other alert type fields + alert.PerformanceCondition = string.Empty; + alert.WmiEventNamespace = string.Empty; + alert.WmiEventQuery = string.Empty; } } }