Skip to content

Commit ebbd6d3

Browse files
authored
Improve databricks_grants error messages (#1888)
1 parent 724bfa1 commit ebbd6d3

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

catalog/resource_grants.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ func (sm securableMapping) validate(d attributeGetter, pl PermissionsList) error
154154
for _, v := range pl.Assignments {
155155
for _, priv := range v.Privileges {
156156
if !allowed[strings.ToUpper(priv)] {
157+
// check if user uses spaces instead of underscores
158+
if allowed[strings.ReplaceAll(priv, " ", "_")] {
159+
return fmt.Errorf(`%s is not allowed on %s. Did you mean %s?`, priv, securable, strings.ReplaceAll(priv, " ", "_"))
160+
}
157161
return fmt.Errorf(`%s is not allowed on %s`, priv, securable)
158162
}
159163
}

catalog/resource_grants_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,27 @@ func TestShareGrantUpdate(t *testing.T) {
347347
}`,
348348
}.ApplyNoError(t)
349349
}
350+
351+
func TestPrivilegeWithSpace(t *testing.T) {
352+
d := data{"table": "me"}
353+
err := mapping.validate(d, PermissionsList{
354+
Assignments: []PrivilegeAssignment{
355+
{
356+
Principal: "me",
357+
Privileges: []string{"ALL PRIVILEGES"},
358+
},
359+
},
360+
})
361+
assert.EqualError(t, err, "ALL PRIVILEGES is not allowed on table. Did you mean ALL_PRIVILEGES?")
362+
363+
d = data{"external_location": "me"}
364+
err = mapping.validate(d, PermissionsList{
365+
Assignments: []PrivilegeAssignment{
366+
{
367+
Principal: "me",
368+
Privileges: []string{"CREATE TABLE"},
369+
},
370+
},
371+
})
372+
assert.EqualError(t, err, "CREATE TABLE is not allowed on external_location. Did you mean CREATE_TABLE?")
373+
}

docs/resources/grants.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Terraform will handle any configuration drift on every `terraform apply` run, ev
3333

3434
It is required to define all permissions for a securable in a single resource, otherwise Terraform cannot guarantee config drift prevention.
3535

36-
Below summarizes which privilege types apply to each securable object in the catalog:
36+
Unlike the [SQL specification](https://docs.databricks.com/sql/language-manual/sql-ref-privileges.html#privilege-types), all privileges to be written with underscore instead of space, e.g. `CREATE_TABLE` and not `CREATE TABLE`. Below summarizes which privilege types apply to each securable object in the catalog:
3737

3838
## Metastore grants
3939

0 commit comments

Comments
 (0)