Skip to content

Commit bb4846e

Browse files
authored
Add index block API (#4873)
This commit adds the indices.add_block API to the client.
1 parent a2382dc commit bb4846e

File tree

13 files changed

+346
-4
lines changed

13 files changed

+346
-4
lines changed

src/ApiGenerator/Configuration/CodeConfiguration.cs

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public static class CodeConfiguration
3636

3737
public static string[] IgnoredApisHighLevel { get; } =
3838
{
39-
"indices.add_block.json", // TODO: implement
4039
"indices.resolve_index.json", // TODO: implement
4140
"security.clear_cached_privileges.json", // TODO: implement
4241

src/ApiGenerator/Domain/Specification/UrlPart.cs

+3
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ public string HighLevelTypeName
104104
case "type":
105105
return Type == "string" ? "Name" : "Names";
106106

107+
case "block":
108+
return "IndexBlock";
109+
107110
case "index_uuid":
108111
return "IndexUuid";
109112

src/Elasticsearch.Net/Api/RequestParameters/RequestParameters.Indices.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
namespace Elasticsearch.Net.Specification.IndicesApi
2626
{
2727
///<summary>Request options for AddBlock <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-blocks.html</para></summary>
28-
public class AddBlockRequestParameters : RequestParameters<AddBlockRequestParameters>
28+
public class AddIndexBlockRequestParameters : RequestParameters<AddIndexBlockRequestParameters>
2929
{
3030
public override HttpMethod DefaultHttpMethod => HttpMethod.PUT;
3131
public override bool SupportsBody => false;

src/Elasticsearch.Net/ElasticLowLevelClient.Indices.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ internal LowLevelIndicesNamespace(ElasticLowLevelClient client): base(client)
4747
///<param name = "index">A comma separated list of indices to add a block to</param>
4848
///<param name = "block">The block to add (one of read, write, read_only or metadata)</param>
4949
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
50-
public TResponse AddBlock<TResponse>(string index, string block, AddBlockRequestParameters requestParameters = null)
50+
public TResponse AddBlock<TResponse>(string index, string block, AddIndexBlockRequestParameters requestParameters = null)
5151
where TResponse : class, IElasticsearchResponse, new() => DoRequest<TResponse>(PUT, Url($"{index:index}/_block/{block:block}"), null, RequestParams(requestParameters));
5252
///<summary>PUT on /{index}/_block/{block} <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-blocks.html</para></summary>
5353
///<param name = "index">A comma separated list of indices to add a block to</param>
5454
///<param name = "block">The block to add (one of read, write, read_only or metadata)</param>
5555
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
5656
[MapsApi("indices.add_block", "index, block")]
57-
public Task<TResponse> AddBlockAsync<TResponse>(string index, string block, AddBlockRequestParameters requestParameters = null, CancellationToken ctx = default)
57+
public Task<TResponse> AddBlockAsync<TResponse>(string index, string block, AddIndexBlockRequestParameters requestParameters = null, CancellationToken ctx = default)
5858
where TResponse : class, IElasticsearchResponse, new() => DoRequestAsync<TResponse>(PUT, Url($"{index:index}/_block/{block:block}"), ctx, null, RequestParams(requestParameters));
5959
///<summary>POST on /_analyze <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-analyze.html</para></summary>
6060
///<param name = "body">Define analyzer/tokenizer parameters and the text on which the analysis should be performed</param>

src/Nest/Descriptors.Indices.cs

+40
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,46 @@
3030
// ReSharper disable RedundantNameQualifier
3131
namespace Nest
3232
{
33+
///<summary>Descriptor for AddBlock <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-blocks.html</para></summary>
34+
public partial class AddIndexBlockDescriptor : RequestDescriptorBase<AddIndexBlockDescriptor, AddIndexBlockRequestParameters, IAddIndexBlockRequest>, IAddIndexBlockRequest
35+
{
36+
internal override ApiUrls ApiUrls => ApiUrlsLookups.IndicesAddBlock;
37+
///<summary>/{index}/_block/{block}</summary>
38+
///<param name = "index">this parameter is required</param>
39+
///<param name = "block">this parameter is required</param>
40+
public AddIndexBlockDescriptor(Indices index, IndexBlock block): base(r => r.Required("index", index).Required("block", block))
41+
{
42+
}
43+
44+
///<summary>Used for serialization purposes, making sure we have a parameterless constructor</summary>
45+
[SerializationConstructor]
46+
protected AddIndexBlockDescriptor(): base()
47+
{
48+
}
49+
50+
// values part of the url path
51+
Indices IAddIndexBlockRequest.Index => Self.RouteValues.Get<Indices>("index");
52+
IndexBlock IAddIndexBlockRequest.Block => Self.RouteValues.Get<IndexBlock>("block");
53+
///<summary>A comma separated list of indices to add a block to</summary>
54+
public AddIndexBlockDescriptor Index(Indices index) => Assign(index, (a, v) => a.RouteValues.Required("index", v));
55+
///<summary>a shortcut into calling Index(typeof(TOther))</summary>
56+
public AddIndexBlockDescriptor Index<TOther>()
57+
where TOther : class => Assign(typeof(TOther), (a, v) => a.RouteValues.Required("index", (Indices)v));
58+
///<summary>A shortcut into calling Index(Indices.All)</summary>
59+
public AddIndexBlockDescriptor AllIndices() => Index(Indices.All);
60+
// Request parameters
61+
///<summary>Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified)</summary>
62+
public AddIndexBlockDescriptor AllowNoIndices(bool? allownoindices = true) => Qs("allow_no_indices", allownoindices);
63+
///<summary>Whether to expand wildcard expression to concrete indices that are open, closed or both.</summary>
64+
public AddIndexBlockDescriptor ExpandWildcards(ExpandWildcards? expandwildcards) => Qs("expand_wildcards", expandwildcards);
65+
///<summary>Whether specified concrete indices should be ignored when unavailable (missing or closed)</summary>
66+
public AddIndexBlockDescriptor IgnoreUnavailable(bool? ignoreunavailable = true) => Qs("ignore_unavailable", ignoreunavailable);
67+
///<summary>Specify timeout for connection to master</summary>
68+
public AddIndexBlockDescriptor MasterTimeout(Time mastertimeout) => Qs("master_timeout", mastertimeout);
69+
///<summary>Explicit operation timeout</summary>
70+
public AddIndexBlockDescriptor Timeout(Time timeout) => Qs("timeout", timeout);
71+
}
72+
3373
///<summary>Descriptor for Analyze <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-analyze.html</para></summary>
3474
public partial class AnalyzeDescriptor : RequestDescriptorBase<AnalyzeDescriptor, AnalyzeRequestParameters, IAnalyzeRequest>, IAnalyzeRequest
3575
{

src/Nest/ElasticClient.Indices.cs

+24
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ internal IndicesNamespace(ElasticClient client): base(client)
3636
{
3737
}
3838

39+
/// <summary>
40+
/// <c>PUT</c> request to the <c>indices.add_block</c> API, read more about this API online:
41+
/// <para></para>
42+
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-blocks.html">https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-blocks.html</a>
43+
/// </summary>
44+
public AddIndexBlockResponse AddBlock(Indices index, IndexBlock block, Func<AddIndexBlockDescriptor, IAddIndexBlockRequest> selector = null) => AddBlock(selector.InvokeOrDefault(new AddIndexBlockDescriptor(index: index, block: block)));
45+
/// <summary>
46+
/// <c>PUT</c> request to the <c>indices.add_block</c> API, read more about this API online:
47+
/// <para></para>
48+
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-blocks.html">https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-blocks.html</a>
49+
/// </summary>
50+
public Task<AddIndexBlockResponse> AddBlockAsync(Indices index, IndexBlock block, Func<AddIndexBlockDescriptor, IAddIndexBlockRequest> selector = null, CancellationToken ct = default) => AddBlockAsync(selector.InvokeOrDefault(new AddIndexBlockDescriptor(index: index, block: block)), ct);
51+
/// <summary>
52+
/// <c>PUT</c> request to the <c>indices.add_block</c> API, read more about this API online:
53+
/// <para></para>
54+
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-blocks.html">https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-blocks.html</a>
55+
/// </summary>
56+
public AddIndexBlockResponse AddBlock(IAddIndexBlockRequest request) => DoRequest<IAddIndexBlockRequest, AddIndexBlockResponse>(request, request.RequestParameters);
57+
/// <summary>
58+
/// <c>PUT</c> request to the <c>indices.add_block</c> API, read more about this API online:
59+
/// <para></para>
60+
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-blocks.html">https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-blocks.html</a>
61+
/// </summary>
62+
public Task<AddIndexBlockResponse> AddBlockAsync(IAddIndexBlockRequest request, CancellationToken ct = default) => DoRequestAsync<IAddIndexBlockRequest, AddIndexBlockResponse>(request, request.RequestParameters, ct);
3963
/// <summary>
4064
/// <c>POST</c> request to the <c>indices.analyze</c> API, read more about this API online:
4165
/// <para></para>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
namespace Nest
6+
{
7+
/// <summary>
8+
/// A request to the indices add block API
9+
/// </summary>
10+
[MapsApi("indices.add_block.json")]
11+
[ReadAs(typeof(AddIndexBlockRequest))]
12+
public partial interface IAddIndexBlockRequest
13+
{
14+
}
15+
16+
/// <inheritdoc cref="IAddIndexBlockRequest" />
17+
public partial class AddIndexBlockRequest
18+
{
19+
}
20+
21+
/// <inheritdoc cref="IAddIndexBlockRequest" />
22+
public partial class AddIndexBlockDescriptor
23+
{
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.Collections.Generic;
6+
using System.Runtime.Serialization;
7+
using Elasticsearch.Net;
8+
9+
namespace Nest
10+
{
11+
public class AddIndexBlockResponse : AcknowledgedResponseBase
12+
{
13+
[DataMember(Name = "shards_acknowledged")]
14+
public bool ShardsAcknowledged { get; internal set; }
15+
16+
[DataMember(Name = "indices")]
17+
public IReadOnlyCollection<BlockedIndex> Indices { get; internal set; } = EmptyReadOnly<BlockedIndex>.Collection;
18+
}
19+
20+
public class BlockedIndex
21+
{
22+
[DataMember(Name = "name")]
23+
public string Name { get; internal set; }
24+
25+
[DataMember(Name = "blocked")]
26+
public bool Blocked { get; internal set; }
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
// Licensed to Elasticsearch B.V under one or more agreements.
6+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
7+
// See the LICENSE file in the project root for more information
8+
9+
using Elasticsearch.Net;
10+
11+
namespace Nest
12+
{
13+
/// <summary>
14+
/// Block type for an index.
15+
/// </summary>
16+
public class IndexBlock : IUrlParameter
17+
{
18+
private IndexBlock(string value) => Value = value;
19+
20+
public string Value { get; }
21+
22+
public string GetString(IConnectionConfigurationValues settings) => Value;
23+
24+
/// <summary>
25+
/// Disable metadata changes, such as closing the index.
26+
/// </summary>
27+
public static IndexBlock Metadata { get; } = new IndexBlock("metadata");
28+
29+
/// <summary>
30+
/// Disable read operations.
31+
/// </summary>
32+
public static IndexBlock Read { get; } = new IndexBlock("read");
33+
34+
/// <summary>
35+
/// Disable write operations and metadata changes.
36+
/// </summary>
37+
public static IndexBlock ReadOnly { get; } = new IndexBlock("read_only");
38+
39+
/// <summary>
40+
/// Disable write operations. However, metadata changes are still allowed.
41+
/// </summary>
42+
public static IndexBlock Write { get; } = new IndexBlock("write");
43+
}
44+
}

src/Nest/Requests.Indices.cs

+79
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,85 @@
3131
// ReSharper disable RedundantNameQualifier
3232
namespace Nest
3333
{
34+
[InterfaceDataContract]
35+
public partial interface IAddIndexBlockRequest : IRequest<AddIndexBlockRequestParameters>
36+
{
37+
[IgnoreDataMember]
38+
Indices Index
39+
{
40+
get;
41+
}
42+
43+
[IgnoreDataMember]
44+
IndexBlock Block
45+
{
46+
get;
47+
}
48+
}
49+
50+
///<summary>Request for AddBlock <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-blocks.html</para></summary>
51+
public partial class AddIndexBlockRequest : PlainRequestBase<AddIndexBlockRequestParameters>, IAddIndexBlockRequest
52+
{
53+
protected IAddIndexBlockRequest Self => this;
54+
internal override ApiUrls ApiUrls => ApiUrlsLookups.IndicesAddBlock;
55+
///<summary>/{index}/_block/{block}</summary>
56+
///<param name = "index">this parameter is required</param>
57+
///<param name = "block">this parameter is required</param>
58+
public AddIndexBlockRequest(Indices index, IndexBlock block): base(r => r.Required("index", index).Required("block", block))
59+
{
60+
}
61+
62+
///<summary>Used for serialization purposes, making sure we have a parameterless constructor</summary>
63+
[SerializationConstructor]
64+
protected AddIndexBlockRequest(): base()
65+
{
66+
}
67+
68+
// values part of the url path
69+
[IgnoreDataMember]
70+
Indices IAddIndexBlockRequest.Index => Self.RouteValues.Get<Indices>("index");
71+
[IgnoreDataMember]
72+
IndexBlock IAddIndexBlockRequest.Block => Self.RouteValues.Get<IndexBlock>("block");
73+
// Request parameters
74+
///<summary>
75+
/// Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have
76+
/// been specified)
77+
///</summary>
78+
public bool? AllowNoIndices
79+
{
80+
get => Q<bool? >("allow_no_indices");
81+
set => Q("allow_no_indices", value);
82+
}
83+
84+
///<summary>Whether to expand wildcard expression to concrete indices that are open, closed or both.</summary>
85+
public ExpandWildcards? ExpandWildcards
86+
{
87+
get => Q<ExpandWildcards? >("expand_wildcards");
88+
set => Q("expand_wildcards", value);
89+
}
90+
91+
///<summary>Whether specified concrete indices should be ignored when unavailable (missing or closed)</summary>
92+
public bool? IgnoreUnavailable
93+
{
94+
get => Q<bool? >("ignore_unavailable");
95+
set => Q("ignore_unavailable", value);
96+
}
97+
98+
///<summary>Specify timeout for connection to master</summary>
99+
public Time MasterTimeout
100+
{
101+
get => Q<Time>("master_timeout");
102+
set => Q("master_timeout", value);
103+
}
104+
105+
///<summary>Explicit operation timeout</summary>
106+
public Time Timeout
107+
{
108+
get => Q<Time>("timeout");
109+
set => Q("timeout", value);
110+
}
111+
}
112+
34113
[InterfaceDataContract]
35114
public partial interface IAnalyzeRequest : IRequest<AnalyzeRequestParameters>
36115
{

src/Nest/_Generated/ApiUrlsLookup.generated.cs

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ internal static class ApiUrlsLookups
105105
internal static ApiUrls IndexLifecycleManagementStart = new ApiUrls(new[]{"_ilm/start"});
106106
internal static ApiUrls IndexLifecycleManagementStop = new ApiUrls(new[]{"_ilm/stop"});
107107
internal static ApiUrls NoNamespaceIndex = new ApiUrls(new[]{"{index}/_doc/{id}", "{index}/_doc"});
108+
internal static ApiUrls IndicesAddBlock = new ApiUrls(new[]{"{index}/_block/{block}"});
108109
internal static ApiUrls IndicesAnalyze = new ApiUrls(new[]{"_analyze", "{index}/_analyze"});
109110
internal static ApiUrls IndicesClearCache = new ApiUrls(new[]{"_cache/clear", "{index}/_cache/clear"});
110111
internal static ApiUrls IndicesClone = new ApiUrls(new[]{"{index}/_clone/{target}"});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System;
6+
using System.Linq;
7+
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
8+
using Elasticsearch.Net;
9+
using FluentAssertions;
10+
using Nest;
11+
using Tests.Core.Extensions;
12+
using Tests.Core.ManagedElasticsearch.Clusters;
13+
using Tests.Framework.EndpointTests;
14+
using Tests.Framework.EndpointTests.TestState;
15+
16+
namespace Tests.Indices.IndexManagement.AddBlock
17+
{
18+
[SkipVersion("<7.9.0", "indices add index introduced in 7.9.0")]
19+
public class AddIndexBlockApiTests
20+
: ApiIntegrationTestBase<WritableCluster, AddIndexBlockResponse, IAddIndexBlockRequest, AddIndexBlockDescriptor, AddIndexBlockRequest>
21+
{
22+
public AddIndexBlockApiTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
23+
24+
protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values)
25+
{
26+
foreach (var value in values)
27+
{
28+
var createIndexResponse = client.Indices.Create(value.Value, c => c
29+
.Settings(s => s
30+
.NumberOfShards(1)
31+
.NumberOfReplicas(0)
32+
)
33+
);
34+
35+
if (!createIndexResponse.IsValid)
36+
throw new Exception($"exception whilst setting up integration test: {createIndexResponse.DebugInformation}");
37+
}
38+
}
39+
40+
protected override bool ExpectIsValid => true;
41+
42+
protected override int ExpectStatusCode => 200;
43+
44+
protected override Func<AddIndexBlockDescriptor, IAddIndexBlockRequest> Fluent => d => d;
45+
46+
protected override HttpMethod HttpMethod => HttpMethod.PUT;
47+
48+
protected override AddIndexBlockRequest Initializer => new AddIndexBlockRequest(CallIsolatedValue, IndexBlock.Write);
49+
50+
protected override string UrlPath => $"/{CallIsolatedValue}/_block/write";
51+
52+
protected override LazyResponses ClientUsage() => Calls(
53+
(client, f) => client.Indices.AddBlock(CallIsolatedValue, IndexBlock.Write, f),
54+
(client, f) => client.Indices.AddBlockAsync(CallIsolatedValue, IndexBlock.Write, f),
55+
(client, r) => client.Indices.AddBlock(r),
56+
(client, r) => client.Indices.AddBlockAsync(r)
57+
);
58+
59+
protected override AddIndexBlockDescriptor NewDescriptor() => new AddIndexBlockDescriptor(CallIsolatedValue, IndexBlock.Write);
60+
61+
protected override void ExpectResponse(AddIndexBlockResponse response)
62+
{
63+
response.ShouldBeValid();
64+
response.Acknowledged.Should().BeTrue();
65+
response.ShardsAcknowledged.Should().BeTrue();
66+
response.Indices.Should().HaveCount(1);
67+
var first = response.Indices.First();
68+
first.Name.Should().Be(CallIsolatedValue);
69+
first.Blocked.Should().BeTrue();
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)