-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
code/DeltaKustoLib/CommandModel/Policies/AlterIngestionTimePolicyCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using Kusto.Language.Syntax; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
|
||
namespace DeltaKustoLib.CommandModel.Policies | ||
{ | ||
/// <summary> | ||
/// Models <see cref="https://learn.microsoft.com/en-us/azure/data-explorer/kusto/management/alter-ingestion-time-policy-command"/> | ||
/// </summary> | ||
[Command(21100, "Alter Ingestion Time Policies")] | ||
public class AlterIngestionTimePolicyCommand : TableOnlyPolicyCommandBase | ||
{ | ||
public bool IsEnabled { get; } | ||
|
||
public override string CommandFriendlyName => ".alter <entity> policy ingestiontime"; | ||
|
||
public override string ScriptPath => $"tables/policies/ingestiontime/create/{TableName}"; | ||
|
||
public AlterIngestionTimePolicyCommand(EntityName tableName, bool isEnabled) | ||
: base(tableName) | ||
{ | ||
IsEnabled = isEnabled; | ||
} | ||
|
||
internal static CommandBase FromCode(SyntaxElement rootElement) | ||
{ | ||
var tableName = rootElement.GetDescendants<NameReference>().Last(); | ||
var policyText = QuotedText.FromLiteral( | ||
rootElement.GetUniqueDescendant<LiteralExpression>( | ||
"AutoDeletePolicy", | ||
e => e.NameInParent == "AutoDeletePolicy")); | ||
var policy = Deserialize<JsonDocument>(policyText.Text); | ||
|
||
if (policy == null) | ||
{ | ||
throw new DeltaException( | ||
$"Can't extract policy objects from {policyText.ToScript()}"); | ||
} | ||
|
||
return new AlterIngestionTimePolicyCommand(EntityName.FromCode(tableName.Name), policy); | ||
} | ||
|
||
public override string ToScript(ScriptingContext? context) | ||
{ | ||
var builder = new StringBuilder(); | ||
|
||
builder.Append(".alter table "); | ||
builder.Append(TableName); | ||
builder.Append(" policy ingestiontime "); | ||
builder.AppendLine(IsEnabled.ToString().ToLower()); | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
internal static IEnumerable<CommandBase> ComputeDelta( | ||
AlterAutoDeletePolicyCommand? currentCommand, | ||
AlterAutoDeletePolicyCommand? targetCommand) | ||
{ | ||
var hasCurrent = currentCommand != null; | ||
var hasTarget = targetCommand != null; | ||
|
||
if (hasCurrent && !hasTarget) | ||
{ // No target, we remove the current policy | ||
yield return new DeleteAutoDeletePolicyCommand(currentCommand!.TableName); | ||
} | ||
else if (hasTarget) | ||
{ | ||
if (!hasCurrent || !currentCommand!.Equals(targetCommand!)) | ||
{ // There is a target and either no current or the current is different | ||
yield return targetCommand!; | ||
} | ||
} | ||
else | ||
{ // Both target and current are null: no delta | ||
} | ||
} | ||
} | ||
} |