From c723247f079ab418fb8094c6cfc041bda55e0abf Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Mon, 26 Apr 2021 10:13:33 +0100 Subject: [PATCH 1/5] Add migrate to data stream API in NEST (#5633) * Add request and response types * Generate NEST code * Add and update tests * Fix test --- src/Nest/Descriptors.Indices.cs | 21 +++++++ src/Nest/ElasticClient.Indices.cs | 24 +++++++ src/Nest/Requests.Indices.cs | 33 ++++++++++ .../Migrate/MigrateToDataStreamRequest.cs | 25 ++++++++ .../Migrate/MigrateToDataStreamResponse.cs | 8 +++ .../_Generated/ApiUrlsLookup.generated.cs | 1 + .../XPack/DataStreams/DataStreamsApiTests.cs | 63 +++++++++++++++++-- .../Migrate/MigrateToDataStreamUrlTests.cs | 18 ++++++ 8 files changed, 187 insertions(+), 6 deletions(-) create mode 100644 src/Nest/XPack/DataStreams/Migrate/MigrateToDataStreamRequest.cs create mode 100644 src/Nest/XPack/DataStreams/Migrate/MigrateToDataStreamResponse.cs create mode 100644 tests/Tests/XPack/DataStreams/Migrate/MigrateToDataStreamUrlTests.cs diff --git a/src/Nest/Descriptors.Indices.cs b/src/Nest/Descriptors.Indices.cs index 63047b5366f..65fc5760b6c 100644 --- a/src/Nest/Descriptors.Indices.cs +++ b/src/Nest/Descriptors.Indices.cs @@ -988,6 +988,27 @@ public GetIndexTemplateDescriptor(Names name): base(r => r.Optional("name", name public GetIndexTemplateDescriptor MasterTimeout(Time mastertimeout) => Qs("master_timeout", mastertimeout); } + ///Descriptor for MigrateToDataStream https://www.elastic.co/guide/en/elasticsearch/reference/master/data-streams.html + public partial class MigrateToDataStreamDescriptor : RequestDescriptorBase, IMigrateToDataStreamRequest + { + internal override ApiUrls ApiUrls => ApiUrlsLookups.IndicesMigrateToDataStream; + ////_data_stream/_migrate/{name} + ///this parameter is required + public MigrateToDataStreamDescriptor(Name name): base(r => r.Required("name", name)) + { + } + + ///Used for serialization purposes, making sure we have a parameterless constructor + [SerializationConstructor] + protected MigrateToDataStreamDescriptor(): base() + { + } + + // values part of the url path + Name IMigrateToDataStreamRequest.Name => Self.RouteValues.Get("name"); + // Request parameters + } + ///Descriptor for Open https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-open-close.html public partial class OpenIndexDescriptor : RequestDescriptorBase, IOpenIndexRequest { diff --git a/src/Nest/ElasticClient.Indices.cs b/src/Nest/ElasticClient.Indices.cs index 5987b34ca20..aa037e71317 100644 --- a/src/Nest/ElasticClient.Indices.cs +++ b/src/Nest/ElasticClient.Indices.cs @@ -660,6 +660,30 @@ public Task GetMappingAsync(Func public Task GetTemplateAsync(IGetIndexTemplateRequest request, CancellationToken ct = default) => DoRequestAsync(request, request.RequestParameters, ct); /// + /// POST request to the indices.migrate_to_data_stream API, read more about this API online: + /// + /// https://www.elastic.co/guide/en/elasticsearch/reference/master/data-streams.html + /// + public MigrateToDataStreamResponse MigrateToDataStream(Name name, Func selector = null) => MigrateToDataStream(selector.InvokeOrDefault(new MigrateToDataStreamDescriptor(name: name))); + /// + /// POST request to the indices.migrate_to_data_stream API, read more about this API online: + /// + /// https://www.elastic.co/guide/en/elasticsearch/reference/master/data-streams.html + /// + public Task MigrateToDataStreamAsync(Name name, Func selector = null, CancellationToken ct = default) => MigrateToDataStreamAsync(selector.InvokeOrDefault(new MigrateToDataStreamDescriptor(name: name)), ct); + /// + /// POST request to the indices.migrate_to_data_stream API, read more about this API online: + /// + /// https://www.elastic.co/guide/en/elasticsearch/reference/master/data-streams.html + /// + public MigrateToDataStreamResponse MigrateToDataStream(IMigrateToDataStreamRequest request) => DoRequest(request, request.RequestParameters); + /// + /// POST request to the indices.migrate_to_data_stream API, read more about this API online: + /// + /// https://www.elastic.co/guide/en/elasticsearch/reference/master/data-streams.html + /// + public Task MigrateToDataStreamAsync(IMigrateToDataStreamRequest request, CancellationToken ct = default) => DoRequestAsync(request, request.RequestParameters, ct); + /// /// POST request to the indices.open API, read more about this API online: /// /// https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-open-close.html diff --git a/src/Nest/Requests.Indices.cs b/src/Nest/Requests.Indices.cs index fba4210ba98..03aac44c043 100644 --- a/src/Nest/Requests.Indices.cs +++ b/src/Nest/Requests.Indices.cs @@ -1705,6 +1705,39 @@ public Time MasterTimeout } } + [InterfaceDataContract] + public partial interface IMigrateToDataStreamRequest : IRequest + { + [IgnoreDataMember] + Name Name + { + get; + } + } + + ///Request for MigrateToDataStream https://www.elastic.co/guide/en/elasticsearch/reference/master/data-streams.html + public partial class MigrateToDataStreamRequest : PlainRequestBase, IMigrateToDataStreamRequest + { + protected IMigrateToDataStreamRequest Self => this; + internal override ApiUrls ApiUrls => ApiUrlsLookups.IndicesMigrateToDataStream; + ////_data_stream/_migrate/{name} + ///this parameter is required + public MigrateToDataStreamRequest(Name name): base(r => r.Required("name", name)) + { + } + + ///Used for serialization purposes, making sure we have a parameterless constructor + [SerializationConstructor] + protected MigrateToDataStreamRequest(): base() + { + } + + // values part of the url path + [IgnoreDataMember] + Name IMigrateToDataStreamRequest.Name => Self.RouteValues.Get("name"); + // Request parameters + } + [InterfaceDataContract] public partial interface IOpenIndexRequest : IRequest { diff --git a/src/Nest/XPack/DataStreams/Migrate/MigrateToDataStreamRequest.cs b/src/Nest/XPack/DataStreams/Migrate/MigrateToDataStreamRequest.cs new file mode 100644 index 00000000000..5ac1c7c03ea --- /dev/null +++ b/src/Nest/XPack/DataStreams/Migrate/MigrateToDataStreamRequest.cs @@ -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 +{ + /// + /// Converts an index alias to a data stream. + /// + [MapsApi("indices.migrate_to_data_stream.json")] + [ReadAs(typeof(MigrateToDataStreamRequest))] + public partial interface IMigrateToDataStreamRequest + { + } + + /// + public partial class MigrateToDataStreamRequest : IMigrateToDataStreamRequest + { + } + + /// + public partial class MigrateToDataStreamDescriptor + { + } +} diff --git a/src/Nest/XPack/DataStreams/Migrate/MigrateToDataStreamResponse.cs b/src/Nest/XPack/DataStreams/Migrate/MigrateToDataStreamResponse.cs new file mode 100644 index 00000000000..d2e38117604 --- /dev/null +++ b/src/Nest/XPack/DataStreams/Migrate/MigrateToDataStreamResponse.cs @@ -0,0 +1,8 @@ +// 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 +{ + public class MigrateToDataStreamResponse : AcknowledgedResponseBase { } +} diff --git a/src/Nest/_Generated/ApiUrlsLookup.generated.cs b/src/Nest/_Generated/ApiUrlsLookup.generated.cs index 8a322a04617..292785f1150 100644 --- a/src/Nest/_Generated/ApiUrlsLookup.generated.cs +++ b/src/Nest/_Generated/ApiUrlsLookup.generated.cs @@ -150,6 +150,7 @@ internal static class ApiUrlsLookups internal static ApiUrls IndicesGetMapping = new ApiUrls(new[]{"_mapping", "{index}/_mapping"}); internal static ApiUrls IndicesGetSettings = new ApiUrls(new[]{"_settings", "{index}/_settings", "{index}/_settings/{name}", "_settings/{name}"}); internal static ApiUrls IndicesGetTemplate = new ApiUrls(new[]{"_template", "_template/{name}"}); + internal static ApiUrls IndicesMigrateToDataStream = new ApiUrls(new[]{"_data_stream/_migrate/{name}"}); internal static ApiUrls IndicesOpen = new ApiUrls(new[]{"{index}/_open"}); internal static ApiUrls IndicesPutAlias = new ApiUrls(new[]{"{index}/_alias/{name}"}); internal static ApiUrls IndicesPutMapping = new ApiUrls(new[]{"{index}/_mapping"}); diff --git a/tests/Tests/XPack/DataStreams/DataStreamsApiTests.cs b/tests/Tests/XPack/DataStreams/DataStreamsApiTests.cs index 82d6af81e88..00011671d25 100644 --- a/tests/Tests/XPack/DataStreams/DataStreamsApiTests.cs +++ b/tests/Tests/XPack/DataStreams/DataStreamsApiTests.cs @@ -37,7 +37,7 @@ namespace Tests.XPack.DataStreams [SkipVersion("<7.9.0", "Introduced in 7.9.0")] public class DataStreamsApiTests : CoordinatedIntegrationTestBase { - private static readonly Metric Document = new Metric + private static readonly Metric Document = new() { Timestamp = new DateTime(2020, 8, 3, 14, 0, 0, DateTimeKind.Utc), Accept = 3, @@ -47,13 +47,16 @@ public class DataStreamsApiTests : CoordinatedIntegrationTestBase d.Index(v).Refresh(Refresh.WaitFor), - (v, c, f) => c.Index(Document, f), - (v, c, f) => c.IndexAsync(Document, f), - (v, c, r) => c.Index(r), - (v, c, r) => c.IndexAsync(r) + (v, c, f) => c.Index(Document, f), + (v, c, f) => c.IndexAsync(Document, f), + (v, c, r) => c.Index(r), + (v, c, r) => c.IndexAsync(r) ) }, {GetDataStreamStep, u => @@ -145,6 +148,48 @@ public DataStreamsApiTests(WritableCluster cluster, EndpointUsage usage) : base( (v, c, r) => c.Indices.DeleteDataStreamAsync(r) ) }, + // Used for migrate step + {PrepareIndexStep, ">= 7.13.0", u => + u.Calls( + v => new CreateIndexRequest($"my-index{v}-test") + { + Mappings = new TypeMapping + { + Properties = new Properties + { + { "@timestamp", new DateNanosProperty() } + } + } + }, + (v, d) => d.Map(m=> m.Properties(p=> p.DateNanos(dn => dn.Name("@timestamp")))), + (v, c, f) => c.Indices.Create($"my-index{v}-test", f), + (v, c, f) => c.Indices.CreateAsync($"my-index{v}-test", f), + (v, c, r) => c.Indices.Create(r), + (v, c, r) => c.Indices.CreateAsync(r) + ) + }, + // Used for migrate step + {PrepareAliasStep,">= 7.13.0", u => + u.Calls( + v => new PutAliasRequest($"my-index{v}-test", $"{v}-alias"), + (v, d) => d, + (v, c, f) => c.Indices.PutAlias($"my-index{v}-test", $"{v}-alias", f), + (v, c, f) => c.Indices.PutAliasAsync($"my-index{v}-test", $"{v}-alias", f), + (v, c, r) => c.Indices.PutAlias(r), + (v, c, r) => c.Indices.PutAliasAsync(r) + ) + }, + // Migrate to data stream added in 7.13.0 + {MigrateToDataStreamStep,">= 7.13.0", u => + u.Calls( + v => new MigrateToDataStreamRequest($"{v}-alias"), + (v, d) => d, + (v, c, f) => c.Indices.MigrateToDataStream($"{v}-alias", f), + (v, c, f) => c.Indices.MigrateToDataStreamAsync($"{v}-alias", f), + (v, c, r) => c.Indices.MigrateToDataStream(r), + (v, c, r) => c.Indices.MigrateToDataStreamAsync(r) + ) + }, }) { } [I] public async Task CreateDataStreamResponse() => await Assert(CreateDataStreamStep, (v, r) => @@ -199,5 +244,11 @@ [I] public async Task DeleteDataStreamResponse() => await Assert await Assert(MigrateToDataStreamStep, r => + { + r.ShouldBeValid(); + r.Acknowledged.Should().BeTrue(); + }); } } diff --git a/tests/Tests/XPack/DataStreams/Migrate/MigrateToDataStreamUrlTests.cs b/tests/Tests/XPack/DataStreams/Migrate/MigrateToDataStreamUrlTests.cs new file mode 100644 index 00000000000..38097a29ad9 --- /dev/null +++ b/tests/Tests/XPack/DataStreams/Migrate/MigrateToDataStreamUrlTests.cs @@ -0,0 +1,18 @@ +using System.Threading.Tasks; +using Elastic.Elasticsearch.Xunit.XunitPlumbing; +using Nest; +using Tests.Framework.EndpointTests; +using static Tests.Framework.EndpointTests.UrlTester; + +namespace Tests.XPack.DataStreams.Migrate +{ + public class MigrateToDataStreamUrlTests : UrlTestsBase + { + [U] + public override async Task Urls() => await POST("/_data_stream/_migrate/stream") + .Fluent(c => c.Indices.MigrateToDataStream("stream", f => f)) + .Request(c => c.Indices.MigrateToDataStream(new MigrateToDataStreamRequest("stream"))) + .FluentAsync(c => c.Indices.MigrateToDataStreamAsync("stream", f => f)) + .RequestAsync(c => c.Indices.MigrateToDataStreamAsync(new MigrateToDataStreamRequest("stream"))); + } +} From 194b4c135927052de50993b9f681b5c3727b7a42 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Tue, 27 Apr 2021 11:47:00 +0100 Subject: [PATCH 2/5] Fixup --- src/Nest/Descriptors.Indices.cs | 4 ++++ src/Nest/Requests.Indices.cs | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/Nest/Descriptors.Indices.cs b/src/Nest/Descriptors.Indices.cs index 65fc5760b6c..a0eb53ace95 100644 --- a/src/Nest/Descriptors.Indices.cs +++ b/src/Nest/Descriptors.Indices.cs @@ -992,6 +992,10 @@ public GetIndexTemplateDescriptor(Names name): base(r => r.Optional("name", name public partial class MigrateToDataStreamDescriptor : RequestDescriptorBase, IMigrateToDataStreamRequest { internal override ApiUrls ApiUrls => ApiUrlsLookups.IndicesMigrateToDataStream; + + protected override HttpMethod HttpMethod => HttpMethod.POST; + + protected override bool SupportsBody => false; ////_data_stream/_migrate/{name} ///this parameter is required public MigrateToDataStreamDescriptor(Name name): base(r => r.Required("name", name)) diff --git a/src/Nest/Requests.Indices.cs b/src/Nest/Requests.Indices.cs index 03aac44c043..7ff814fbdb6 100644 --- a/src/Nest/Requests.Indices.cs +++ b/src/Nest/Requests.Indices.cs @@ -1720,6 +1720,8 @@ public partial class MigrateToDataStreamRequest : PlainRequestBase this; internal override ApiUrls ApiUrls => ApiUrlsLookups.IndicesMigrateToDataStream; + protected override HttpMethod HttpMethod => HttpMethod.POST; + protected override bool SupportsBody => false; ////_data_stream/_migrate/{name} ///this parameter is required public MigrateToDataStreamRequest(Name name): base(r => r.Required("name", name)) From ba212d81813efed0a5c83dc9cac4ba106b5940e2 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Tue, 27 Apr 2021 11:56:39 +0100 Subject: [PATCH 3/5] Update license headers --- .../Migrate/MigrateToDataStreamResponse.cs | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Nest/XPack/DataStreams/Migrate/MigrateToDataStreamResponse.cs b/src/Nest/XPack/DataStreams/Migrate/MigrateToDataStreamResponse.cs index d2e38117604..9d939c96342 100644 --- a/src/Nest/XPack/DataStreams/Migrate/MigrateToDataStreamResponse.cs +++ b/src/Nest/XPack/DataStreams/Migrate/MigrateToDataStreamResponse.cs @@ -1,7 +1,22 @@ -// 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 contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. 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. + */ + namespace Nest { public class MigrateToDataStreamResponse : AcknowledgedResponseBase { } From 783f1ce37b41326b6be1ab198ad64910881c1b70 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Tue, 27 Apr 2021 11:58:35 +0100 Subject: [PATCH 4/5] Update licence header --- .../Migrate/MigrateToDataStreamRequest.cs | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Nest/XPack/DataStreams/Migrate/MigrateToDataStreamRequest.cs b/src/Nest/XPack/DataStreams/Migrate/MigrateToDataStreamRequest.cs index 5ac1c7c03ea..029dddcd566 100644 --- a/src/Nest/XPack/DataStreams/Migrate/MigrateToDataStreamRequest.cs +++ b/src/Nest/XPack/DataStreams/Migrate/MigrateToDataStreamRequest.cs @@ -1,6 +1,21 @@ -// 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 contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. 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. + */ namespace Nest { From 70760a3de7cb127b8b12c38614efdfc70c2c2da4 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Tue, 27 Apr 2021 12:03:27 +0100 Subject: [PATCH 5/5] Update license header --- .../Migrate/MigrateToDataStreamUrlTests.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/Tests/XPack/DataStreams/Migrate/MigrateToDataStreamUrlTests.cs b/tests/Tests/XPack/DataStreams/Migrate/MigrateToDataStreamUrlTests.cs index 38097a29ad9..e1067e18870 100644 --- a/tests/Tests/XPack/DataStreams/Migrate/MigrateToDataStreamUrlTests.cs +++ b/tests/Tests/XPack/DataStreams/Migrate/MigrateToDataStreamUrlTests.cs @@ -1,3 +1,22 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. 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. + */ + using System.Threading.Tasks; using Elastic.Elasticsearch.Xunit.XunitPlumbing; using Nest;