Skip to content

Commit

Permalink
azurerm_mssql_server - updated to use parser (#13151)
Browse files Browse the repository at this point in the history
* update mssql_server to use parser

* update mssql_server log statements
  • Loading branch information
catriona-m authored Aug 27, 2021
1 parent f1e39e1 commit 12f3dc0
Showing 1 changed file with 38 additions and 48 deletions.
86 changes: 38 additions & 48 deletions internal/services/mssql/mssql_server_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,15 @@ func resourceMsSqlServer() *pluginsdk.Resource {

func resourceMsSqlServerCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MSSQL.ServersClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
auditingClient := meta.(*clients.Client).MSSQL.ServerExtendedBlobAuditingPoliciesClient
connectionClient := meta.(*clients.Client).MSSQL.ServerConnectionPoliciesClient
adminClient := meta.(*clients.Client).MSSQL.ServerAzureADAdministratorsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
id := parse.NewServerID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

location := azure.NormalizeLocation(d.Get("location").(string))
adminUsername := d.Get("administrator_login").(string)
version := d.Get("version").(string)
Expand All @@ -196,10 +197,10 @@ func resourceMsSqlServerCreateUpdate(d *pluginsdk.ResourceData, meta interface{}
metadata := tags.Expand(t)

if d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, name)
existing, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing SQL Server %q (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("checking for presence of existing %s: %+v", id.String(), err)
}
}

Expand Down Expand Up @@ -236,44 +237,39 @@ func resourceMsSqlServerCreateUpdate(d *pluginsdk.ResourceData, meta interface{}
props.ServerProperties.MinimalTLSVersion = utils.String(v.(string))
}

future, err := client.CreateOrUpdate(ctx, resGroup, name, props)
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, props)
if err != nil {
return fmt.Errorf("issuing create/update request for SQL Server %q (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("issuing create/update request for %s: %+v", id.String(), err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
if response.WasConflict(future.Response()) {
return fmt.Errorf("SQL Server names need to be globally unique and %q is already in use.", name)
return fmt.Errorf("SQL Server names need to be globally unique and %q is already in use.", id.Name)
}

return fmt.Errorf("waiting on create/update future for SQL Server %q (Resource Group %q): %+v", name, resGroup, err)
}

resp, err := client.Get(ctx, resGroup, name)
if err != nil {
return fmt.Errorf("issuing get request for SQL Server %q (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("waiting for creation/update of %s: %+v", id.String(), err)
}

d.SetId(*resp.ID)
d.SetId(id.ID())

if d.HasChange("azuread_administrator") {
adminDelFuture, err := adminClient.Delete(ctx, resGroup, name)
adminDelFuture, err := adminClient.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("deleting SQL Server %q AAD admin (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("deleting AAD admin %s: %+v", id.String(), err)
}

if err = adminDelFuture.WaitForCompletionRef(ctx, adminClient.Client); err != nil {
return fmt.Errorf("waiting for SQL Server %q AAD admin (Resource Group %q) to be deleted: %+v", name, resGroup, err)
return fmt.Errorf("waiting for deletion of AAD admin %s: %+v", id.String(), err)
}

if adminParams := expandMsSqlServerAdministrator(d.Get("azuread_administrator").([]interface{})); adminParams != nil {
adminFuture, err := adminClient.CreateOrUpdate(ctx, resGroup, name, *adminParams)
adminFuture, err := adminClient.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, *adminParams)
if err != nil {
return fmt.Errorf("creating SQL Server %q AAD admin (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("creating AAD admin %s: %+v", id.String(), err)
}

if err = adminFuture.WaitForCompletionRef(ctx, adminClient.Client); err != nil {
return fmt.Errorf("waiting for creation of SQL Server %q AAD admin (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("waiting for creation of AAD admin %s: %+v", id.String(), err)
}
}
}
Expand All @@ -283,21 +279,21 @@ func resourceMsSqlServerCreateUpdate(d *pluginsdk.ResourceData, meta interface{}
ConnectionType: sql.ServerConnectionType(d.Get("connection_policy").(string)),
},
}
if _, err = connectionClient.CreateOrUpdate(ctx, resGroup, name, connection); err != nil {
return fmt.Errorf("issuing create/update request for SQL Server %q Connection Policy (Resource Group %q): %+v", name, resGroup, err)
if _, err = connectionClient.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, connection); err != nil {
return fmt.Errorf("issuing create/update request for Connection Policy %s: %+v", id.String(), err)
}

auditingProps := sql.ExtendedServerBlobAuditingPolicy{
ExtendedServerBlobAuditingPolicyProperties: helper.ExpandSqlServerBlobAuditingPolicies(d.Get("extended_auditing_policy").([]interface{})),
}

auditingFuture, err := auditingClient.CreateOrUpdate(ctx, resGroup, name, auditingProps)
auditingFuture, err := auditingClient.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, auditingProps)
if err != nil {
return fmt.Errorf("issuing create/update request for SQL Server %q Blob Auditing Policies(Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("issuing create/update request for Blob Auditing Policies %s: %+v", id.String(), err)
}

if err = auditingFuture.WaitForCompletionRef(ctx, auditingClient.Client); err != nil {
return fmt.Errorf("waiting for creation of SQL Server %q Blob Auditing Policies(Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("waiting for creation of Blob Auditing Policies %s: %+v", id.String(), err)
}

return resourceMsSqlServerRead(d, meta)
Expand All @@ -312,27 +308,24 @@ func resourceMsSqlServerRead(d *pluginsdk.ResourceData, meta interface{}) error
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.ServerID(d.Id())
if err != nil {
return err
}

resGroup := id.ResourceGroup
name := id.Path["servers"]

resp, err := client.Get(ctx, resGroup, name)
resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Error reading SQL Server %q - removing from state", d.Id())
log.Printf("[INFO] Error reading SQL Server %s - removing from state", id.String())
d.SetId("")
return nil
}

return fmt.Errorf("reading SQL Server %s: %v", name, err)
return fmt.Errorf("reading SQL Server %s: %v", id.Name, err)
}

d.Set("name", name)
d.Set("resource_group_name", resGroup)
d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
Expand All @@ -349,38 +342,38 @@ func resourceMsSqlServerRead(d *pluginsdk.ResourceData, meta interface{}) error
d.Set("public_network_access_enabled", props.PublicNetworkAccess == sql.ServerPublicNetworkAccessEnabled)
}

adminResp, err := adminClient.Get(ctx, resGroup, name)
adminResp, err := adminClient.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(adminResp.Response) {
return fmt.Errorf("reading SQL Server %s AAD admin: %v", name, err)
return fmt.Errorf("reading AAD admin %s: %v", id.Name, err)
}
} else {
if err := d.Set("azuread_administrator", flatternMsSqlServerAdministrator(adminResp)); err != nil {
return fmt.Errorf("setting `azuread_administrator`: %+v", err)
}
}

connection, err := connectionClient.Get(ctx, resGroup, name)
connection, err := connectionClient.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("reading SQL Server %s Blob Connection Policy: %v ", name, err)
return fmt.Errorf("reading SQL Server %s Blob Connection Policy: %v ", id.Name, err)
}

if props := connection.ServerConnectionPolicyProperties; props != nil {
d.Set("connection_policy", string(props.ConnectionType))
}

auditingResp, err := auditingClient.Get(ctx, resGroup, name)
auditingResp, err := auditingClient.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("reading SQL Server %s Blob Auditing Policies: %v ", name, err)
return fmt.Errorf("reading SQL Server %s Blob Auditing Policies: %v ", id.Name, err)
}

if err := d.Set("extended_auditing_policy", helper.FlattenSqlServerBlobAuditingPolicies(&auditingResp, d)); err != nil {
return fmt.Errorf("setting `extended_auditing_policy`: %+v", err)
}

restorableResp, err := restorableDroppedDatabasesClient.ListByServer(ctx, resGroup, name)
restorableResp, err := restorableDroppedDatabasesClient.ListByServer(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("listing SQL Server %s Restorable Dropped Databases: %v", name, err)
return fmt.Errorf("listing SQL Server %s Restorable Dropped Databases: %v", id.Name, err)
}
if err := d.Set("restorable_dropped_database_ids", flattenSqlServerRestorableDatabases(restorableResp)); err != nil {
return fmt.Errorf("setting `restorable_dropped_database_ids`: %+v", err)
Expand All @@ -394,17 +387,14 @@ func resourceMsSqlServerDelete(d *pluginsdk.ResourceData, meta interface{}) erro
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.ServerID(d.Id())
if err != nil {
return err
}

resGroup := id.ResourceGroup
name := id.Path["servers"]

future, err := client.Delete(ctx, resGroup, name)
future, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("deleting SQL Server %s: %+v", name, err)
return fmt.Errorf("deleting SQL Server %s: %+v", id.Name, err)
}

return future.WaitForCompletionRef(ctx, client.Client)
Expand Down

0 comments on commit 12f3dc0

Please sign in to comment.