Skip to content

Commit

Permalink
Add ID field to Agent class
Browse files Browse the repository at this point in the history
  • Loading branch information
QU3B1M committed Jan 22, 2025
1 parent 4105917 commit ad9ccd7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@
public class Agent implements ToXContentObject {
public static final String AGENT = "agent";
public static final String GROUPS = "groups";
public static final String ID = "id";
private final String id;
private final List<String> groups;

/**
* Default constructor.
*
* @param groups Agent's groups
*/
public Agent(List<String> groups) {
public Agent(String id, List<String> groups) {
this.id = id;
this.groups = groups;
}

Expand All @@ -47,20 +50,23 @@ public Agent(List<String> groups) {
*/
public static Agent parse(XContentParser parser) throws IOException {
List<Object> groups = List.of();
String id = null;

while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
String fieldName = parser.currentName();
parser.nextToken();
if (fieldName.equals(GROUPS)) {
groups = parser.list();
} else if (fieldName.equals(ID)) {
id = parser.text();
} else {
parser.skipChildren();
}
}

// Cast args field Object list to String list
List<String> convertedGroupFields = (List<String>) (List<?>) (groups);
return new Agent(convertedGroupFields);
return new Agent(id, convertedGroupFields);
}

@Override
Expand All @@ -72,6 +78,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws

@Override
public String toString() {
return "Agent{" + "groups=" + groups + '}';
return "Agent{" + "id=" + id + '\'' + ", groups=" + groups + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ private static Orders commandsToOrders(NodeClient client, List<Command> commands
final Map<String, Object> agentMap =
Search.getNestedObject(hit.getSourceAsMap(), "agent", Map.class);
if (agentMap != null) {
Agent agent = new Agent((List<String>) agentMap.get("groups"));
String agentId = (String) agentMap.get(Agent.ID);
List<String> agentGroups = (List<String>) agentMap.get(Agent.GROUPS);
Agent agent = new Agent(agentId, agentGroups);
agentList.add(agent);
}
}
Expand Down

0 comments on commit ad9ccd7

Please sign in to comment.