Skip to content

Commit

Permalink
Fix of CreateUpdate mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
Aris van Ommeren committed Oct 15, 2021
1 parent 255b4ee commit 8ebace3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
5 changes: 5 additions & 0 deletions internal/services/mssql/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Client struct {
ReplicationLinksClient *sql.ReplicationLinksClient
RestorableDroppedDatabasesClient *sql.RestorableDroppedDatabasesClient
ServerAzureADAdministratorsClient *sql.ServerAzureADAdministratorsClient
ServerAzureADOnlyAuthenticationsClient *sql.ServerAzureADOnlyAuthenticationsClient
ServerConnectionPoliciesClient *sql.ServerConnectionPoliciesClient
ServerExtendedBlobAuditingPoliciesClient *sql.ExtendedServerBlobAuditingPoliciesClient
ServerSecurityAlertPoliciesClient *sql.ServerSecurityAlertPoliciesClient
Expand Down Expand Up @@ -85,6 +86,9 @@ func NewClient(o *common.ClientOptions) *Client {
serverAzureADAdministratorsClient := sql.NewServerAzureADAdministratorsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&serverAzureADAdministratorsClient.Client, o.ResourceManagerAuthorizer)

serverAzureADOnlyAuthenticationsClient := sql.NewServerAzureADOnlyAuthenticationsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&serverAzureADOnlyAuthenticationsClient.Client, o.ResourceManagerAuthorizer)

serversClient := sql.NewServersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&serversClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -122,6 +126,7 @@ func NewClient(o *common.ClientOptions) *Client {
ReplicationLinksClient: &replicationLinksClient,
RestorableDroppedDatabasesClient: &restorableDroppedDatabasesClient,
ServerAzureADAdministratorsClient: &serverAzureADAdministratorsClient,
ServerAzureADOnlyAuthenticationsClient: &serverAzureADOnlyAuthenticationsClient,
ServersClient: &serversClient,
ServerExtendedBlobAuditingPoliciesClient: &serverExtendedBlobAuditingPoliciesClient,
ServerConnectionPoliciesClient: &serverConnectionPoliciesClient,
Expand Down
44 changes: 36 additions & 8 deletions internal/services/mssql/mssql_server_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ func resourceMsSqlServerCreateUpdate(d *pluginsdk.ResourceData, meta interface{}
auditingClient := meta.(*clients.Client).MSSQL.ServerExtendedBlobAuditingPoliciesClient
connectionClient := meta.(*clients.Client).MSSQL.ServerConnectionPoliciesClient
adminClient := meta.(*clients.Client).MSSQL.ServerAzureADAdministratorsClient
aadOnlyAuthentictionsClient := meta.(*clients.Client).MSSQL.ServerAzureADOnlyAuthenticationsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand Down Expand Up @@ -288,6 +289,18 @@ func resourceMsSqlServerCreateUpdate(d *pluginsdk.ResourceData, meta interface{}
d.SetId(id.ID())

if d.HasChange("azuread_administrator") {
aadOnlyAuthentictionsFuture, err := aadOnlyAuthentictionsClient.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
if aadOnlyAuthentictionsFuture.Response().StatusCode != 400 {
return fmt.Errorf("deleting AD Only Authentications %s: %+v", id.String(), err)
}
log.Printf("[INFO] AD Only Authentication is not removed as AD Admin is not set for %s: %+v", id.String(), err)
} else {
if err = aadOnlyAuthentictionsFuture.WaitForCompletionRef(ctx, adminClient.Client); err != nil {
return fmt.Errorf("waiting for deletion of AD Only Authentications %s: %+v", id.String(), err)
}
}

adminDelFuture, err := adminClient.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("deleting AAD admin %s: %+v", id.String(), err)
Expand All @@ -306,6 +319,15 @@ func resourceMsSqlServerCreateUpdate(d *pluginsdk.ResourceData, meta interface{}
if err = adminFuture.WaitForCompletionRef(ctx, adminClient.Client); err != nil {
return fmt.Errorf("waiting for creation of AAD admin %s: %+v", id.String(), err)
}

if aadOnlyAuthentictionsEnabled := expandMsSqlServerAADOnlyAuthentictions(d.Get("azuread_administrator").([]interface{})); aadOnlyAuthentictionsEnabled {
aadOnlyAuthentictionsParams := sql.ServerAzureADOnlyAuthentication{
AzureADOnlyAuthProperties: &sql.AzureADOnlyAuthProperties{
AzureADOnlyAuthentication: utils.Bool(aadOnlyAuthentictionsEnabled),
},
}
aadOnlyAuthentictionsClient.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, aadOnlyAuthentictionsParams)
}
}
}

Expand Down Expand Up @@ -500,6 +522,17 @@ func flattenSqlServerIdentity(identity *sql.ResourceIdentity) ([]interface{}, er
return []interface{}{result}, nil
}

func expandMsSqlServerAADOnlyAuthentictions(input []interface{}) bool {
if len(input) == 0 || input[0] == nil {
return false
}
admin := input[0].(map[string]interface{})
if v, ok := admin["azuread_only_authentication"]; ok && v != nil {
return v.(bool)
}
return false
}

func expandMsSqlServerAdministrator(input []interface{}) *sql.ServerAzureADAdministrator {
if len(input) == 0 || input[0] == nil {
return nil
Expand All @@ -516,11 +549,6 @@ func expandMsSqlServerAdministrator(input []interface{}) *sql.ServerAzureADAdmin
},
}

if v, ok := admin["azuread_only_authentication"]; ok {
adOnly, _ := v.(bool)
adminParams.AzureADOnlyAuthentication = &adOnly
}

if v, ok := admin["tenant_id"]; ok && v != "" {
tid, _ := uuid.FromString(v.(string))
adminParams.TenantID = &tid
Expand All @@ -543,17 +571,17 @@ func flatternMsSqlServerAdministrator(admin sql.ServerAzureADAdministrator) []in
tid = admin.TenantID.String()
}

var adOnly bool
var aadOnlyAuthentictionsEnabled bool
if admin.AzureADOnlyAuthentication != nil {
adOnly = *admin.AzureADOnlyAuthentication
aadOnlyAuthentictionsEnabled = *admin.AzureADOnlyAuthentication
}

return []interface{}{
map[string]interface{}{
"login_username": login,
"object_id": sid,
"tenant_id": tid,
"azuread_only_authentication": adOnly,
"azuread_only_authentication": aadOnlyAuthentictionsEnabled,
},
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/services/mssql/mssql_server_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,9 +618,9 @@ resource "azurerm_mssql_server" "test" {
administrator_login_password = "thisIsKat11"
azuread_administrator {
login_username = "AzureAD Admin2"
object_id = data.azuread_service_principal.test.id
azuread_only_authentication = true
login_username = "AzureAD Admin2"
object_id = data.azuread_service_principal.test.id
azuread_only_authentication = true
}
}
`, data.RandomInteger, data.Locations.Primary, os.Getenv("ARM_CLIENT_ID"))
Expand Down

0 comments on commit 8ebace3

Please sign in to comment.