-
Notifications
You must be signed in to change notification settings - Fork 19
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
9 changed files
with
379 additions
and
3 deletions.
There are no files selected for viewing
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
67 changes: 67 additions & 0 deletions
67
code/DeltaKustoLib/CommandModel/Policies/AlterRestrictedViewPluralPolicyCommand.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,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); | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
code/DeltaKustoLib/CommandModel/Policies/AlterRestrictedViewPolicyCommand.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,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 | ||
} | ||
} | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
code/DeltaKustoLib/CommandModel/Policies/DeleteRestrictedViewPolicyCommand.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,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(); | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
code/DeltaKustoUnitTest/CommandParsing/Policies/AlterRestrictedViewPluralPolicyTest.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,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; | ||
} | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
code/DeltaKustoUnitTest/CommandParsing/Policies/AlterRestrictedViewPolicyTest.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,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; | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.