Skip to content

Commit

Permalink
schema_grant: refactor schema grant Update function (#482)
Browse files Browse the repository at this point in the history
Related to #469
  • Loading branch information
igungor committed Mar 4, 2021
1 parent 5b0d0a0 commit f93e403
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions pkg/resources/schema_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ var schemaGrantSchema = map[string]*schema.Schema{
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "Grants privilege to these shares (only valid if on_future is unset).",
ForceNew: true,
},
"on_future": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -150,25 +149,15 @@ func UpdateSchemaGrant(d *schema.ResourceData, meta interface{}) error {
return nil
}

// difference calculates roles/shares to add/revoke
difference := func(key string) (toAdd []string, toRevoke []string) {
old, new := d.GetChange("roles")
oldSet := old.(*schema.Set)
newSet := new.(*schema.Set)
toAdd = expandStringList(newSet.Difference(oldSet).List())
toRevoke = expandStringList(oldSet.Difference(newSet).List())
return
}

rolesToAdd := []string{}
rolesToRevoke := []string{}
sharesToAdd := []string{}
sharesToRevoke := []string{}
if d.HasChange("roles") {
rolesToAdd, rolesToRevoke = difference("roles")
rolesToAdd, rolesToRevoke = changeDiff(d, "roles")
}
if d.HasChange("shares") {
sharesToAdd, sharesToRevoke = difference("shares")
sharesToAdd, sharesToRevoke = changeDiff(d, "shares")
}

grantID, err := grantIDFromString(d.Id())
Expand All @@ -189,15 +178,25 @@ func UpdateSchemaGrant(d *schema.ResourceData, meta interface{}) error {
}

// first revoke
err = deleteGenericGrantRolesAndShares(
meta, builder, grantID.Privilege, rolesToRevoke, sharesToRevoke)
if err != nil {
if err := deleteGenericGrantRolesAndShares(
meta,
builder,
grantID.Privilege,
rolesToRevoke,
sharesToRevoke,
); err != nil {
return err
}

// then add
err = createGenericGrantRolesAndShares(
meta, builder, grantID.Privilege, grantID.GrantOption, rolesToAdd, sharesToAdd)
if err != nil {
if err := createGenericGrantRolesAndShares(
meta,
builder,
grantID.Privilege,
grantID.GrantOption,
rolesToAdd,
sharesToAdd,
); err != nil {
return err
}

Expand Down

0 comments on commit f93e403

Please sign in to comment.