From 710f6048c3f4fd2df7feae8f503321474acca498 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Mon, 25 Aug 2014 16:44:37 +0200 Subject: [PATCH] fix #503 add support for template exists requests --- .../Allow404/ApiEndpointsThatAllow404.cs | 1 + .../RequestParameters.Generated.cs | 4 +- .../Elasticsearch.Net.csproj | 1 + .../ElasticsearchClient.Generated.cs | 48 ++++++++++----- .../IElasticsearchClient.Generated.cs | 8 +-- .../Obsolete/IndicesTemplateExists.cs | 48 +++++++++++++++ src/Nest/DSL/TemplateExistsDescriptor.cs | 37 ++++++++++++ src/Nest/DSL/_Descriptors.generated.cs | 10 +--- src/Nest/DSL/_Requests.generated.cs | 8 +-- src/Nest/ElasticClient-TemplateExists.cs | 59 +++++++++++++++++++ src/Nest/IElasticClient.cs | 12 ++++ src/Nest/Nest.csproj | 2 + src/Nest/Obsolete/Obsolete.cs | 9 +++ src/Nest/RawDispatch.generated.cs | 4 +- .../Template/TemplateTests.cs | 22 +++++++ 15 files changed, 234 insertions(+), 39 deletions(-) create mode 100644 src/Elasticsearch.Net/Obsolete/IndicesTemplateExists.cs create mode 100644 src/Nest/DSL/TemplateExistsDescriptor.cs create mode 100644 src/Nest/ElasticClient-TemplateExists.cs diff --git a/src/CodeGeneration/CodeGeneration.LowLevelClient/Overrides/Allow404/ApiEndpointsThatAllow404.cs b/src/CodeGeneration/CodeGeneration.LowLevelClient/Overrides/Allow404/ApiEndpointsThatAllow404.cs index 7873aaad4cc..8c7344b7131 100644 --- a/src/CodeGeneration/CodeGeneration.LowLevelClient/Overrides/Allow404/ApiEndpointsThatAllow404.cs +++ b/src/CodeGeneration/CodeGeneration.LowLevelClient/Overrides/Allow404/ApiEndpointsThatAllow404.cs @@ -15,6 +15,7 @@ public static class ApiEndpointsThatAllow404 "Delete", "IndexExists", "AliasExists", + "TemplateExists", "TypeExists", "Exists" }; diff --git a/src/Elasticsearch.Net/Domain/RequestParameters/RequestParameters.Generated.cs b/src/Elasticsearch.Net/Domain/RequestParameters/RequestParameters.Generated.cs index 371875c899d..6fb1721bb92 100644 --- a/src/Elasticsearch.Net/Domain/RequestParameters/RequestParameters.Generated.cs +++ b/src/Elasticsearch.Net/Domain/RequestParameters/RequestParameters.Generated.cs @@ -2838,12 +2838,12 @@ public AliasExistsRequestParameters Local(bool local) ///http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.x/indices-templates.html /// /// - public class IndicesExistsTemplateRequestParameters : FluentRequestParameters + public class TemplateExistsRequestParameters : FluentRequestParameters { internal bool _local { get; set; } ///Return local information, do not retrieve the state from master node (default: false) - public IndicesExistsTemplateRequestParameters Local(bool local) + public TemplateExistsRequestParameters Local(bool local) { this._local = local; this.AddQueryString("local", this._local); diff --git a/src/Elasticsearch.Net/Elasticsearch.Net.csproj b/src/Elasticsearch.Net/Elasticsearch.Net.csproj index 2f96338bcff..16d59fe1afa 100644 --- a/src/Elasticsearch.Net/Elasticsearch.Net.csproj +++ b/src/Elasticsearch.Net/Elasticsearch.Net.csproj @@ -67,6 +67,7 @@ + diff --git a/src/Elasticsearch.Net/ElasticsearchClient.Generated.cs b/src/Elasticsearch.Net/ElasticsearchClient.Generated.cs index 145a2bf71cf..062f65edf02 100644 --- a/src/Elasticsearch.Net/ElasticsearchClient.Generated.cs +++ b/src/Elasticsearch.Net/ElasticsearchClient.Generated.cs @@ -12064,15 +12064,19 @@ public Task> IndicesExistsAliasAsync(st /// - If T is of type byte[] deserialization will be shortcircuited /// - If T is of type VoidResponse the response stream will be ignored completely /// - public ElasticsearchResponse IndicesExistsTemplateForAll(string name, Func requestParameters = null) + public ElasticsearchResponse IndicesExistsTemplateForAll(string name, Func requestParameters = null) { name.ThrowIfNullOrEmpty("name"); var url = "_template/{0}".F(Encoded(name)); IRequestParameters requestParams = null; - if (requestParameters != null) - { - requestParams = requestParameters(new IndicesExistsTemplateRequestParameters()); + requestParameters = requestParameters ?? (s => s); + var passIn = new TemplateExistsRequestParameters(); + requestParams = requestParameters(passIn); + if (requestParams.RequestConfiguration == null) + requestParams.RequestConfiguration = new RequestConfiguration { AllowedStatusCodes = new [] { 404 } }; + else { + requestParams.RequestConfiguration.AllowedStatusCodes = new [] { 404 }; } @@ -12098,15 +12102,19 @@ public ElasticsearchResponse IndicesExistsTemplateForAll(string name, Func /// - If T is of type byte[] deserialization will be shortcircuited /// - If T is of type VoidResponse the response stream will be ignored completely /// - public Task> IndicesExistsTemplateForAllAsync(string name, Func requestParameters = null) + public Task> IndicesExistsTemplateForAllAsync(string name, Func requestParameters = null) { name.ThrowIfNullOrEmpty("name"); var url = "_template/{0}".F(Encoded(name)); IRequestParameters requestParams = null; - if (requestParameters != null) - { - requestParams = requestParameters(new IndicesExistsTemplateRequestParameters()); + requestParameters = requestParameters ?? (s => s); + var passIn = new TemplateExistsRequestParameters(); + requestParams = requestParameters(passIn); + if (requestParams.RequestConfiguration == null) + requestParams.RequestConfiguration = new RequestConfiguration { AllowedStatusCodes = new [] { 404 } }; + else { + requestParams.RequestConfiguration.AllowedStatusCodes = new [] { 404 }; } @@ -12134,15 +12142,19 @@ public Task> IndicesExistsTemplateForAllAsync(string /// - i.e result.Response.hits.hits[0].property.nested["nested_deeper"] /// - can be safely dispatched to a nullable type even if intermediate properties do not exist /// - public ElasticsearchResponse IndicesExistsTemplateForAll(string name, Func requestParameters = null) + public ElasticsearchResponse IndicesExistsTemplateForAll(string name, Func requestParameters = null) { name.ThrowIfNullOrEmpty("name"); var url = "_template/{0}".F(Encoded(name)); IRequestParameters requestParams = null; - if (requestParameters != null) - { - requestParams = requestParameters(new IndicesExistsTemplateRequestParameters()); + requestParameters = requestParameters ?? (s => s); + var passIn = new TemplateExistsRequestParameters(); + requestParams = requestParameters(passIn); + if (requestParams.RequestConfiguration == null) + requestParams.RequestConfiguration = new RequestConfiguration { AllowedStatusCodes = new [] { 404 } }; + else { + requestParams.RequestConfiguration.AllowedStatusCodes = new [] { 404 }; } @@ -12170,15 +12182,19 @@ public ElasticsearchResponse IndicesExistsTemplateForAll(stri /// - i.e result.Response.hits.hits[0].property.nested["nested_deeper"] /// - can be safely dispatched to a nullable type even if intermediate properties do not exist /// - public Task> IndicesExistsTemplateForAllAsync(string name, Func requestParameters = null) + public Task> IndicesExistsTemplateForAllAsync(string name, Func requestParameters = null) { name.ThrowIfNullOrEmpty("name"); var url = "_template/{0}".F(Encoded(name)); IRequestParameters requestParams = null; - if (requestParameters != null) - { - requestParams = requestParameters(new IndicesExistsTemplateRequestParameters()); + requestParameters = requestParameters ?? (s => s); + var passIn = new TemplateExistsRequestParameters(); + requestParams = requestParameters(passIn); + if (requestParams.RequestConfiguration == null) + requestParams.RequestConfiguration = new RequestConfiguration { AllowedStatusCodes = new [] { 404 } }; + else { + requestParams.RequestConfiguration.AllowedStatusCodes = new [] { 404 }; } diff --git a/src/Elasticsearch.Net/IElasticsearchClient.Generated.cs b/src/Elasticsearch.Net/IElasticsearchClient.Generated.cs index adf45b3a981..9122c685abf 100644 --- a/src/Elasticsearch.Net/IElasticsearchClient.Generated.cs +++ b/src/Elasticsearch.Net/IElasticsearchClient.Generated.cs @@ -6392,7 +6392,7 @@ public interface IElasticsearchClient /// - If T is of type VoidResponse the response stream will be ignored completely /// - ElasticsearchResponse IndicesExistsTemplateForAll(string name, Func requestParameters = null); + ElasticsearchResponse IndicesExistsTemplateForAll(string name, Func requestParameters = null); ///Represents a HEAD on /_template/{name} ///Returns: A task that'll return an ElasticsearchResponse<T> holding the reponse body deserialized as T. @@ -6410,7 +6410,7 @@ public interface IElasticsearchClient /// - If T is of type VoidResponse the response stream will be ignored completely /// - Task> IndicesExistsTemplateForAllAsync(string name, Func requestParameters = null); + Task> IndicesExistsTemplateForAllAsync(string name, Func requestParameters = null); ///Represents a HEAD on /_template/{name} ///Returns: ElasticsearchResponse<T> holding the response body deserialized as DynamicDictionary @@ -6430,7 +6430,7 @@ public interface IElasticsearchClient /// - can be safely dispatched to a nullable type even if intermediate properties do not exist /// - ElasticsearchResponse IndicesExistsTemplateForAll(string name, Func requestParameters = null); + ElasticsearchResponse IndicesExistsTemplateForAll(string name, Func requestParameters = null); ///Represents a HEAD on /_template/{name} ///Returns: Task that'll return an ElasticsearchResponse<T$gt; holding the response body deserialized as DynamicDictionary @@ -6450,7 +6450,7 @@ public interface IElasticsearchClient /// - can be safely dispatched to a nullable type even if intermediate properties do not exist /// - Task> IndicesExistsTemplateForAllAsync(string name, Func requestParameters = null); + Task> IndicesExistsTemplateForAllAsync(string name, Func requestParameters = null); ///Represents a HEAD on /{index}/{type} ///Returns: ElasticsearchResponse<T> holding the reponse body deserialized as T. diff --git a/src/Elasticsearch.Net/Obsolete/IndicesTemplateExists.cs b/src/Elasticsearch.Net/Obsolete/IndicesTemplateExists.cs new file mode 100644 index 00000000000..28f3200f21e --- /dev/null +++ b/src/Elasticsearch.Net/Obsolete/IndicesTemplateExists.cs @@ -0,0 +1,48 @@ +using System; +using System.Threading.Tasks; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Elasticsearch.Net +{ + #pragma warning disable 0618 + using IndicesExistsTemplateSelector = Func; + #pragma warning restore 0618 + + [Obsolete("Scheduled to be removed in 2.0, renamed to AliasExistsRequestParameters")] + public class IndicesExistsTemplateRequestParameters : TemplateExistsRequestParameters { } + + public static class IndicesExistsTemplateClientExtensions + { + [Obsolete("Scheduled to be removed in 2.0, use the method that takes a Func of IndicesExistsRequestParameters")] + public static ElasticsearchResponse IndicesExistsTemplateForAll(this IElasticsearchClient client, string name, IndicesExistsTemplateSelector requestParameters = null) + { + var selector = Obsolete.UpCastSelector(requestParameters); + return client.IndicesExistsTemplateForAll(name, requestParameters); + } + + [Obsolete("Scheduled to be removed in 2.0, use the method that takes a Func of IndicesExistsRequestParameters")] + public static Task> IndicesExistsTemplateForAllAsync(this IElasticsearchClient client, string name, IndicesExistsTemplateSelector requestParameters = null) + { + var selector = Obsolete.UpCastSelector(requestParameters); + return client.IndicesExistsTemplateForAllAsync(name, requestParameters); + } + + [Obsolete("Scheduled to be removed in 2.0, use the method that takes a Func of IndicesExistsRequestParameters")] + public static ElasticsearchResponse IndicesExistsTemplateForAll(this IElasticsearchClient client, string name, IndicesExistsTemplateSelector requestParameters = null) + { + var selector = Obsolete.UpCastSelector(requestParameters); + return client.IndicesExistsTemplateForAll(name, requestParameters); + } + + [Obsolete("Scheduled to be removed in 2.0, use the method that takes a Func of IndicesExistsRequestParameters")] + public static Task> IndicesExistsTemplateForAllAsync(this IElasticsearchClient client, string name, IndicesExistsTemplateSelector requestParameters = null) + { + var selector = Obsolete.UpCastSelector(requestParameters); + return client.IndicesExistsTemplateForAllAsync(name, requestParameters); + } + + + } +} diff --git a/src/Nest/DSL/TemplateExistsDescriptor.cs b/src/Nest/DSL/TemplateExistsDescriptor.cs new file mode 100644 index 00000000000..7385e6b00ae --- /dev/null +++ b/src/Nest/DSL/TemplateExistsDescriptor.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Elasticsearch.Net; +using Newtonsoft.Json; + +namespace Nest +{ + [JsonObject(MemberSerialization = MemberSerialization.OptIn)] + public interface ITemplateExistsRequest : INamePath { } + + internal static class TemplateExistsPathInfo + { + public static void Update(ElasticsearchPathInfo pathInfo, ITemplateExistsRequest request) + { + pathInfo.HttpMethod = PathInfoHttpMethod.HEAD; + } + } + + public partial class TemplateExistsRequest : NamePathBase, ITemplateExistsRequest + { + public TemplateExistsRequest(string name) : base(name) { } + + protected override void UpdatePathInfo(IConnectionSettingsValues settings, ElasticsearchPathInfo pathInfo) + { + TemplateExistsPathInfo.Update(pathInfo, this); + } + } + [DescriptorFor("IndicesExistsTemplate")] + public partial class TemplateExistsDescriptor : NamePathDescriptor, ITemplateExistsRequest + { + protected override void UpdatePathInfo(IConnectionSettingsValues settings, ElasticsearchPathInfo pathInfo) + { + TemplateExistsPathInfo.Update(pathInfo, this); + } + } +} diff --git a/src/Nest/DSL/_Descriptors.generated.cs b/src/Nest/DSL/_Descriptors.generated.cs index 571af21245b..97618d4d8a1 100644 --- a/src/Nest/DSL/_Descriptors.generated.cs +++ b/src/Nest/DSL/_Descriptors.generated.cs @@ -2729,24 +2729,18 @@ public AliasExistsDescriptor Local(bool local = true) ///http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.x/indices-templates.html /// /// - public partial class IndicesExistsTemplateDescriptor : BaseRequest + public partial class TemplateExistsDescriptor { ///Return local information, do not retrieve the state from master node (default: false) - public IndicesExistsTemplateDescriptor Local(bool local = true) + public TemplateExistsDescriptor Local(bool local = true) { this.Request.RequestParameters.Local(local); return this; } - - protected override void UpdatePathInfo(IConnectionSettingsValues settings, ElasticsearchPathInfo pathInfo) - { - throw new NotImplementedException(); - } - } diff --git a/src/Nest/DSL/_Requests.generated.cs b/src/Nest/DSL/_Requests.generated.cs index 2eab5b01bb8..575bc2fac0a 100644 --- a/src/Nest/DSL/_Requests.generated.cs +++ b/src/Nest/DSL/_Requests.generated.cs @@ -2480,7 +2480,7 @@ public bool Local ///http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.x/indices-templates.html /// /// - public partial class IndicesExistsTemplateRequest : BasePathRequest + public partial class TemplateExistsRequest { ///Return local information, do not retrieve the state from master node (default: false) @@ -2490,12 +2490,6 @@ public bool Local set { this.Request.RequestParameters.AddQueryString("local", value); } } - - protected override void UpdatePathInfo(IConnectionSettingsValues settings, ElasticsearchPathInfo pathInfo) - { - throw new NotImplementedException(); - } - } diff --git a/src/Nest/ElasticClient-TemplateExists.cs b/src/Nest/ElasticClient-TemplateExists.cs new file mode 100644 index 00000000000..7921665fe9e --- /dev/null +++ b/src/Nest/ElasticClient-TemplateExists.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Elasticsearch.Net; + +namespace Nest +{ + using TemplateExistConverter = Func; + + public partial class ElasticClient + { + /// + public IExistsResponse TemplateExists(Func selector) + { + return this.Dispatch( + selector, + (p, d) => this.RawDispatch.IndicesExistsTemplateDispatch( + p.DeserializationState(new TemplateExistConverter(DeserializeExistsResponse)) + ) + ); + } + + /// + public IExistsResponse TemplateExists(ITemplateExistsRequest templateRequest) + { + return this.Dispatch( + templateRequest, + (p, d) => this.RawDispatch.IndicesExistsTemplateDispatch( + p.DeserializationState(new TemplateExistConverter(DeserializeExistsResponse)) + ) + ); + } + + /// + public Task TemplateExistsAsync(Func selector) + { + return this.DispatchAsync( + selector, + (p, d) => this.RawDispatch.IndicesExistsTemplateDispatchAsync( + p.DeserializationState(new TemplateExistConverter(DeserializeExistsResponse)) + ) + ); + } + + /// + public Task TemplateExistsAsync(ITemplateExistsRequest templateRequest) + { + return this.DispatchAsync( + templateRequest, + (p, d) => this.RawDispatch.IndicesExistsTemplateDispatchAsync( + p.DeserializationState(new TemplateExistConverter(DeserializeExistsResponse)) + ) + ); + } + + } +} \ No newline at end of file diff --git a/src/Nest/IElasticClient.cs b/src/Nest/IElasticClient.cs index 5f5d2112ac1..3c9c16af779 100644 --- a/src/Nest/IElasticClient.cs +++ b/src/Nest/IElasticClient.cs @@ -1306,5 +1306,17 @@ Task GetFieldMappingAsync(Func Task GetFieldMappingAsync(IGetFieldMappingRequest getFieldMappingRequest); + + /// + IExistsResponse TemplateExists(Func selector); + + /// + IExistsResponse TemplateExists(ITemplateExistsRequest templateRequest); + + /// + Task TemplateExistsAsync(Func selector); + + /// + Task TemplateExistsAsync(ITemplateExistsRequest templateRequest); } } diff --git a/src/Nest/Nest.csproj b/src/Nest/Nest.csproj index f5f240356d3..a58748ec256 100644 --- a/src/Nest/Nest.csproj +++ b/src/Nest/Nest.csproj @@ -168,6 +168,7 @@ + @@ -252,6 +253,7 @@ + diff --git a/src/Nest/Obsolete/Obsolete.cs b/src/Nest/Obsolete/Obsolete.cs index 0aa884a05d9..e916ff174cd 100644 --- a/src/Nest/Obsolete/Obsolete.cs +++ b/src/Nest/Obsolete/Obsolete.cs @@ -32,6 +32,15 @@ public IndicesExistsTypeRequest() : base("", ""){} public IndicesExistsTypeRequest(IndexNameMarker index, TypeNameMarker typeNameMarker) : base(index, typeNameMarker) { } } + [Obsolete("Scheduled to be removed in 2.0, renamed to TemplateExistsRequest")] + public class IndicesExistsTemplateRequest : TemplateExistsRequest + { + public IndicesExistsTemplateRequest(string name) : base(name) { } + } + + [Obsolete("Scheduled to be removed in 2.0, renamed to TemplateExistsDescriptor")] + public class IndicesExistsTemplateDescriptor : TemplateExistsDescriptor { } + [Obsolete("Scheduled to be removed in 2.0, use the generic variant of this class instead")] public class ExplainDescriptor : ExplainDescriptor diff --git a/src/Nest/RawDispatch.generated.cs b/src/Nest/RawDispatch.generated.cs index 79e7caaa4e9..27ca7155806 100644 --- a/src/Nest/RawDispatch.generated.cs +++ b/src/Nest/RawDispatch.generated.cs @@ -1522,7 +1522,7 @@ internal Task> IndicesExistsAliasDispatchAsync(Elast } - internal ElasticsearchResponse IndicesExistsTemplateDispatch(ElasticsearchPathInfo pathInfo ) + internal ElasticsearchResponse IndicesExistsTemplateDispatch(ElasticsearchPathInfo pathInfo ) { switch(pathInfo.HttpMethod) { @@ -1537,7 +1537,7 @@ internal ElasticsearchResponse IndicesExistsTemplateDispatch(Elasticsearch } - internal Task> IndicesExistsTemplateDispatchAsync(ElasticsearchPathInfo pathInfo ) + internal Task> IndicesExistsTemplateDispatchAsync(ElasticsearchPathInfo pathInfo ) { switch(pathInfo.HttpMethod) { diff --git a/src/Tests/Nest.Tests.Integration/Template/TemplateTests.cs b/src/Tests/Nest.Tests.Integration/Template/TemplateTests.cs index cf78a3a291b..dfc74255d44 100644 --- a/src/Tests/Nest.Tests.Integration/Template/TemplateTests.cs +++ b/src/Tests/Nest.Tests.Integration/Template/TemplateTests.cs @@ -18,6 +18,7 @@ public void SimplePutAndGet() ); Assert.IsTrue(putResponse.Acknowledged); + var templateResponse = this.Client.GetTemplate("put-template-with-settings"); templateResponse.Should().NotBeNull(); templateResponse.IsValid.Should().BeTrue(); @@ -30,6 +31,27 @@ public void SimplePutAndGet() settings.Should().NotBeNull(); } + [Test] + public void TemplateExistsReturnsNoFalsePositives() + { + var name = ElasticsearchConfiguration.NewUniqueIndexName(); + var putResponse = this.Client.PutTemplate(new PutTemplateRequest(name) + { + TemplateMapping = new TemplateMapping() + { + Order = 1377, + Template = name + } + }); + putResponse.IsValid.Should().BeTrue(); + + var falseExists = this.Client.TemplateExists(new TemplateExistsRequest("hello-world-blah")); + falseExists.IsValid.Should().BeTrue(); + falseExists.Exists.Should().BeFalse(); + + var realExists = this.Client.TemplateExists(new TemplateExistsRequest(name)); + realExists.Exists.Should().BeTrue(); + } [Test] public void PutTemplateWithSettings()