Skip to content

Commit 5a680ac

Browse files
[Backport master] Add migrate to data stream API in NEST (#5635)
* Add migrate to data stream API in NEST (#5633) * Add request and response types * Generate NEST code * Add and update tests * Fix test * Fixup * Update license headers * Update licence header * Update license header Co-authored-by: Steve Gordon <[email protected]>
1 parent 0d5722d commit 5a680ac

File tree

8 files changed

+242
-6
lines changed

8 files changed

+242
-6
lines changed

src/Nest/Descriptors.Indices.cs

+25
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,31 @@ public GetIndexTemplateDescriptor(Names name): base(r => r.Optional("name", name
988988
public GetIndexTemplateDescriptor MasterTimeout(Time mastertimeout) => Qs("master_timeout", mastertimeout);
989989
}
990990

991+
///<summary>Descriptor for MigrateToDataStream <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/data-streams.html</para></summary>
992+
public partial class MigrateToDataStreamDescriptor : RequestDescriptorBase<MigrateToDataStreamDescriptor, MigrateToDataStreamRequestParameters, IMigrateToDataStreamRequest>, IMigrateToDataStreamRequest
993+
{
994+
internal override ApiUrls ApiUrls => ApiUrlsLookups.IndicesMigrateToDataStream;
995+
996+
protected override HttpMethod HttpMethod => HttpMethod.POST;
997+
998+
protected override bool SupportsBody => false;
999+
///<summary>/_data_stream/_migrate/{name}</summary>
1000+
///<param name = "name">this parameter is required</param>
1001+
public MigrateToDataStreamDescriptor(Name name): base(r => r.Required("name", name))
1002+
{
1003+
}
1004+
1005+
///<summary>Used for serialization purposes, making sure we have a parameterless constructor</summary>
1006+
[SerializationConstructor]
1007+
protected MigrateToDataStreamDescriptor(): base()
1008+
{
1009+
}
1010+
1011+
// values part of the url path
1012+
Name IMigrateToDataStreamRequest.Name => Self.RouteValues.Get<Name>("name");
1013+
// Request parameters
1014+
}
1015+
9911016
///<summary>Descriptor for Open <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-open-close.html</para></summary>
9921017
public partial class OpenIndexDescriptor : RequestDescriptorBase<OpenIndexDescriptor, OpenIndexRequestParameters, IOpenIndexRequest>, IOpenIndexRequest
9931018
{

src/Nest/ElasticClient.Indices.cs

+24
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,30 @@ public Task<GetMappingResponse> GetMappingAsync<TDocument>(Func<GetMappingDescri
660660
/// </summary>
661661
public Task<GetIndexTemplateResponse> GetTemplateAsync(IGetIndexTemplateRequest request, CancellationToken ct = default) => DoRequestAsync<IGetIndexTemplateRequest, GetIndexTemplateResponse>(request, request.RequestParameters, ct);
662662
/// <summary>
663+
/// <c>POST</c> request to the <c>indices.migrate_to_data_stream</c> API, read more about this API online:
664+
/// <para></para>
665+
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/master/data-streams.html">https://www.elastic.co/guide/en/elasticsearch/reference/master/data-streams.html</a>
666+
/// </summary>
667+
public MigrateToDataStreamResponse MigrateToDataStream(Name name, Func<MigrateToDataStreamDescriptor, IMigrateToDataStreamRequest> selector = null) => MigrateToDataStream(selector.InvokeOrDefault(new MigrateToDataStreamDescriptor(name: name)));
668+
/// <summary>
669+
/// <c>POST</c> request to the <c>indices.migrate_to_data_stream</c> API, read more about this API online:
670+
/// <para></para>
671+
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/master/data-streams.html">https://www.elastic.co/guide/en/elasticsearch/reference/master/data-streams.html</a>
672+
/// </summary>
673+
public Task<MigrateToDataStreamResponse> MigrateToDataStreamAsync(Name name, Func<MigrateToDataStreamDescriptor, IMigrateToDataStreamRequest> selector = null, CancellationToken ct = default) => MigrateToDataStreamAsync(selector.InvokeOrDefault(new MigrateToDataStreamDescriptor(name: name)), ct);
674+
/// <summary>
675+
/// <c>POST</c> request to the <c>indices.migrate_to_data_stream</c> API, read more about this API online:
676+
/// <para></para>
677+
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/master/data-streams.html">https://www.elastic.co/guide/en/elasticsearch/reference/master/data-streams.html</a>
678+
/// </summary>
679+
public MigrateToDataStreamResponse MigrateToDataStream(IMigrateToDataStreamRequest request) => DoRequest<IMigrateToDataStreamRequest, MigrateToDataStreamResponse>(request, request.RequestParameters);
680+
/// <summary>
681+
/// <c>POST</c> request to the <c>indices.migrate_to_data_stream</c> API, read more about this API online:
682+
/// <para></para>
683+
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/master/data-streams.html">https://www.elastic.co/guide/en/elasticsearch/reference/master/data-streams.html</a>
684+
/// </summary>
685+
public Task<MigrateToDataStreamResponse> MigrateToDataStreamAsync(IMigrateToDataStreamRequest request, CancellationToken ct = default) => DoRequestAsync<IMigrateToDataStreamRequest, MigrateToDataStreamResponse>(request, request.RequestParameters, ct);
686+
/// <summary>
663687
/// <c>POST</c> request to the <c>indices.open</c> API, read more about this API online:
664688
/// <para></para>
665689
/// <a href = "https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-open-close.html">https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-open-close.html</a>

src/Nest/Requests.Indices.cs

+35
Original file line numberDiff line numberDiff line change
@@ -1705,6 +1705,41 @@ public Time MasterTimeout
17051705
}
17061706
}
17071707

1708+
[InterfaceDataContract]
1709+
public partial interface IMigrateToDataStreamRequest : IRequest<MigrateToDataStreamRequestParameters>
1710+
{
1711+
[IgnoreDataMember]
1712+
Name Name
1713+
{
1714+
get;
1715+
}
1716+
}
1717+
1718+
///<summary>Request for MigrateToDataStream <para>https://www.elastic.co/guide/en/elasticsearch/reference/master/data-streams.html</para></summary>
1719+
public partial class MigrateToDataStreamRequest : PlainRequestBase<MigrateToDataStreamRequestParameters>, IMigrateToDataStreamRequest
1720+
{
1721+
protected IMigrateToDataStreamRequest Self => this;
1722+
internal override ApiUrls ApiUrls => ApiUrlsLookups.IndicesMigrateToDataStream;
1723+
protected override HttpMethod HttpMethod => HttpMethod.POST;
1724+
protected override bool SupportsBody => false;
1725+
///<summary>/_data_stream/_migrate/{name}</summary>
1726+
///<param name = "name">this parameter is required</param>
1727+
public MigrateToDataStreamRequest(Name name): base(r => r.Required("name", name))
1728+
{
1729+
}
1730+
1731+
///<summary>Used for serialization purposes, making sure we have a parameterless constructor</summary>
1732+
[SerializationConstructor]
1733+
protected MigrateToDataStreamRequest(): base()
1734+
{
1735+
}
1736+
1737+
// values part of the url path
1738+
[IgnoreDataMember]
1739+
Name IMigrateToDataStreamRequest.Name => Self.RouteValues.Get<Name>("name");
1740+
// Request parameters
1741+
}
1742+
17081743
[InterfaceDataContract]
17091744
public partial interface IOpenIndexRequest : IRequest<OpenIndexRequestParameters>
17101745
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
namespace Nest
21+
{
22+
/// <summary>
23+
/// Converts an index alias to a data stream.
24+
/// </summary>
25+
[MapsApi("indices.migrate_to_data_stream.json")]
26+
[ReadAs(typeof(MigrateToDataStreamRequest))]
27+
public partial interface IMigrateToDataStreamRequest
28+
{
29+
}
30+
31+
/// <inheritdoc cref="IMigrateToDataStreamRequest"/>
32+
public partial class MigrateToDataStreamRequest : IMigrateToDataStreamRequest
33+
{
34+
}
35+
36+
/// <inheritdoc cref="IMigrateToDataStreamRequest"/>
37+
public partial class MigrateToDataStreamDescriptor
38+
{
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
namespace Nest
21+
{
22+
public class MigrateToDataStreamResponse : AcknowledgedResponseBase { }
23+
}

src/Nest/_Generated/ApiUrlsLookup.generated.cs

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ internal static class ApiUrlsLookups
150150
internal static ApiUrls IndicesGetMapping = new ApiUrls(new[]{"_mapping", "{index}/_mapping"});
151151
internal static ApiUrls IndicesGetSettings = new ApiUrls(new[]{"_settings", "{index}/_settings", "{index}/_settings/{name}", "_settings/{name}"});
152152
internal static ApiUrls IndicesGetTemplate = new ApiUrls(new[]{"_template", "_template/{name}"});
153+
internal static ApiUrls IndicesMigrateToDataStream = new ApiUrls(new[]{"_data_stream/_migrate/{name}"});
153154
internal static ApiUrls IndicesOpen = new ApiUrls(new[]{"{index}/_open"});
154155
internal static ApiUrls IndicesPutAlias = new ApiUrls(new[]{"{index}/_alias/{name}"});
155156
internal static ApiUrls IndicesPutMapping = new ApiUrls(new[]{"{index}/_mapping"});

tests/Tests/XPack/DataStreams/DataStreamsApiTests.cs

+57-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace Tests.XPack.DataStreams
3737
[SkipVersion("<7.9.0", "Introduced in 7.9.0")]
3838
public class DataStreamsApiTests : CoordinatedIntegrationTestBase<WritableCluster>
3939
{
40-
private static readonly Metric Document = new Metric
40+
private static readonly Metric Document = new()
4141
{
4242
Timestamp = new DateTime(2020, 8, 3, 14, 0, 0, DateTimeKind.Utc),
4343
Accept = 3,
@@ -47,13 +47,16 @@ public class DataStreamsApiTests : CoordinatedIntegrationTestBase<WritableCluste
4747
Response = 300,
4848
Total = 3
4949
};
50-
50+
5151
private const string CreateDataStreamStep = nameof(CreateDataStreamStep);
5252
private const string IndexStep = nameof(IndexStep);
5353
private const string GetDataStreamStep = nameof(GetDataStreamStep);
5454
private const string PutIndexTemplateStep = nameof(PutIndexTemplateStep);
5555
private const string DataStreamsStatsStep = nameof(DataStreamsStatsStep);
5656
private const string DeleteDataStreamStep = nameof(DeleteDataStreamStep);
57+
private const string PrepareIndexStep = nameof(PrepareIndexStep);
58+
private const string PrepareAliasStep = nameof(PrepareAliasStep);
59+
private const string MigrateToDataStreamStep = nameof(MigrateToDataStreamStep);
5760

5861
public DataStreamsApiTests(WritableCluster cluster, EndpointUsage usage) : base(new CoordinatedUsage(cluster, usage, testOnlyOne: true)
5962
{
@@ -109,10 +112,10 @@ public DataStreamsApiTests(WritableCluster cluster, EndpointUsage usage) : base(
109112
Refresh = Refresh.WaitFor
110113
},
111114
(v, d) => d.Index(v).Refresh(Refresh.WaitFor),
112-
(v, c, f) => c.Index<Metric>(Document, f),
113-
(v, c, f) => c.IndexAsync<Metric>(Document, f),
114-
(v, c, r) => c.Index<Metric>(r),
115-
(v, c, r) => c.IndexAsync<Metric>(r)
115+
(v, c, f) => c.Index(Document, f),
116+
(v, c, f) => c.IndexAsync(Document, f),
117+
(v, c, r) => c.Index(r),
118+
(v, c, r) => c.IndexAsync(r)
116119
)
117120
},
118121
{GetDataStreamStep, u =>
@@ -145,6 +148,48 @@ public DataStreamsApiTests(WritableCluster cluster, EndpointUsage usage) : base(
145148
(v, c, r) => c.Indices.DeleteDataStreamAsync(r)
146149
)
147150
},
151+
// Used for migrate step
152+
{PrepareIndexStep, ">= 7.13.0", u =>
153+
u.Calls<CreateIndexDescriptor, CreateIndexRequest, ICreateIndexRequest, CreateIndexResponse>(
154+
v => new CreateIndexRequest($"my-index{v}-test")
155+
{
156+
Mappings = new TypeMapping
157+
{
158+
Properties = new Properties
159+
{
160+
{ "@timestamp", new DateNanosProperty() }
161+
}
162+
}
163+
},
164+
(v, d) => d.Map(m=> m.Properties(p=> p.DateNanos(dn => dn.Name("@timestamp")))),
165+
(v, c, f) => c.Indices.Create($"my-index{v}-test", f),
166+
(v, c, f) => c.Indices.CreateAsync($"my-index{v}-test", f),
167+
(v, c, r) => c.Indices.Create(r),
168+
(v, c, r) => c.Indices.CreateAsync(r)
169+
)
170+
},
171+
// Used for migrate step
172+
{PrepareAliasStep,">= 7.13.0", u =>
173+
u.Calls<PutAliasDescriptor, PutAliasRequest, IPutAliasRequest, PutAliasResponse>(
174+
v => new PutAliasRequest($"my-index{v}-test", $"{v}-alias"),
175+
(v, d) => d,
176+
(v, c, f) => c.Indices.PutAlias($"my-index{v}-test", $"{v}-alias", f),
177+
(v, c, f) => c.Indices.PutAliasAsync($"my-index{v}-test", $"{v}-alias", f),
178+
(v, c, r) => c.Indices.PutAlias(r),
179+
(v, c, r) => c.Indices.PutAliasAsync(r)
180+
)
181+
},
182+
// Migrate to data stream added in 7.13.0
183+
{MigrateToDataStreamStep,">= 7.13.0", u =>
184+
u.Calls<MigrateToDataStreamDescriptor, MigrateToDataStreamRequest, IMigrateToDataStreamRequest, MigrateToDataStreamResponse>(
185+
v => new MigrateToDataStreamRequest($"{v}-alias"),
186+
(v, d) => d,
187+
(v, c, f) => c.Indices.MigrateToDataStream($"{v}-alias", f),
188+
(v, c, f) => c.Indices.MigrateToDataStreamAsync($"{v}-alias", f),
189+
(v, c, r) => c.Indices.MigrateToDataStream(r),
190+
(v, c, r) => c.Indices.MigrateToDataStreamAsync(r)
191+
)
192+
},
148193
}) { }
149194

150195
[I] public async Task CreateDataStreamResponse() => await Assert<CreateDataStreamResponse>(CreateDataStreamStep, (v, r) =>
@@ -199,5 +244,11 @@ [I] public async Task DeleteDataStreamResponse() => await Assert<DeleteDataStrea
199244
r.ShouldBeValid();
200245
r.Acknowledged.Should().BeTrue();
201246
});
247+
248+
[I] public async Task MigrateToDataStreamResponse() => await Assert<MigrateToDataStreamResponse>(MigrateToDataStreamStep, r =>
249+
{
250+
r.ShouldBeValid();
251+
r.Acknowledged.Should().BeTrue();
252+
});
202253
}
203254
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
using System.Threading.Tasks;
21+
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
22+
using Nest;
23+
using Tests.Framework.EndpointTests;
24+
using static Tests.Framework.EndpointTests.UrlTester;
25+
26+
namespace Tests.XPack.DataStreams.Migrate
27+
{
28+
public class MigrateToDataStreamUrlTests : UrlTestsBase
29+
{
30+
[U]
31+
public override async Task Urls() => await POST("/_data_stream/_migrate/stream")
32+
.Fluent(c => c.Indices.MigrateToDataStream("stream", f => f))
33+
.Request(c => c.Indices.MigrateToDataStream(new MigrateToDataStreamRequest("stream")))
34+
.FluentAsync(c => c.Indices.MigrateToDataStreamAsync("stream", f => f))
35+
.RequestAsync(c => c.Indices.MigrateToDataStreamAsync(new MigrateToDataStreamRequest("stream")));
36+
}
37+
}

0 commit comments

Comments
 (0)