Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for adding comments to PostgreSQL roles #474

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions postgresql/resource_postgresql_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
roleSearchPathAttr = "search_path"
roleStatementTimeoutAttr = "statement_timeout"
roleAssumeRoleAttr = "assume_role"
roleCommentAttr = "comment"

// Deprecated options
roleDepEncryptedAttr = "encrypted"
Expand Down Expand Up @@ -173,6 +174,11 @@ func resourcePostgreSQLRole() *schema.Resource {
Optional: true,
Description: "Role to switch to at login",
},
roleCommentAttr: {
Type: schema.TypeString,
Optional: true,
Description: "The comment to set for the role",
},
},
}
}
Expand Down Expand Up @@ -311,6 +317,10 @@ func resourcePostgreSQLRoleCreate(db *DBConnection, d *schema.ResourceData) erro
return err
}

if err = setRoleComment(txn, d); err != nil {
return err
}

if err = txn.Commit(); err != nil {
return fmt.Errorf("could not commit transaction: %w", err)
}
Expand Down Expand Up @@ -689,6 +699,10 @@ func resourcePostgreSQLRoleUpdate(db *DBConnection, d *schema.ResourceData) erro
return err
}

if err := setRoleComment(txn, d); err != nil {
return err
}

if err = txn.Commit(); err != nil {
return fmt.Errorf("could not commit transaction: %w", err)
}
Expand Down Expand Up @@ -1062,3 +1076,26 @@ func setAssumeRole(txn *sql.Tx, d *schema.ResourceData) error {
}
return nil
}

func setRoleComment(txn *sql.Tx, d *schema.ResourceData) error {
if !d.HasChange(roleCommentAttr) {
return nil
}

comment := d.Get(roleCommentAttr).(string)
roleName := d.Get(roleNameAttr).(string)

if comment != "" {
sql := fmt.Sprintf("COMMENT ON ROLE %s IS '%s'", pq.QuoteIdentifier(roleName), pqQuoteLiteral(comment))
if _, err := txn.Exec(sql); err != nil {
return fmt.Errorf("Error setting comment on role %s: %w", roleName, err)
}
} else {
sql := fmt.Sprintf("COMMENT ON ROLE %s IS NULL", pq.QuoteIdentifier(roleName))
if _, err := txn.Exec(sql); err != nil {
return fmt.Errorf("Error clearing comment on role %s: %w", roleName, err)
}
}

return nil
}
14 changes: 14 additions & 0 deletions postgresql/resource_postgresql_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func TestAccPostgresqlRole_Basic(t *testing.T) {
resource.TestCheckResourceAttr("postgresql_role.sub_role", "roles.1", "role_simple"),

testAccCheckPostgresqlRoleExists("role_with_search_path", nil, []string{"bar", "foo-with-hyphen"}),

testAccCheckPostgresqlRoleExists("role_with_comment", nil, nil),
resource.TestCheckResourceAttr("postgresql_role.role_with_comment", "name", "role_with_comment"),
resource.TestCheckResourceAttr("postgresql_role.role_with_comment", "comment", "This is a test comment"),
),
},
},
Expand Down Expand Up @@ -104,6 +108,7 @@ resource "postgresql_role" "update_role" {
login = true
password = "toto"
valid_until = "2099-05-04 12:00:00+00"
comment = "Initial comment"
}
`

Expand All @@ -122,6 +127,7 @@ resource "postgresql_role" "update_role" {
statement_timeout = 30000
idle_in_transaction_session_timeout = 60000
assume_role = "${postgresql_role.group_role.name}"
comment = "Updated comment"
}
`
resource.Test(t, resource.TestCase{
Expand All @@ -146,6 +152,7 @@ resource "postgresql_role" "update_role" {
resource.TestCheckResourceAttr("postgresql_role.update_role", "statement_timeout", "0"),
resource.TestCheckResourceAttr("postgresql_role.update_role", "idle_in_transaction_session_timeout", "0"),
resource.TestCheckResourceAttr("postgresql_role.update_role", "assume_role", ""),
resource.TestCheckResourceAttr("postgresql_role.update_role", "comment", "Initial comment"),
testAccCheckRoleCanLogin(t, "update_role", "toto"),
),
},
Expand All @@ -167,6 +174,7 @@ resource "postgresql_role" "update_role" {
resource.TestCheckResourceAttr("postgresql_role.update_role", "statement_timeout", "30000"),
resource.TestCheckResourceAttr("postgresql_role.update_role", "idle_in_transaction_session_timeout", "60000"),
resource.TestCheckResourceAttr("postgresql_role.update_role", "assume_role", "group_role"),
resource.TestCheckResourceAttr("postgresql_role.update_role", "comment", "Updated comment"),
testAccCheckRoleCanLogin(t, "update_role2", "titi"),
),
},
Expand All @@ -185,6 +193,7 @@ resource "postgresql_role" "update_role" {
resource.TestCheckResourceAttr("postgresql_role.update_role", "statement_timeout", "0"),
resource.TestCheckResourceAttr("postgresql_role.update_role", "idle_in_transaction_session_timeout", "0"),
resource.TestCheckResourceAttr("postgresql_role.update_role", "assume_role", ""),
resource.TestCheckResourceAttr("postgresql_role.update_role", "comment", "Initial comment"),
testAccCheckRoleCanLogin(t, "update_role", "toto"),
),
},
Expand Down Expand Up @@ -446,4 +455,9 @@ resource "postgresql_role" "role_with_search_path" {
name = "role_with_search_path"
search_path = ["bar", "foo-with-hyphen"]
}

resource "postgresql_role" "role_with_comment" {
name = "role_with_comment"
comment = "This is a test comment"
}
`