Skip to content

Commit

Permalink
Make IPAddress writeable (elastic#101093)
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 authored Oct 27, 2023
1 parent debe882 commit 2b058d6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/changelog/101093.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 101093
summary: Make IPAddress writeable
area: Infra/Scripting
type: bug
issues:
- 101082
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ static TransportVersion def(int id) {
public static final TransportVersion TOO_MANY_SCROLL_CONTEXTS_EXCEPTION_ADDED = def(8_521_00_0);
public static final TransportVersion UNCONTENDED_REGISTER_ANALYSIS_ADDED = def(8_522_00_0);
public static final TransportVersion TRANSFORM_GET_CHECKPOINT_TIMEOUT_ADDED = def(8_523_00_0);
public static final TransportVersion IP_ADDRESS_WRITEABLE = def(8_524_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,16 @@

package org.elasticsearch.script.field;

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 +38,14 @@ 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 2b058d6

Please sign in to comment.