diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs index c3e0010d..e834209c 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs @@ -410,17 +410,52 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent await Task.Run(async () => { var result = new AgentAlertsResult(); - result.Alerts = new List().ToArray(); - - ConnectionInfo connInfo; - ConnectionServiceInstance.TryFindConnection(parameters.OwnerUri, out connInfo); - - if (connInfo != null) + try { + ConnectionInfo connInfo; + ConnectionServiceInstance.TryFindConnection(parameters.OwnerUri, out connInfo); CDataContainer dataContainer = CDataContainer.CreateDataContainer(connInfo, databaseExists: true); - AlertCollection alerts = dataContainer.Server.JobServer.Alerts; - } + int alertsCount = dataContainer.Server.JobServer.Alerts.Count; + var alerts = new AgentAlertInfo[alertsCount]; + for (int i = 0; i < alertsCount; ++i) + { + var alert = dataContainer.Server.JobServer.Alerts[i]; + alerts[i] = new AgentAlertInfo + { + Id = alert.ID, + DelayBetweenResponses = alert.DelayBetweenResponses, + EventDescriptionKeyword = alert.EventDescriptionKeyword, + EventSource = alert.EventSource, + HasNotification = alert.HasNotification, + IncludeEventDescription = (Contracts.NotifyMethods)alert.IncludeEventDescription, + IsEnabled = alert.IsEnabled, + JobId = alert.JobID != null ? alert.JobID.ToString() : null, + JobName = alert.JobName, + LastOccurrenceDate = alert.LastOccurrenceDate != null ? alert.LastOccurrenceDate.ToString() : null, + LastResponseDate = alert.LastResponseDate != null ? alert.LastResponseDate.ToString() : null, + MessageId = alert.MessageID, + NotificationMessage = alert.NotificationMessage, + OccurrenceCount = alert.OccurrenceCount, + PerformanceCondition = alert.PerformanceCondition, + Severity = alert.Severity, + DatabaseName = alert.DatabaseName, + CountResetDate = alert.CountResetDate != null ? alert.CountResetDate.ToString() : null, + CategoryName = alert.CategoryName, + AlertType = (Contracts.AlertType)alert.AlertType, + WmiEventNamespace = alert.WmiEventNamespace, + WmiEventQuery = alert.WmiEventQuery + }; + } + + result.Alerts = alerts; + result.Success = true; + } + catch (Exception ex) + { + result.Success = false; + result.ErrorMessage = ex.ToString(); + } await requestContext.SendResult(result); }); } @@ -485,8 +520,53 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent internal async Task HandleAgentOperatorsRequest(AgentOperatorsParams parameters, RequestContext requestContext) { - await requestContext.SendResult(null); - } + await Task.Run(async () => + { + var result = new AgentOperatorsResult(); + try + { + ConnectionInfo connInfo; + ConnectionServiceInstance.TryFindConnection(parameters.OwnerUri, out connInfo); + CDataContainer dataContainer = CDataContainer.CreateDataContainer(connInfo, databaseExists: true); + + int operatorCount = dataContainer.Server.JobServer.Operators.Count; + var operators = new AgentOperatorInfo[operatorCount]; + for (int i = 0; i < operatorCount; ++i) + { + var item = dataContainer.Server.JobServer.Operators[i]; + operators[i] = new AgentOperatorInfo + { + Name = item.Name, + Id = item.ID, + EmailAddress = item.EmailAddress, + Enabled = item.Enabled, + LastEmailDate = item.LastEmailDate.ToString(), + LastNetSendDate = item.LastNetSendDate.ToString(), + LastPagerDate = item.LastPagerDate.ToString(), + PagerAddress = item.PagerAddress, + CategoryName = item.CategoryName, + PagerDays = (Contracts.WeekDays)item.PagerDays, + SaturdayPagerEndTime = item.SaturdayPagerEndTime.ToString(), + SaturdayPagerStartTime = item.SaturdayPagerEndTime.ToString(), + SundayPagerEndTime = item.SundayPagerEndTime.ToString(), + SundayPagerStartTime = item.SundayPagerStartTime.ToString(), + NetSendAddress = item.NetSendAddress, + WeekdayPagerStartTime = item.WeekdayPagerStartTime.ToString(), + WeekdayPagerEndTime = item.WeekdayPagerEndTime.ToString() + }; + } + + result.Operators = operators; + result.Success = true; + } + catch (Exception ex) + { + result.Success = false; + result.ErrorMessage = ex.ToString(); + } + await requestContext.SendResult(result); + }); + } internal async Task HandleCreateAgentOperatorRequest( CreateAgentOperatorParams parameters, @@ -548,7 +628,42 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent internal async Task HandleAgentProxiesRequest(AgentProxiesParams parameters, RequestContext requestContext) { - await requestContext.SendResult(null); + await Task.Run(async () => + { + var result = new AgentProxiesResult(); + try + { + ConnectionInfo connInfo; + ConnectionServiceInstance.TryFindConnection(parameters.OwnerUri, out connInfo); + CDataContainer dataContainer = CDataContainer.CreateDataContainer(connInfo, databaseExists: true); + + int proxyCount = dataContainer.Server.JobServer.ProxyAccounts.Count; + var proxies = new AgentProxyInfo[proxyCount]; + for (int i = 0; i < proxyCount; ++i) + { + var proxy = dataContainer.Server.JobServer.ProxyAccounts[i]; + proxies[i] = new AgentProxyInfo + { + Id = proxy.ID, + AccountName = proxy.Name, + Description = proxy.Description, + CredentialName = proxy.CredentialName, + CredentialIdentity = proxy.CredentialIdentity, + CredentialId = proxy.CredentialID, + IsEnabled = proxy.IsEnabled, + }; + } + result.Proxies = proxies; + result.Success = true; + } + catch (Exception ex) + { + result.Success = false; + result.ErrorMessage = ex.ToString(); + } + + await requestContext.SendResult(result); + }); } internal async Task HandleCreateAgentProxyRequest(CreateAgentProxyParams parameters, RequestContext requestContext) diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentAlertInfo.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentAlertInfo.cs index 47ddecf2..37ee5f60 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentAlertInfo.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentAlertInfo.cs @@ -39,17 +39,17 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent.Contracts public int HasNotification { get; set; } public NotifyMethods IncludeEventDescription { get; set; } public bool IsEnabled { get; set; } - public Guid JobId { get; set; } + public string JobId { get; set; } public string JobName { get; set; } - public DateTime LastOccurrenceDate { get; set; } - public DateTime LastResponseDate { get; set; } + public string LastOccurrenceDate { get; set; } + public string LastResponseDate { 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 string DatabaseName { get; set; } - public DateTime CountResetDate { get; set; } + public string CountResetDate { get; set; } public string CategoryName { get; set; } public AlertType AlertType { get; set; } public string WmiEventNamespace { get; set; } diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentOperatorInfo.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentOperatorInfo.cs index 4aa2316e..9c387690 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentOperatorInfo.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentOperatorInfo.cs @@ -33,18 +33,18 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent.Contracts public int Id { get; set; } public string EmailAddress { get; set; } public bool Enabled { get; set; } - public DateTime LastEmailDate { get; set; } - public DateTime LastNetSendDate { get; set; } - public DateTime LastPagerDate { get; set; } + public string LastEmailDate { get; set; } + public string LastNetSendDate { get; set; } + public string LastPagerDate { get; set; } public string PagerAddress { get; set; } public string CategoryName { get; set; } public WeekDays PagerDays { get; set; } - public TimeSpan SaturdayPagerEndTime { get; set; } - public TimeSpan SaturdayPagerStartTime { get; set; } - public TimeSpan SundayPagerEndTime { get; set; } - public TimeSpan SundayPagerStartTime { get; set; } + public string SaturdayPagerEndTime { get; set; } + public string SaturdayPagerStartTime { get; set; } + public string SundayPagerEndTime { get; set; } + public string SundayPagerStartTime { get; set; } public string NetSendAddress { get; set; } - public TimeSpan WeekdayPagerStartTime { get; set; } - public TimeSpan WeekdayPagerEndTime { get; set; } + public string WeekdayPagerStartTime { get; set; } + public string WeekdayPagerEndTime { get; set; } } } diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentOperatorData.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentOperatorData.cs index c8433a4e..12e0e01c 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentOperatorData.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/AgentOperatorData.cs @@ -585,20 +585,56 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent if ((operatorInfo.PagerDays & Contracts.WeekDays.WeekDays) > 0) { - currentOperator.WeekdayPagerStartTime = operatorInfo.WeekdayPagerStartTime; - currentOperator.WeekdayPagerEndTime = operatorInfo.WeekdayPagerEndTime; + TimeSpan weekdayPagerStartTime = default(TimeSpan); + if (!string.IsNullOrWhiteSpace(operatorInfo.WeekdayPagerStartTime)) + { + weekdayPagerStartTime = TimeSpan.Parse(operatorInfo.WeekdayPagerStartTime); + } + + TimeSpan weekdayPagerEndTime = default(TimeSpan); + if (!string.IsNullOrWhiteSpace(operatorInfo.WeekdayPagerEndTime)) + { + weekdayPagerEndTime = TimeSpan.Parse(operatorInfo.WeekdayPagerEndTime); + } + + currentOperator.WeekdayPagerStartTime = weekdayPagerStartTime; + currentOperator.WeekdayPagerEndTime = weekdayPagerEndTime; } if ((operatorInfo.PagerDays & Contracts.WeekDays.Saturday) > 0) { - currentOperator.SaturdayPagerStartTime = operatorInfo.SaturdayPagerStartTime; - currentOperator.SaturdayPagerEndTime = operatorInfo.SaturdayPagerEndTime; + TimeSpan saturdayPagerStartTime = default(TimeSpan); + if (!string.IsNullOrWhiteSpace(operatorInfo.SaturdayPagerStartTime)) + { + saturdayPagerStartTime = TimeSpan.Parse(operatorInfo.SaturdayPagerStartTime); + } + + TimeSpan saturdayPagerEndTime = default(TimeSpan); + if (!string.IsNullOrWhiteSpace(operatorInfo.SaturdayPagerEndTime)) + { + saturdayPagerEndTime = TimeSpan.Parse(operatorInfo.SaturdayPagerEndTime); + } + + currentOperator.SaturdayPagerStartTime = saturdayPagerStartTime; + currentOperator.SaturdayPagerEndTime = saturdayPagerEndTime; } if ((operatorInfo.PagerDays & Contracts.WeekDays.Sunday) > 0) { - currentOperator.SundayPagerStartTime = operatorInfo.SundayPagerStartTime; - currentOperator.SundayPagerEndTime = operatorInfo.SundayPagerEndTime; + TimeSpan sundayPagerStartTime = default(TimeSpan); + if (!string.IsNullOrWhiteSpace(operatorInfo.SundayPagerStartTime)) + { + sundayPagerStartTime = TimeSpan.Parse(operatorInfo.SundayPagerStartTime); + } + + TimeSpan sundayPagerEndTime = default(TimeSpan); + if (!string.IsNullOrWhiteSpace(operatorInfo.SundayPagerEndTime)) + { + sundayPagerEndTime = TimeSpan.Parse(operatorInfo.SundayPagerEndTime); + } + + currentOperator.SundayPagerStartTime = sundayPagerStartTime; + currentOperator.SundayPagerEndTime = sundayPagerEndTime; } if (this.createMode) diff --git a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Agent/AgentAlertTests.cs b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Agent/AgentAlertTests.cs index 70c219ef..82c586e4 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Agent/AgentAlertTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Agent/AgentAlertTests.cs @@ -20,7 +20,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent /// /// Verify default agent/alerts handlers /// - //[Fact] + [Fact] public async Task TestHandleAgentAlertsRequest() { using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile()) @@ -33,11 +33,10 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent }; var requestContext = new Mock>(); - AgentService service = new AgentService(); await service.HandleAgentAlertsRequest(requestParams, requestContext.Object); + requestContext.VerifyAll(); } - } /// diff --git a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Agent/AgentOperatorTests.cs b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Agent/AgentOperatorTests.cs index adb62f7d..9d052f97 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Agent/AgentOperatorTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Agent/AgentOperatorTests.cs @@ -16,6 +16,28 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent { public class AgentOperatorTests { + /// + /// Verify default agent/operators handlers + /// + [Fact] + public async Task TestHandleAgentOperatorsRequest() + { + using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile()) + { + var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath); + + var requestParams = new AgentOperatorsParams() + { + OwnerUri = connectionResult.ConnectionInfo.OwnerUri + }; + + var requestContext = new Mock>(); + AgentService service = new AgentService(); + await service.HandleAgentOperatorsRequest(requestParams, requestContext.Object); + requestContext.VerifyAll(); + } + } + /// /// Verify the default "create agent alert" request handler with valid parameters /// diff --git a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Agent/AgentProxyTests.cs b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Agent/AgentProxyTests.cs index b058a288..291dde38 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Agent/AgentProxyTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/Agent/AgentProxyTests.cs @@ -21,6 +21,27 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent { public class AgentProxyTests { + /// + /// Verify default agent/proxies handlers + /// + [Fact] + public async Task TestHandleAgentProxiesRequest() + { + using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile()) + { + var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath); + var requestParams = new AgentProxiesParams() + { + OwnerUri = connectionResult.ConnectionInfo.OwnerUri + }; + + var requestContext = new Mock>(); + AgentService service = new AgentService(); + await service.HandleAgentProxiesRequest(requestParams, requestContext.Object); + requestContext.VerifyAll(); + } + } + /// /// TestHandleCreateAgentProxyRequest ///