mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-18 17:23:52 -05:00
SQL Agent configuration for Operators, Alerts and Proxies (WIP) (#621)
* Initial non-refactored SQL Agent alert classes (WIP) * Move agent classes into subdirectories * Refactor the agent config code a bit more * Add more implementation for handlers * Add more code to the create alert handler * Clean up agent alert class * Clean up alert methods a bit * Initial Operator contracts * Additonal SQL Agent config changes * More Proxy config cleanup * Cleanup AgentProxy class * Additional cleanups * Run SRGen * Add security service to create credential objects
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
//
|
||||
// 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.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlServer.Management.Smo;
|
||||
using Microsoft.SqlServer.Management.XEvent;
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Agent;
|
||||
using Microsoft.SqlTools.ServiceLayer.Agent.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.Profiler;
|
||||
using Microsoft.SqlTools.ServiceLayer.Profiler.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
{
|
||||
public class AgentAlertTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Verify default agent/alerts handlers
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task TestHandleAgentAlertsRequest()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
|
||||
var requestParams = new AgentAlertsParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri
|
||||
};
|
||||
|
||||
var requestContext = new Mock<RequestContext<AgentAlertsResult>>();
|
||||
|
||||
AgentService service = new AgentService();
|
||||
await service.HandleAgentAlertsRequest(requestParams, requestContext.Object);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify the default "create agent alert" request handler with valid parameters
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task TestHandleCreateAgentAlertsRequest()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
|
||||
var requestParams = new CreateAgentAlertParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Alert = new AgentAlertInfo()
|
||||
{
|
||||
JobName = "Test Job Name"
|
||||
}
|
||||
};
|
||||
|
||||
var requestContext = new Mock<RequestContext<CreateAgentAlertResult>>();
|
||||
|
||||
AgentService service = new AgentService();
|
||||
await service.HandleCreateAgentAlertRequest(requestParams, requestContext.Object);
|
||||
|
||||
// var agentAlerts = await AgentTestUtils.GetAgentAlerts(connectionResult.ConnectionInfo.OwnerUri);
|
||||
// if (agentAlerts != null && agentAlerts.Length > 0)
|
||||
// {
|
||||
// foreach (var agentAlert in agentAlerts)
|
||||
// {
|
||||
// if (agentAlert.JobName.Equals(alert.JobName))
|
||||
// {
|
||||
// await service.HandleDeleteAgentAlertRequest(new DeleteAgentAlertParams()
|
||||
// {
|
||||
// OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
// Alert = alert
|
||||
// }, deleteContext.Object);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify the default "update agent alert" request handler with valid parameters
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task TestHandleUpdateAgentAlertsRequest()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var createContext = new Mock<RequestContext<CreateAgentAlertResult>>();
|
||||
var updateContext = new Mock<RequestContext<UpdateAgentAlertResult>>();
|
||||
var deleteContext = new Mock<RequestContext<DeleteAgentAlertResult>>();
|
||||
|
||||
var service = new AgentService();
|
||||
var alert = new AgentAlertInfo()
|
||||
{
|
||||
JobName = "test_update_job",
|
||||
AlertType = AlertType.SqlServerEvent,
|
||||
Severity = 1
|
||||
};
|
||||
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
await service.HandleDeleteAgentAlertRequest(new DeleteAgentAlertParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Alert = alert
|
||||
}, deleteContext.Object);
|
||||
|
||||
await service.HandleCreateAgentAlertRequest(new CreateAgentAlertParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Alert = alert
|
||||
}, createContext.Object);
|
||||
|
||||
await service.HandleUpdateAgentAlertRequest(new UpdateAgentAlertParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Alert = alert
|
||||
}, updateContext.Object);
|
||||
|
||||
await service.HandleDeleteAgentAlertRequest(new DeleteAgentAlertParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Alert = alert
|
||||
}, deleteContext.Object);
|
||||
|
||||
createContext.VerifyAll();
|
||||
updateContext.VerifyAll();
|
||||
deleteContext.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// 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.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Agent;
|
||||
using Microsoft.SqlTools.ServiceLayer.Agent.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
{
|
||||
public class AgentOperatorTests
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Verify the default "update agent alert" request handler with valid parameters
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task TestHandleUpdateAgentOperatorRequest()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var createContext = new Mock<RequestContext<CreateAgentOperatorResult>>();
|
||||
|
||||
var service = new AgentService();
|
||||
var operatorInfo = new AgentOperatorInfo()
|
||||
{
|
||||
Id = 10,
|
||||
Name = "Joe DBA",
|
||||
EmailAddress = "test@aol.com"
|
||||
};
|
||||
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
|
||||
await service.HandleCreateAgentOperatorRequest(new CreateAgentOperatorParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Operator = operatorInfo
|
||||
}, createContext.Object);
|
||||
|
||||
createContext.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
//
|
||||
// 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.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Agent;
|
||||
using Microsoft.SqlTools.ServiceLayer.Agent.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
{
|
||||
public class AgentProxyTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Verify the default "update agent alert" request handler with valid parameters
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task TestHandleUpdateAgentProxyRequest()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var createContext = new Mock<RequestContext<CreateAgentProxyResult>>();
|
||||
var updateContext = new Mock<RequestContext<UpdateAgentProxyResult>>();
|
||||
var deleteContext = new Mock<RequestContext<DeleteAgentProxyResult>>();
|
||||
|
||||
var service = new AgentService();
|
||||
var proxy = new AgentProxyInfo()
|
||||
{
|
||||
Id = 10,
|
||||
AccountName = "Test Proxy 2",
|
||||
CredentialName = "User",
|
||||
Description = "",
|
||||
IsEnabled = true
|
||||
};
|
||||
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
await service.HandleDeleteAgentProxyRequest(new DeleteAgentProxyParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Proxy = proxy
|
||||
}, deleteContext.Object);
|
||||
|
||||
deleteContext.VerifyAll();
|
||||
|
||||
await service.HandleCreateAgentProxyRequest(new CreateAgentProxyParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Proxy = proxy
|
||||
}, createContext.Object);
|
||||
|
||||
createContext.VerifyAll();
|
||||
|
||||
string originalProxyName = proxy.AccountName;
|
||||
proxy.AccountName = proxy.AccountName + " Updated";
|
||||
await service.HandleUpdateAgentProxyRequest(new UpdateAgentProxyParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
OriginalProxyName = originalProxyName,
|
||||
Proxy = proxy
|
||||
}, updateContext.Object);
|
||||
|
||||
updateContext.VerifyAll();
|
||||
|
||||
await service.HandleDeleteAgentProxyRequest(new DeleteAgentProxyParams()
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Proxy = proxy
|
||||
}, deleteContext.Object);
|
||||
|
||||
deleteContext.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,7 +46,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
AgentService service = new AgentService();
|
||||
await service.HandleAgentJobsRequest(requestParams, requestContext.Object);
|
||||
requestContext.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -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.
|
||||
//
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Agent;
|
||||
using Microsoft.SqlTools.ServiceLayer.Agent.Contracts;
|
||||
using Moq;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
|
||||
{
|
||||
public static class AgentTestUtils
|
||||
{
|
||||
internal static async Task<AgentAlertInfo[]> GetAgentAlerts(string connectionUri)
|
||||
{
|
||||
var requestParams = new AgentAlertsParams()
|
||||
{
|
||||
OwnerUri = connectionUri
|
||||
};
|
||||
|
||||
var requestContext = new Mock<RequestContext<AgentAlertsResult>>();
|
||||
|
||||
AgentAlertInfo[] agentAlerts = null;
|
||||
requestContext.Setup(x => x.SendResult(It.IsAny<AgentAlertsResult>()))
|
||||
.Callback<AgentAlertsResult>(r => agentAlerts = r.Alerts);
|
||||
|
||||
AgentService service = new AgentService();
|
||||
await service.HandleAgentAlertsRequest(requestParams, requestContext.Object);
|
||||
return agentAlerts;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.Security;
|
||||
using Microsoft.SqlTools.ServiceLayer.Security.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
||||
using Moq;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
using static Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility.LiveConnectionHelper;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Security
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests for the security service component
|
||||
/// </summary>
|
||||
public class SecuritygServiceTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Verify the script object request
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task TestHandleCreateCredentialRequest()
|
||||
{
|
||||
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||
{
|
||||
var createContext = new Mock<RequestContext<CreateCredentialResult>>();
|
||||
var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||
|
||||
var service = new SecurityService();
|
||||
var credential = new CredentialInfo()
|
||||
{
|
||||
};
|
||||
|
||||
await service.HandleCreateCredentialRequest(new CreateCredentialParams
|
||||
{
|
||||
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
|
||||
Credential = credential
|
||||
}, createContext.Object);
|
||||
|
||||
createContext.VerifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user