From 92652a093fa8cd7150cfca460b63e77e6e5dc3af Mon Sep 17 00:00:00 2001 From: Russ Cam Date: Mon, 5 Jun 2017 12:16:33 -0700 Subject: [PATCH] Add remove_index operation to Alias operations Closes #2774 --- src/Elasticsearch.sln | 10 +-- .../Alias/Actions/AliasRemoveIndex.cs | 45 +++++++++++++ .../Actions/AliasRemoveIndexOperation.cs | 10 +++ .../AliasManagement/Alias/BulkAliasRequest.cs | 11 ++-- src/Nest/Nest.csproj | 2 +- .../Alias/AliasApiRemoveIndexTests.cs | 66 +++++++++++++++++++ 6 files changed, 134 insertions(+), 10 deletions(-) create mode 100644 src/Nest/Indices/AliasManagement/Alias/Actions/AliasRemoveIndex.cs create mode 100644 src/Nest/Indices/AliasManagement/Alias/Actions/AliasRemoveIndexOperation.cs create mode 100644 src/Tests/Indices/AliasManagement/Alias/AliasApiRemoveIndexTests.cs diff --git a/src/Elasticsearch.sln b/src/Elasticsearch.sln index 51965ac1e5e..fec542660c8 100644 --- a/src/Elasticsearch.sln +++ b/src/Elasticsearch.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26228.9 +VisualStudioVersion = 15.0.26430.6 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nest", "Nest\Nest.csproj", "{072BA7DA-7B60-407D-8B6E-95E3186BE70C}" EndProject @@ -29,7 +29,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApiGenerator", "CodeGenerat EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elasticsearch.Net", "Elasticsearch.Net\Elasticsearch.Net.csproj", "{E97CCF40-0BA6-43FE-9F2D-58D454134088}" EndProject -Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "scripts", "..\build\scripts\scripts.fsproj", "{28328694-2598-44D3-BB25-8B6C3FBDD3EF}" +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "scripts", "..\build\scripts\scripts.fsproj", "{D6997ADC-E933-418E-831C-DE1A78897493}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj", "{37164C11-88EF-4428-803A-9AA24FB8B44D}" EndProject @@ -53,8 +53,8 @@ Global {E97CCF40-0BA6-43FE-9F2D-58D454134088}.Debug|Any CPU.Build.0 = Debug|Any CPU {E97CCF40-0BA6-43FE-9F2D-58D454134088}.Release|Any CPU.ActiveCfg = Release|Any CPU {E97CCF40-0BA6-43FE-9F2D-58D454134088}.Release|Any CPU.Build.0 = Release|Any CPU - {28328694-2598-44D3-BB25-8B6C3FBDD3EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {28328694-2598-44D3-BB25-8B6C3FBDD3EF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D6997ADC-E933-418E-831C-DE1A78897493}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D6997ADC-E933-418E-831C-DE1A78897493}.Release|Any CPU.ActiveCfg = Release|Any CPU {37164C11-88EF-4428-803A-9AA24FB8B44D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {37164C11-88EF-4428-803A-9AA24FB8B44D}.Debug|Any CPU.Build.0 = Debug|Any CPU {37164C11-88EF-4428-803A-9AA24FB8B44D}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -69,7 +69,7 @@ Global EndGlobalSection GlobalSection(NestedProjects) = preSolution {56A87048-9065-459B-826D-3DF68B409845} = {93331BEE-0AA0-47B7-B1D2-BD5BD31634D1} - {28328694-2598-44D3-BB25-8B6C3FBDD3EF} = {432D5575-2347-4D3C-BF8C-3E38410C46CA} + {D6997ADC-E933-418E-831C-DE1A78897493} = {432D5575-2347-4D3C-BF8C-3E38410C46CA} {98400F59-4BA8-4534-9A78-9C7FA0B42901} = {93331BEE-0AA0-47B7-B1D2-BD5BD31634D1} EndGlobalSection EndGlobal diff --git a/src/Nest/Indices/AliasManagement/Alias/Actions/AliasRemoveIndex.cs b/src/Nest/Indices/AliasManagement/Alias/Actions/AliasRemoveIndex.cs new file mode 100644 index 00000000000..20ad8de3677 --- /dev/null +++ b/src/Nest/Indices/AliasManagement/Alias/Actions/AliasRemoveIndex.cs @@ -0,0 +1,45 @@ +using System; +using Newtonsoft.Json; + +namespace Nest +{ + [JsonObject(MemberSerialization = MemberSerialization.OptIn)] + public interface IAliasRemoveIndexAction : IAliasAction + { + [JsonProperty("remove_index")] + AliasRemoveIndexOperation RemoveIndex { get; set; } + } + + public class AliasRemoveIndexAction : IAliasRemoveIndexAction + { + public AliasRemoveIndexOperation RemoveIndex { get; set; } + } + + public class AliasRemoveIndexDescriptor : DescriptorBase, IAliasRemoveIndexAction + { + AliasRemoveIndexOperation IAliasRemoveIndexAction.RemoveIndex { get; set; } + + public AliasRemoveIndexDescriptor() + { + Self.RemoveIndex = new AliasRemoveIndexOperation(); + } + + public AliasRemoveIndexDescriptor Index(IndexName index) + { + Self.RemoveIndex.Index = index; + return this; + } + + public AliasRemoveIndexDescriptor Index(Type index) + { + Self.RemoveIndex.Index = index; + return this; + } + + public AliasRemoveIndexDescriptor Index() where T : class + { + Self.RemoveIndex.Index = typeof(T); + return this; + } + } +} diff --git a/src/Nest/Indices/AliasManagement/Alias/Actions/AliasRemoveIndexOperation.cs b/src/Nest/Indices/AliasManagement/Alias/Actions/AliasRemoveIndexOperation.cs new file mode 100644 index 00000000000..b34df37c07b --- /dev/null +++ b/src/Nest/Indices/AliasManagement/Alias/Actions/AliasRemoveIndexOperation.cs @@ -0,0 +1,10 @@ +using Newtonsoft.Json; + +namespace Nest +{ + public class AliasRemoveIndexOperation + { + [JsonProperty("index")] + public IndexName Index { get; set; } + } +} diff --git a/src/Nest/Indices/AliasManagement/Alias/BulkAliasRequest.cs b/src/Nest/Indices/AliasManagement/Alias/BulkAliasRequest.cs index a7e35972b89..39af33b995f 100644 --- a/src/Nest/Indices/AliasManagement/Alias/BulkAliasRequest.cs +++ b/src/Nest/Indices/AliasManagement/Alias/BulkAliasRequest.cs @@ -4,22 +4,22 @@ namespace Nest { - public partial interface IBulkAliasRequest + public partial interface IBulkAliasRequest { [JsonProperty("actions")] IList Actions { get; set; } } - public partial class BulkAliasRequest + public partial class BulkAliasRequest { public IList Actions { get; set; } } [DescriptorFor("IndicesUpdateAliases")] - public partial class BulkAliasDescriptor + public partial class BulkAliasDescriptor { - public BulkAliasDescriptor Add(IAliasAction action) => + public BulkAliasDescriptor Add(IAliasAction action) => Fluent.Assign(this, a=> a.Actions.AddIfNotNull(action)); IList IBulkAliasRequest.Actions { get; set; } = new List(); @@ -27,5 +27,8 @@ public BulkAliasDescriptor Add(IAliasAction action) => public BulkAliasDescriptor Add(Func addSelector) => Add(addSelector?.Invoke(new AliasAddDescriptor())); public BulkAliasDescriptor Remove(Func removeSelector)=> Add(removeSelector?.Invoke(new AliasRemoveDescriptor())); + + public IBulkAliasRequest RemoveIndex(Func removeIndexSelector) => + Add(removeIndexSelector?.Invoke(new AliasRemoveIndexDescriptor())); } } diff --git a/src/Nest/Nest.csproj b/src/Nest/Nest.csproj index 8874695f150..708bd90f44b 100644 --- a/src/Nest/Nest.csproj +++ b/src/Nest/Nest.csproj @@ -19,4 +19,4 @@ - + \ No newline at end of file diff --git a/src/Tests/Indices/AliasManagement/Alias/AliasApiRemoveIndexTests.cs b/src/Tests/Indices/AliasManagement/Alias/AliasApiRemoveIndexTests.cs new file mode 100644 index 00000000000..f72882d4b66 --- /dev/null +++ b/src/Tests/Indices/AliasManagement/Alias/AliasApiRemoveIndexTests.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using Elasticsearch.Net; +using Nest; +using Tests.Framework; +using Tests.Framework.Integration; +using Tests.Framework.ManagedElasticsearch.Clusters; + +namespace Tests.Indices.AliasManagement.Alias +{ + public class AliasApiRemoveIndexTests : ApiIntegrationAgainstNewIndexTestBase + { + public AliasApiRemoveIndexTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { } + + protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values) + { + foreach (var value in values.Values) + { + var createIndexResponse = client.CreateIndex(value + "-1", c => c); + if (!createIndexResponse.IsValid) + throw new Exception(createIndexResponse.DebugInformation); + + createIndexResponse = client.CreateIndex(value + "-2", c => c); + if (!createIndexResponse.IsValid) + throw new Exception(createIndexResponse.DebugInformation); + } + } + + protected override LazyResponses ClientUsage() => Calls( + fluent: (client, f) => client.Alias(f), + fluentAsync: (client, f) => client.AliasAsync(f), + request: (client, r) => client.Alias(r), + requestAsync: (client, r) => client.AliasAsync(r) + ); + + protected override bool ExpectIsValid => true; + protected override int ExpectStatusCode => 200; + protected override HttpMethod HttpMethod => HttpMethod.POST; + protected override string UrlPath => $"/_aliases"; + + protected override bool SupportsDeserialization => false; + + protected override object ExpectJson => new + { + actions = new object[] + { + new Dictionary { { "add", new { alias = CallIsolatedValue + "-1", index = CallIsolatedValue + "-2" } } }, + new Dictionary { { "remove_index", new { index = CallIsolatedValue + "-1"} } }, + } + }; + + protected override Func Fluent => d => d + .Add(a=>a.Alias(CallIsolatedValue + "-1").Index(CallIsolatedValue + "-2")) + .RemoveIndex(a => a.Index(CallIsolatedValue + "-1")) + ; + + protected override BulkAliasRequest Initializer => new BulkAliasRequest + { + Actions = new List + { + new AliasAddAction { Add = new AliasAddOperation {Alias = CallIsolatedValue + "-1", Index = CallIsolatedValue + "-2"} }, + new AliasRemoveIndexAction {RemoveIndex = new AliasRemoveIndexOperation {Index = Infer.Index(CallIsolatedValue + "-1") }}, + } + }; + } +}