Skip to content

Commit

Permalink
custom hash for set
Browse files Browse the repository at this point in the history
  • Loading branch information
nkvuong committed Jun 7, 2024
1 parent 53eb818 commit a2f7650
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
6 changes: 3 additions & 3 deletions catalog/permissions/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,22 @@ var Mappings = SecurableMapping{
"volume": catalog.SecurableType("volume"),
}

func normalizePrivilege(privilege string) string {
func NormalizePrivilege(privilege string) string {
return strings.ToUpper(strings.Replace(privilege, " ", "_", -1))
}

// Utils for Slice and Set
func SliceToSet(in []catalog.Privilege) *schema.Set {
var out []any
for _, v := range in {
out = append(out, normalizePrivilege(v.String()))
out = append(out, NormalizePrivilege(v.String()))
}
return schema.NewSet(schema.HashString, out)
}

func SetToSlice(set *schema.Set) (ss []catalog.Privilege) {
for _, v := range set.List() {
ss = append(ss, catalog.Privilege(normalizePrivilege(v.(string))))
ss = append(ss, catalog.Privilege(NormalizePrivilege(v.(string))))
}
return
}
Expand Down
8 changes: 6 additions & 2 deletions catalog/resource_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,13 @@ func parseSecurableId(d *schema.ResourceData) (string, string, string, error) {
func ResourceGrant() common.Resource {
s := common.StructToSchema(permissions.UnityCatalogPrivilegeAssignment{},
func(m map[string]*schema.Schema) map[string]*schema.Schema {
common.CustomizeSchemaPath(m, "principal").SetForceNew().SetCustomSuppressDiff(common.EqualFoldDiffSuppress)
common.MustSchemaPath(m, "principal").DiffSuppressFunc = common.EqualFoldDiffSuppress

m["principal"].ForceNew = true
m["principal"].DiffSuppressFunc = common.EqualFoldDiffSuppress
common.MustSchemaPath(m, "privileges").Set = func(i any) int {
privilege := i.(string)
return schema.HashString(permissions.NormalizePrivilege(privilege))
}

allFields := []string{}
for field := range permissions.Mappings {
Expand Down
14 changes: 14 additions & 0 deletions catalog/resource_grants.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ func parseId(d *schema.ResourceData) (string, string, error) {
func ResourceGrants() common.Resource {
s := common.StructToSchema(PermissionsList{},
func(s map[string]*schema.Schema) map[string]*schema.Schema {
common.MustSchemaPath(s, "grant", "privileges").Set = func(i any) int {
privilege := i.(string)
return schema.HashString(permissions.NormalizePrivilege(privilege))
}
common.MustSchemaPath(s, "grant").Set = func(i any) int {
objectStruct := i.(map[string]any)
principal := objectStruct["principal"].(string)
privileges := objectStruct["privileges"].(*schema.Set)
hashString := strings.ToLower(principal)
for _, privilege := range privileges.List() {
hashString += "|" + permissions.NormalizePrivilege(privilege.(string))
}
return schema.HashString(hashString)
}
alof := []string{}
for field := range permissions.Mappings {
s[field] = &schema.Schema{
Expand Down
6 changes: 4 additions & 2 deletions internal/acceptance/grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ func TestUcAccGrant(t *testing.T) {
}, step{
Template: strings.ReplaceAll(grantTemplate, "%s", "{env.TEST_DATA_SCI_GROUP}"),
}, step{
Template: strings.ReplaceAll(grantTemplate, `"%s"`, (`upper("{env.TEST_DATA_SCI_GROUP}")`)),
Template: strings.ReplaceAll(grantTemplate, `"%s"`, `upper("{env.TEST_DATA_SCI_GROUP}")`),
}, step{
Template: strings.ReplaceAll(grantTemplate, "ALL_PRIVILEGES", "ALL PRIVILEGES"),
})
}

Expand Down Expand Up @@ -133,6 +135,6 @@ func TestUcAccGrantForIdChange(t *testing.T) {
Template: grantTemplateForNamePermissionChange("-new", "ALL_PRIVILEGES"),
}, step{
Template: grantTemplateForNamePermissionChange("-fail", "abc"),
ExpectError: regexp.MustCompile(`cannot create grant: Privilege abc is not applicable to this entity`),
ExpectError: regexp.MustCompile(`cannot create grant: Privilege ABC is not applicable to this entity`),
})
}
6 changes: 5 additions & 1 deletion internal/acceptance/grants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ func TestUcAccGrants(t *testing.T) {
Template: strings.ReplaceAll(grantsTemplate, "%s", "{env.TEST_DATA_ENG_GROUP}"),
}, step{
Template: strings.ReplaceAll(grantsTemplate, "%s", "{env.TEST_DATA_SCI_GROUP}"),
}, step{
Template: strings.ReplaceAll(grantsTemplate, `"%s"`, `upper("{env.TEST_DATA_SCI_GROUP}")`),
}, step{
Template: strings.ReplaceAll(grantsTemplate, "ALL_PRIVILEGES", "ALL PRIVILEGES"),
})
}

Expand Down Expand Up @@ -139,6 +143,6 @@ func TestUcAccGrantsForIdChange(t *testing.T) {
Template: grantsTemplateForNamePermissionChange("-new", "ALL_PRIVILEGES"),
}, step{
Template: grantsTemplateForNamePermissionChange("-fail", "abc"),
ExpectError: regexp.MustCompile(`Error: cannot create grants: Privilege abc is not applicable to this entity`),
ExpectError: regexp.MustCompile(`Error: cannot create grants: Privilege ABC is not applicable to this entity`),
})
}

0 comments on commit a2f7650

Please sign in to comment.