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

[Backport main] adding BWC for connector config field #2185

Merged
merged 1 commit into from
Mar 8, 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 @@ -7,6 +7,7 @@

import lombok.Builder;
import lombok.Data;
import org.opensearch.Version;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;
Expand Down Expand Up @@ -45,7 +46,7 @@ public class MLCreateConnectorInput implements ToXContentObject, Writeable {
public static final String ACCESS_MODE_FIELD = "access_mode";
public static final String DRY_RUN_FIELD = "dry_run";


private static final Version MINIMAL_SUPPORTED_VERSION_FOR_CLIENT_CONFIG = Version.V_2_13_0;

public static final String DRY_RUN_CONNECTOR_NAME = "dryRunConnector";

Expand Down Expand Up @@ -226,6 +227,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws

@Override
public void writeTo(StreamOutput output) throws IOException {
Version streamOutputVersion = output.getVersion();
output.writeOptionalString(name);
output.writeOptionalString(description);
output.writeOptionalString(version);
Expand Down Expand Up @@ -266,16 +268,18 @@ public void writeTo(StreamOutput output) throws IOException {
}
output.writeBoolean(dryRun);
output.writeBoolean(updateConnector);
if (connectorClientConfig != null) {
output.writeBoolean(true);
connectorClientConfig.writeTo(output);
} else {
output.writeBoolean(false);
if (streamOutputVersion.onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_CLIENT_CONFIG)) {
if (connectorClientConfig != null) {
output.writeBoolean(true);
connectorClientConfig.writeTo(output);
} else {
output.writeBoolean(false);
}
}

}

public MLCreateConnectorInput(StreamInput input) throws IOException {
Version streamInputVersion = input.getVersion();
name = input.readOptionalString();
description = input.readOptionalString();
version = input.readOptionalString();
Expand All @@ -302,8 +306,11 @@ public MLCreateConnectorInput(StreamInput input) throws IOException {
}
dryRun = input.readBoolean();
updateConnector = input.readBoolean();
if (input.readBoolean()) {
this.connectorClientConfig = new ConnectorClientConfig(input);
if (streamInputVersion.onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_CLIENT_CONFIG)) {
if (input.readBoolean()) {
this.connectorClientConfig = new ConnectorClientConfig(input);
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.opensearch.Version;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
Expand Down Expand Up @@ -218,6 +219,77 @@ public void readInputStream_SuccessWithNullFields() throws IOException {
});
}

@Test
public void testBuilder_NullActions_ShouldNotThrowException() {
// Actions can be null for a connector without any specific actions defined.
MLCreateConnectorInput input = MLCreateConnectorInput.builder()
.name("test_connector_name")
.description("this is a test connector")
.version("1")
.protocol("http")
.parameters(Map.of("input", "test input value"))
.credential(Map.of("key", "test_key_value"))
.actions(null) // Setting actions to null
.access(AccessMode.PUBLIC)
.backendRoles(Arrays.asList("role1", "role2"))
.addAllBackendRoles(false)
.build();

assertNull(input.getActions());
}

@Test
public void testParse_MissingNameField_ShouldThrowException() throws IOException {
String jsonMissingName = "{\"description\":\"this is a test connector\",\"version\":\"1\",\"protocol\":\"http\"}";
XContentParser parser = createParser(jsonMissingName);

exceptionRule.expect(IllegalArgumentException.class);
exceptionRule.expectMessage("Connector name is null");

MLCreateConnectorInput.parse(parser);
}

@Test
public void testWriteToVersionCompatibility() throws IOException {
MLCreateConnectorInput input = mlCreateConnectorInput; // Assuming mlCreateConnectorInput is already initialized

// Simulate an older version of OpenSearch that does not support connectorClientConfig
Version oldVersion = Version.fromString("2.12.0"); // Change this as per your old version
BytesStreamOutput output = new BytesStreamOutput();
output.setVersion(oldVersion);

input.writeTo(output);

StreamInput streamInput = output.bytes().streamInput();
streamInput.setVersion(oldVersion);

MLCreateConnectorInput deserializedInput = new MLCreateConnectorInput(streamInput);

// The ConnectorClientConfig should be null as it's not supported in the older version
assertNull(deserializedInput.getConnectorClientConfig());
}

@Test
public void testDryRunConnectorInput_IgnoreValidation() {
MLCreateConnectorInput input = MLCreateConnectorInput.builder()
.dryRun(true) // Set dryRun to true
.build();

// No exception for missing mandatory fields when dryRun is true
assertTrue(input.isDryRun());
assertNull(input.getName()); // Name is not set, but no exception due to dryRun
}

// Helper method to create XContentParser from a JSON string
private XContentParser createParser(String jsonString) throws IOException {
XContentParser parser = XContentType.JSON.xContent().createParser(
new NamedXContentRegistry(new SearchModule(Settings.EMPTY, Collections.emptyList()).getNamedXContents()),
LoggingDeprecationHandler.INSTANCE,
jsonString);
parser.nextToken(); // Move to the first token
return parser;
}

private void testParseFromJsonString(String expectedInputString, Consumer<MLCreateConnectorInput> verify) throws Exception {
XContentParser parser = XContentType.JSON.xContent().createParser(new NamedXContentRegistry(new SearchModule(Settings.EMPTY,
Collections.emptyList()).getNamedXContents()), LoggingDeprecationHandler.INSTANCE, expectedInputString);
Expand Down
Loading