Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WaitForSnapshot ILM lifecycle action #4375

Merged
merged 1 commit into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Nest/XPack/Ilm/Actions/WaitForSnapshotLifecycleAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Runtime.Serialization;

namespace Nest
{
/// <summary>
/// The Wait For Snapshot Action waits for defined SLM policy to be executed to ensure that snapshot of index exists before deletion.
/// <para></para>
/// Available in Elasticsearch 7.6.0+
/// </summary>
public interface IWaitForSnapshotLifecycleAction : ILifecycleAction
{
/// <summary>
/// The Snapshot Lifecycle Management (SLM) policy name that this action should wait for
/// </summary>
[DataMember(Name = "policy")]
string Policy { get; set; }
}

/// <inheritdoc />
public class WaitForSnapshotLifecycleAction : IWaitForSnapshotLifecycleAction
{
/// <inheritdoc />
public string Policy { get; set; }
}

/// <inheritdoc cref="IWaitForSnapshotLifecycleAction"/>
public class WaitForSnapshotLifecycleActionDescriptor
: DescriptorBase<WaitForSnapshotLifecycleActionDescriptor, IWaitForSnapshotLifecycleAction>, IWaitForSnapshotLifecycleAction
{
string IWaitForSnapshotLifecycleAction.Policy { get; set; }

/// <inheritdoc cref="IWaitForSnapshotLifecycleAction.Policy"/>
public WaitForSnapshotLifecycleActionDescriptor Policy(string policy) => Assign(policy, (a, v) => a.Policy = policy);
}
}
33 changes: 33 additions & 0 deletions src/Nest/XPack/Ilm/LifecycleActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,35 @@ public LifecycleActions() { }

public LifecycleActions(IDictionary<string, ILifecycleAction> container) : base(container) { }

/// <inheritdoc cref="IAllocateLifecycleAction"/>
public void Add(IAllocateLifecycleAction action) => BackingDictionary.Add("allocate", action);

/// <inheritdoc cref="IDeleteLifecycleAction"/>
public void Add(IDeleteLifecycleAction action) => BackingDictionary.Add("delete", action);

/// <inheritdoc cref="IForceMergeLifecycleAction"/>
public void Add(IForceMergeLifecycleAction action) => BackingDictionary.Add("forcemerge", action);

/// <inheritdoc cref="IFreezeLifecycleAction"/>
public void Add(IFreezeLifecycleAction action) => BackingDictionary.Add("freeze", action);

/// <inheritdoc cref="IReadOnlyLifecycleAction"/>
public void Add(IReadOnlyLifecycleAction action) => BackingDictionary.Add("readonly", action);

/// <inheritdoc cref="IRolloverLifecycleAction"/>
public void Add(IRolloverLifecycleAction action) => BackingDictionary.Add("rollover", action);

/// <inheritdoc cref="ISetPriorityLifecycleAction"/>
public void Add(ISetPriorityLifecycleAction action) => BackingDictionary.Add("set_priority", action);

/// <inheritdoc cref="IShrinkLifecycleAction"/>
public void Add(IShrinkLifecycleAction action) => BackingDictionary.Add("shrink", action);

/// <inheritdoc cref="IUnfollowLifecycleAction"/>
public void Add(IUnfollowLifecycleAction action) => BackingDictionary.Add("unfollow", action);

/// <inheritdoc cref="IWaitForSnapshotLifecycleAction"/>
public void Add(IWaitForSnapshotLifecycleAction action) => BackingDictionary.Add("wait_for_snapshot", action);
}

internal class LifecycleActionsJsonFormatter : IJsonFormatter<ILifecycleActions>
Expand All @@ -47,6 +59,7 @@ internal class LifecycleActionsJsonFormatter : IJsonFormatter<ILifecycleActions>
{ "set_priority", 6 },
{ "shrink", 7 },
{ "unfollow", 8 },
{ "wait_for_snapshot", 9 },
};

public ILifecycleActions Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
Expand Down Expand Up @@ -104,6 +117,10 @@ public ILifecycleActions Deserialize(ref JsonReader reader, IJsonFormatterResolv
lifecycleAction = formatterResolver.GetFormatter<UnfollowLifecycleAction>()
.Deserialize(ref reader, formatterResolver);
break;
case 9:
lifecycleAction = formatterResolver.GetFormatter<WaitForSnapshotLifecycleAction>()
.Deserialize(ref reader, formatterResolver);
break;
}

lifecycles.Add(type.Utf8String(), lifecycleAction);
Expand Down Expand Up @@ -161,6 +178,9 @@ public void Serialize(ref JsonWriter writer, ILifecycleActions value, IJsonForma
case "unfollow":
Serialize<IUnfollowLifecycleAction>(ref writer, action.Value, formatterResolver);
break;
case "wait_for_snapshot":
Serialize<IWaitForSnapshotLifecycleAction>(ref writer, action.Value, formatterResolver);
break;
default:
DynamicObjectResolver.ExcludeNullCamelCase.GetFormatter<ILifecycleAction>()
.Serialize(ref writer, action.Value, formatterResolver);
Expand All @@ -181,31 +201,44 @@ public class LifecycleActionsDescriptor : IsADictionaryDescriptorBase<LifecycleA
{
public LifecycleActionsDescriptor() : base(new LifecycleActions()) { }

/// <inheritdoc cref="IAllocateLifecycleAction"/>
public LifecycleActionsDescriptor Allocate(Func<AllocateLifecycleActionDescriptor, IAllocateLifecycleAction> selector) =>
Assign("allocate", selector.InvokeOrDefault(new AllocateLifecycleActionDescriptor()));

/// <inheritdoc cref="IDeleteLifecycleAction"/>
public LifecycleActionsDescriptor Delete(Func<DeleteLifecycleActionDescriptor, IDeleteLifecycleAction> selector) =>
Assign("delete", selector.InvokeOrDefault(new DeleteLifecycleActionDescriptor()));

/// <inheritdoc cref="IForceMergeLifecycleAction"/>
public LifecycleActionsDescriptor ForceMerge(Func<ForceMergeLifecycleActionDescriptor, IForceMergeLifecycleAction> selector) =>
Assign("forcemerge", selector.InvokeOrDefault(new ForceMergeLifecycleActionDescriptor()));

/// <inheritdoc cref="IFreezeLifecycleAction"/>
public LifecycleActionsDescriptor Freeze(Func<FreezeLifecycleActionDescriptor, IFreezeLifecycleAction> selector) =>
Assign("freeze", selector.InvokeOrDefault(new FreezeLifecycleActionDescriptor()));

/// <inheritdoc cref="IReadOnlyLifecycleAction"/>
public LifecycleActionsDescriptor ReadOnly(Func<ReadOnlyLifecycleActionDescriptor, IReadOnlyLifecycleAction> selector) =>
Assign("readonly", selector.InvokeOrDefault(new ReadOnlyLifecycleActionDescriptor()));

/// <inheritdoc cref="IRolloverLifecycleAction"/>
public LifecycleActionsDescriptor Rollover(Func<RolloverLifecycleActionDescriptor, IRolloverLifecycleAction> selector) =>
Assign("rollover", selector.InvokeOrDefault(new RolloverLifecycleActionDescriptor()));

/// <inheritdoc cref="ISetPriorityLifecycleAction"/>
public LifecycleActionsDescriptor SetPriority(Func<SetPriorityLifecycleActionDescriptor, ISetPriorityLifecycleAction> selector) =>
Assign("set_priority", selector.InvokeOrDefault(new SetPriorityLifecycleActionDescriptor()));

/// <inheritdoc cref="IShrinkLifecycleAction"/>
public LifecycleActionsDescriptor Shrink(Func<ShrinkLifecycleActionDescriptor, IShrinkLifecycleAction> selector) =>
Assign("shrink", selector.InvokeOrDefault(new ShrinkLifecycleActionDescriptor()));

/// <inheritdoc cref="IUnfollowLifecycleAction"/>
public LifecycleActionsDescriptor Unfollow(Func<UnfollowLifecycleActionDescriptor, IUnfollowLifecycleAction> selector) =>
Assign("unfollow", selector.InvokeOrDefault(new UnfollowLifecycleActionDescriptor()));

/// <inheritdoc cref="IWaitForSnapshotLifecycleAction"/>
public LifecycleActionsDescriptor WaitForSnapshot(Func<WaitForSnapshotLifecycleActionDescriptor, IWaitForSnapshotLifecycleAction> selector) =>
Assign("wait_for_snapshot", selector.InvokeOrDefault(new WaitForSnapshotLifecycleActionDescriptor()));
}
}