Skip to content

Commit

Permalink
Make IPAddress writeable
Browse files Browse the repository at this point in the history
IPAddress may be returned from scripts and thus needs to be writeable.
This commit makes it writeable as a generic writeable.

closes elastic#101082
  • Loading branch information
rjernst committed Oct 19, 2023
1 parent eb8b752 commit 68e5d5f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ static TransportVersion def(int id) {
public static final TransportVersion PIPELINES_IN_BULK_RESPONSE_ADDED = def(8_519_00_0);
public static final TransportVersion PLUGIN_DESCRIPTOR_STRING_VERSION = def(8_520_00_0);
public static final TransportVersion TOO_MANY_SCROLL_CONTEXTS_EXCEPTION_ADDED = def(8_521_00_0);
public static final TransportVersion IP_ADDRESS_WRITEABLE = def(8_522_00_0);

/*
* STOP! READ THIS FIRST! No, really,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

package org.elasticsearch.script.field;

import org.elasticsearch.TransportVersion;
import org.elasticsearch.TransportVersions;
import org.elasticsearch.common.io.stream.GenericNamedWriteable;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
Expand All @@ -20,7 +25,8 @@
/**
* IP address for use in scripting.
*/
public class IPAddress implements ToXContentObject {
public class IPAddress implements ToXContentObject, GenericNamedWriteable {
static final String NAMED_WRITEABLE_NAME = "IPAddress";
protected final InetAddress address;

IPAddress(InetAddress address) {
Expand All @@ -31,6 +37,14 @@ public IPAddress(String address) {
this.address = InetAddresses.forString(address);
}

public IPAddress(StreamInput input) throws IOException {
this(input.readString());
}

public void writeTo(StreamOutput output) throws IOException {
output.writeString(toString());
}

public boolean isV4() {
return address instanceof Inet4Address;
}
Expand All @@ -49,4 +63,13 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder.value(this.toString());
}

@Override
public String getWriteableName() {
return NAMED_WRITEABLE_NAME;
}

@Override
public TransportVersion getMinimalSupportedVersion() {
return TransportVersions.IP_ADDRESS_WRITEABLE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@

package org.elasticsearch.script.field;

import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.GenericNamedWriteable;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry.Entry;
import org.elasticsearch.test.ESTestCase;

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

import static org.hamcrest.Matchers.equalTo;

public class IPAddressTests extends ESTestCase {

public void testToString() {
Expand All @@ -30,4 +39,12 @@ public void testV6() {
assertFalse(addr4.isV4());
assertTrue(addr4.isV6());
}

public void testWriteable() throws IOException {
var registry = new NamedWriteableRegistry(List.of(new Entry(GenericNamedWriteable.class, IPAddress.NAMED_WRITEABLE_NAME, IPAddress::new)));
var original = new IPAddress("192.168.1.1");
var generic = copyNamedWriteable(original, registry, GenericNamedWriteable.class);
var copied = asInstanceOf(IPAddress.class, generic);
assertThat(copied.toString(), equalTo(original.toString()));
}
}

0 comments on commit 68e5d5f

Please sign in to comment.