@@ -14,6 +14,7 @@ import (
14
14
)
15
15
16
16
var allowedObjectTypes = []string {
17
+ "system" ,
17
18
"database" ,
18
19
"function" ,
19
20
"procedure" ,
@@ -255,6 +256,16 @@ func resourcePostgreSQLGrantDelete(db *DBConnection, d *schema.ResourceData) err
255
256
return nil
256
257
}
257
258
259
+ func readSystemRolePriviges (txn * sql.Tx , role string ) error {
260
+ var query string
261
+ var privileges pq.ByteaArray
262
+ query = fmt .Sprintf (`with a as (show system grants for %s) select array_agg(privilege_type) from a` , role )
263
+ if err := txn .QueryRow (query ).Scan (& privileges ); err != nil {
264
+ return fmt .Errorf ("could not read system privileges: %w" , err )
265
+ }
266
+ return nil
267
+ }
268
+
258
269
func readDatabaseRolePriviges (txn * sql.Tx , db * DBConnection , d * schema.ResourceData , roleOID uint32 , role string ) error {
259
270
dbName := d .Get ("database" ).(string )
260
271
var query string
@@ -443,6 +454,8 @@ func readRolePrivileges(txn *sql.Tx, db *DBConnection, d *schema.ResourceData) e
443
454
var rows * sql.Rows
444
455
445
456
switch objectType {
457
+ case "system" :
458
+ return readSystemRolePriviges (txn , role )
446
459
case "database" :
447
460
return readDatabaseRolePriviges (txn , db , d , roleOID , role )
448
461
@@ -547,6 +560,12 @@ func createGrantQuery(d *schema.ResourceData, privileges []string) string {
547
560
var query string
548
561
549
562
switch strings .ToUpper (d .Get ("object_type" ).(string )) {
563
+ case "SYSTEM" :
564
+ query = fmt .Sprintf (
565
+ "GRANT SYSTEM %s TO %s" ,
566
+ strings .Join (privileges , "," ),
567
+ pq .QuoteIdentifier (d .Get ("role" ).(string )),
568
+ )
550
569
case "DATABASE" :
551
570
query = fmt .Sprintf (
552
571
"GRANT %s ON DATABASE %s TO %s" ,
@@ -618,6 +637,11 @@ func createRevokeQuery(d *schema.ResourceData) string {
618
637
var query string
619
638
620
639
switch strings .ToUpper (d .Get ("object_type" ).(string )) {
640
+ case "SYSTEM" :
641
+ query = fmt .Sprintf (
642
+ "REVOKE SYSTEM ALL FROM %s" ,
643
+ pq .QuoteIdentifier (d .Get ("role" ).(string )),
644
+ )
621
645
case "DATABASE" :
622
646
query = fmt .Sprintf (
623
647
"REVOKE ALL PRIVILEGES ON DATABASE %s FROM %s" ,
@@ -839,19 +863,25 @@ func getRolesToGrant(txn *sql.Tx, d *schema.ResourceData) ([]string, error) {
839
863
func validateFeatureSupport (db * DBConnection , d * schema.ResourceData ) error {
840
864
if ! db .featureSupported (featurePrivileges ) {
841
865
return fmt .Errorf (
842
- "postgresql_grant resource is not supported for this Postgres version (%s)" ,
866
+ "postgresql_grant resource is not supported for this version (%s)" ,
843
867
db .version ,
844
868
)
845
869
}
846
870
if d .Get ("object_type" ) == "procedure" && ! db .featureSupported (featureProcedure ) {
847
871
return fmt .Errorf (
848
- "object type PROCEDURE is not supported for this Postgres version (%s)" ,
872
+ "object type PROCEDURE is not supported for this version (%s)" ,
849
873
db .version ,
850
874
)
851
875
}
852
876
if d .Get ("object_type" ) == "routine" && ! db .featureSupported (featureRoutine ) {
853
877
return fmt .Errorf (
854
- "object type ROUTINE is not supported for this Postgres version (%s)" ,
878
+ "object type ROUTINE is not supported for this version (%s)" ,
879
+ db .version ,
880
+ )
881
+ }
882
+ if d .Get ("object_type" ) == "system" && ! db .featureSupported (featureSysPrivileges ) {
883
+ return fmt .Errorf (
884
+ "privelege type System is not supported for this version (%s)" ,
855
885
db .version ,
856
886
)
857
887
}
0 commit comments