Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit c105423

Browse files
authored
Add maxPerPage to ORM (#3016)
* Add support for maxPerPage in OMR * Fix small bug
1 parent 41fa0a7 commit c105423

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

src/ApiService/ApiService/Functions/AgentCommands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private async Async.Task<HttpResponseData> Get(HttpRequestData req) {
3131
}
3232
var nodeCommand = request.OkV;
3333

34-
var message = await _context.NodeMessageOperations.GetMessage(nodeCommand.MachineId).FirstOrDefaultAsync();
34+
var message = await _context.NodeMessageOperations.GetMessage(nodeCommand.MachineId);
3535
if (message != null) {
3636
var command = message.Message;
3737
var messageId = message.MessageId;

src/ApiService/ApiService/Functions/Node.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private async Async.Task<HttpResponseData> Get(HttpRequestData req) {
4646

4747
var (tasks, messages) = await (
4848
_context.NodeTasksOperations.GetByMachineId(machineId).ToListAsync().AsTask(),
49-
_context.NodeMessageOperations.GetMessage(machineId).ToListAsync().AsTask());
49+
_context.NodeMessageOperations.GetMessages(machineId).ToListAsync().AsTask());
5050

5151
var commands = messages.Select(m => m.Message).ToList();
5252
return await RequestHandling.Ok(req, NodeToNodeSearchResult(node with { Tasks = tasks, Messages = commands }));

src/ApiService/ApiService/onefuzzlib/NodeMessageOperations.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using ApiService.OneFuzzLib.Orm;
1+
using System.Threading.Tasks;
2+
using ApiService.OneFuzzLib.Orm;
23
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;
34

45
namespace Microsoft.OneFuzz.Service;
@@ -14,7 +15,9 @@ public NodeMessage(Guid machineId, NodeCommand message) : this(machineId, NewSor
1415
};
1516

1617
public interface INodeMessageOperations : IOrm<NodeMessage> {
17-
IAsyncEnumerable<NodeMessage> GetMessage(Guid machineId);
18+
IAsyncEnumerable<NodeMessage> GetMessages(Guid machineId);
19+
20+
Async.Task<NodeMessage?> GetMessage(Guid machineId);
1821
Async.Task ClearMessages(Guid machineId);
1922

2023
Async.Task SendMessage(Guid machineId, NodeCommand message, string? messageId = null);
@@ -25,7 +28,7 @@ public class NodeMessageOperations : Orm<NodeMessage>, INodeMessageOperations {
2528
public NodeMessageOperations(ILogTracer log, IOnefuzzContext context)
2629
: base(log, context) { }
2730

28-
public IAsyncEnumerable<NodeMessage> GetMessage(Guid machineId)
31+
public IAsyncEnumerable<NodeMessage> GetMessages(Guid machineId)
2932
=> QueryAsync(Query.PartitionKey(machineId.ToString()));
3033

3134
public async Async.Task ClearMessages(Guid machineId) {
@@ -45,4 +48,7 @@ public async Async.Task SendMessage(Guid machineId, NodeCommand message, string?
4548
_logTracer.WithHttpStatus(r.ErrorV).Error($"failed to insert message with id: {messageId:Tag:MessageId} for machine id: {machineId:Tag:MachineId} message: {message:Tag:Message}");
4649
}
4750
}
51+
52+
public async Task<NodeMessage?> GetMessage(Guid machineId)
53+
=> await QueryAsync(Query.PartitionKey(machineId.ToString()), maxPerPage: 1).FirstOrDefaultAsync();
4854
}

src/ApiService/ApiService/onefuzzlib/orm/Orm.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace ApiService.OneFuzzLib.Orm {
1313
public interface IOrm<T> where T : EntityBase {
1414
Task<TableClient> GetTableClient(string table, ResourceIdentifier? accountId = null);
15-
IAsyncEnumerable<T> QueryAsync(string? filter = null);
15+
IAsyncEnumerable<T> QueryAsync(string? filter = null, int? maxPerPage = null);
1616

1717
Task<T> GetEntityAsync(string partitionKey, string rowKey);
1818
Task<ResultVoid<(HttpStatusCode Status, string Reason)>> Insert(T entity);
@@ -49,14 +49,14 @@ public Orm(ILogTracer logTracer, IOnefuzzContext context) {
4949
_entityConverter = _context.EntityConverter;
5050
}
5151

52-
public async IAsyncEnumerable<T> QueryAsync(string? filter = null) {
52+
public async IAsyncEnumerable<T> QueryAsync(string? filter = null, int? maxPerPage = null) {
5353
var tableClient = await GetTableClient(typeof(T).Name);
5454

5555
if (filter == "") {
5656
filter = null;
5757
}
5858

59-
await foreach (var x in tableClient.QueryAsync<TableEntity>(filter).Select(x => _entityConverter.ToRecord<T>(x))) {
59+
await foreach (var x in tableClient.QueryAsync<TableEntity>(filter: filter, maxPerPage: maxPerPage).Select(x => _entityConverter.ToRecord<T>(x))) {
6060
yield return x;
6161
}
6262
}

src/ApiService/IntegrationTests/AgentCommandsTests.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Net;
1+
using System;
2+
using System.Net;
3+
using FluentAssertions;
24
using IntegrationTests.Fakes;
35
using Microsoft.OneFuzz.Service;
46
using Microsoft.OneFuzz.Service.Functions;
@@ -50,4 +52,32 @@ public async Async.Task AgentAuthorization_IsAccepted() {
5052
var result = await func.Run(TestHttpRequestData.Empty("GET"));
5153
Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode); // BadRequest due to no body, not Unauthorized
5254
}
55+
56+
[Fact]
57+
public async Async.Task AgentCommand_GetsCommand() {
58+
var machineId = Guid.NewGuid();
59+
var messageId = Guid.NewGuid().ToString();
60+
var command = new NodeCommand {
61+
Stop = new StopNodeCommand()
62+
};
63+
await Context.InsertAll(new[] {
64+
new NodeMessage (
65+
machineId,
66+
messageId,
67+
command
68+
),
69+
});
70+
71+
var commandRequest = new NodeCommandGet(machineId);
72+
var auth = new TestEndpointAuthorization(RequestType.Agent, Logger, Context);
73+
var func = new AgentCommands(Logger, auth, Context);
74+
75+
var result = await func.Run(TestHttpRequestData.FromJson("GET", commandRequest));
76+
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
77+
78+
var pendingNodeCommand = BodyAs<PendingNodeCommand>(result);
79+
pendingNodeCommand.Envelope.Should().NotBeNull();
80+
pendingNodeCommand.Envelope?.Command.Should().BeEquivalentTo(command);
81+
pendingNodeCommand.Envelope?.MessageId.Should().Be(messageId);
82+
}
5383
}

0 commit comments

Comments
 (0)