Skip to content

Commit

Permalink
Merge pull request #430 from cmu-sei/v8
Browse files Browse the repository at this point in the history
adds endpoints for machine group single add and removes
  • Loading branch information
sei-dupdyke authored Oct 9, 2024
2 parents 32bb34a + 7fde8c7 commit 6253b69
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/Ghosts.Api/Controllers/Api/MachineGroupsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,34 @@ public async Task<IActionResult> PutMachineGroup([FromBody] Group model, Cancell
return Ok(model);
}

/// <summary>
/// Adds a single machine to a machine group
/// </summary>
/// <param name="machineId"></param>
/// <param name="ct">Cancellation Token</param>
/// <param name="groupId"></param>
/// <returns>The updated group</returns>
[SwaggerOperation("MachineGroupsAddMachine")]
[HttpPost("{groupId:int}/{machineId:guid}")]
public async Task<IActionResult> AddMachineToGroup([FromRoute] int groupId, [FromRoute] Guid machineId, CancellationToken ct)
{
return Ok(await _service.AddMachineToGroup(groupId, machineId, ct));
}

/// <summary>
/// Removes a single machine from a machine group
/// </summary>
/// <param name="machineId"></param>
/// <param name="ct">Cancellation Token</param>
/// <param name="groupId"></param>
/// <returns>The updated group</returns>
[SwaggerOperation("MachineGroupsRemoveMachine")]
[HttpDelete("{groupId:int}/{machineId:guid}")]
public async Task<IActionResult> RemoveMachineFromGroup([FromRoute] int groupId, [FromRoute] Guid machineId, CancellationToken ct)
{
return Ok(await _service.RemoveMachineFromGroup(groupId, machineId, ct));
}

/// <summary>
/// Create new group
/// </summary>
Expand Down
26 changes: 26 additions & 0 deletions src/Ghosts.Api/Infrastructure/Services/MachineGroupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public interface IMachineGroupService
Task<int> CreateAsync(Group model, CancellationToken ct);
Task<Group> UpdateAsync(Group model, CancellationToken ct);
Task<int> DeleteAsync(int model, CancellationToken ct);

Task<Group> AddMachineToGroup(int groupId, Guid machineId, CancellationToken ct);
Task<Group> RemoveMachineFromGroup(int groupId, Guid machineId, CancellationToken ct);
Task<List<HistoryTimeline>> GetActivity(int id, int skip, int take, CancellationToken ct);
}

Expand Down Expand Up @@ -108,6 +111,29 @@ public async Task<int> DeleteAsync(int id, CancellationToken ct)

return id;
}

public async Task<Group> AddMachineToGroup(int groupId, Guid machineId, CancellationToken ct)
{
if (!_context.GroupMachines.Any(x => x.GroupId == groupId && x.MachineId == machineId))
{
_context.GroupMachines.Add(new GroupMachine { GroupId = groupId, MachineId = machineId });
await _context.SaveChangesAsync(ct);

}
return await _context.Groups.Include(x => x.GroupMachines).FirstOrDefaultAsync(x => x.Id == groupId, cancellationToken: ct);
}

public async Task<Group> RemoveMachineFromGroup(int groupId, Guid machineId, CancellationToken ct)
{
foreach (var r in
_context.GroupMachines.Where(x => x.GroupId == groupId && x.MachineId == machineId))
{
_context.GroupMachines.Remove(r);
}
await _context.SaveChangesAsync(ct);

return await _context.Groups.Include(x => x.GroupMachines).FirstOrDefaultAsync(x => x.Id == groupId, cancellationToken: ct);
}

public async Task<List<HistoryTimeline>> GetActivity(int id, int skip, int take, CancellationToken ct)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Ghosts.Api/ghosts.api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AssemblyName>ghosts.api</AssemblyName>

<AssemblyVersion>8.0.0.0</AssemblyVersion>
<FileVersion>8.2.404.0</FileVersion>
<FileVersion>8.2.499.0</FileVersion>

<Authors>GHOSTS Development Team for CERT > Software Engineering Institute > Carnegie Mellon University</Authors>
<Company>Carnegie Mellon University</Company>
Expand Down
30 changes: 30 additions & 0 deletions src/ghosts.ui/src/generated/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,36 @@ const endpoints = makeApi([
],
response: Group,
},

{
method: "post",
path: "/api/machinegroups/:id/:machine_id",
alias: "machineGroupsAddMachine",
requestFormat: "json",
parameters: [
{
name: "id",
description: "group id",
type: "Path",
schema: z.string(),
},
{
name: "machine_id",
description: "machine id",
type: "Path",
schema: z.string(),
},
],
response: Group,
},
{
method: "delete",
path: "/api/machinegroups/:id/:machine_id",
alias: "machineGroupsRemoveMachine",
requestFormat: "json",
response: Group,
},

{
method: "delete",
path: "/api/machinegroups/:id",
Expand Down

0 comments on commit 6253b69

Please sign in to comment.