Skip to content

Commit a8eef5a

Browse files
stevejgordongithub-actions[bot]
authored andcommitted
Support _meta on ILM policy (#5812)
1 parent 05d6957 commit a8eef5a

File tree

2 files changed

+114
-41
lines changed

2 files changed

+114
-41
lines changed

src/Nest/XPack/Ilm/Policy.cs

+22
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,48 @@
33
// See the LICENSE file in the project root for more information
44

55
using System;
6+
using System.Collections.Generic;
67
using System.Runtime.Serialization;
8+
using Elasticsearch.Net.Utf8Json;
79

810
namespace Nest
911
{
1012
[ReadAs(typeof(Policy))]
1113
public interface IPolicy
1214
{
15+
/// <summary>
16+
/// Custom meta data to associated with the policy. Not used by Elasticsearch,
17+
/// but can be used to store application-specific metadata.
18+
/// </summary>
19+
[DataMember(Name = "_meta")]
20+
[JsonFormatter(typeof(VerbatimDictionaryInterfaceKeysFormatter<string, object>))]
21+
IDictionary<string, object> Meta { get; set; }
22+
1323
[DataMember(Name = "phases")]
1424
IPhases Phases { get; set; }
1525
}
1626

1727
public class Policy : IPolicy
1828
{
29+
/// <inheritdoc />
30+
public IDictionary<string, object> Meta { get; set; }
31+
1932
public IPhases Phases { get; set; }
2033
}
2134

2235
public class PolicyDescriptor : DescriptorBase<PolicyDescriptor, IPolicy>, IPolicy
2336
{
37+
IDictionary<string, object> IPolicy.Meta { get; set; }
2438
IPhases IPolicy.Phases { get; set; }
39+
40+
/// <inheritdoc cref="IPolicy.Meta" />
41+
public PolicyDescriptor Meta(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> metaSelector) =>
42+
Assign(metaSelector(new FluentDictionary<string, object>()), (a, v) => a.Meta = v);
2543

44+
/// <inheritdoc cref="IPolicy.Meta" />
45+
public PolicyDescriptor Meta(Dictionary<string, object> metaDictionary) => Assign(metaDictionary, (a, v) => a.Meta = v);
46+
47+
/// <inheritdoc cref="ITypeMapping.Properties" />
2648
public PolicyDescriptor Phases(Func<PhasesDescriptor, IPhases> selector) =>
2749
Assign(selector, (a, v) => a.Phases = v?.InvokeOrDefault(new PhasesDescriptor()));
2850
}

tests/Tests/XPack/Ilm/IlmApiTests.cs

+92-41
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,29 @@
77
using System.Linq;
88
using System.Threading.Tasks;
99
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
10+
using Elasticsearch.Net;
1011
using FluentAssertions;
1112
using Nest;
1213
using Tests.Core.ManagedElasticsearch.Clusters;
1314
using Tests.Domain;
1415
using Tests.Framework.EndpointTests;
1516
using Tests.Framework.EndpointTests.TestState;
17+
using static Elasticsearch.Net.HttpMethod;
1618

1719
namespace Tests.XPack.Ilm
1820
{
1921
[SkipVersion("<6.7.0", "All APIs exist in Elasticsearch 6.7.0")]
2022
public class IlmApiTests : CoordinatedIntegrationTestBase<XPackCluster>
2123
{
22-
private const string IlmGetStatusStep = nameof(IlmGetStatusStep);
23-
private const string IlmPutLifecycleStep = nameof(IlmPutLifecycleStep);
24-
private const string IlmGetLifecycleStep = nameof(IlmGetLifecycleStep);
25-
private const string IlmGeAllLifecycleStep = nameof(IlmGeAllLifecycleStep);
2624
private const string IlmDeleteLifecycleStep = nameof(IlmDeleteLifecycleStep);
27-
private const string PutDocumentStep = nameof(PutDocumentStep);
2825
private const string IlmExplainLifecycleStep = nameof(IlmExplainLifecycleStep);
26+
private const string IlmGeAllLifecycleStep = nameof(IlmGeAllLifecycleStep);
27+
private const string IlmGetLifecycleStep = nameof(IlmGetLifecycleStep);
28+
private const string IlmGetStatusStep = nameof(IlmGetStatusStep);
29+
private const string IlmPutLifecycleStep = nameof(IlmPutLifecycleStep);
2930
private const string IlmRemovePolicyStep = nameof(IlmRemovePolicyStep);
3031
private const string IlmStopStep = nameof(IlmStopStep);
32+
private const string PutDocumentStep = nameof(PutDocumentStep);
3133

3234
public IlmApiTests(XPackCluster cluster, EndpointUsage usage) : base(new CoordinatedUsage(cluster, usage)
3335
{
@@ -42,14 +44,15 @@ public IlmApiTests(XPackCluster cluster, EndpointUsage usage) : base(new Coordin
4244
)
4345
},
4446
{
45-
IlmExplainLifecycleStep, u => u.Calls<ExplainLifecycleDescriptor, ExplainLifecycleRequest, IExplainLifecycleRequest, ExplainLifecycleResponse>(
46-
v => new ExplainLifecycleRequest("project"),
47-
(v, d) => d,
48-
(v, c, f) => c.IndexLifecycleManagement.ExplainLifecycle("project", f),
49-
(v, c, f) => c.IndexLifecycleManagement.ExplainLifecycleAsync("project", f),
50-
(v, c, r) => c.IndexLifecycleManagement.ExplainLifecycle(r),
51-
(v, c, r) => c.IndexLifecycleManagement.ExplainLifecycleAsync(r)
52-
)
47+
IlmExplainLifecycleStep, u =>
48+
u.Calls<ExplainLifecycleDescriptor, ExplainLifecycleRequest, IExplainLifecycleRequest, ExplainLifecycleResponse>(
49+
v => new ExplainLifecycleRequest("project"),
50+
(v, d) => d,
51+
(v, c, f) => c.IndexLifecycleManagement.ExplainLifecycle("project", f),
52+
(v, c, f) => c.IndexLifecycleManagement.ExplainLifecycleAsync("project", f),
53+
(v, c, r) => c.IndexLifecycleManagement.ExplainLifecycle(r),
54+
(v, c, r) => c.IndexLifecycleManagement.ExplainLifecycleAsync(r)
55+
)
5356
},
5457
{
5558
IlmGetStatusStep, u => u.Calls<GetIlmStatusDescriptor, GetIlmStatusRequest, IGetIlmStatusRequest, GetIlmStatusResponse>(
@@ -73,32 +76,15 @@ public IlmApiTests(XPackCluster cluster, EndpointUsage usage) : base(new Coordin
7376
{
7477
Actions = new LifecycleActions
7578
{
76-
new FreezeLifecycleAction(),
77-
new SetPriorityLifecycleAction
78-
{
79-
Priority = 50
80-
}
79+
new FreezeLifecycleAction(), new SetPriorityLifecycleAction { Priority = 50 }
8180
}
8281
},
8382
Warm = new Phase
8483
{
8584
MinimumAge = "10d",
86-
Actions = new LifecycleActions
87-
{
88-
new ForceMergeLifecycleAction
89-
{
90-
MaximumNumberOfSegments = 1
91-
}
92-
}
85+
Actions = new LifecycleActions { new ForceMergeLifecycleAction { MaximumNumberOfSegments = 1 } }
9386
},
94-
Delete = new Phase
95-
{
96-
MinimumAge = "30d",
97-
Actions = new LifecycleActions
98-
{
99-
new DeleteLifecycleAction()
100-
}
101-
}
87+
Delete = new Phase { MinimumAge = "30d", Actions = new LifecycleActions { new DeleteLifecycleAction() } }
10288
}
10389
}
10490
},
@@ -163,14 +149,15 @@ public IlmApiTests(XPackCluster cluster, EndpointUsage usage) : base(new Coordin
163149
)
164150
},
165151
{
166-
IlmDeleteLifecycleStep, u => u.Calls<DeleteLifecycleDescriptor, DeleteLifecycleRequest, IDeleteLifecycleRequest, DeleteLifecycleResponse>(
167-
v => new DeleteLifecycleRequest("policy" + v),
168-
(v, d) => d,
169-
(v, c, f) => c.IndexLifecycleManagement.DeleteLifecycle("policy" + v, f),
170-
(v, c, f) => c.IndexLifecycleManagement.DeleteLifecycleAsync("policy" + v, f),
171-
(v, c, r) => c.IndexLifecycleManagement.DeleteLifecycle(r),
172-
(v, c, r) => c.IndexLifecycleManagement.DeleteLifecycleAsync(r)
173-
)
152+
IlmDeleteLifecycleStep, u =>
153+
u.Calls<DeleteLifecycleDescriptor, DeleteLifecycleRequest, IDeleteLifecycleRequest, DeleteLifecycleResponse>(
154+
v => new DeleteLifecycleRequest("policy" + v),
155+
(v, d) => d,
156+
(v, c, f) => c.IndexLifecycleManagement.DeleteLifecycle("policy" + v, f),
157+
(v, c, f) => c.IndexLifecycleManagement.DeleteLifecycleAsync("policy" + v, f),
158+
(v, c, r) => c.IndexLifecycleManagement.DeleteLifecycle(r),
159+
(v, c, r) => c.IndexLifecycleManagement.DeleteLifecycleAsync(r)
160+
)
174161
},
175162
{
176163
IlmStopStep, u => u.Calls<StopIlmDescriptor, StopIlmRequest, IStopIlmRequest, StopIlmResponse>(
@@ -285,4 +272,68 @@ [I] public async Task IlmDeleteLifecycleResponse() => await Assert<DeleteLifecyc
285272
r.Acknowledged.Should().BeTrue();
286273
});
287274
}
275+
276+
[SkipVersion("<7.14.0", "_meta introduced in 7.14.0")]
277+
public class PutIlmLifecyclePolicyApiTests
278+
: ApiTestBase<XPackCluster, PutLifecycleResponse, IPutLifecycleRequest, PutLifecycleDescriptor, PutLifecycleRequest>
279+
{
280+
public PutIlmLifecyclePolicyApiTests(XPackCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
281+
282+
protected override object ExpectJson => new
283+
{
284+
policy = new
285+
{
286+
_meta = new { foo = "bar" },
287+
phases = new { cold = new { actions = new { freeze = new object(), set_priority = new { priority = 50 } } } }
288+
}
289+
};
290+
291+
protected override Func<PutLifecycleDescriptor, IPutLifecycleRequest> Fluent => f => f
292+
.Policy(p => p
293+
.Phases(a => a
294+
.Cold(w => w
295+
.Actions(ac => ac
296+
.Freeze(fr => fr)
297+
.SetPriority(pr => pr.Priority(50))
298+
)
299+
)
300+
)
301+
.Meta(m => m
302+
.Add("foo", "bar")
303+
)
304+
);
305+
306+
protected override HttpMethod HttpMethod => PUT;
307+
308+
protected override PutLifecycleRequest Initializer => new(CallIsolatedValue)
309+
{
310+
Policy = new Policy
311+
{
312+
Phases = new Phases
313+
{
314+
Cold = new Phase
315+
{
316+
Actions = new LifecycleActions
317+
{
318+
new FreezeLifecycleAction(), new SetPriorityLifecycleAction { Priority = 50 }
319+
}
320+
}
321+
},
322+
Meta = new Dictionary<string, object> { { "foo", "bar" } }
323+
}
324+
};
325+
326+
protected override bool SupportsDeserialization => false;
327+
328+
protected override string UrlPath => $"/_ilm/policy/{CallIsolatedValue}";
329+
330+
protected override PutLifecycleDescriptor NewDescriptor() => new(CallIsolatedValue);
331+
332+
protected override LazyResponses ClientUsage() => Calls(
333+
(client, f) => client.IndexLifecycleManagement.PutLifecycle(CallIsolatedValue, f),
334+
(client, f) => client.IndexLifecycleManagement.PutLifecycleAsync(CallIsolatedValue, f),
335+
(client, r) => client.IndexLifecycleManagement.PutLifecycle(r),
336+
(client, r) => client.IndexLifecycleManagement.PutLifecycleAsync(r)
337+
);
338+
}
288339
}

0 commit comments

Comments
 (0)