-
Notifications
You must be signed in to change notification settings - Fork 25.2k
add start trial API to HLRC #33406
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
add start trial API to HLRC #33406
Changes from all commits
5a42c26
8f2fa42
faba897
5df4272
07554dd
74afddf
20f8c22
a84d80b
a81882d
310fc41
807a0ae
7288587
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.client.license; | ||
|
||
import org.elasticsearch.client.Validatable; | ||
import org.elasticsearch.common.Nullable; | ||
|
||
public class StartTrialRequest implements Validatable { | ||
|
||
private final boolean acknowledge; | ||
private final String licenseType; | ||
|
||
public StartTrialRequest() { | ||
this(false); | ||
} | ||
|
||
public StartTrialRequest(boolean acknowledge) { | ||
this(acknowledge, null); | ||
} | ||
|
||
public StartTrialRequest(boolean acknowledge, @Nullable String licenseType) { | ||
this.acknowledge = acknowledge; | ||
this.licenseType = licenseType; | ||
} | ||
|
||
public boolean isAcknowledge() { | ||
return acknowledge; | ||
} | ||
|
||
public String getLicenseType() { | ||
return licenseType; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.client.license; | ||
|
||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.collect.Tuple; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.XContentParseException; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; | ||
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; | ||
|
||
public class StartTrialResponse { | ||
|
||
private static final ConstructingObjectParser<StartTrialResponse, Void> PARSER = new ConstructingObjectParser<>( | ||
"start_trial_response", | ||
true, | ||
(Object[] arguments, Void aVoid) -> { | ||
final boolean acknowledged = (boolean) arguments[0]; | ||
final boolean trialWasStarted = (boolean) arguments[1]; | ||
final String licenseType = (String) arguments[2]; | ||
final String errorMessage = (String) arguments[3]; | ||
|
||
@SuppressWarnings("unchecked") | ||
final Tuple<String, Map<String, String[]>> acknowledgeDetails = (Tuple<String, Map<String, String[]>>) arguments[4]; | ||
final String acknowledgeHeader; | ||
final Map<String, String[]> acknowledgeMessages; | ||
|
||
if (acknowledgeDetails != null) { | ||
acknowledgeHeader = acknowledgeDetails.v1(); | ||
acknowledgeMessages = acknowledgeDetails.v2(); | ||
} else { | ||
acknowledgeHeader = null; | ||
acknowledgeMessages = null; | ||
} | ||
|
||
return new StartTrialResponse(acknowledged, trialWasStarted, licenseType, errorMessage, acknowledgeHeader, | ||
acknowledgeMessages); | ||
} | ||
); | ||
|
||
static { | ||
PARSER.declareBoolean(constructorArg(), new ParseField("acknowledged")); | ||
PARSER.declareBoolean(constructorArg(), new ParseField("trial_was_started")); | ||
PARSER.declareString(optionalConstructorArg(), new ParseField("type")); | ||
PARSER.declareString(optionalConstructorArg(), new ParseField("error_message")); | ||
// todo consolidate this parsing with the parsing in PutLicenseResponse | ||
PARSER.declareObject(optionalConstructorArg(), (parser, aVoid) -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont know a ton about this data structure here, but is it possible to use the parser's built in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so, I'm not super knowledgeable about the parser facility. I'll give it a shot There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like It's a little tricky because the object looks like this, where the header ("message") is parsed as part of the map but is its own particular field that we pull out {
"acknowledge": {
"message": "you need to acknowledge blah blah",
"security": [ "security won't be enabled blah blah" ],
"watcher": [ "another acknowledge message "],
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HAH, ill have a look. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that its a bit nicer, given that you cant actually parse something incorrectly. Sure its manipulating a bunch of k/v stuff in that map, but the actual parsing logic is way trappier and more subject to fail if slightly messed up. mucking with a map of list of lists is pretty low from a trappy perspective. |
||
final Map<String, String[]> acknowledgeMessages = new HashMap<>(); | ||
String message = null; | ||
|
||
final Map<String, Object> parsedMap = parser.map(); | ||
for (Map.Entry<String, Object> entry : parsedMap.entrySet()) { | ||
if (entry.getKey().equals("message")) { | ||
if (entry.getValue() instanceof String) { | ||
message = (String) entry.getValue(); | ||
} else { | ||
throw new XContentParseException(parser.getTokenLocation(), "unexpected acknowledgement header type"); | ||
} | ||
} else { | ||
if (entry.getValue() instanceof List) { | ||
final List<String> messageStrings = new ArrayList<>(); | ||
@SuppressWarnings("unchecked") | ||
final List<Object> messageObjects = (List<Object>) entry.getValue(); | ||
for (Object messageObject : messageObjects) { | ||
if (messageObject instanceof String) { | ||
messageStrings.add((String) messageObject); | ||
} else { | ||
throw new XContentParseException(parser.getTokenLocation(), "expected text in acknowledgement message"); | ||
} | ||
} | ||
|
||
acknowledgeMessages.put(entry.getKey(), messageStrings.toArray(new String[messageStrings.size()])); | ||
} else { | ||
throw new XContentParseException(parser.getTokenLocation(), "unexpected acknowledgement message type"); | ||
} | ||
} | ||
} | ||
|
||
if (message == null) { | ||
throw new XContentParseException(parser.getTokenLocation(), "expected acknowledgement header"); | ||
} | ||
|
||
return new Tuple<>(message, acknowledgeMessages); | ||
|
||
}, new ParseField("acknowledge")); | ||
} | ||
|
||
public static StartTrialResponse fromXContent(XContentParser parser) throws IOException { | ||
return PARSER.apply(parser, null); | ||
} | ||
|
||
private final boolean acknowledged; | ||
private final boolean trialWasStarted; | ||
private final String licenseType; | ||
private final String errorMessage; | ||
private final String acknowledgeHeader; | ||
private final Map<String, String[]> acknowledgeMessages; | ||
|
||
public StartTrialResponse(boolean acknowledged, | ||
boolean trialWasStarted, | ||
String licenseType, | ||
String errorMessage, | ||
String acknowledgeHeader, | ||
Map<String, String[]> acknowledgeMessages) { | ||
|
||
this.acknowledged = acknowledged; | ||
this.trialWasStarted = trialWasStarted; | ||
this.licenseType = licenseType; | ||
this.errorMessage = errorMessage; | ||
this.acknowledgeHeader = acknowledgeHeader; | ||
this.acknowledgeMessages = acknowledgeMessages; | ||
} | ||
|
||
/** | ||
* Returns true if the request that corresponds to this response acknowledged license changes that would occur as a result of starting | ||
* a trial license | ||
*/ | ||
public boolean isAcknowledged() { | ||
return acknowledged; | ||
} | ||
|
||
/** | ||
* Returns true if a trial license was started as a result of the request corresponding to this response. Returns false if the cluster | ||
* did not start a trial, or a trial had already been started before the corresponding request was made | ||
*/ | ||
public boolean isTrialWasStarted() { | ||
return trialWasStarted; | ||
} | ||
|
||
/** | ||
* If a trial license was started as a result of the request corresponding to this response (see {@link #isTrialWasStarted()}) then | ||
* returns the type of license that was started on the cluster. Returns null otherwise | ||
*/ | ||
public String getLicenseType() { | ||
return licenseType; | ||
} | ||
|
||
/** | ||
* If a trial license was not started as a result of the request corresponding to this response (see {@link #isTrialWasStarted()} then | ||
* returns a brief message explaining why the trial could not be started. Returns false otherwise | ||
*/ | ||
public String getErrorMessage() { | ||
return errorMessage; | ||
} | ||
|
||
/** | ||
* If the request corresponding to this response did not acknowledge licensing changes that would result from starting a trial license | ||
* (see {@link #isAcknowledged()}), returns a message describing how the user must acknowledge licensing changes as a result of | ||
* such a request. Returns null otherwise | ||
*/ | ||
public String getAcknowledgeHeader() { | ||
return acknowledgeHeader; | ||
} | ||
|
||
/** | ||
* If the request corresponding to this response did not acknowledge licensing changes that would result from starting a trial license | ||
* (see {@link #isAcknowledged()}, returns a map. The map's keys are names of commercial Elasticsearch features, and their values are | ||
* messages about how those features will be affected by licensing changes as a result of starting a trial license | ||
*/ | ||
public Map<String, String[]> getAcknowledgeMessages() { | ||
return acknowledgeMessages; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks great now, ty for cleaning these up as well.