-
Notifications
You must be signed in to change notification settings - Fork 24.7k
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: Add support for XPack Post Start Basic Licence API #33606
Merged
vladimirdolzhenko
merged 16 commits into
elastic:master
from
vladimirdolzhenko:hlrc_post_start_basic
Oct 16, 2018
Merged
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
71d6de1
HLRC: Add support for XPack Post Start Basic Licence API
5f00b7f
drop `post` verb
625bd66
Merge remote-tracking branch 'remotes/origin/master' into hlrc_post_s…
8dfb03d
don't touch xpack / server
9430892
drop `post` verb; drop unused functionality; polished doc
7fde9be
added LicenseRequestConvertersTests
c4ab5de
added LicensingIT
2320bd8
Merge remote-tracking branch 'remotes/origin/master' into hlrc_post_s…
4015b25
Merge remote-tracking branch 'remotes/origin/master' into hlrc_post_s…
31ec2b8
Merge remote-tracking branch 'remotes/origin/master' into hlrc_post_s…
ccbc485
TimedRequest rocks!
9519f21
Merge remote-tracking branch 'remotes/origin/master' into hlrc_post_s…
9c64a65
Merge remote-tracking branch 'remotes/origin/master' into hlrc_post_s…
8539641
added default value for timeout
34ed1f3
muted tests those change licence to basic - has to wait for HLRC star…
a6adade
add roll-back license
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartBasicRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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.action.ActionRequestValidationException; | ||
import org.elasticsearch.action.support.master.AcknowledgedRequest; | ||
|
||
public class StartBasicRequest extends AcknowledgedRequest<StartBasicRequest> { | ||
private final boolean acknowledge; | ||
|
||
public StartBasicRequest() { | ||
this(false); | ||
} | ||
|
||
public StartBasicRequest(boolean acknowledge) { | ||
this.acknowledge = acknowledge; | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
} | ||
|
||
public boolean isAcknowledge() { | ||
return acknowledge; | ||
} | ||
} | ||
|
168 changes: 168 additions & 0 deletions
168
...nt/rest-high-level/src/main/java/org/elasticsearch/client/license/StartBasicResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* | ||
* 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 org.elasticsearch.rest.RestStatus; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; | ||
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; | ||
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; | ||
|
||
public class StartBasicResponse { | ||
|
||
private static final ConstructingObjectParser<StartBasicResponse, Void> PARSER = new ConstructingObjectParser<>( | ||
"start_basic_response", true, (a, v) -> { | ||
boolean basicWasStarted = (Boolean) a[0]; | ||
String errorMessage = (String) a[1]; | ||
|
||
if (basicWasStarted) { | ||
return new StartBasicResponse(StartBasicResponse.Status.GENERATED_BASIC); | ||
} | ||
StartBasicResponse.Status status = StartBasicResponse.Status.fromErrorMessage(errorMessage); | ||
@SuppressWarnings("unchecked") Tuple<String, Map<String, String[]>> acknowledgements = (Tuple<String, Map<String, String[]>>) a[2]; | ||
return new StartBasicResponse(status, acknowledgements.v2(), acknowledgements.v1()); | ||
}); | ||
|
||
static { | ||
PARSER.declareBoolean(constructorArg(), new ParseField("basic_was_started")); | ||
PARSER.declareString(optionalConstructorArg(), new ParseField("error_message")); | ||
PARSER.declareObject(optionalConstructorArg(), (parser, v) -> { | ||
Map<String, String[]> acknowledgeMessages = new HashMap<>(); | ||
String message = null; | ||
XContentParser.Token token; | ||
String currentFieldName = null; | ||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { | ||
if (token == XContentParser.Token.FIELD_NAME) { | ||
currentFieldName = parser.currentName(); | ||
} else { | ||
if (currentFieldName == null) { | ||
throw new XContentParseException(parser.getTokenLocation(), "expected message header or acknowledgement"); | ||
} | ||
if (new ParseField("message").getPreferredName().equals(currentFieldName)) { | ||
ensureExpectedToken(XContentParser.Token.VALUE_STRING, token, parser::getTokenLocation); | ||
message = parser.text(); | ||
} else { | ||
if (token != XContentParser.Token.START_ARRAY) { | ||
throw new XContentParseException(parser.getTokenLocation(), "unexpected acknowledgement type"); | ||
} | ||
List<String> acknowledgeMessagesList = new ArrayList<>(); | ||
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { | ||
ensureExpectedToken(XContentParser.Token.VALUE_STRING, token, parser::getTokenLocation); | ||
acknowledgeMessagesList.add(parser.text()); | ||
} | ||
acknowledgeMessages.put(currentFieldName, acknowledgeMessagesList.toArray(new String[0])); | ||
} | ||
} | ||
} | ||
return new Tuple<>(message, acknowledgeMessages); | ||
}, | ||
new ParseField("acknowledge")); | ||
} | ||
|
||
private Map<String, String[]> acknowledgeMessages; | ||
private String acknowledgeMessage; | ||
|
||
enum Status { | ||
GENERATED_BASIC(true, null, RestStatus.OK), | ||
ALREADY_USING_BASIC(false, "Operation failed: Current license is basic.", RestStatus.FORBIDDEN), | ||
NEED_ACKNOWLEDGEMENT(false, "Operation failed: Needs acknowledgement.", RestStatus.OK); | ||
|
||
private final boolean isBasicStarted; | ||
private final String errorMessage; | ||
private final RestStatus restStatus; | ||
|
||
Status(boolean isBasicStarted, String errorMessage, RestStatus restStatus) { | ||
this.isBasicStarted = isBasicStarted; | ||
this.errorMessage = errorMessage; | ||
this.restStatus = restStatus; | ||
} | ||
|
||
String getErrorMessage() { | ||
return errorMessage; | ||
} | ||
|
||
boolean isBasicStarted() { | ||
return isBasicStarted; | ||
} | ||
|
||
static StartBasicResponse.Status fromErrorMessage(final String errorMessage) { | ||
final StartBasicResponse.Status[] values = StartBasicResponse.Status.values(); | ||
for (StartBasicResponse.Status status : values) { | ||
if (Objects.equals(status.errorMessage, errorMessage)) { | ||
return status; | ||
} | ||
} | ||
throw new IllegalArgumentException("No status for error message ['" + errorMessage + "']"); | ||
} | ||
} | ||
|
||
private StartBasicResponse.Status status; | ||
|
||
public StartBasicResponse() { | ||
} | ||
|
||
StartBasicResponse(StartBasicResponse.Status status) { | ||
this(status, Collections.emptyMap(), null); | ||
} | ||
|
||
StartBasicResponse(StartBasicResponse.Status status, | ||
Map<String, String[]> acknowledgeMessages, String acknowledgeMessage) { | ||
this.status = status; | ||
this.acknowledgeMessages = acknowledgeMessages; | ||
this.acknowledgeMessage = acknowledgeMessage; | ||
} | ||
|
||
public boolean isAcknowledged() { | ||
return status != StartBasicResponse.Status.NEED_ACKNOWLEDGEMENT; | ||
} | ||
|
||
public boolean isBasicStarted() { | ||
return status.isBasicStarted; | ||
} | ||
|
||
public String getErrorMessage() { | ||
return status.errorMessage; | ||
} | ||
|
||
public String getAcknowledgeMessage() { | ||
return acknowledgeMessage; | ||
} | ||
|
||
public Map<String, String[]> getAcknowledgeMessages() { | ||
return acknowledgeMessages; | ||
} | ||
|
||
public static StartBasicResponse fromXContent(XContentParser parser) throws IOException { | ||
return PARSER.parse(parser, null); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
...rest-high-level/src/test/java/org/elasticsearch/client/LicenseRequestConvertersTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* 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; | ||
|
||
import org.apache.http.client.methods.HttpPost; | ||
import org.elasticsearch.action.support.master.AcknowledgedRequest; | ||
import org.elasticsearch.client.license.StartBasicRequest; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.elasticsearch.client.RequestConvertersTests.setRandomMasterTimeout; | ||
import static org.elasticsearch.client.RequestConvertersTests.setRandomTimeout; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.nullValue; | ||
|
||
public class LicenseRequestConvertersTests extends ESTestCase { | ||
public void testStartBasic() { | ||
final boolean acknowledge = randomBoolean(); | ||
StartBasicRequest startBasicRequest = new StartBasicRequest(acknowledge); | ||
Map<String, String> expectedParams = new HashMap<>(); | ||
if (acknowledge) { | ||
expectedParams.put("acknowledge", Boolean.TRUE.toString()); | ||
} | ||
|
||
setRandomTimeout(startBasicRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); | ||
setRandomMasterTimeout(startBasicRequest, expectedParams); | ||
Request request = LicenseRequestConverters.startBasic(startBasicRequest); | ||
|
||
assertThat(request.getMethod(), equalTo(HttpPost.METHOD_NAME)); | ||
assertThat(request.getEndpoint(), equalTo("/_xpack/license/start_basic")); | ||
assertThat(request.getParameters(), equalTo(expectedParams)); | ||
assertThat(request.getEntity(), is(nullValue())); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You can leave this out if you are not using it
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.
with ccbc485
StartBasicRequest
extendsTimedRequest
, added couple test routines for that