-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update roles when users are modified (#1317)
- Loading branch information
Showing
4 changed files
with
276 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package helpers | ||
|
||
type SQLRoleDelta struct { | ||
AddedRoles map[string]struct{} | ||
DeletedRoles map[string]struct{} | ||
} | ||
|
||
func DiffCurrentAndExpectedSQLRoles(currentRoles map[string]struct{}, expectedRoles map[string]struct{}) SQLRoleDelta { | ||
result := SQLRoleDelta{ | ||
AddedRoles: make(map[string]struct{}), | ||
DeletedRoles: make(map[string]struct{}), | ||
} | ||
|
||
for role := range expectedRoles { | ||
// If an expected role isn't in the current role set, we need to add it | ||
if _, ok := currentRoles[role]; !ok { | ||
result.AddedRoles[role] = struct{}{} | ||
} | ||
} | ||
|
||
for role := range currentRoles { | ||
// If a current role isn't in the expected set, we need to remove it | ||
if _, ok := expectedRoles[role]; !ok { | ||
result.DeletedRoles[role] = struct{}{} | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
package helpers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDiffCurrentAndExpectedSQLRoles(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
cases := []struct { | ||
name string | ||
currentRoles map[string]struct{} | ||
expectedRoles map[string]struct{} | ||
expectedRoleCreates map[string]struct{} | ||
expectedRoleDeletes map[string]struct{} | ||
}{ | ||
{ | ||
name: "Current and expected equal", | ||
currentRoles: map[string]struct{}{"USAGE": {}}, | ||
expectedRoles: map[string]struct{}{"USAGE": {}}, | ||
expectedRoleCreates: make(map[string]struct{}), | ||
expectedRoleDeletes: make(map[string]struct{}), | ||
}, | ||
{ | ||
name: "Expected has single role more than current", | ||
currentRoles: map[string]struct{}{"USAGE": {}}, | ||
expectedRoles: map[string]struct{}{"USAGE": {}, "SELECT": {}}, | ||
expectedRoleCreates: map[string]struct{}{"SELECT": {}}, | ||
expectedRoleDeletes: make(map[string]struct{}), | ||
}, | ||
{ | ||
name: "Expected has single role less than current", | ||
currentRoles: map[string]struct{}{"USAGE": {}, "SELECT": {}}, | ||
expectedRoles: map[string]struct{}{"USAGE": {}}, | ||
expectedRoleCreates: make(map[string]struct{}), | ||
expectedRoleDeletes: map[string]struct{}{"SELECT": {}}, | ||
}, | ||
{ | ||
name: "Expected has many roles less than current", | ||
currentRoles: map[string]struct{}{"SELECT": {}, "INSERT": {}, "UPDATE": {}, "DELETE": {}, "CREATE": {}, "DROP": {}, "RELOAD": {}}, | ||
expectedRoles: map[string]struct{}{"SELECT": {}, "INSERT": {}}, | ||
expectedRoleCreates: make(map[string]struct{}), | ||
expectedRoleDeletes: map[string]struct{}{"UPDATE": {}, "DELETE": {}, "CREATE": {}, "DROP": {}, "RELOAD": {}}, | ||
}, | ||
{ | ||
name: "Expected has many roles more than current", | ||
currentRoles: map[string]struct{}{"SELECT": {}, "INSERT": {}}, | ||
expectedRoles: map[string]struct{}{"SELECT": {}, "INSERT": {}, "UPDATE": {}, "DELETE": {}, "CREATE": {}, "DROP": {}, "RELOAD": {}}, | ||
expectedRoleCreates: map[string]struct{}{"UPDATE": {}, "DELETE": {}, "CREATE": {}, "DROP": {}, "RELOAD": {}}, | ||
expectedRoleDeletes: make(map[string]struct{}), | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
c := c | ||
t.Run(c.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
result := DiffCurrentAndExpectedSQLRoles(c.currentRoles, c.expectedRoles) | ||
assert.Equal(c.expectedRoleCreates, result.AddedRoles) | ||
assert.Equal(c.expectedRoleDeletes, result.DeletedRoles) | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters