summaryrefslogtreecommitdiff
path: root/src/marlin.web/Controllers/AgentsController.cs
blob: 199acf9e9e97d6b29750d47090dad48ac65a37a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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 });
    }
}