Skip to content

Commit 450ce85

Browse files
Add support for routines in postgresql_default_privileges (#535)
Hi, This PR adds some support for routines in the `postgresql_default_privileges` resource. I hope it is satisfactory. Go is not my strength, so please let me know if it is insufficient as I will retract this PR in that case. Related: #470 --------- Signed-off-by: Thomas D. Spear <[email protected]> Co-authored-by: Cyril Gaudin <[email protected]>
1 parent 8cbf5c7 commit 450ce85

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

postgresql/resource_postgresql_default_privileges.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ func resourcePostgreSQLDefaultPrivileges() *schema.Resource {
5353
"table",
5454
"sequence",
5555
"function",
56+
"routine",
5657
"type",
5758
"schema",
5859
}, false),
59-
Description: "The PostgreSQL object type to set the default privileges on (one of: table, sequence, function, type, schema)",
60+
Description: "The PostgreSQL object type to set the default privileges on (one of: table, sequence, function, routine, type, schema)",
6061
},
6162
"privileges": {
6263
Type: schema.TypeSet,
@@ -87,6 +88,13 @@ func resourcePostgreSQLDefaultPrivilegesRead(db *DBConnection, d *schema.Resourc
8788
)
8889
}
8990

91+
if objectType == "routine" && !db.featureSupported(featureRoutine) {
92+
return fmt.Errorf(
93+
"object type ROUTINE is not supported for this Postgres version (%s)",
94+
db.version,
95+
)
96+
}
97+
9098
exists, err := checkRoleDBSchemaExists(db, d)
9199
if err != nil {
92100
return err
@@ -119,6 +127,13 @@ func resourcePostgreSQLDefaultPrivilegesCreate(db *DBConnection, d *schema.Resou
119127
return fmt.Errorf("cannot specify `schema` when `object_type` is `schema`")
120128
}
121129

130+
if objectType == "routine" && !db.featureSupported(featureRoutine) {
131+
return fmt.Errorf(
132+
"object type ROUTINE is not supported for this Postgres version (%s)",
133+
db.version,
134+
)
135+
}
136+
122137
if d.Get("with_grant_option").(bool) && strings.ToLower(d.Get("role").(string)) == "public" {
123138
return fmt.Errorf("with_grant_option cannot be true for role 'public'")
124139
}

postgresql/resource_postgresql_default_privileges_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,55 @@ resource "postgresql_default_privileges" "test_ro" {
346346
})
347347
}
348348
}
349+
350+
func TestAccPostgresqlDefaultPrivileges_Routines(t *testing.T) {
351+
skipIfNotAcc(t)
352+
353+
dbSuffix, teardown := setupTestDatabase(t, true, true)
354+
defer teardown()
355+
356+
config := getTestConfig(t)
357+
dbName, roleName := getTestDBNames(dbSuffix)
358+
359+
resourceConfig := fmt.Sprintf(`
360+
resource "postgresql_default_privileges" "test" {
361+
database = "%s"
362+
schema = "test_schema"
363+
owner = "%s"
364+
role = "%s"
365+
object_type = "routine"
366+
privileges = ["EXECUTE"]
367+
}
368+
`, dbName, config.Username, roleName)
369+
370+
resource.Test(t, resource.TestCase{
371+
PreCheck: func() {
372+
testAccPreCheck(t)
373+
testCheckCompatibleVersion(t, featurePrivileges)
374+
},
375+
Providers: testAccProviders,
376+
Steps: []resource.TestStep{
377+
{
378+
Config: resourceConfig,
379+
Check: resource.ComposeTestCheckFunc(
380+
// Create a test function to check if default privileges are applied
381+
func(*terraform.State) error {
382+
dbExecute(
383+
t, config.connStr(dbName),
384+
"CREATE FUNCTION test_schema.test_function() RETURNS int AS $$ SELECT 1; $$ LANGUAGE sql;",
385+
)
386+
387+
db := connectAsTestRole(t, roleName, dbName)
388+
defer closeDB(t, db)
389+
390+
if _, err := db.Exec("SELECT test_schema.test_function();"); err != nil {
391+
t.Fatalf("Expected test role to be able to execute function, got error: %s", err)
392+
}
393+
394+
return nil
395+
},
396+
),
397+
},
398+
},
399+
})
400+
}

postgresql/resource_postgresql_grant.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var objectTypes = map[string]string{
3030
"table": "r",
3131
"sequence": "S",
3232
"function": "f",
33+
"routine": "f",
3334
"type": "T",
3435
"schema": "n",
3536
}

website/docs/r/postgresql_default_privileges.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ resource "postgresql_default_privileges" "read_only_tables" {
3232
* `database` - (Required) The database to grant default privileges for this role.
3333
* `owner` - (Required) Specifies the role that creates objects for which the default privileges will be applied.
3434
* `schema` - (Optional) The database schema to set default privileges for this role.
35-
* `object_type` - (Required) The PostgreSQL object type to set the default privileges on (one of: table, sequence, function, type, schema).
35+
* `object_type` - (Required) The PostgreSQL object type to set the default privileges on (one of: table, sequence, function, routine, type, schema).
3636
* `privileges` - (Required) List of privileges (e.g., SELECT, INSERT, UPDATE, DELETE) to grant on new objects created by the owner. An empty list could be provided to revoke all default privileges for this role.
3737

3838

0 commit comments

Comments
 (0)