Added endpoint for fetching all notebook jobs (#834)

* Added endpoint for fetching all notebook jobs

* Refractored NotebookJobInfo to AgentNotebookInfo to make it more consistent with the rest of the codebase

* Added Notebook History endpoint in contracts.

* Added Create, Update, Delete notebook endpoints. Also added separate fetch template, materialized notebook endpoints. This will make the Notebook Request and Notebook History responses lighter.

* AgentNotebookInfo is now derived from AgentJobInfo

* added fetch noteook history endpoint

* Added fetching materialized notebook endpoint

* Added code for cleaning up the directory

* Added create notebook api

* Added Update and delete notebook job

* Fixed notebook history API

* Added last run info to the script and template folder

* Added execute database feature for notebook Jobs

* SQL commands are now using sqlparameters to prevent
any injection attacks

* Changed rundate and runtime to string to preserve
leading zeros

* integration test for agentnotebooks api

* Made some changes mentioned in PR

* Refactored the code, removed enpoint logic from the notebook handler and
wrote test cases

* changes select statements, fixed a bug in the test job cleanup
and fixed other stuff mentioned in the PR.

* added notebook_error column in notebook history select statement

* Added get template notebook endpoint
This commit is contained in:
Aasim Khan
2019-08-22 12:37:19 -07:00
committed by GitHub
parent 4f928133e1
commit 487235b1e9
15 changed files with 1825 additions and 55 deletions

View File

@@ -4,12 +4,14 @@
//
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
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.Security;
using Microsoft.SqlTools.ServiceLayer.Management;
using Microsoft.SqlTools.ServiceLayer.Utility;
using Moq;
using static Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility.LiveConnectionHelper;
@@ -22,7 +24,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
internal static AgentJobStepInfo GetTestJobStepInfo(
TestConnectionResult connectionResult,
AgentJobInfo job,
AgentJobInfo job,
string stepName = "Test Job Step1")
{
return new AgentJobStepInfo()
@@ -57,7 +59,8 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
CategoryType = 1,
LastRun = "today",
NextRun = "tomorrow",
JobId = Guid.NewGuid().ToString()
JobId = Guid.NewGuid().ToString(),
Owner = "sa"
};
}
@@ -74,12 +77,12 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
internal static AgentProxyInfo GetTestProxyInfo()
{
return new AgentProxyInfo()
{
{
AccountName = "Test Proxy",
CredentialName = SecurityTestUtils.TestCredentialName,
Description = "Test proxy description",
IsEnabled = true
};
IsEnabled = true
};
}
internal static AgentScheduleInfo GetTestScheduleInfo()
@@ -93,11 +96,11 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
}
internal static async Task CreateAgentJob(
AgentService service,
TestConnectionResult connectionResult,
AgentService service,
TestConnectionResult connectionResult,
AgentJobInfo job)
{
var context = new Mock<RequestContext<CreateAgentJobResult>>();
var context = new Mock<RequestContext<CreateAgentJobResult>>();
await service.HandleCreateAgentJobRequest(new CreateAgentJobParams
{
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
@@ -107,12 +110,12 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
}
internal static async Task UpdateAgentJob(
AgentService service,
TestConnectionResult connectionResult,
AgentService service,
TestConnectionResult connectionResult,
AgentJobInfo job)
{
job.Description = "Update job description";
var context = new Mock<RequestContext<UpdateAgentJobResult>>();
var context = new Mock<RequestContext<UpdateAgentJobResult>>();
await service.HandleUpdateAgentJobRequest(new UpdateAgentJobParams
{
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
@@ -122,12 +125,12 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
}
internal static async Task DeleteAgentJob(
AgentService service,
TestConnectionResult connectionResult,
AgentJobInfo job,
AgentService service,
TestConnectionResult connectionResult,
AgentJobInfo job,
bool verify = true)
{
var context = new Mock<RequestContext<ResultStatus>>();
var context = new Mock<RequestContext<ResultStatus>>();
await service.HandleDeleteAgentJobRequest(new DeleteAgentJobParams
{
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
@@ -141,11 +144,11 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
}
internal static async Task CreateAgentJobStep(
AgentService service,
TestConnectionResult connectionResult,
AgentService service,
TestConnectionResult connectionResult,
AgentJobStepInfo stepInfo)
{
var context = new Mock<RequestContext<CreateAgentJobStepResult>>();
var context = new Mock<RequestContext<CreateAgentJobStepResult>>();
await service.HandleCreateAgentJobStepRequest(new CreateAgentJobStepParams
{
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
@@ -153,13 +156,13 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
}, context.Object);
context.VerifyAll();
}
internal static async Task UpdateAgentJobStep(
AgentService service,
TestConnectionResult connectionResult,
AgentService service,
TestConnectionResult connectionResult,
AgentJobStepInfo stepInfo)
{
var context = new Mock<RequestContext<UpdateAgentJobStepResult>>();
var context = new Mock<RequestContext<UpdateAgentJobStepResult>>();
await service.HandleUpdateAgentJobStepRequest(new UpdateAgentJobStepParams
{
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
@@ -169,11 +172,11 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
}
internal static async Task DeleteAgentJobStep(
AgentService service,
TestConnectionResult connectionResult,
AgentService service,
TestConnectionResult connectionResult,
AgentJobStepInfo stepInfo)
{
var context = new Mock<RequestContext<ResultStatus>>();
var context = new Mock<RequestContext<ResultStatus>>();
await service.HandleDeleteAgentJobStepRequest(new DeleteAgentJobStepParams
{
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
@@ -183,7 +186,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
}
internal static async Task CreateAgentOperator(
AgentService service,
AgentService service,
TestConnectionResult connectionResult,
AgentOperatorInfo operatorInfo)
{
@@ -197,7 +200,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
}
internal static async Task UpdateAgentOperator(
AgentService service,
AgentService service,
TestConnectionResult connectionResult,
AgentOperatorInfo operatorInfo)
{
@@ -211,7 +214,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
}
internal static async Task DeleteAgentOperator(
AgentService service,
AgentService service,
TestConnectionResult connectionResult,
AgentOperatorInfo operatorInfo)
{
@@ -225,7 +228,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
}
internal static async Task CreateAgentProxy(
AgentService service,
AgentService service,
TestConnectionResult connectionResult,
AgentProxyInfo proxy)
{
@@ -239,7 +242,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
}
internal static async Task UpdateAgentProxy(
AgentService service,
AgentService service,
TestConnectionResult connectionResult,
string originalProxyName,
AgentProxyInfo proxy)
@@ -255,8 +258,8 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
}
internal static async Task DeleteAgentProxy(
AgentService service,
TestConnectionResult connectionResult,
AgentService service,
TestConnectionResult connectionResult,
AgentProxyInfo proxy)
{
var context = new Mock<RequestContext<ResultStatus>>();
@@ -269,7 +272,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
}
internal static async Task CreateAgentSchedule(
AgentService service,
AgentService service,
TestConnectionResult connectionResult,
AgentScheduleInfo schedule)
{
@@ -277,16 +280,16 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
await service.HandleCreateAgentScheduleRequest(new CreateAgentScheduleParams
{
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
Schedule = schedule
Schedule = schedule
}, context.Object);
context.VerifyAll();
}
internal static async Task UpdateAgentSchedule(
AgentService service,
TestConnectionResult connectionResult,
string originalScheduleName,
AgentScheduleInfo schedule)
internal static async Task UpdateAgentSchedule(
AgentService service,
TestConnectionResult connectionResult,
string originalScheduleName,
AgentScheduleInfo schedule)
{
var context = new Mock<RequestContext<AgentScheduleResult>>();
await service.HandleUpdateAgentScheduleRequest(new UpdateAgentScheduleParams()
@@ -299,8 +302,8 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
}
internal static async Task DeleteAgentSchedule(
AgentService service,
TestConnectionResult connectionResult,
AgentService service,
TestConnectionResult connectionResult,
AgentScheduleInfo schedule)
{
var context = new Mock<RequestContext<ResultStatus>>();
@@ -346,5 +349,116 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
var service = new AgentService();
await DeleteAgentJob(service, connectionResult, job);
}
public static async Task<AgentNotebookInfo> SetupNotebookJob(
TestConnectionResult connectionResult,
AgentNotebookInfo notebook = null)
{
var service = new AgentService();
if (notebook == null)
{
notebook = GetTestNotebookInfo("myTestNotebookJob" + Guid.NewGuid().ToString(), "master");
}
string tempNotebookPath = CreateTemplateNotebookFile();
await AgentNotebookHelper.CreateNotebook(
service,
connectionResult.ConnectionInfo.OwnerUri,
notebook,
tempNotebookPath,
ManagementUtils.asRunType(0)
);
var createdNotebook = GetNotebook(connectionResult, notebook.Name);
File.Delete(tempNotebookPath);
return createdNotebook;
}
public static async Task CleanupNotebookJob(TestConnectionResult connectionResult, AgentNotebookInfo notebook)
{
var service = new AgentService();
await AgentNotebookHelper.DeleteNotebook(
service,
connectionResult.ConnectionInfo.OwnerUri,
notebook,
ManagementUtils.asRunType(0)
);
}
public static AgentNotebookInfo GetNotebook(TestConnectionResult connectionResult, string name){
var notebookList = AgentNotebookHelper.GetAgentNotebooks(connectionResult.ConnectionInfo).Result;
foreach(AgentNotebookInfo n in notebookList)
{
if(n.Name == name)
{
return n;
}
}
return null;
}
public static bool VerifyNotebook(TestConnectionResult connectionResult, AgentNotebookInfo notebook)
{
var notebookList = AgentNotebookHelper.GetAgentNotebooks(connectionResult.ConnectionInfo).Result;
foreach (AgentNotebookInfo n in notebookList)
{
if (NotebookObjectEquals(notebook, n))
{
return true;
}
}
return false;
}
static bool NotebookObjectEquals(AgentNotebookInfo expectedNotebook, AgentNotebookInfo actualNotebook)
{
return (
expectedNotebook.Name == actualNotebook.Name
&&
expectedNotebook.Description == actualNotebook.Description
);
}
internal static string CreateTemplateNotebookFile()
{
Assembly assembly = Assembly.GetAssembly(typeof(AgentNotebookTests));
Stream scriptStream = assembly.GetManifestResourceStream(assembly.GetName().Name + ".Agent.NotebookResources.TestNotebook.ipynb");
StreamReader reader = new StreamReader(scriptStream);
string testNotebookString = reader.ReadToEnd();
string tempNotebookPath = System.IO.Path.GetTempFileName().Replace(".tmp", ".ipynb");
File.WriteAllText(tempNotebookPath, testNotebookString);
return tempNotebookPath;
}
internal static AgentNotebookInfo GetTestNotebookInfo(string TestJobName, string TargetDatabase)
{
return new AgentNotebookInfo()
{
Name = TestJobName,
Description = "Test job description",
CurrentExecutionStatus = JobExecutionStatus.Executing,
LastRunOutcome = CompletionResult.InProgress,
CurrentExecutionStep = "Step 1",
Enabled = false,
HasTarget = false,
HasSchedule = false,
HasStep = false,
Runnable = true,
Category = "Cateory 1",
CategoryId = 1,
CategoryType = 1,
LastRun = "today",
NextRun = "tomorrow",
JobId = new Guid().ToString(),
TargetDatabase = TargetDatabase,
Owner = "sa",
ExecuteDatabase = TargetDatabase,
JobSchedules = new AgentScheduleInfo[0],
Alerts = new AgentAlertInfo[0]
};
}
}
}