Skip to content

Commit

Permalink
Add index block API (#4873)
Browse files Browse the repository at this point in the history
This commit adds the indices.add_block API
to the client.
  • Loading branch information
russcam authored Jul 27, 2020
1 parent a2382dc commit bb4846e
Show file tree
Hide file tree
Showing 13 changed files with 346 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/ApiGenerator/Configuration/CodeConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public static class CodeConfiguration

public static string[] IgnoredApisHighLevel { get; } =
{
"indices.add_block.json", // TODO: implement
"indices.resolve_index.json", // TODO: implement
"security.clear_cached_privileges.json", // TODO: implement

Expand Down
3 changes: 3 additions & 0 deletions src/ApiGenerator/Domain/Specification/UrlPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ public string HighLevelTypeName
case "type":
return Type == "string" ? "Name" : "Names";

case "block":
return "IndexBlock";

case "index_uuid":
return "IndexUuid";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
namespace Elasticsearch.Net.Specification.IndicesApi
{
///<summary>Request options for AddBlock <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-blocks.html</para></summary>
public class AddBlockRequestParameters : RequestParameters<AddBlockRequestParameters>
public class AddIndexBlockRequestParameters : RequestParameters<AddIndexBlockRequestParameters>
{
public override HttpMethod DefaultHttpMethod => HttpMethod.PUT;
public override bool SupportsBody => false;
Expand Down
4 changes: 2 additions & 2 deletions src/Elasticsearch.Net/ElasticLowLevelClient.Indices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ internal LowLevelIndicesNamespace(ElasticLowLevelClient client): base(client)
///<param name = "index">A comma separated list of indices to add a block to</param>
///<param name = "block">The block to add (one of read, write, read_only or metadata)</param>
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
public TResponse AddBlock<TResponse>(string index, string block, AddBlockRequestParameters requestParameters = null)
public TResponse AddBlock<TResponse>(string index, string block, AddIndexBlockRequestParameters requestParameters = null)
where TResponse : class, IElasticsearchResponse, new() => DoRequest<TResponse>(PUT, Url($"{index:index}/_block/{block:block}"), null, RequestParams(requestParameters));
///<summary>PUT on /{index}/_block/{block} <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-blocks.html</para></summary>
///<param name = "index">A comma separated list of indices to add a block to</param>
///<param name = "block">The block to add (one of read, write, read_only or metadata)</param>
///<param name = "requestParameters">Request specific configuration such as querystring parameters &amp; request specific connection settings.</param>
[MapsApi("indices.add_block", "index, block")]
public Task<TResponse> AddBlockAsync<TResponse>(string index, string block, AddBlockRequestParameters requestParameters = null, CancellationToken ctx = default)
public Task<TResponse> AddBlockAsync<TResponse>(string index, string block, AddIndexBlockRequestParameters requestParameters = null, CancellationToken ctx = default)
where TResponse : class, IElasticsearchResponse, new() => DoRequestAsync<TResponse>(PUT, Url($"{index:index}/_block/{block:block}"), ctx, null, RequestParams(requestParameters));
///<summary>POST on /_analyze <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-analyze.html</para></summary>
///<param name = "body">Define analyzer/tokenizer parameters and the text on which the analysis should be performed</param>
Expand Down
40 changes: 40 additions & 0 deletions src/Nest/Descriptors.Indices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,46 @@
// ReSharper disable RedundantNameQualifier
namespace Nest
{
///<summary>Descriptor for AddBlock <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-blocks.html</para></summary>
public partial class AddIndexBlockDescriptor : RequestDescriptorBase<AddIndexBlockDescriptor, AddIndexBlockRequestParameters, IAddIndexBlockRequest>, IAddIndexBlockRequest
{
internal override ApiUrls ApiUrls => ApiUrlsLookups.IndicesAddBlock;
///<summary>/{index}/_block/{block}</summary>
///<param name = "index">this parameter is required</param>
///<param name = "block">this parameter is required</param>
public AddIndexBlockDescriptor(Indices index, IndexBlock block): base(r => r.Required("index", index).Required("block", block))
{
}

///<summary>Used for serialization purposes, making sure we have a parameterless constructor</summary>
[SerializationConstructor]
protected AddIndexBlockDescriptor(): base()
{
}

// values part of the url path
Indices IAddIndexBlockRequest.Index => Self.RouteValues.Get<Indices>("index");
IndexBlock IAddIndexBlockRequest.Block => Self.RouteValues.Get<IndexBlock>("block");
///<summary>A comma separated list of indices to add a block to</summary>
public AddIndexBlockDescriptor Index(Indices index) => Assign(index, (a, v) => a.RouteValues.Required("index", v));
///<summary>a shortcut into calling Index(typeof(TOther))</summary>
public AddIndexBlockDescriptor Index<TOther>()
where TOther : class => Assign(typeof(TOther), (a, v) => a.RouteValues.Required("index", (Indices)v));
///<summary>A shortcut into calling Index(Indices.All)</summary>
public AddIndexBlockDescriptor AllIndices() => Index(Indices.All);
// Request parameters
///<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>
public AddIndexBlockDescriptor AllowNoIndices(bool? allownoindices = true) => Qs("allow_no_indices", allownoindices);
///<summary>Whether to expand wildcard expression to concrete indices that are open, closed or both.</summary>
public AddIndexBlockDescriptor ExpandWildcards(ExpandWildcards? expandwildcards) => Qs("expand_wildcards", expandwildcards);
///<summary>Whether specified concrete indices should be ignored when unavailable (missing or closed)</summary>
public AddIndexBlockDescriptor IgnoreUnavailable(bool? ignoreunavailable = true) => Qs("ignore_unavailable", ignoreunavailable);
///<summary>Specify timeout for connection to master</summary>
public AddIndexBlockDescriptor MasterTimeout(Time mastertimeout) => Qs("master_timeout", mastertimeout);
///<summary>Explicit operation timeout</summary>
public AddIndexBlockDescriptor Timeout(Time timeout) => Qs("timeout", timeout);
}

///<summary>Descriptor for Analyze <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-analyze.html</para></summary>
public partial class AnalyzeDescriptor : RequestDescriptorBase<AnalyzeDescriptor, AnalyzeRequestParameters, IAnalyzeRequest>, IAnalyzeRequest
{
Expand Down
24 changes: 24 additions & 0 deletions src/Nest/ElasticClient.Indices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ internal IndicesNamespace(ElasticClient client): base(client)
{
}

/// <summary>
/// <c>PUT</c> request to the <c>indices.add_block</c> API, read more about this API online:
/// <para></para>
/// <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>
/// </summary>
public AddIndexBlockResponse AddBlock(Indices index, IndexBlock block, Func<AddIndexBlockDescriptor, IAddIndexBlockRequest> selector = null) => AddBlock(selector.InvokeOrDefault(new AddIndexBlockDescriptor(index: index, block: block)));
/// <summary>
/// <c>PUT</c> request to the <c>indices.add_block</c> API, read more about this API online:
/// <para></para>
/// <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>
/// </summary>
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);
/// <summary>
/// <c>PUT</c> request to the <c>indices.add_block</c> API, read more about this API online:
/// <para></para>
/// <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>
/// </summary>
public AddIndexBlockResponse AddBlock(IAddIndexBlockRequest request) => DoRequest<IAddIndexBlockRequest, AddIndexBlockResponse>(request, request.RequestParameters);
/// <summary>
/// <c>PUT</c> request to the <c>indices.add_block</c> API, read more about this API online:
/// <para></para>
/// <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>
/// </summary>
public Task<AddIndexBlockResponse> AddBlockAsync(IAddIndexBlockRequest request, CancellationToken ct = default) => DoRequestAsync<IAddIndexBlockRequest, AddIndexBlockResponse>(request, request.RequestParameters, ct);
/// <summary>
/// <c>POST</c> request to the <c>indices.analyze</c> API, read more about this API online:
/// <para></para>
Expand Down
25 changes: 25 additions & 0 deletions src/Nest/Indices/IndexManagement/AddBlock/AddIndexBlockRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

namespace Nest
{
/// <summary>
/// A request to the indices add block API
/// </summary>
[MapsApi("indices.add_block.json")]
[ReadAs(typeof(AddIndexBlockRequest))]
public partial interface IAddIndexBlockRequest
{
}

/// <inheritdoc cref="IAddIndexBlockRequest" />
public partial class AddIndexBlockRequest
{
}

/// <inheritdoc cref="IAddIndexBlockRequest" />
public partial class AddIndexBlockDescriptor
{
}
}
28 changes: 28 additions & 0 deletions src/Nest/Indices/IndexManagement/AddBlock/AddIndexBlockResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System.Collections.Generic;
using System.Runtime.Serialization;
using Elasticsearch.Net;

namespace Nest
{
public class AddIndexBlockResponse : AcknowledgedResponseBase
{
[DataMember(Name = "shards_acknowledged")]
public bool ShardsAcknowledged { get; internal set; }

[DataMember(Name = "indices")]
public IReadOnlyCollection<BlockedIndex> Indices { get; internal set; } = EmptyReadOnly<BlockedIndex>.Collection;
}

public class BlockedIndex
{
[DataMember(Name = "name")]
public string Name { get; internal set; }

[DataMember(Name = "blocked")]
public bool Blocked { get; internal set; }
}
}
44 changes: 44 additions & 0 deletions src/Nest/Indices/IndexManagement/AddBlock/IndexBlock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using Elasticsearch.Net;

namespace Nest
{
/// <summary>
/// Block type for an index.
/// </summary>
public class IndexBlock : IUrlParameter
{
private IndexBlock(string value) => Value = value;

public string Value { get; }

public string GetString(IConnectionConfigurationValues settings) => Value;

/// <summary>
/// Disable metadata changes, such as closing the index.
/// </summary>
public static IndexBlock Metadata { get; } = new IndexBlock("metadata");

/// <summary>
/// Disable read operations.
/// </summary>
public static IndexBlock Read { get; } = new IndexBlock("read");

/// <summary>
/// Disable write operations and metadata changes.
/// </summary>
public static IndexBlock ReadOnly { get; } = new IndexBlock("read_only");

/// <summary>
/// Disable write operations. However, metadata changes are still allowed.
/// </summary>
public static IndexBlock Write { get; } = new IndexBlock("write");
}
}
79 changes: 79 additions & 0 deletions src/Nest/Requests.Indices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,85 @@
// ReSharper disable RedundantNameQualifier
namespace Nest
{
[InterfaceDataContract]
public partial interface IAddIndexBlockRequest : IRequest<AddIndexBlockRequestParameters>
{
[IgnoreDataMember]
Indices Index
{
get;
}

[IgnoreDataMember]
IndexBlock Block
{
get;
}
}

///<summary>Request for AddBlock <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-blocks.html</para></summary>
public partial class AddIndexBlockRequest : PlainRequestBase<AddIndexBlockRequestParameters>, IAddIndexBlockRequest
{
protected IAddIndexBlockRequest Self => this;
internal override ApiUrls ApiUrls => ApiUrlsLookups.IndicesAddBlock;
///<summary>/{index}/_block/{block}</summary>
///<param name = "index">this parameter is required</param>
///<param name = "block">this parameter is required</param>
public AddIndexBlockRequest(Indices index, IndexBlock block): base(r => r.Required("index", index).Required("block", block))
{
}

///<summary>Used for serialization purposes, making sure we have a parameterless constructor</summary>
[SerializationConstructor]
protected AddIndexBlockRequest(): base()
{
}

// values part of the url path
[IgnoreDataMember]
Indices IAddIndexBlockRequest.Index => Self.RouteValues.Get<Indices>("index");
[IgnoreDataMember]
IndexBlock IAddIndexBlockRequest.Block => Self.RouteValues.Get<IndexBlock>("block");
// Request parameters
///<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>
public bool? AllowNoIndices
{
get => Q<bool? >("allow_no_indices");
set => Q("allow_no_indices", value);
}

///<summary>Whether to expand wildcard expression to concrete indices that are open, closed or both.</summary>
public ExpandWildcards? ExpandWildcards
{
get => Q<ExpandWildcards? >("expand_wildcards");
set => Q("expand_wildcards", value);
}

///<summary>Whether specified concrete indices should be ignored when unavailable (missing or closed)</summary>
public bool? IgnoreUnavailable
{
get => Q<bool? >("ignore_unavailable");
set => Q("ignore_unavailable", value);
}

///<summary>Specify timeout for connection to master</summary>
public Time MasterTimeout
{
get => Q<Time>("master_timeout");
set => Q("master_timeout", value);
}

///<summary>Explicit operation timeout</summary>
public Time Timeout
{
get => Q<Time>("timeout");
set => Q("timeout", value);
}
}

[InterfaceDataContract]
public partial interface IAnalyzeRequest : IRequest<AnalyzeRequestParameters>
{
Expand Down
1 change: 1 addition & 0 deletions src/Nest/_Generated/ApiUrlsLookup.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ internal static class ApiUrlsLookups
internal static ApiUrls IndexLifecycleManagementStart = new ApiUrls(new[]{"_ilm/start"});
internal static ApiUrls IndexLifecycleManagementStop = new ApiUrls(new[]{"_ilm/stop"});
internal static ApiUrls NoNamespaceIndex = new ApiUrls(new[]{"{index}/_doc/{id}", "{index}/_doc"});
internal static ApiUrls IndicesAddBlock = new ApiUrls(new[]{"{index}/_block/{block}"});
internal static ApiUrls IndicesAnalyze = new ApiUrls(new[]{"_analyze", "{index}/_analyze"});
internal static ApiUrls IndicesClearCache = new ApiUrls(new[]{"_cache/clear", "{index}/_cache/clear"});
internal static ApiUrls IndicesClone = new ApiUrls(new[]{"{index}/_clone/{target}"});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System;
using System.Linq;
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
using Elasticsearch.Net;
using FluentAssertions;
using Nest;
using Tests.Core.Extensions;
using Tests.Core.ManagedElasticsearch.Clusters;
using Tests.Framework.EndpointTests;
using Tests.Framework.EndpointTests.TestState;

namespace Tests.Indices.IndexManagement.AddBlock
{
[SkipVersion("<7.9.0", "indices add index introduced in 7.9.0")]
public class AddIndexBlockApiTests
: ApiIntegrationTestBase<WritableCluster, AddIndexBlockResponse, IAddIndexBlockRequest, AddIndexBlockDescriptor, AddIndexBlockRequest>
{
public AddIndexBlockApiTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values)
{
foreach (var value in values)
{
var createIndexResponse = client.Indices.Create(value.Value, c => c
.Settings(s => s
.NumberOfShards(1)
.NumberOfReplicas(0)
)
);

if (!createIndexResponse.IsValid)
throw new Exception($"exception whilst setting up integration test: {createIndexResponse.DebugInformation}");
}
}

protected override bool ExpectIsValid => true;

protected override int ExpectStatusCode => 200;

protected override Func<AddIndexBlockDescriptor, IAddIndexBlockRequest> Fluent => d => d;

protected override HttpMethod HttpMethod => HttpMethod.PUT;

protected override AddIndexBlockRequest Initializer => new AddIndexBlockRequest(CallIsolatedValue, IndexBlock.Write);

protected override string UrlPath => $"/{CallIsolatedValue}/_block/write";

protected override LazyResponses ClientUsage() => Calls(
(client, f) => client.Indices.AddBlock(CallIsolatedValue, IndexBlock.Write, f),
(client, f) => client.Indices.AddBlockAsync(CallIsolatedValue, IndexBlock.Write, f),
(client, r) => client.Indices.AddBlock(r),
(client, r) => client.Indices.AddBlockAsync(r)
);

protected override AddIndexBlockDescriptor NewDescriptor() => new AddIndexBlockDescriptor(CallIsolatedValue, IndexBlock.Write);

protected override void ExpectResponse(AddIndexBlockResponse response)
{
response.ShouldBeValid();
response.Acknowledged.Should().BeTrue();
response.ShardsAcknowledged.Should().BeTrue();
response.Indices.Should().HaveCount(1);
var first = response.Indices.First();
first.Name.Should().Be(CallIsolatedValue);
first.Blocked.Should().BeTrue();
}
}
}
Loading

0 comments on commit bb4846e

Please sign in to comment.