Skip to content

Commit

Permalink
Add delete alias to the HLRC (elastic#48819)
Browse files Browse the repository at this point in the history
The delete alias call is a rest only API call, but should still be added
to the rest client. This commit adds it as well as relevant tests.

Ref elastic#47678
  • Loading branch information
hub-cap authored Nov 11, 2019
1 parent c868a11 commit 7115d3e
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.elasticsearch.client.indices.CloseIndexResponse;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.DeleteAliasRequest;
import org.elasticsearch.client.indices.FreezeIndexRequest;
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
import org.elasticsearch.client.indices.GetFieldMappingsResponse;
Expand Down Expand Up @@ -1062,4 +1063,28 @@ public Cancellable reloadAnalyzersAsync(ReloadAnalyzersRequest request, RequestO
return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::reloadAnalyzers, options,
ReloadAnalyzersResponse::fromXContent, listener, emptySet());
}

/**
* Synchronously calls the delete alias api
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
*/
public org.elasticsearch.client.core.AcknowledgedResponse deleteAlias(DeleteAliasRequest request,
RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::deleteAlias, options,
org.elasticsearch.client.core.AcknowledgedResponse::fromXContent, emptySet());
}

/**
* Asynchronously calls the delete alias api
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion
* @return cancellable that may be used to cancel the request
*/
public Cancellable deleteAliasAsync(DeleteAliasRequest request, RequestOptions options,
ActionListener<org.elasticsearch.client.core.AcknowledgedResponse> listener) {
return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::deleteAlias, options,
org.elasticsearch.client.core.AcknowledgedResponse::fromXContent, listener, emptySet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.elasticsearch.client.indices.AnalyzeRequest;
import org.elasticsearch.client.indices.CloseIndexRequest;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.DeleteAliasRequest;
import org.elasticsearch.client.indices.FreezeIndexRequest;
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
Expand Down Expand Up @@ -483,4 +484,17 @@ static Request reloadAnalyzers(ReloadAnalyzersRequest reloadAnalyzersRequest) {
request.addParameters(parameters.asMap());
return request;
}

static Request deleteAlias(DeleteAliasRequest deleteAliasRequest) {
String endpoint = new RequestConverters.EndpointBuilder()
.addPathPart(deleteAliasRequest.getIndex())
.addPathPartAsIs("_alias")
.addPathPart(deleteAliasRequest.getAlias()).build();
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
RequestConverters.Params parameters = new RequestConverters.Params();
parameters.withTimeout(deleteAliasRequest.timeout());
parameters.withMasterTimeout(deleteAliasRequest.masterNodeTimeout());
request.addParameters(parameters.asMap());
return request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.indices;

import org.elasticsearch.client.TimedRequest;

public class DeleteAliasRequest extends TimedRequest {

private final String index;
private final String alias;

public DeleteAliasRequest(String index, String alias) {
this.index = index;
this.alias = alias;
}

public String getIndex() {
return index;
}

public String getAlias() {
return alias;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.elasticsearch.client.indices.CloseIndexResponse;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.DeleteAliasRequest;
import org.elasticsearch.client.indices.FreezeIndexRequest;
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
import org.elasticsearch.client.indices.GetFieldMappingsResponse;
Expand Down Expand Up @@ -1550,4 +1551,32 @@ public void testReloadAnalyzer() throws IOException {
assertNotNull(reloadResponse.shards());
assertTrue(reloadResponse.getReloadedDetails().containsKey("test"));
}

public void testDeleteAlias() throws IOException {
String index = "test";
createIndex(index, Settings.EMPTY);

String alias = "alias";
String alias2 = "alias2";
IndicesAliasesRequest aliasesAddRemoveRequest = new IndicesAliasesRequest();
aliasesAddRemoveRequest.addAliasAction(new AliasActions(AliasActions.Type.ADD).indices(index).alias(alias));
aliasesAddRemoveRequest.addAliasAction(new AliasActions(AliasActions.Type.ADD).indices(index).alias(alias + "2"));
AcknowledgedResponse aliasResponse = execute(aliasesAddRemoveRequest, highLevelClient().indices()::updateAliases,
highLevelClient().indices()::updateAliasesAsync);
assertTrue(aliasResponse.isAcknowledged());
assertThat(aliasExists(alias), equalTo(true));
assertThat(aliasExists(alias2), equalTo(true));
assertThat(aliasExists(index, alias), equalTo(true));
assertThat(aliasExists(index, alias2), equalTo(true));

DeleteAliasRequest request = new DeleteAliasRequest(index, alias);
org.elasticsearch.client.core.AcknowledgedResponse aliasDeleteResponse = execute(request,
highLevelClient().indices()::deleteAlias,
highLevelClient().indices()::deleteAliasAsync);

assertThat(aliasExists(alias), equalTo(false));
assertThat(aliasExists(alias2), equalTo(true));
assertThat(aliasExists(index, alias), equalTo(false));
assertThat(aliasExists(index, alias2), equalTo(true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.elasticsearch.client.indices.AnalyzeRequest;
import org.elasticsearch.client.indices.CloseIndexRequest;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.DeleteAliasRequest;
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
Expand Down Expand Up @@ -934,4 +935,18 @@ public void testReloadAnalyzers() {
Assert.assertThat(request.getParameters(), equalTo(expectedParams));
Assert.assertThat(request.getEntity(), nullValue());
}

public void testDeleteAlias() {
DeleteAliasRequest deleteAliasRequest = new DeleteAliasRequest(randomAlphaOfLength(4), randomAlphaOfLength(4));

Map<String, String> expectedParams = new HashMap<>();
RequestConvertersTests.setRandomMasterTimeout(deleteAliasRequest, expectedParams);
RequestConvertersTests.setRandomTimeout(deleteAliasRequest, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);

Request request = IndicesRequestConverters.deleteAlias(deleteAliasRequest);
Assert.assertThat(request.getMethod(), equalTo(HttpDelete.METHOD_NAME));
Assert.assertThat(request.getEndpoint(), equalTo("/" + deleteAliasRequest.getIndex() + "/_alias/" + deleteAliasRequest.getAlias()));
Assert.assertThat(request.getParameters(), equalTo(expectedParams));
Assert.assertThat(request.getEntity(), nullValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,6 @@ public void testApiNamingConventions() throws Exception {
"create",
"get_script_context",
"get_source",
"indices.delete_alias",
"indices.exists_type",
"indices.get_upgrade",
"indices.put_alias",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.elasticsearch.client.indices.CloseIndexResponse;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.DeleteAliasRequest;
import org.elasticsearch.client.indices.DetailAnalyzeResponse;
import org.elasticsearch.client.indices.FreezeIndexRequest;
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
Expand Down Expand Up @@ -2880,4 +2881,87 @@ public void onFailure(Exception e) {
// end::reload-analyzers-notfound
}
}

@SuppressWarnings("unused")
public void testDeleteAlias() throws Exception {
RestHighLevelClient client = highLevelClient();

{
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index1"), RequestOptions.DEFAULT);
assertTrue(createIndexResponse.isAcknowledged());
}
{
IndicesAliasesRequest request = new IndicesAliasesRequest();
AliasActions aliasAction =
new AliasActions(AliasActions.Type.ADD)
.index("index1")
.alias("alias1");
request.addAliasAction(aliasAction);
AcknowledgedResponse indicesAliasesResponse =
client.indices().updateAliases(request, RequestOptions.DEFAULT);
assertTrue(indicesAliasesResponse.isAcknowledged());
}
{
IndicesAliasesRequest request = new IndicesAliasesRequest();
AliasActions aliasAction =
new AliasActions(AliasActions.Type.ADD)
.index("index1")
.alias("alias2");
request.addAliasAction(aliasAction);
AcknowledgedResponse indicesAliasesResponse =
client.indices().updateAliases(request, RequestOptions.DEFAULT);
assertTrue(indicesAliasesResponse.isAcknowledged());
}
{
// tag::delete-alias-request
DeleteAliasRequest request = new DeleteAliasRequest("index1", "alias1");
// end::delete-alias-request

// tag::delete-alias-request-timeout
request.setTimeout(TimeValue.timeValueMinutes(2)); // <1>
// end::delete-alias-request-timeout
// tag::delete-alias-request-masterTimeout
request.setMasterTimeout(TimeValue.timeValueMinutes(1)); // <1>
// end::delete-alias-request-masterTimeout

// tag::delete-alias-execute
org.elasticsearch.client.core.AcknowledgedResponse deleteAliasResponse =
client.indices().deleteAlias(request, RequestOptions.DEFAULT);
// end::delete-alias-execute

// tag::delete-alias-response
boolean acknowledged = deleteAliasResponse.isAcknowledged(); // <1>
// end::delete-alias-response
assertTrue(acknowledged);
}

{
DeleteAliasRequest request = new DeleteAliasRequest("index1", "alias2"); // <1>

// tag::delete-alias-execute-listener
ActionListener<org.elasticsearch.client.core.AcknowledgedResponse> listener =
new ActionListener<org.elasticsearch.client.core.AcknowledgedResponse>() {
@Override
public void onResponse(org.elasticsearch.client.core.AcknowledgedResponse deleteAliasResponse) {
// <1>
}

@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::delete-alias-execute-listener

// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);

// tag::delete-alias-execute-async
client.indices().deleteAliasAsync(request, RequestOptions.DEFAULT, listener); // <1>
// end::delete-alias-execute-async

assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
}
}
49 changes: 49 additions & 0 deletions docs/java-rest/high-level/indices/delete_alias.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--
:api: delete-alias
:request: DeleteAliasRequest
:response: AcknowledgedResponse
--

[id="{upid}-{api}"]
=== Delete Alias API

[id="{upid}-{api}-request"]
==== Delete Alias Request

An +{request}+ requires an `index` and an `alias` argument:

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-request]
--------------------------------------------------

==== Optional arguments
The following arguments can optionally be provided:

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-request-timeout]
--------------------------------------------------
<1> Timeout to wait for the all the nodes to acknowledge the index is opened
as a `TimeValue`

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-request-masterTimeout]
--------------------------------------------------
<1> Timeout to connect to the master node as a `TimeValue`

[id="{upid}-{api}-response"]
==== Delete Alias Response

The returned +{response}+ indicates if the request to delete the alias
was received.

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-response]
--------------------------------------------------
<1> Whether or not the request to delete the alias was
acknowledged.

include::../execution.asciidoc[]
2 changes: 2 additions & 0 deletions docs/java-rest/high-level/supported-apis.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ Alias Management::
* <<{upid}-update-aliases>>
* <<{upid}-exists-alias>>
* <<{upid}-get-alias>>
* <<{upid}-delete-alias>>

Template Management::
* <<{upid}-get-templates>>
Expand All @@ -145,6 +146,7 @@ include::indices/put_mapping.asciidoc[]
include::indices/get_mappings.asciidoc[]
include::indices/get_field_mappings.asciidoc[]
include::indices/update_aliases.asciidoc[]
include::indices/delete_alias.asciidoc[]
include::indices/exists_alias.asciidoc[]
include::indices/get_alias.asciidoc[]
include::indices/put_settings.asciidoc[]
Expand Down

0 comments on commit 7115d3e

Please sign in to comment.