Skip to content

Commit

Permalink
Added UMI related config
Browse files Browse the repository at this point in the history
  • Loading branch information
viparek committed Jun 1, 2021
1 parent 213fde4 commit 9820b27
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public string ServerKeyType
get { return this._serverKeyType; }
set { this._serverKeyType = value; }
}

/// <summary>
/// Initializes a new instance of the
/// EncryptionProtectorCreateOrUpdateProperties class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public DiagnosticSettingsResource UpdateDiagnosticSettings(DiagnosticSettingsRes
if (server.Identity == null ||
server.Identity.Type != ResourceIdentityType.SystemAssigned.ToString())
{
server.Identity = ResourceIdentityHelper.GetIdentityObjectFromType(true);
server.Identity = ResourceIdentityHelper.GetIdentityObjectFromType(server.Identity.Type, null);
server = GetCurrentSqlClient().Servers.CreateOrUpdate(resourceGroupName, serverName, server);
}

Expand Down
46 changes: 27 additions & 19 deletions src/Sql/Sql/Common/ResourceIdentityHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Management.Sql.Models;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;

namespace Microsoft.Azure.Commands.Sql.Common
{
Expand All @@ -24,30 +27,35 @@ public enum ResourceIdentityType
}

public class ResourceIdentityHelper
{
public static Management.Sql.Models.ResourceIdentity GetSystemAssignedIdentity()
{
public static Management.Sql.Models.ResourceIdentity GetIdentityObjectFromType(string AssignIdentity, List<string> userAssignedIdentities)
{
Management.Sql.Models.ResourceIdentity identityResult = null;

identityResult = new Management.Sql.Models.ResourceIdentity()
if (AssignIdentity.Equals(ResourceIdentityType.SystemAssigned))
{
Type = ResourceIdentityType.SystemAssigned.ToString()
};

return identityResult;
}
identityResult = new Management.Sql.Models.ResourceIdentity()
{
Type = ResourceIdentityType.SystemAssigned.ToString()
};
}

public static Management.Sql.Models.ResourceIdentity GetUserAssignedIdentity(List<string> userAssignedIdentities)
{
Management.Sql.Models.ResourceIdentity identityResult = null;

identityResult = new Management.Sql.Models.ResourceIdentity()
if (AssignIdentity.Equals(ResourceIdentityType.UserAssigned) && userAssignedIdentities.Any())
{
Type = ResourceIdentityType.UserAssigned.ToString(),
// TODO
// Add user assigned identities.
};

Dictionary<string, UserIdentity> umiDict = new Dictionary<string, UserIdentity>();

foreach (string identity in userAssignedIdentities)
{
umiDict.Add(identity, new UserIdentity());
}

identityResult = new Management.Sql.Models.ResourceIdentity()
{
Type = ResourceIdentityType.UserAssigned.ToString(),
UserAssignedIdentities = umiDict
};
}

return identityResult;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ public override void ExecuteCmdlet()
AdministratorPassword = (this.AdministratorCredential != null) ? this.AdministratorCredential.Password : null,
AdministratorLogin = (this.AdministratorCredential != null) ? this.AdministratorCredential.UserName : null,
Tags = TagsConversionHelper.CreateTagDictionary(Tag, validate: true),
Identity = this.AssignIdentity.Equals(ResourceIdentityType.SystemAssigned) ? ResourceIdentityHelper.GetSystemAssignedIdentity() : ResourceIdentityHelper.GetUserAssignedIdentity(this.UserAssignedIdentities),
Identity = ResourceIdentityHelper.GetIdentityObjectFromType(this.AssignIdentity.IsPresent ? this.AssignIdentity.ToString() : null, UserAssignedIdentities ?? null),
LicenseType = this.LicenseType,
// `-StorageSizeInGB 0` as a parameter to this cmdlet means "use default".
// For non-MI database, we can just pass in 0 and the server will treat 0 as default.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ protected override IEnumerable<AzureSqlManagedInstanceModel> ApplyUserInputToMod
PublicDataEndpointEnabled = this.PublicDataEndpointEnabled,
ProxyOverride = this.ProxyOverride,
Tags = TagsConversionHelper.CreateTagDictionary(Tag, validate: true),
Identity = this.AssignIdentity.Equals(ResourceIdentityType.SystemAssigned) ? ResourceIdentityHelper.GetSystemAssignedIdentity() : ResourceIdentityHelper.GetUserAssignedIdentity(this.UserAssignedIdentities),
Identity = model.FirstOrDefault().Identity ?? ResourceIdentityHelper.GetIdentityObjectFromType(this.AssignIdentity.IsPresent ? this.AssignIdentity.ToString() : null, UserAssignedIdentities ?? null),
InstancePoolName = this.InstancePoolName,
MinimalTlsVersion = this.MinimalTlsVersion,
MaintenanceConfigurationId = this.MaintenanceConfigurationId,
Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Sql/Server/Cmdlet/NewAzureSqlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public override void ExecuteCmdlet()
SqlAdministratorPassword = (this.SqlAdministratorCredentials != null) ? this.SqlAdministratorCredentials.Password : null,
SqlAdministratorLogin = (this.SqlAdministratorCredentials != null) ? this.SqlAdministratorCredentials.UserName : null,
Tags = TagsConversionHelper.CreateTagDictionary(Tags, validate: true),
Identity = this.AssignIdentity.Equals(ResourceIdentityType.SystemAssigned) ? ResourceIdentityHelper.GetSystemAssignedIdentity() : ResourceIdentityHelper.GetUserAssignedIdentity(this.UserAssignedIdentities),
Identity = ResourceIdentityHelper.GetIdentityObjectFromType(this.AssignIdentity.IsPresent ? this.AssignIdentity.ToString() : null, UserAssignedIdentities ?? null),
MinimalTlsVersion = this.MinimalTlsVersion,
PublicNetworkAccess = this.PublicNetworkAccess,
PrimaryUserAssignedIdentityId = this.PrimaryUserAssignedIdentityId,
Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Sql/Server/Cmdlet/SetAzureSqlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public class SetAzureSqlServer : AzureSqlServerCmdletBase
Tags = TagsConversionHelper.ReadOrFetchTags(this, model.FirstOrDefault().Tags),
ServerVersion = this.ServerVersion,
Location = model.FirstOrDefault().Location,
Identity = this.AssignIdentity.Equals(ResourceIdentityType.SystemAssigned) ? ResourceIdentityHelper.GetSystemAssignedIdentity() : ResourceIdentityHelper.GetUserAssignedIdentity(this.UserAssignedIdentities),
Identity = model.FirstOrDefault().Identity ?? ResourceIdentityHelper.GetIdentityObjectFromType(this.AssignIdentity.IsPresent ? this.AssignIdentity.ToString() : null, UserAssignedIdentities ?? null),
PublicNetworkAccess = this.PublicNetworkAccess,
MinimalTlsVersion = this.MinimalTlsVersion,
PrimaryUserAssignedIdentityId = this.PrimaryUserAssignedIdentityId,
Expand Down

0 comments on commit 9820b27

Please sign in to comment.