using Admin.NET.Plugin.AiDOP.DataPlatform.Core; using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Edge; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress; [ApiController, AllowAnonymous, NonUnify] [Route("api/v1/edge")] public sealed class MdpEdgeDeviceController(MdpEdgeDeviceService devices) : ControllerBase { [HttpPost("register"), RequestSizeLimit(12288)] public Task Register(EdgeRegistration input, CancellationToken token) => Execute(async () => Ok(await devices.RegisterAsync(input, token))); [HttpPost("certificate/renew"), RequestSizeLimit(12288)] public Task Renew(EdgeRenewal input, CancellationToken token) => Execute(async () => Ok(await devices.RenewAsync(await DeviceCertificate(token), input, token))); [HttpPost("certificate/confirm"), RequestSizeLimit(1024)] public Task Confirm(CancellationToken token) => Execute(async () => { await devices.ConfirmCertificateAsync(await DeviceCertificate(token), token); return NoContent(); }); [HttpPost("heartbeat"), RequestSizeLimit(4096)] public Task Poll(EdgeReport report, CancellationToken token) => Execute(async () => Ok(await devices.PollAsync(await DeviceCertificate(token), report))); [HttpGet("batches/{id:guid}")] public Task Status(Guid id, CancellationToken token) => Execute(async () => { var receipt = await devices.FindReceiptAsync(await DeviceCertificate(token), id); return receipt is null ? NotFound() : Ok(receipt); }); [HttpPost("batches"), RequestSizeLimit(1500000)] public Task Upload(EdgeBatch batch, CancellationToken token) => Execute(async () => Ok(await devices.ReceiveAsync(await DeviceCertificate(token), batch, token))); private async Task DeviceCertificate(CancellationToken token) => await HttpContext.Connection.GetClientCertificateAsync(token) ?? throw new EdgeContractException("EDGE_DEVICE_FORBIDDEN"); private async Task Execute(Func> action) { if (!Request.IsHttps) return StatusCode(403, new { code = "EDGE_TLS_REQUIRED" }); Response.Headers.CacheControl = "no-store"; try { return await action(); } catch (Exception error) when (error is EdgeContractException or MdpIngestHttpException or IngestContractException or System.Data.Common.DbException or SqlSugarException) { return EdgeFailure(error); } } internal static ObjectResult EdgeFailure(Exception error) { var code = error switch { EdgeContractException edge => edge.Code, MdpIngestHttpException http => http.Code, IngestContractException ingest => ingest.Code.ToString(), _ => "EDGE_STORE_UNAVAILABLE" }; var status = error is MdpIngestHttpException custom ? custom.StatusCode : code switch { "EDGE_DISABLED" or "EDGE_DEVICE_NOT_FOUND" => 404, "EDGE_DEVICE_FORBIDDEN" or "EDGE_ACTIVATION_INVALID" or "EDGE_WRITER_FORBIDDEN" => 403, "EDGE_REPORT_INVALID" or "EDGE_CERT_REQUEST_INVALID" or "EDGE_BATCH_INVALID" or "EDGE_TASK_INVALID" => 400, "EDGE_STORE_UNAVAILABLE" or "EDGE_CA_UNAVAILABLE" => 503, _ => 409 }; return new ObjectResult(new { code }) { StatusCode = status }; } }