using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Logging; using AyaNova.Models; using AyaNova.Api.ControllerHelpers; using AyaNova.Biz; using Newtonsoft.Json.Linq; namespace AyaNova.Api.Controllers { /// /// ServerJob controller /// [ApiController] [ApiVersion("8.0")] [Route("api/v{version:apiVersion}/job-operations")] [Produces("application/json")] [Authorize] public class JobOperationsController : ControllerBase { private readonly AyContext ct; private readonly ILogger log; private readonly ApiServerState serverState; /// /// ctor /// /// /// /// public JobOperationsController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) { ct = dbcontext; log = logger; serverState = apiServerState; } /// /// Get Operations jobs list /// /// List of operations jobs [HttpGet] public async Task List() { if (serverState.IsClosed) return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.ServerJob)) { return StatusCode(403, new ApiNotAuthorizedResponse()); } if (!ModelState.IsValid) { return BadRequest(new ApiErrorResponse(ModelState)); } //Instantiate the business object handler JobOperationsBiz biz = new JobOperationsBiz(ct, UserIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items)); List l = await biz.GetJobListAsync(); return Ok(ApiOkResponse.Response(l)); } /// /// Get current job status for a job /// /// /// A single job's current status [HttpGet("status/{gid}")] public async Task GetJobStatus([FromRoute] Guid gid) { //this is called from things that might be running and have server temporarily locked down (e.g. seeding) //so it should never return an error on closed // if (serverState.IsClosed) // return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); //This is called by the UI to monitor any operation that triggers a job so it really should be available to any logged in user // if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.ServerJob)) // { // return StatusCode(403, new ApiNotAuthorizedResponse()); // } if (!ModelState.IsValid) return BadRequest(new ApiErrorResponse(ModelState)); return Ok(ApiOkResponse.Response(await JobsBiz.GetJobStatusAsync(gid))); } /// /// Get Operations log for a job /// /// /// A single job's log [HttpGet("logs/{gid}")] public async Task GetLogs([FromRoute] Guid gid) { if (serverState.IsClosed) return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); //## NOTE: deliberately do *not* check for authorization as this is called by any bulk operation users may submit via extensions //and the user would need the exact Guid to view a job so not likely they will fish for it in a nefarious way // if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.ServerJob)) // { // return StatusCode(403, new ApiNotAuthorizedResponse()); // } if (!ModelState.IsValid) { return BadRequest(new ApiErrorResponse(ModelState)); } //Instantiate the business object handler JobOperationsBiz biz = new JobOperationsBiz(ct, UserIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items)); List l = await biz.GetJobLogListAsync(gid); return Ok(ApiOkResponse.Response(l)); } /// /// Get Operations log for all jobs /// /// Log for all jobs in system [HttpGet("logs/all-jobs")] public async Task GetAllLogs() { if (serverState.IsClosed) return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.ServerJob)) { return StatusCode(403, new ApiNotAuthorizedResponse()); } if (!ModelState.IsValid) { return BadRequest(new ApiErrorResponse(ModelState)); } //Instantiate the business object handler JobOperationsBiz biz = new JobOperationsBiz(ct, UserIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items)); List l = await biz.GetAllJobsLogsListAsync(); return Ok(ApiOkResponse.Response(l)); } /// /// Trigger a test job that simulates a (30 second) long running operation for testing and ops confirmation /// /// Job id [HttpPost("test-job")] public async Task TestWidgetJob() { if (!serverState.IsOpen) return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.ServerJob)) return StatusCode(403, new ApiNotAuthorizedResponse()); OpsJob j = new OpsJob(); j.Name = "TestJob"; j.JobType = JobType.TestJob; await JobsBiz.AddJobAsync(j); await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.ServerJob, AyaEvent.Created, $"{j.JobType} {j.Name}"), ct); return Accepted(new { JobId = j.GId });//202 accepted } //////////////////////////////////////////////////////////////////////////////////////////////// // EXTENSION BULK JOBS // // /// /// Bulk DELETE list of object id's specified /// /// /// Job Id [HttpPost("bulk-delete")] public async Task BulkDeleteObjects([FromBody] DataListSelection dataListSelection) { if (!serverState.IsOpen) return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); if (!ModelState.IsValid) return BadRequest(new ApiErrorResponse(ModelState)); if (dataListSelection.IsEmpty) return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_REQUIRED, null, "DataListSelection is required")); if (!Authorized.HasDeleteRole(HttpContext.Items, dataListSelection.ObjectType)) return StatusCode(403, new ApiNotAuthorizedResponse()); await dataListSelection.RehydrateIdList(ct, UserRolesFromContext.Roles(HttpContext.Items), log); if (dataListSelection.SelectedRowIds.Length == 0) return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_REQUIRED, null, "List of ids")); var JobName = $"LT:BatchOperation: DELETE on {dataListSelection.ObjectType} ({dataListSelection.SelectedRowIds.LongLength} specified)"; JObject o = JObject.FromObject(new { idList = dataListSelection.SelectedRowIds }); OpsJob j = new OpsJob(); j.Name = JobName; j.ObjectType = dataListSelection.ObjectType; j.JobType = JobType.BulkCoreBizObjectOperation; j.SubType = JobSubType.Delete; j.Exclusive = false; j.JobInfo = o.ToString(); await JobsBiz.AddJobAsync(j); await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.ServerJob, AyaEvent.Created, JobName), ct); return Accepted(new { JobId = j.GId }); } //------------ }//eoc }//eons