Skip to content

Commit 28878c2

Browse files
strehan1993viparekBethanyZhou
authored
[SQL] Bugfix - Fix identity cmdlets (#21086)
* [SQL] Fix identity for SetAzSqlDatabase cmdlet * Fix changelog * Fix logic --------- Co-authored-by: Vinit Dinesh Parekh <[email protected]> Co-authored-by: Beisi Zhou <[email protected]>
1 parent bee4218 commit 28878c2

File tree

7 files changed

+16
-28
lines changed

7 files changed

+16
-28
lines changed

src/Sql/Sql/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Fixed identity assignment in `Set-AzSqlDatabase` cmdlet
2122
* Added new parameters to `New-AzSqlDatabase`, `Get-AzSqlDatabase`, `Set-AzSqlDatabase`, `New-AzSqlDatabaseCopy`, `New-AzSqlDatabaseSecondary` cmdlets
2223
- AssignIdentity
2324
- EncryptionProtector

src/Sql/Sql/Common/DatabaseIdentityAndKeysHelper.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ public class DatabaseIdentityAndKeysHelper
3030
/// </summary>
3131
/// <param name="assignIdentityIsPresent">Flag to check if AssignIdentity flag is used in the cmdlet.</param>
3232
/// <param name="userAssignedIdentities">User assigned identities</param>
33-
/// <param name="existingIdentity">Existing identity on database</param>
3433
/// <returns>Database Identity</returns>
35-
public static DatabaseIdentity GetDatabaseIdentity(bool assignIdentityIsPresent, string[] userAssignedIdentities, Management.Sql.Models.DatabaseIdentity existingIdentity)
34+
public static DatabaseIdentity GetDatabaseIdentity(bool assignIdentityIsPresent, string[] userAssignedIdentities)
3635
{
3736
DatabaseIdentity identityResult = null;
3837
if (!assignIdentityIsPresent)
@@ -47,34 +46,22 @@ public static DatabaseIdentity GetDatabaseIdentity(bool assignIdentityIsPresent,
4746

4847
Dictionary<string, DatabaseUserIdentity> identityDict = new Dictionary<string, DatabaseUserIdentity>();
4948

50-
// Update an existing database identity
49+
// Create identity on database
5150
//
52-
if (existingIdentity != null && userAssignedIdentities.Any() &&
53-
existingIdentity.UserAssignedIdentities != null)
51+
foreach (string identity in userAssignedIdentities)
5452
{
55-
foreach (string identity in userAssignedIdentities)
56-
{
57-
existingIdentity.UserAssignedIdentities.Add(identity, new DatabaseUserIdentity());
58-
}
59-
60-
identityResult = existingIdentity;
61-
}
62-
else
63-
{
64-
// Create identity on database
65-
//
66-
foreach (string identity in userAssignedIdentities)
53+
if (!identityDict.ContainsKey(identity))
6754
{
6855
identityDict.Add(identity, new DatabaseUserIdentity());
6956
}
70-
71-
identityResult = new DatabaseIdentity()
72-
{
73-
Type = DatabaseIdentityType.UserAssigned,
74-
UserAssignedIdentities = identityDict
75-
};
7657
}
7758

59+
identityResult = new DatabaseIdentity()
60+
{
61+
Type = DatabaseIdentityType.UserAssigned,
62+
UserAssignedIdentities = identityDict
63+
};
64+
7865
return identityResult;
7966
}
8067

src/Sql/Sql/Database Backup/Cmdlet/RestoreAzureRMSqlDatabase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ protected override AzureSqlDatabaseModel GetEntity()
411411
Tags = TagsConversionHelper.CreateTagDictionary(Tag, validate: true),
412412
ZoneRedundant = this.IsParameterBound(p => p.ZoneRedundant) ? ZoneRedundant.ToBool() : (bool?)null,
413413
HighAvailabilityReplicaCount = HAReplicaCount,
414-
Identity = DatabaseIdentityAndKeysHelper.GetDatabaseIdentity(this.AssignIdentity.IsPresent, this.UserAssignedIdentityId, null),
414+
Identity = DatabaseIdentityAndKeysHelper.GetDatabaseIdentity(this.AssignIdentity.IsPresent, this.UserAssignedIdentityId),
415415
Keys = DatabaseIdentityAndKeysHelper.GetDatabaseKeysDictionary(this.KeyList),
416416
EncryptionProtector = this.EncryptionProtector,
417417
FederatedClientId = this.FederatedClientId,

src/Sql/Sql/Database/Cmdlet/NewAzureSqlDatabase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ protected override AzureSqlDatabaseCreateOrUpdateModel ApplyUserInputToModel(Azu
371371
MaintenanceConfigurationId = MaintenanceConfigurationId,
372372
EnableLedger = this.IsParameterBound(p => p.EnableLedger) ? EnableLedger.ToBool() : (bool?)null,
373373
PreferredEnclaveType = this.PreferredEnclaveType,
374-
Identity = DatabaseIdentityAndKeysHelper.GetDatabaseIdentity(this.AssignIdentity.IsPresent, this.UserAssignedIdentityId, null),
374+
Identity = DatabaseIdentityAndKeysHelper.GetDatabaseIdentity(this.AssignIdentity.IsPresent, this.UserAssignedIdentityId),
375375
Keys = DatabaseIdentityAndKeysHelper.GetDatabaseKeysDictionary(this.KeyList),
376376
EncryptionProtector = this.EncryptionProtector,
377377
FederatedClientId = this.FederatedClientId

src/Sql/Sql/Database/Cmdlet/SetAzureSqlDatabase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ protected override IEnumerable<AzureSqlDatabaseModel> ApplyUserInputToModel(IEnu
352352
SecondaryType = SecondaryType,
353353
MaintenanceConfigurationId = MaintenanceConfigurationId,
354354
PreferredEnclaveType = PreferredEnclaveType,
355-
Identity = DatabaseIdentityAndKeysHelper.GetDatabaseIdentity(this.AssignIdentity.IsPresent, this.UserAssignedIdentityId, model.FirstOrDefault().Identity),
355+
Identity = DatabaseIdentityAndKeysHelper.GetDatabaseIdentity(this.AssignIdentity.IsPresent, this.UserAssignedIdentityId),
356356
EncryptionProtector = this.EncryptionProtector ?? model.FirstOrDefault().EncryptionProtector,
357357
FederatedClientId = this.FederatedClientId ?? model.FirstOrDefault().FederatedClientId,
358358
};

src/Sql/Sql/Replication/Cmdlet/NewAzureSqlDatabaseCopy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ protected override IEnumerable<AzureSqlDatabaseCopyModel> ApplyUserInputToModel(
262262
LicenseType = LicenseType, // note: default license type is LicenseIncluded
263263
RequestedBackupStorageRedundancy = BackupStorageRedundancy,
264264
ZoneRedundant = this.IsParameterBound(p => p.ZoneRedundant) ? ZoneRedundant.ToBool() : (bool?)null,
265-
Identity = Common.DatabaseIdentityAndKeysHelper.GetDatabaseIdentity(this.AssignIdentity.IsPresent, this.UserAssignedIdentityId, null),
265+
Identity = Common.DatabaseIdentityAndKeysHelper.GetDatabaseIdentity(this.AssignIdentity.IsPresent, this.UserAssignedIdentityId),
266266
Keys = Common.DatabaseIdentityAndKeysHelper.GetDatabaseKeysDictionary(this.KeyList),
267267
EncryptionProtector = this.EncryptionProtector,
268268
FederatedClientId = this.FederatedClientId

src/Sql/Sql/Replication/Cmdlet/NewAzureSqlDatabaseSecondary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ protected override IEnumerable<AzureReplicationLinkModel> ApplyUserInputToModel(
279279
SecondaryType = SecondaryType,
280280
HighAvailabilityReplicaCount = this.IsParameterBound(p => p.HighAvailabilityReplicaCount) ? HighAvailabilityReplicaCount : (int?)null,
281281
ZoneRedundant = this.IsParameterBound(p => p.ZoneRedundant) ? ZoneRedundant.ToBool() : (bool?)null,
282-
Identity = Common.DatabaseIdentityAndKeysHelper.GetDatabaseIdentity(this.AssignIdentity.IsPresent, this.UserAssignedIdentityId, null),
282+
Identity = Common.DatabaseIdentityAndKeysHelper.GetDatabaseIdentity(this.AssignIdentity.IsPresent, this.UserAssignedIdentityId),
283283
Keys = Common.DatabaseIdentityAndKeysHelper.GetDatabaseKeysDictionary(this.KeyList),
284284
EncryptionProtector = this.EncryptionProtector,
285285
FederatedClientId = this.FederatedClientId

0 commit comments

Comments
 (0)