Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update commands index model #101

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package com.wazuh.commandmanager.index;

import com.wazuh.commandmanager.CommandManagerPlugin;
import com.wazuh.commandmanager.model.Command;
import com.wazuh.commandmanager.model.Document;
import com.wazuh.commandmanager.utils.IndexTemplateUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -53,10 +53,10 @@ public CommandIndex(Client client, ClusterService clusterService, ThreadPool thr
}

/**
* @param command: A Command model object
* @param document: A Command model object
* @return A CompletableFuture with the RestStatus response from the operation
*/
public CompletableFuture<RestStatus> asyncCreate(Command command) {
public CompletableFuture<RestStatus> asyncCreate(Document document) {
CompletableFuture<RestStatus> future = new CompletableFuture<>();
ExecutorService executor = this.threadPool.executor(ThreadPool.Names.WRITE);

Expand All @@ -70,12 +70,12 @@ public CompletableFuture<RestStatus> asyncCreate(Command command) {
);
}

logger.debug("Indexing command {}", command);
logger.debug("Indexing command {}", document);
try {
IndexRequest request = new IndexRequest()
.index(CommandManagerPlugin.COMMAND_MANAGER_INDEX_NAME)
.source(command.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS))
.id(command.getId())
.source(document.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS))
.id(document.getId())
.create(true);
executor.submit(
() -> {
Expand All @@ -89,12 +89,14 @@ public CompletableFuture<RestStatus> asyncCreate(Command command) {
);
} catch (IOException e) {
logger.error(
"Failed to index command with ID {}: {}", command.getId(), e);
"Failed to index command with ID {}: {}", document.getId(), e);
}
return future;
}

/**
*
* @param template_name
* @return
*/
public boolean indexTemplateExists(String template_name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@
* Command's action fields.
*/
public class Action implements ToXContentObject {

public static final String TYPE = "type";
public static final String ACTION = "action";
public static final String NAME = "name";
public static final String ARGS = "args";
public static final String VERSION = "version";
private final String type;
private final String name;
private final List<String> args;
private final String version;

/**
* Default constructor.
*
* @param type action type to be executed on the target,
* @param name action to be executed on the target,
* @param args actual command.
* @param version version of the action.
*/
public Action(String type, List<String> args, String version) {
this.type = type;
public Action(String name, List<String> args, String version) {
this.name = name;
this.args = args;
this.version = version;
}
Expand All @@ -45,16 +45,16 @@ public Action(String type, List<String> args, String version) {
* @throws IOException
*/
public static Action parse(XContentParser parser) throws IOException {
String type = "";
String name = "";
List<Object> args = List.of();
String version = "";

while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
String fieldName = parser.currentName();
parser.nextToken();
switch (fieldName) {
case TYPE:
type = parser.text();
case NAME:
name = parser.text();
break;
case ARGS:
args = parser.list();
Expand All @@ -70,40 +70,13 @@ public static Action parse(XContentParser parser) throws IOException {

// Cast args field Object list to String list
List<String> convertedArgsFields = (List<String>) (List<?>) (args);
return new Action(type, convertedArgsFields, version);
}

/**
* Return action's type field.
*
* @return type
*/
public String getType() {
return this.type;
}

/**
* Returns action's args field.
*
* @return args
*/
public List<String> getArgs() {
return this.args;
}

/**
* Returns action's version field.
*
* @return version
*/
public String getVersion() {
return this.version;
return new Action(name, convertedArgsFields, version);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("action");
builder.field(TYPE, this.type);
builder.startObject(ACTION);
builder.field(NAME, this.name);
builder.field(ARGS, this.args);
builder.field(VERSION, this.version);
return builder.endObject();
Expand All @@ -112,7 +85,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
@Override
public String toString() {
return "Action{" +
"type='" + type + '\'' +
"name='" + name + '\'' +
", args=" + args +
", version='" + version + '\'' +
'}';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package com.wazuh.commandmanager.model;

import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;

import java.io.IOException;
import java.util.List;

/**
* Command's agent fields.
*/
public class Agent implements ToXContentObject {
public static final String AGENT = "agent";
public static final String GROUPS = "groups";
private final List<String> groups;

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

/**
* @param parser
* @return
* @throws IOException
*/
public static Agent parse(XContentParser parser) throws IOException {
List<Object> groups = List.of();

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

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

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(AGENT);
builder.field(GROUPS, this.groups);
return builder.endObject();
}

@Override
public String toString() {
return "Agent{" +
"groups=" + groups +
'}';
}
}
Loading