Skip to content

Commit

Permalink
Add Command
Browse files Browse the repository at this point in the history
  • Loading branch information
vplauzon committed Oct 19, 2023
1 parent 2d865bc commit 96d69f7
Showing 1 changed file with 83 additions and 0 deletions.
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
}
}
}
}

0 comments on commit 96d69f7

Please sign in to comment.