Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
vplauzon committed Oct 21, 2023
2 parents e2891be + 880f47f commit 43ae06e
Show file tree
Hide file tree
Showing 9 changed files with 379 additions and 3 deletions.
6 changes: 6 additions & 0 deletions code/DeltaKustoLib/CommandModel/CommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ public override int GetHashCode()
case "DeleteTablePolicyStreamingIngestion":
case "DeleteDatabasePolicyStreamingIngestion":
return DeleteStreamingIngestionPolicyCommand.FromCode(commandBlock);
case "AlterTablePolicyRestrictedViewAccess":
return AlterRestrictedViewPolicyCommand.FromCode(commandBlock);
case "AlterTablesPolicyRestrictedViewAccess":
return AlterRestrictedViewPluralPolicyCommand.FromCode(commandBlock);
case "DeleteTablePolicyRestrictedViewAccess":
return DeleteRestrictedViewPolicyCommand.FromCode(commandBlock);
case "AlterTablePolicyPartitioning":
return AlterPartitioningPolicyCommand.FromCode(commandBlock);
case "DeleteTablePolicyPartitioning":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Text.Json;
using Kusto.Language.Syntax;

namespace DeltaKustoLib.CommandModel.Policies
{
/// <summary>
/// Models <see cref="https://learn.microsoft.com/en-us/azure/data-explorer/kusto/management/alter-table-restricted-view-access-policy-command"/>
/// </summary>
[Command(19200, "Alter tables restricted view Policy")]
public class AlterRestrictedViewPluralPolicyCommand : PolicyCommandBase
{
public IImmutableList<EntityName> TableNames { get; }

public bool AreEnabled { get; }

public override string CommandFriendlyName => ".alter table policy restricted_view_access";

public override string SortIndex => TableNames.First().Name;

public override string ScriptPath =>
"tables/policies/restricted_view_access/create-many";

public AlterRestrictedViewPluralPolicyCommand(
IEnumerable<EntityName> tableNames,
bool areEnabled)
{
if (!tableNames.Any())
{
throw new ArgumentOutOfRangeException(
nameof(tableNames),
"Must have at least one table name");
}

TableNames = tableNames.ToImmutableArray();
AreEnabled = areEnabled;
}

public override string ToScript(ScriptingContext? context = null)
{
var builder = new StringBuilder();

builder.Append(".alter tables (");
builder.Append(string.Join(", ", TableNames.Select(t => t.ToScript())));
builder.Append($") policy restricted_view_access {AreEnabled}");
builder.AppendLine();

return builder.ToString();
}

internal static CommandBase? FromCode(CommandBlock commandBlock)
{
var nameReferences = commandBlock.GetDescendants<NameReference>();
var booleanToken = commandBlock.GetUniqueDescendant<SyntaxToken>(
"boolean",
t => t.Kind == SyntaxKind.BooleanLiteralToken);

return new AlterRestrictedViewPluralPolicyCommand(
nameReferences.Select(r => EntityName.FromCode(r)),
(bool)booleanToken.Value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Kusto.Language.Syntax;

namespace DeltaKustoLib.CommandModel.Policies
{
/// <summary>
/// Models <see cref="https://learn.microsoft.com/en-us/azure/data-explorer/kusto/management/alter-table-restricted-view-access-policy-command"/>
/// </summary>
[Command(19100, "Alter restricted view Policy")]
public class AlterRestrictedViewPolicyCommand : TableOnlyPolicyCommandBase
{
public bool IsEnabled { get; }

public override string CommandFriendlyName => ".alter table policy restricted_view_access";

public override string ScriptPath =>
$"tables/policies/restricted_view_access/create/{TableName}";

public AlterRestrictedViewPolicyCommand(
EntityName tableName,
bool isEnabled)
: base(tableName)
{
IsEnabled = isEnabled;
}

public override string ToScript(ScriptingContext? context = null)
{
return $".alter table {TableName} policy restricted_view_access {IsEnabled}";
}

internal static CommandBase? FromCode(CommandBlock commandBlock)
{
var nameReferences = commandBlock.GetDescendants<NameReference>();
var tableNameReference = nameReferences.Last();
var booleanToken = commandBlock.GetUniqueDescendant<SyntaxToken>(
"boolean",
t => t.Kind == SyntaxKind.BooleanLiteralToken);

return new AlterRestrictedViewPolicyCommand(
EntityName.FromCode(tableNameReference.Name),
(bool)booleanToken.Value);
}

internal static IEnumerable<CommandBase> ComputeDelta(
AlterRestrictedViewPolicyCommand? currentCommand,
AlterRestrictedViewPolicyCommand? targetCommand)
{
var hasCurrent = currentCommand != null;
var hasTarget = targetCommand != null;

if (hasCurrent && !hasTarget)
{
// No target, we remove the current policy
throw new NotImplementedException();
//yield return new DeleteStreamingIngestionPolicyCommand(
// currentCommand!.EntityType,
// currentCommand!.EntityName);
}
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
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public class DeleteAutoDeletePolicyCommand : TableOnlyPolicyCommandBase
{
public override string CommandFriendlyName => ".delete <entity> policy auto_delete";

public override string ScriptPath => $"tables/policies/auto_delete/delete";
public DeleteAutoDeletePolicyCommand(EntityName tableName):base (tableName)
public override string ScriptPath => "tables/policies/auto_delete/delete";

public DeleteAutoDeletePolicyCommand(EntityName tableName) : base(tableName)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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/delete-table-restricted-view-access-policy-command"/>
/// </summary>
[Command(19000, "Delete restricted view Policies")]
public class DeleteRestrictedViewPolicyCommand : TableOnlyPolicyCommandBase
{
public override string CommandFriendlyName => ".delete table policy restricted_view_access";

public override string ScriptPath => "tables/policies/restricted_view_access/delete";

public DeleteRestrictedViewPolicyCommand(EntityName tableName) : base(tableName)
{
}

internal static CommandBase FromCode(SyntaxElement rootElement)
{
var entityName = rootElement.GetFirstDescendant<NameReference>();

return new DeleteRestrictedViewPolicyCommand(EntityName.FromCode(entityName.Name));
}

public override string ToScript(ScriptingContext? context)
{
var builder = new StringBuilder();

builder.Append(".delete table ");
builder.Append(TableName.ToScript());
builder.Append(" policy restricted_view_access");

return builder.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using DeltaKustoLib.CommandModel.Policies;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text.Json;
using Xunit;

namespace DeltaKustoUnitTest.CommandParsing.Policies
{
public class AlterRestrictedViewPluralPolicyTest : ParsingTestBase
{
[Fact]
public void SimpleTables()
{
TestRestrictedViewPolicy("MyTable", "YourTable");
}

[Fact]
public void FunkyTables()
{
TestRestrictedViewPolicy("MyTable", "['A- 1']", "['Beta-- 1']");
}

private void TestRestrictedViewPolicy(params string[] tableNames)
{
TestRestrictedViewPolicy(tableNames, true);
TestRestrictedViewPolicy(tableNames, false);
}

private void TestRestrictedViewPolicy(IEnumerable<string> tableNames, bool areEnabled)
{
var tableListText = string.Join(", ", tableNames);
var commandText = @$"
.alter tables ({tableListText}) policy restricted_view_access {areEnabled.ToString().ToLower()}";
var command = ParseOneCommand(commandText);

Assert.IsType<AlterRestrictedViewPluralPolicyCommand>(command);

var realCommand = (AlterRestrictedViewPluralPolicyCommand)command;

Assert.Equal(areEnabled, realCommand.AreEnabled);
Assert.Equal(tableNames.Count(), realCommand.TableNames.Count);

var expectedNames = ImmutableHashSet.Create(tableNames
.Select(t => GetActualTableName(t))
.ToArray());
var observedNames = ImmutableHashSet.Create(realCommand.TableNames
.Select(t => t.Name)
.ToArray());

foreach (var name in expectedNames)
{
Assert.Contains(name, observedNames);
}
}

private static string GetActualTableName(string tableName)
{
var actualTableName = tableName.Split('.').Last();

if (actualTableName.StartsWith('['))
{
return actualTableName.Substring(2, actualTableName.Length - 4);
}
else
{
return actualTableName;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using DeltaKustoLib.CommandModel.Policies;
using System;
using System.Linq;
using System.Text.Json;
using Xunit;

namespace DeltaKustoUnitTest.CommandParsing.Policies
{
public class AlterRestrictedViewPolicyTest : ParsingTestBase
{
[Fact]
public void SimpleTable()
{
TestRestrictedViewPolicy("MyTable");
}

[Fact]
public void FunkyTable()
{
TestRestrictedViewPolicy("['A- 1']");
}

[Fact]
public void DbComposedTableName()
{
TestRestrictedViewPolicy("mydb.mytable");
}

[Fact]
public void ClusterComposedTableName()
{
TestRestrictedViewPolicy("mycluster.['my db'].mytable");
}

private void TestRestrictedViewPolicy(string tableName)
{
TestRestrictedViewPolicy(tableName, true);
TestRestrictedViewPolicy(tableName, false);
}

private void TestRestrictedViewPolicy(string tableName, bool isEnabled)
{
var commandText = @$"
.alter table {tableName} policy restricted_view_access {isEnabled.ToString().ToLower()}";
var command = ParseOneCommand(commandText);
var actualTableName = GetActualTableName(tableName);

Assert.IsType<AlterRestrictedViewPolicyCommand>(command);
Assert.Equal(isEnabled, ((AlterRestrictedViewPolicyCommand)command).IsEnabled);
Assert.Equal(
actualTableName,
((AlterRestrictedViewPolicyCommand)command).TableName.Name);
}

private static string GetActualTableName(string tableName)
{
var actualTableName = tableName.Split('.').Last();

if (actualTableName.StartsWith('['))
{
return actualTableName.Substring(2, actualTableName.Length - 4);
}
else
{
return actualTableName;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ private void TestAutoDeletePolicy(string tableName)
var command = ParseOneCommand(commandText);

Assert.IsType<DeleteAutoDeletePolicyCommand>(command);

var realCommand = (DeleteAutoDeletePolicyCommand)command;

Assert.Equal(tableName, realCommand.TableName.Name);
}
}
}
Loading

0 comments on commit 43ae06e

Please sign in to comment.