Skip to content

Commit 0873534

Browse files
committed
Merge pull request #1153 from yoavrubin/dev
SQL Data masking cmdlets to stop using RuleId
2 parents e1653a2 + f63d784 commit 0873534

18 files changed

+7612
-4008
lines changed

src/ResourceManager/Sql/Commands.Sql.Test/ScenarioTests/AuditingTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ protected override void SetupManagementClients()
4343
helper.SetupSomeOfManagementClients(sqlCSMClient, storageClient, storageV2Client, resourcesClient, authorizationClient);
4444
}
4545

46-
[Fact(Skip = "PSGet: TODO fix by moving SM specific logic to test setup")] [Trait(Category.AcceptanceType, Category.Sql)]
46+
[Fact(Skip = "PSGet: TODO fix by moving SM specific logic to test setup")]
47+
[Trait(Category.AcceptanceType, Category.Sql)]
4748
public void TestAuditingDatabaseUpdatePolicyWithStorage()
4849
{
4950
RunPowerShellTest("Test-AuditingDatabaseUpdatePolicyWithStorage");

src/ResourceManager/Sql/Commands.Sql.Test/ScenarioTests/DataMaskingTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void TestDatabaseDataMaskingTextRuleLifecycle()
5656
RunPowerShellTest("Test-DatabaseDataMaskingTextRuleLifecycle");
5757
}
5858

59-
[Fact]
59+
[Fact(Skip = "Waiting for database structure validation")]
6060
[Trait(Category.AcceptanceType, Category.BVT)]
6161
public void TestDatabaseDataMaskingRuleCreationFailures()
6262
{

src/ResourceManager/Sql/Commands.Sql.Test/ScenarioTests/DataMaskingTests.ps1

Lines changed: 39 additions & 67 deletions
Large diffs are not rendered by default.

src/ResourceManager/Sql/Commands.Sql.Test/SessionRecords/Microsoft.Azure.Commands.Sql.Test.ScenarioTests.DataMaskingTests/TestDatabaseDataMaskingBasicRuleLifecycle.json

Lines changed: 1056 additions & 816 deletions
Large diffs are not rendered by default.

src/ResourceManager/Sql/Commands.Sql.Test/SessionRecords/Microsoft.Azure.Commands.Sql.Test.ScenarioTests.DataMaskingTests/TestDatabaseDataMaskingNumberRuleLifecycle.json

Lines changed: 1567 additions & 859 deletions
Large diffs are not rendered by default.

src/ResourceManager/Sql/Commands.Sql.Test/SessionRecords/Microsoft.Azure.Commands.Sql.Test.ScenarioTests.DataMaskingTests/TestDatabaseDataMaskingRuleCreationWithoutPolicy.json

Lines changed: 739 additions & 1249 deletions
Large diffs are not rendered by default.

src/ResourceManager/Sql/Commands.Sql.Test/SessionRecords/Microsoft.Azure.Commands.Sql.Test.ScenarioTests.DataMaskingTests/TestDatabaseDataMaskingTextRuleLifecycle.json

Lines changed: 4005 additions & 801 deletions
Large diffs are not rendered by default.

src/ResourceManager/Sql/Commands.Sql/Common/AzureSqlCmdletBase.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ internal AzureSqlCmdletBase()
8585
/// <returns>An initialized and ready to use ModelAdapter object</returns>
8686
protected abstract A InitModelAdapter(AzureSubscription subscription);
8787

88+
/// <summary>
89+
/// Transforms the given model object to be an object that is written out
90+
/// </summary>
91+
/// <param name="model">The about to be written model object</param>
92+
/// <returns>The prepared object to be written out</returns>
93+
protected virtual object TransformModelToOutputObject(M model)
94+
{
95+
return model;
96+
}
97+
8898
/// <summary>
8999
/// Executes the cmdlet
90100
/// </summary>
@@ -99,14 +109,14 @@ protected override void ProcessRecord()
99109
{
100110
if (WriteResult())
101111
{
102-
this.WriteObject(responseModel, true);
112+
this.WriteObject(TransformModelToOutputObject(responseModel), true);
103113
}
104114
}
105115
else
106116
{
107117
if (WriteResult())
108118
{
109-
this.WriteObject(updatedModel);
119+
this.WriteObject(TransformModelToOutputObject(updatedModel));
110120
}
111121
}
112122
}

src/ResourceManager/Sql/Commands.Sql/Data Masking/Cmdlet/BuildAzureSqlDatabaseDataMaskingRule.cs

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,6 @@ namespace Microsoft.Azure.Commands.Sql.DataMasking.Cmdlet
2929
/// </summary>
3030
public abstract class BuildAzureSqlDatabaseDataMaskingRule : SqlDatabaseDataMaskingRuleCmdletBase
3131
{
32-
/// <summary>
33-
/// Gets or sets the schema name
34-
/// </summary>
35-
public virtual string SchemaName { get; set; }
36-
37-
/// <summary>
38-
/// Gets or sets the table name
39-
/// </summary>
40-
public virtual string TableName { get; set; }
41-
42-
/// <summary>
43-
/// Gets or sets the column name
44-
/// </summary>
45-
public virtual string ColumnName { get; set; }
46-
4732
/// <summary>
4833
/// Gets or sets the masking function - the definition of this property as a cmdlet parameter is done in the subclasses
4934
/// </summary>
@@ -91,12 +76,7 @@ public abstract class BuildAzureSqlDatabaseDataMaskingRule : SqlDatabaseDataMask
9176
/// <param name="model">A model object</param>
9277
protected override IEnumerable<DatabaseDataMaskingRuleModel> ApplyUserInputToModel(IEnumerable<DatabaseDataMaskingRuleModel> rules)
9378
{
94-
string errorMessage = ValidateRuleTarget(rules);
95-
if (string.IsNullOrEmpty(errorMessage))
96-
{
97-
errorMessage = ValidateOperation(rules);
98-
}
99-
79+
string errorMessage = ValidateOperation(rules);
10080
if(!string.IsNullOrEmpty(errorMessage))
10181
{
10282
throw new Exception(errorMessage);
@@ -106,20 +86,6 @@ protected override IEnumerable<DatabaseDataMaskingRuleModel> ApplyUserInputToMod
10686
return UpdateRuleList(rules, rule);
10787
}
10888

109-
/// <summary>
110-
/// Validation that the rule's target is set properly to be either a table and column for which there's no other rule, or an alias for which there's no other rule.
111-
/// </summary>
112-
/// <param name="rules">The data masking rules of the current database</param>
113-
/// <returns>A string containing error message or null in case all is fine</returns>
114-
protected string ValidateRuleTarget(IEnumerable<DatabaseDataMaskingRuleModel> rules)
115-
{
116-
if (rules.Any(r => r.TableName == TableName && r.ColumnName == ColumnName && r.RuleId != RuleId))
117-
{
118-
return string.Format(CultureInfo.InvariantCulture, Microsoft.Azure.Commands.Sql.Properties.Resources.DataMaskingTableAndColumnUsedError, TableName, ColumnName);
119-
}
120-
return null;
121-
}
122-
12389
/// <summary>
12490
/// Update the rule this cmdlet is operating on based on the values provided by the user
12591
/// </summary>
@@ -254,7 +220,7 @@ private MaskingFunction ModelizeMaskingFunction()
254220
/// <param name="model">The model object with the data to be sent to the REST endpoints</param>
255221
protected override IEnumerable<DatabaseDataMaskingRuleModel> PersistChanges(IEnumerable<DatabaseDataMaskingRuleModel> rules)
256222
{
257-
ModelAdapter.SetDatabaseDataMaskingRule(rules.First(r => r.RuleId == RuleId), clientRequestId);
223+
ModelAdapter.SetDatabaseDataMaskingRule(rules.First(IsModelOfRule), clientRequestId);
258224
return null;
259225
}
260226

src/ResourceManager/Sql/Commands.Sql/Data Masking/Cmdlet/GetAzureSqlDatabaseDataMaskingRule.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using Microsoft.Azure.Commands.Sql.DataMasking.Model;
16+
using System;
1617
using System.Collections.Generic;
18+
using System.Linq;
1719
using System.Management.Automation;
1820

1921
namespace Microsoft.Azure.Commands.Sql.DataMasking.Cmdlet
@@ -23,13 +25,37 @@ namespace Microsoft.Azure.Commands.Sql.DataMasking.Cmdlet
2325
/// </summary>
2426
[Cmdlet(VerbsCommon.Get, "AzureRmSqlDatabaseDataMaskingRule"), OutputType(typeof(IEnumerable<DatabaseDataMaskingRuleModel>))]
2527
public class GetAzureSqlDatabaseDataMaskingRule : SqlDatabaseDataMaskingRuleCmdletBase
26-
{
28+
{
2729
/// <summary>
28-
/// Gets or sets the id of the rule use, if not provided then the list of rules for this DB is returned
30+
/// Gets or sets the schema name
2931
/// </summary>
30-
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Data Masking rule Id.")]
31-
public override string RuleId { get; set; } // intentionally overiding to make this property non mandatory (thus allow a LIST call)
32+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The schema name.")]
33+
[ValidateNotNullOrEmpty]
34+
public override string SchemaName { get; set; }
3235

36+
/// <summary>
37+
/// Gets or sets the table name
38+
/// </summary>
39+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The table name.")]
40+
[ValidateNotNullOrEmpty]
41+
public override string TableName { get; set; }
42+
43+
/// <summary>
44+
/// Gets or sets the column name
45+
/// </summary>
46+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The column name.")]
47+
[ValidateNotNullOrEmpty]
48+
public override string ColumnName { get; set; }
49+
50+
protected override object TransformModelToOutputObject(IEnumerable<DatabaseDataMaskingRuleModel> model)
51+
{
52+
Predicate<DatabaseDataMaskingRuleModel> colPred = (DatabaseDataMaskingRuleModel r) => { return string.IsNullOrEmpty(ColumnName) ? true : r.ColumnName == ColumnName; };
53+
Predicate<DatabaseDataMaskingRuleModel> tablePred = (DatabaseDataMaskingRuleModel r) => { return string.IsNullOrEmpty(TableName) ? true : r.TableName == TableName; };
54+
Predicate<DatabaseDataMaskingRuleModel> schemaPred = (DatabaseDataMaskingRuleModel r) => { return string.IsNullOrEmpty(SchemaName) ? true : r.SchemaName == SchemaName; };
55+
return model.Where(r => { return colPred(r) && tablePred(r) && schemaPred(r); }).ToList();
56+
}
57+
58+
3359
/// <summary>
3460
/// No sending is needed as this is a Get cmdlet
3561
/// </summary>

0 commit comments

Comments
 (0)