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

HLRC XPack Protocol clean up: Licence, Misc #34469

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest;
import org.elasticsearch.protocol.xpack.license.GetLicenseRequest;
import org.elasticsearch.protocol.xpack.license.GetLicenseResponse;
import org.elasticsearch.protocol.xpack.license.PutLicenseRequest;
import org.elasticsearch.protocol.xpack.license.PutLicenseResponse;
import org.elasticsearch.client.license.DeleteLicenseRequest;
import org.elasticsearch.client.license.GetLicenseRequest;
import org.elasticsearch.client.license.GetLicenseResponse;
import org.elasticsearch.client.license.PutLicenseRequest;
import org.elasticsearch.client.license.PutLicenseResponse;

import java.io.IOException;
import java.io.InputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest;
import org.elasticsearch.protocol.xpack.license.GetLicenseRequest;
import org.elasticsearch.protocol.xpack.license.PutLicenseRequest;
import org.elasticsearch.client.license.DeleteLicenseRequest;
import org.elasticsearch.client.license.GetLicenseRequest;
import org.elasticsearch.client.license.PutLicenseRequest;

public class LicenseRequestConverters {
static Request putLicense(PutLicenseRequest putLicenseRequest) {
String endpoint = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_xpack")
.addPathPartAsIs("license")
.addPathPartAsIs("_xpack", "license")
.build();
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
RequestConverters.Params parameters = new RequestConverters.Params(request);
Expand All @@ -45,17 +44,19 @@ static Request putLicense(PutLicenseRequest putLicenseRequest) {

static Request getLicense(GetLicenseRequest getLicenseRequest) {
String endpoint = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_xpack")
.addPathPartAsIs("license")
.addPathPartAsIs("_xpack", "license")
.build();
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
RequestConverters.Params parameters = new RequestConverters.Params(request);
parameters.withLocal(getLicenseRequest.local());
parameters.withLocal(getLicenseRequest.isLocal());
return request;
}

static Request deleteLicense(DeleteLicenseRequest deleteLicenseRequest) {
Request request = new Request(HttpDelete.METHOD_NAME, "/_xpack/license");
String endpoint = new RequestConverters.EndpointBuilder()
.addPathPartAsIs("_xpack", "license")
.build();
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
RequestConverters.Params parameters = new RequestConverters.Params(request);
parameters.withTimeout(deleteLicenseRequest.timeout());
parameters.withMasterTimeout(deleteLicenseRequest.masterNodeTimeout());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -980,9 +980,11 @@ EndpointBuilder addCommaSeparatedPathParts(String[] parts) {
return this;
}

EndpointBuilder addPathPartAsIs(String part) {
if (Strings.hasLength(part)) {
joiner.add(part);
EndpointBuilder addPathPartAsIs(String ... parts) {
for (String part : parts) {
if (Strings.hasLength(part)) {
joiner.add(part);
}
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.elasticsearch.common.unit.TimeValue;

import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds;

/**
* A base request for any requests that supply timeouts.
*
Expand All @@ -28,8 +30,11 @@
*/
public class TimedRequest implements Validatable {

private TimeValue timeout;
private TimeValue masterTimeout;
public static final TimeValue DEFAULT_ACK_TIMEOUT = timeValueSeconds(30);
public static final TimeValue DEFAULT_MASTER_NODE_TIMEOUT = timeValueSeconds(30);

private TimeValue timeout = DEFAULT_ACK_TIMEOUT;
private TimeValue masterTimeout = DEFAULT_MASTER_NODE_TIMEOUT;

public void setTimeout(TimeValue timeout) {
this.timeout = timeout;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
package org.elasticsearch.client;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.protocol.xpack.XPackInfoRequest;
import org.elasticsearch.protocol.xpack.XPackInfoResponse;
import org.elasticsearch.protocol.xpack.XPackUsageRequest;
import org.elasticsearch.protocol.xpack.XPackUsageResponse;
import org.elasticsearch.client.xpack.XPackInfoRequest;
import org.elasticsearch.client.xpack.XPackInfoResponse;
import org.elasticsearch.client.xpack.XPackUsageRequest;
import org.elasticsearch.client.xpack.XPackUsageResponse;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
package org.elasticsearch.client;

import org.apache.http.client.methods.HttpGet;
import org.elasticsearch.protocol.xpack.XPackInfoRequest;
import org.elasticsearch.protocol.xpack.XPackUsageRequest;
import org.elasticsearch.client.xpack.XPackInfoRequest;
import org.elasticsearch.client.xpack.XPackUsageRequest;

import java.util.EnumSet;
import java.util.Locale;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,10 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.protocol.xpack.license;
package org.elasticsearch.client.license;

import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.client.TimedRequest;

public class DeleteLicenseRequest extends TimedRequest {

public class DeleteLicenseRequest extends AcknowledgedRequest<DeleteLicenseRequest> {

@Override
public ActionRequestValidationException validate() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.protocol.xpack;
package org.elasticsearch.client.license;

import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.MasterNodeRequest;
import org.elasticsearch.client.Validatable;

public class XPackUsageRequest extends MasterNodeRequest<XPackUsageRequest> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lolwut... thanks for nothing, git.


@Override
public ActionRequestValidationException validate() {
return null;
public class GetLicenseRequest implements Validatable {

protected boolean local = false;

public GetLicenseRequest() {
}

public boolean isLocal() {
return local;
}

public void setLocal(boolean local) {
this.local = local;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.protocol.xpack.license;
package org.elasticsearch.client.license;

import org.elasticsearch.action.ActionResponse;

public class GetLicenseResponse extends ActionResponse {
public class GetLicenseResponse {

private String license;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,12 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.protocol.xpack.license;

import java.io.IOException;

import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
package org.elasticsearch.client.license;

/**
* Status of an X-Pack license.
*/
public enum LicenseStatus implements Writeable {
public enum LicenseStatus {

ACTIVE("active"),
INVALID("invalid"),
Expand All @@ -43,15 +37,6 @@ public String label() {
return label;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(label);
}

public static LicenseStatus readFrom(StreamInput in) throws IOException {
return fromString(in.readString());
}

public static LicenseStatus fromString(String value) {
switch (value) {
case "active":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.protocol.xpack.license;
package org.elasticsearch.client.license;

import java.util.Locale;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,16 @@
* under the License.
*/

package org.elasticsearch.protocol.xpack.license;
package org.elasticsearch.client.license;

import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.client.TimedRequest;

public class PutLicenseRequest extends AcknowledgedRequest<PutLicenseRequest> {
public class PutLicenseRequest extends TimedRequest {

private String licenseDefinition;
private boolean acknowledge = false;

public PutLicenseRequest() {

}

@Override
public ActionRequestValidationException validate() {
return null;
}

public void setLicenseDefinition(String licenseDefinition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@
* under the License.
*/

package org.elasticsearch.protocol.xpack.license;
package org.elasticsearch.client.license;

import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParseException;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.protocol.xpack.common.ProtocolUtils;
Expand All @@ -42,7 +37,9 @@
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;

public class PutLicenseResponse extends AcknowledgedResponse {
public final class PutLicenseResponse {

private static final ParseField ACKNOWLEDGED = new ParseField("acknowledged");

private static final ConstructingObjectParser<PutLicenseResponse, Void> PARSER = new ConstructingObjectParser<>(
"put_license_response", true, (a, v) -> {
Expand All @@ -58,7 +55,7 @@ public class PutLicenseResponse extends AcknowledgedResponse {
});

static {
PARSER.declareBoolean(constructorArg(), new ParseField("acknowledged"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i actually dont mind having these defined here if we dont use them anywhere else.

PARSER.declareBoolean(constructorArg(), ACKNOWLEDGED);
PARSER.declareString(constructorArg(), new ParseField("license_status"));
PARSER.declareObject(optionalConstructorArg(), (parser, v) -> {
Map<String, String[]> acknowledgeMessages = new HashMap<>();
Expand Down Expand Up @@ -97,6 +94,7 @@ public class PutLicenseResponse extends AcknowledgedResponse {
new ParseField("acknowledge"));
}

private boolean acknowledged;
private LicensesStatus status;
private Map<String, String[]> acknowledgeMessages;
private String acknowledgeHeader;
Expand All @@ -110,12 +108,16 @@ public PutLicenseResponse(boolean acknowledged, LicensesStatus status) {

public PutLicenseResponse(boolean acknowledged, LicensesStatus status, String acknowledgeHeader,
Map<String, String[]> acknowledgeMessages) {
super(acknowledged);
this.acknowledged = acknowledged;
this.status = status;
this.acknowledgeHeader = acknowledgeHeader;
this.acknowledgeMessages = acknowledgeMessages;
}

public boolean isAcknowledged() {
return acknowledged;
}

public LicensesStatus status() {
return status;
}
Expand All @@ -128,62 +130,6 @@ public String acknowledgeHeader() {
return acknowledgeHeader;
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
status = LicensesStatus.fromId(in.readVInt());
acknowledgeHeader = in.readOptionalString();
int size = in.readVInt();
Map<String, String[]> acknowledgeMessages = new HashMap<>(size);
for (int i = 0; i < size; i++) {
String feature = in.readString();
int nMessages = in.readVInt();
String[] messages = new String[nMessages];
for (int j = 0; j < nMessages; j++) {
messages[j] = in.readString();
}
acknowledgeMessages.put(feature, messages);
}
this.acknowledgeMessages = acknowledgeMessages;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeVInt(status.id());
out.writeOptionalString(acknowledgeHeader);
out.writeVInt(acknowledgeMessages.size());
for (Map.Entry<String, String[]> entry : acknowledgeMessages.entrySet()) {
out.writeString(entry.getKey());
out.writeVInt(entry.getValue().length);
for (String message : entry.getValue()) {
out.writeString(message);
}
}
}

@Override
protected void addCustomFields(XContentBuilder builder, Params params) throws IOException {
builder.field("license_status", status.toString());
if (!acknowledgeMessages.isEmpty()) {
builder.startObject("acknowledge");
builder.field("message", acknowledgeHeader);
for (Map.Entry<String, String[]> entry : acknowledgeMessages.entrySet()) {
builder.startArray(entry.getKey());
for (String message : entry.getValue()) {
builder.value(message);
}
builder.endArray();
}
builder.endObject();
}
}

@Override
public String toString() {
return Strings.toString(this, true, true);
}

public static PutLicenseResponse fromXContent(XContentParser parser) throws IOException {
return PARSER.parse(parser, null);
}
Expand All @@ -205,5 +151,4 @@ public int hashCode() {
return Objects.hash(super.hashCode(), status, ProtocolUtils.hashCode(acknowledgeMessages), acknowledgeHeader);
}


}
Loading