Skip to content

Commit

Permalink
fix: database nil pointer dereference
Browse files Browse the repository at this point in the history
  • Loading branch information
jrauschenbusch committed May 13, 2020
1 parent 00111fe commit 93c007b
Showing 1 changed file with 12 additions and 8 deletions.
Expand Up @@ -142,16 +142,20 @@ func resourceArmKustoDatabasePrincipalCreate(d *schema.ResourceData, meta interf
fqn = fmt.Sprintf("aadapp=%s;%s", objectID, clientID)
}

databaseResp, err := client.Get(ctx, resourceGroup, clusterName, databaseName)
databaseModel, err := client.Get(ctx, resourceGroup, clusterName, databaseName)
if err != nil {
if utils.ResponseWasNotFound(databaseResp.Response) {
if utils.ResponseWasNotFound(databaseModel.Response) {
return fmt.Errorf("Kusto Database %q (Resource Group %q) was not found", databaseName, resourceGroup)
}

return fmt.Errorf("Error loading Kusto Database %q (Resource Group %q): %+v", databaseName, resourceGroup, err)
}

database, _ := databaseResp.Value.AsDatabase()
if databaseModel.Value == nil {
return fmt.Errorf("Kusto Database %q was not found ", databaseName)
}

database, _ := databaseModel.Value.AsDatabase()
resourceID := fmt.Sprintf("%s/Role/%s/FQN/%s", *database.ID, role, fqn)

if features.ShouldResourcesBeImported() && d.IsNewResource() {
Expand Down Expand Up @@ -214,25 +218,25 @@ func resourceArmKustoDatabasePrincipalRead(d *schema.ResourceData, meta interfac
return err
}

databaseResponse, err := client.Get(ctx, id.ResourceGroup, id.Cluster, id.Database)
databaseModel, err := client.Get(ctx, id.ResourceGroup, id.Cluster, id.Database)
if err != nil {
if utils.ResponseWasNotFound(databaseResponse.Response) {
if utils.ResponseWasNotFound(databaseModel.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving Kusto Database %q (Resource Group %q, Cluster %q): %+v", id.Database, id.ResourceGroup, id.Cluster, err)
}

resp, err := client.ListPrincipals(ctx, id.ResourceGroup, id.Cluster, id.Database)
databasePrincipals, err := client.ListPrincipals(ctx, id.ResourceGroup, id.Cluster, id.Database)
if err != nil {
if !utils.ResponseWasNotFound(resp.Response) {
if !utils.ResponseWasNotFound(databasePrincipals.Response) {
return fmt.Errorf("Error checking for presence of existing Kusto Database Principals %q (Resource Group %q, Cluster %q): %s", id, id.ResourceGroup, id.Cluster, err)
}
}

principal := kusto.DatabasePrincipal{}
found := false
if principals := resp.Value; principals != nil {
if principals := databasePrincipals.Value; principals != nil {
for _, currPrincipal := range *principals {
// kusto database principals are unique when looked at with fqn and role
if string(currPrincipal.Role) == id.Role && currPrincipal.Fqn != nil && *currPrincipal.Fqn == id.Name {
Expand Down

0 comments on commit 93c007b

Please sign in to comment.