diff options
Diffstat (limited to 'src/marlin.web/Controllers/AgentsController.cs')
| -rw-r--r-- | src/marlin.web/Controllers/AgentsController.cs | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/marlin.web/Controllers/AgentsController.cs b/src/marlin.web/Controllers/AgentsController.cs new file mode 100644 index 0000000..199acf9 --- /dev/null +++ b/src/marlin.web/Controllers/AgentsController.cs @@ -0,0 +1,89 @@ +namespace marlin.web.Controllers; + +using core.models; +using core.util; +using Data; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Models; + +[Route("agents")] +public class AgentsController(ApplicationDbContext dbContext) : Controller +{ + [HttpGet("list")] + public async Task<IActionResult> List(CancellationToken ct) + { + var items = await dbContext + .Agents + .Include(x => x.Jobs.Where(j => j.Status == JobStatus.Active)) + .ToListAsync(ct); + + return this.View(items); + } + + [HttpGet("details/{id}")] + public async Task<IActionResult> Details(Guid id, CancellationToken ct) + { + var item = await dbContext.Agents + .Include(x => x.Jobs.Where(j => j.Status == JobStatus.Active)) + .FirstOrDefaultAsync(x => x.Id == id, ct); + + if (item == null) + return this.NotFound(); + + return this.View(item); + } + + [HttpPost("edit/{id}")] + public async Task<IActionResult> Edit(Guid id, Agent agent, CancellationToken ct) + { + var item = await dbContext.Agents + .FirstOrDefaultAsync(x => x.Id == id, ct); + + if (item == null) + return this.NotFound(); + + item.Name = agent.Name; + item.MaxConcurrentJobs = agent.MaxConcurrentJobs; + + await dbContext.SaveChangesAsync(ct); + + return this.RedirectToAction("Details", new { id }); + } + + [HttpPost("details/{id}/regenerate-token")] + public async Task<IActionResult> RegenerateToken(Guid id, CancellationToken ct) + { + var item = await dbContext.Agents + .FirstOrDefaultAsync(x => x.Id == id, ct); + + if (item == null) + return this.NotFound(); + + item.AuthToken = SecureTokenGenerator.CreateAgentToken(); + + await dbContext.SaveChangesAsync(ct); + + return this.RedirectToAction("Details", new { id }); + } + + [HttpGet("create")] + public IActionResult Create() + { + return this.View(); + } + + [HttpPost("create")] + public async Task<IActionResult> CreatePost(Agent agent, CancellationToken ct) + { + agent.Id = Guid.Empty; + agent.AuthToken = SecureTokenGenerator.CreateAgentToken(); + agent.LastSeen = DateTime.UtcNow; + agent.Capabilities = new CapabilitySet(); + + await dbContext.Agents.AddAsync(agent, ct); + await dbContext.SaveChangesAsync(ct); + + return this.RedirectToAction("Details", new { id = agent.Id }); + } +}
\ No newline at end of file |
