Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: OpenAPI for PG roles (#3808)

ecmadao92ec9fa546a5
diff --git a/plugin/advisor/utils_for_tests.go b/plugin/advisor/utils_for_tests.go
index 251ceec3bd..3ca44b15c1 100644
--- a/plugin/advisor/utils_for_tests.go
+++ b/plugin/advisor/utils_for_tests.go
@@ -317,6 +317,26 @@ func (*MockDriver) Restore(_ context.Context, _ io.Reader) error {
return nil
}
+// CreateRole creates the role.
+func (*MockDriver) CreateRole(_ context.Context, _ *database.RoleUpsert) (*database.Role, error) {
+ return nil, nil
+}
+
+// UpdateRole updates the role.
+func (*MockDriver) UpdateRole(_ context.Context, _ string, _ *database.RoleUpsert) (*database.Role, error) {
+ return nil, nil
+}
+
+// FindRole finds the role by name.
+func (*MockDriver) FindRole(_ context.Context, _ string) (*database.Role, error) {
+ return nil, nil
+}
+
+// DeleteRole deletes the role by name.
+func (*MockDriver) DeleteRole(_ context.Context, _ string) error {
+ return nil
+}
+
// SetDefaultSQLReviewRulePayload sets the default payload for this rule.
func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType) (string, error) {
var payload []byte
diff --git a/plugin/db/clickhouse/role.go b/plugin/db/clickhouse/role.go
new file mode 100644
index 0000000000..b3c218bf04
--- /dev/null
+++ b/plugin/db/clickhouse/role.go
@@ -0,0 +1,29 @@
+package clickhouse
+
+import (
+ "context"
+
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/plugin/db"
+)
+
+// CreateRole creates the role.
+func (*Driver) CreateRole(_ context.Context, _ *db.RoleUpsert) (*db.Role, error) {
+ return nil, errors.Errorf("create role for ClickHouse is not implemented yet")
+}
+
+// UpdateRole updates the role.
+func (*Driver) UpdateRole(_ context.Context, _ string, _ *db.RoleUpsert) (*db.Role, error) {
+ return nil, errors.Errorf("update role for ClickHouse is not implemented yet")
+}
+
+// FindRole finds the role by name.
+func (*Driver) FindRole(_ context.Context, _ string) (*db.Role, error) {
+ return nil, errors.Errorf("find role for ClickHouse is not implemented yet")
+}
+
+// DeleteRole deletes the role by name.
+func (*Driver) DeleteRole(_ context.Context, _ string) error {
+ return errors.Errorf("delete role for ClickHouse is not implemented yet")
+}
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index 26b8bf9595..b1df5f6bf4 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -164,6 +164,35 @@ type Schema struct {
ExtensionList []Extension
}
+// RoleAttribute is the attribute for role.
+type RoleAttribute struct {
+ SuperUser bool `json:"superUser"`
+ NoInherit bool `json:"noInherit"`
+ CreateRole bool `json:"createRole"`
+ CreateDB bool `json:"createDB"`
+ CanLogin bool `json:"canLogin"`
+ Replication bool `json:"replication"`
+ ByPassRLS bool `json:"byPassRLS"`
+}
+
+// Role is the API message for role.
+type Role struct {
+ Name string `json:"name"`
+ ConnectionLimit int `json:"connectionLimit"`
+ ValidUntil *string `json:"validUntil"`
+ Attribute *RoleAttribute `json:"attribute"`
+}
+
+// RoleUpsert is the API message for upserting a new role.
+// TODO(ed): we may need to support IN ROLE role_name, ROLE role_name[,...] and ADMIN role_name[,...] statement.
+type RoleUpsert struct {
+ Name string `json:"name"`
+ Password *string `json:"password"`
+ ConnectionLimit *int `json:"connectionLimit"`
+ ValidUntil *string `json:"validUntil"`
+ Attribute *RoleAttribute `json:"attribute"`
+}
+
var (
driversMu sync.RWMutex
drivers = make(map[Type]driverFunc)
@@ -499,6 +528,16 @@ type Driver interface {
// SyncDBSchema syncs a single database schema.
SyncDBSchema(ctx context.Context, database string) (*Schema, error)
+ // Role
+ // CreateRole creates the role.
+ CreateRole(ctx context.Context, upsert *RoleUpsert) (*Role, error)
+ // UpdateRole updates the role.
+ UpdateRole(ctx context.Context, roleName string, upsert *RoleUpsert) (*Role, error)
+ // FindRole finds the role by name.
+ FindRole(ctx context.Context, roleName string) (*Role, error)
+ // DeleteRole deletes the role by name.
+ DeleteRole(ctx context.Context, roleName string) error
+
// Migration related
// Check whether we need to setup migration (e.g. creating/upgrading the migration related tables)
NeedsSetupMigration(ctx context.Context) (bool, error)
diff --git a/plugin/db/mongodb/role.go b/plugin/db/mongodb/role.go
new file mode 100644
index 0000000000..9433df5b83
--- /dev/null
+++ b/plugin/db/mongodb/role.go
@@ -0,0 +1,29 @@
+package mongodb
+
+import (
+ "context"
+
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/plugin/db"
+)
+
+// CreateRole creates the role.
+func (*Driver) CreateRole(_ context.Context, _ *db.RoleUpsert) (*db.Role, error) {
+ return nil, errors.Errorf("create role for MongoDB is not implemented yet")
+}
+
+// UpdateRole updates the role.
+func (*Driver) UpdateRole(_ context.Context, _ string, _ *db.RoleUpsert) (*db.Role, error) {
+ return nil, errors.Errorf("update role for MongoDB is not implemented yet")
+}
+
+// FindRole finds the role by name.
+func (*Driver) FindRole(_ context.Context, _ string) (*db.Role, error) {
+ return nil, errors.Errorf("find role for MongoDB is not implemented yet")
+}
+
+// DeleteRole deletes the role by name.
+func (*Driver) DeleteRole(_ context.Context, _ string) error {
+ return errors.Errorf("delete role for MongoDB is not implemented yet")
+}
diff --git a/plugin/db/mysql/role.go b/plugin/db/mysql/role.go
new file mode 100644
index 0000000000..330d452e7a
--- /dev/null
+++ b/plugin/db/mysql/role.go
@@ -0,0 +1,29 @@
+package mysql
+
+import (
+ "context"
+
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/plugin/db"
+)
+
+// CreateRole creates the role.
+func (*Driver) CreateRole(_ context.Context, _ *db.RoleUpsert) (*db.Role, error) {
+ return nil, errors.Errorf("create role for MySQL is not implemented yet")
+}
+
+// UpdateRole updates the role.
+func (*Driver) UpdateRole(_ context.Context, _ string, _ *db.RoleUpsert) (*db.Role, error) {
+ return nil, errors.Errorf("update role for MySQL is not implemented yet")
+}
+
+// FindRole finds the role by name.
+func (*Driver) FindRole(_ context.Context, _ string) (*db.Role, error) {
+ return nil, errors.Errorf("find role for MySQL is not implemented yet")
+}
+
+// DeleteRole deletes the role by name.
+func (*Driver) DeleteRole(_ context.Context, _ string) error {
+ return errors.Errorf("delete role for MySQL is not implemented yet")
+}
diff --git a/plugin/db/pg/role.go b/plugin/db/pg/role.go
new file mode 100644
index 0000000000..04139baf70
--- /dev/null
+++ b/plugin/db/pg/role.go
@@ -0,0 +1,238 @@
+package pg
+
+import (
+ "context"
+ "database/sql"
+ "fmt"
+ "strings"
+
+ "github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/plugin/db"
+ "github.com/bytebase/bytebase/plugin/db/util"
+)
+
+// RoleAttribute is the attribute string for role.
+type RoleAttribute string
+
+const (
+ // SUPERUSER is the role attribute for rolsuper.
+ SUPERUSER RoleAttribute = "SUPERUSER"
+ // LOGIN is the role attribute for rolcanlogin.
+ LOGIN RoleAttribute = "LOGIN"
+ // NOINHERIT is the role attribute for rolinherit.
+ // INHERIT is the default value for rolinherit, so we need to use the NOINHERIT.
+ NOINHERIT RoleAttribute = "NOINHERIT"
+ // CREATEDB is the role attribute for rolcreatedb.
+ CREATEDB RoleAttribute = "CREATEDB"
+ // CREATEROLE is the role attribute for rolcreaterole.
+ CREATEROLE RoleAttribute = "CREATEROLE"
+ // REPLICATION is the role attribute for rolreplication.
+ REPLICATION RoleAttribute = "REPLICATION"
+ // BYPASSRLS is the role attribute for rolbypassrls.
+ BYPASSRLS RoleAttribute = "BYPASSRLS"
+)
+
+// ToString returns the string value for role attribute.
+func (a RoleAttribute) ToString() string {
+ return string(a)
+}
+
+// CreateRole will create the PG role.
+func (driver *Driver) CreateRole(ctx context.Context, upsert *db.RoleUpsert) (*db.Role, error) {
+ txn, err := driver.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, err
+ }
+ defer txn.Rollback()
+
+ if err := createRoleImpl(ctx, txn, upsert); err != nil {
+ return nil, err
+ }
+
+ role, err := getRoleImpl(ctx, txn, upsert.Name)
+ if err != nil {
+ return nil, err
+ }
+
+ if err := txn.Commit(); err != nil {
+ return nil, err
+ }
+
+ return role, nil
+}
+
+// UpdateRole will alter the PG role.
+func (driver *Driver) UpdateRole(ctx context.Context, roleName string, upsert *db.RoleUpsert) (*db.Role, error) {
+ txn, err := driver.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, err
+ }
+ defer txn.Rollback()
+
+ if err := alterRoleImpl(ctx, txn, roleName, upsert); err != nil {
+ return nil, err
+ }
+
+ role, err := getRoleImpl(ctx, txn, upsert.Name)
+ if err != nil {
+ return nil, err
+ }
+
+ if err := txn.Commit(); err != nil {
+ return nil, err
+ }
+
+ return role, nil
+}
+
+// FindRole will find the PG role by name.
+func (driver *Driver) FindRole(ctx context.Context, roleName string) (*db.Role, error) {
+ txn, err := driver.db.BeginTx(ctx, nil)
+ if err != nil {
+ return nil, err
+ }
+ defer txn.Rollback()
+
+ role, err := getRoleImpl(ctx, txn, roleName)
+ if err != nil {
+ return nil, err
+ }
+
+ if err := txn.Commit(); err != nil {
+ return nil, err
+ }
+
+ return role, nil
+}
+
+// DeleteRole will drop the PG role by name.
+func (driver *Driver) DeleteRole(ctx context.Context, roleName string) error {
+ statement := fmt.Sprintf(`DROP ROLE IF EXISTS "%s"`, roleName)
+ if _, err := driver.db.ExecContext(ctx, statement); err != nil {
+ return util.FormatErrorWithQuery(err, statement)
+ }
+
+ return nil
+}
+
+func getRoleImpl(ctx context.Context, txn *sql.Tx, roleName string) (*db.Role, error) {
+ statement := fmt.Sprintf(`
+ SELECT
+ r.rolname,
+ r.rolsuper,
+ r.rolinherit,
+ r.rolcreaterole,
+ r.rolcreatedb,
+ r.rolcanlogin,
+ r.rolreplication,
+ r.rolbypassrls,
+ r.rolvaliduntil,
+ r.rolconnlimit
+ FROM pg_catalog.pg_roles r
+ WHERE r.rolname = '%s';
+ `, roleName)
+
+ role := &db.Role{
+ Name: roleName,
+ Attribute: &db.RoleAttribute{},
+ }
+
+ inherit := false
+
+ if err := txn.QueryRowContext(ctx, statement).Scan(
+ &role.Name,
+ &role.Attribute.SuperUser,
+ &inherit,
+ &role.Attribute.CreateRole,
+ &role.Attribute.CreateDB,
+ &role.Attribute.CanLogin,
+ &role.Attribute.Replication,
+ &role.Attribute.ByPassRLS,
+ &role.ValidUntil,
+ &role.ConnectionLimit,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(statement)
+ }
+ return nil, util.FormatErrorWithQuery(err, statement)
+ }
+
+ role.Attribute.NoInherit = !inherit
+
+ return role, nil
+}
+
+func createRoleImpl(ctx context.Context, txn *sql.Tx, upsert *db.RoleUpsert) error {
+ statement := fmt.Sprintf(`CREATE ROLE "%s" %s;`, upsert.Name, convertToAttributeStatement(upsert))
+ if _, err := txn.ExecContext(ctx, statement); err != nil {
+ return util.FormatErrorWithQuery(err, statement)
+ }
+
+ return nil
+}
+
+func alterRoleImpl(ctx context.Context, txn *sql.Tx, roleName string, upsert *db.RoleUpsert) error {
+ if roleName != upsert.Name {
+ renameStatement := fmt.Sprintf(`ALTER ROLE "%s" RENAME TO "%s";`, roleName, upsert.Name)
+ if _, err := txn.ExecContext(ctx, renameStatement); err != nil {
+ return util.FormatErrorWithQuery(err, renameStatement)
+ }
+ }
+
+ attributeStatement := convertToAttributeStatement(upsert)
+ if attributeStatement == "" {
+ return nil
+ }
+
+ statement := fmt.Sprintf(`ALTER ROLE "%s" %s;`, upsert.Name, attributeStatement)
+ if _, err := txn.ExecContext(ctx, statement); err != nil {
+ return util.FormatErrorWithQuery(err, statement)
+ }
+
+ return nil
+}
+
+func convertToAttributeStatement(r *db.RoleUpsert) string {
+ attributeList := []string{}
+
+ if r.Attribute != nil {
+ if r.Attribute.SuperUser {
+ attributeList = append(attributeList, SUPERUSER.ToString())
+ }
+ if r.Attribute.NoInherit {
+ attributeList = append(attributeList, NOINHERIT.ToString())
+ }
+ if r.Attribute.CanLogin {
+ attributeList = append(attributeList, LOGIN.ToString())
+ }
+ if r.Attribute.CreateRole {
+ attributeList = append(attributeList, CREATEROLE.ToString())
+ }
+ if r.Attribute.CreateDB {
+ attributeList = append(attributeList, CREATEDB.ToString())
+ }
+ if r.Attribute.Replication {
+ attributeList = append(attributeList, REPLICATION.ToString())
+ }
+ if r.Attribute.ByPassRLS {
+ attributeList = append(attributeList, BYPASSRLS.ToString())
+ }
+ }
+
+ if v := r.Password; v != nil {
+ attributeList = append(attributeList, fmt.Sprintf("ENCRYPTED PASSWORD '%s'", *v))
+ }
+ if v := r.ValidUntil; v != nil {
+ attributeList = append(attributeList, fmt.Sprintf("VALID UNTIL '%s'", *v))
+ }
+ if v := r.ConnectionLimit; v != nil {
+ attributeList = append(attributeList, fmt.Sprintf("CONNECTION LIMIT %d", *v))
+ }
+
+ attribute := ""
+ if len(attributeList) > 0 {
+ attribute = fmt.Sprintf("WITH %s", strings.Join(attributeList, " "))
+ }
+
+ return attribute
+}
diff --git a/plugin/db/snowflake/role.go b/plugin/db/snowflake/role.go
new file mode 100644
index 0000000000..945f460c51
--- /dev/null
+++ b/plugin/db/snowflake/role.go
@@ -0,0 +1,29 @@
+package snowflake
+
+import (
+ "context"
+
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/plugin/db"
+)
+
+// CreateRole creates the role.
+func (*Driver) CreateRole(_ context.Context, _ *db.RoleUpsert) (*db.Role, error) {
+ return nil, errors.Errorf("create role for Snowflake is not implemented yet")
+}
+
+// UpdateRole updates the role.
+func (*Driver) UpdateRole(_ context.Context, _ string, _ *db.RoleUpsert) (*db.Role, error) {
+ return nil, errors.Errorf("update role for Snowflake is not implemented yet")
+}
+
+// FindRole finds the role by name.
+func (*Driver) FindRole(_ context.Context, _ string) (*db.Role, error) {
+ return nil, errors.Errorf("find role for Snowflake is not implemented yet")
+}
+
+// DeleteRole deletes the role by name.
+func (*Driver) DeleteRole(_ context.Context, _ string) error {
+ return errors.Errorf("delete role for Snowflake is not implemented yet")
+}
diff --git a/plugin/db/sqlite/role.go b/plugin/db/sqlite/role.go
new file mode 100644
index 0000000000..1a71b6d26b
--- /dev/null
+++ b/plugin/db/sqlite/role.go
@@ -0,0 +1,29 @@
+package sqlite
+
+import (
+ "context"
+
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/plugin/db"
+)
+
+// CreateRole creates the role.
+func (*Driver) CreateRole(_ context.Context, _ *db.RoleUpsert) (*db.Role, error) {
+ return nil, errors.Errorf("create role for SQLite is not implemented yet")
+}
+
+// UpdateRole updates the role.
+func (*Driver) UpdateRole(_ context.Context, _ string, _ *db.RoleUpsert) (*db.Role, error) {
+ return nil, errors.Errorf("update role for SQLite is not implemented yet")
+}
+
+// FindRole finds the role by name.
+func (*Driver) FindRole(_ context.Context, _ string) (*db.Role, error) {
+ return nil, errors.Errorf("find role for SQLite is not implemented yet")
+}
+
+// DeleteRole deletes the role by name.
+func (*Driver) DeleteRole(_ context.Context, _ string) error {
+ return errors.Errorf("delete role for SQLite is not implemented yet")
+}
diff --git a/server/acl_casbin_policy_dba.csv b/server/acl_casbin_policy_dba.csv
index 61f374d7c6..435d867f3d 100644
--- a/server/acl_casbin_policy_dba.csv
+++ b/server/acl_casbin_policy_dba.csv
@@ -45,6 +45,10 @@ p, DBA, /instance/{instanceID}/migration, POST
p, DBA, /instance/{instanceID}/migration/status, GET
p, DBA, /instance/{instanceID}/migration/history, GET
p, DBA, /instance/{instanceID}/migration/history/{historyID}, GET
+p, DBA, /instance/{instanceID}/role, POST
+p, DBA, /instance/{instanceID}/role/{roleName}, GET
+p, DBA, /instance/{instanceID}/role/{roleName}, PATCH
+p, DBA, /instance/{instanceID}/role/{roleName}, DELETE
p, DBA, /database, GET
p, DBA, /database/{databaseID}, GET
p, DBA, /database/{databaseID}, PATCH
diff --git a/server/acl_casbin_policy_owner.csv b/server/acl_casbin_policy_owner.csv
index d9faf5c524..c1dba21502 100644
--- a/server/acl_casbin_policy_owner.csv
+++ b/server/acl_casbin_policy_owner.csv
@@ -48,6 +48,10 @@ p, OWNER, /instance/{instanceID}/migration, POST
p, OWNER, /instance/{instanceID}/migration/status, GET
p, OWNER, /instance/{instanceID}/migration/history, GET
p, OWNER, /instance/{instanceID}/migration/history/{historyID}, GET
+p, OWNER, /instance/{instanceID}/role, POST
+p, OWNER, /instance/{instanceID}/role/{roleName}, GET
+p, OWNER, /instance/{instanceID}/role/{roleName}, PATCH
+p, OWNER, /instance/{instanceID}/role/{roleName}, DELETE
p, OWNER, /instance/new-embedded-pg, POST
p, OWNER, /database, GET
p, OWNER, /database/{databaseID}, GET
diff --git a/server/openapi_instance.go b/server/openapi_instance.go
index ede2088a6c..f1c534d9ac 100644
--- a/server/openapi_instance.go
+++ b/server/openapi_instance.go
@@ -1,6 +1,7 @@
package server
import (
+ "context"
"encoding/json"
"fmt"
"io"
@@ -12,6 +13,7 @@ import (
"github.com/bytebase/bytebase/api"
openAPIV1 "github.com/bytebase/bytebase/api/v1"
+ "github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/store"
)
@@ -21,6 +23,10 @@ func (s *Server) registerOpenAPIRoutesForInstance(g *echo.Group) {
g.GET("/instance/:instanceID", s.getInstanceByID)
g.PATCH("/instance/:instanceID", s.updateInstanceByOpenAPI)
g.DELETE("/instance/:instanceID", s.deleteInstanceByOpenAPI)
+ g.POST("/instance/:instanceID/role", s.createPGRole)
+ g.GET("/instance/:instanceID/role/:roleName", s.getPGRole)
+ g.PATCH("/instance/:instanceID/role/:roleName", s.updatePGRole)
+ g.DELETE("/instance/:instanceID/role/:roleName", s.deletePGRole)
}
func (s *Server) listInstance(c echo.Context) error {
@@ -184,6 +190,165 @@ func (s *Server) getInstanceByID(c echo.Context) error {
return c.JSON(http.StatusOK, convertToOpenAPIInstance(instance))
}
+func (s *Server) getPGRole(c echo.Context) error {
+ ctx := c.Request().Context()
+ roleName := c.Param("roleName")
+
+ instance, err := s.validateInstance(ctx, c)
+ if err != nil {
+ return err
+ }
+
+ role, err := s.findPGRoleByName(ctx, instance, roleName)
+ if err != nil {
+ return err
+ }
+
+ return c.JSON(http.StatusOK, role)
+}
+
+func (s *Server) createPGRole(c echo.Context) error {
+ ctx := c.Request().Context()
+
+ body, err := io.ReadAll(c.Request().Body)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Failed to read request body").SetInternal(err)
+ }
+
+ upsert := &db.RoleUpsert{}
+ if err := json.Unmarshal(body, upsert); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformed create role request").SetInternal(err)
+ }
+
+ instance, err := s.validateInstance(ctx, c)
+ if err != nil {
+ return err
+ }
+
+ role, err := func() (*db.Role, error) {
+ driver, err := s.dbFactory.GetAdminDatabaseDriver(ctx, instance, "" /* database name */)
+ if err != nil {
+ return nil, err
+ }
+ defer driver.Close(ctx)
+
+ role, err := driver.CreateRole(ctx, upsert)
+ if err != nil {
+ return nil, err
+ }
+
+ return role, nil
+ }()
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to query the role").SetInternal(err)
+ }
+
+ return c.JSON(http.StatusOK, role)
+}
+
+func (s *Server) deletePGRole(c echo.Context) error {
+ ctx := c.Request().Context()
+ roleName := c.Param("roleName")
+
+ instance, err := s.validateInstance(ctx, c)
+ if err != nil {
+ return err
+ }
+
+ if err := func() error {
+ driver, err := s.dbFactory.GetAdminDatabaseDriver(ctx, instance, "" /* database name */)
+ if err != nil {
+ return err
+ }
+ defer driver.Close(ctx)
+
+ return driver.DeleteRole(ctx, roleName)
+ }(); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to drop the role %s", roleName)).SetInternal(err)
+ }
+
+ return c.String(http.StatusOK, "ok")
+}
+
+func (s *Server) updatePGRole(c echo.Context) error {
+ ctx := c.Request().Context()
+
+ body, err := io.ReadAll(c.Request().Body)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Failed to read request body").SetInternal(err)
+ }
+
+ upsert := &db.RoleUpsert{}
+ if err := json.Unmarshal(body, upsert); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformed create role request").SetInternal(err)
+ }
+
+ instance, err := s.validateInstance(ctx, c)
+ if err != nil {
+ return err
+ }
+
+ rawName := c.Param("roleName")
+ role, err := func() (*db.Role, error) {
+ driver, err := s.dbFactory.GetAdminDatabaseDriver(ctx, instance, "" /* database name */)
+ if err != nil {
+ return nil, err
+ }
+ defer driver.Close(ctx)
+
+ role, err := driver.UpdateRole(ctx, rawName, upsert)
+ if err != nil {
+ return nil, err
+ }
+
+ return role, nil
+ }()
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to query the role").SetInternal(err)
+ }
+
+ return c.JSON(http.StatusOK, role)
+}
+
+func (s *Server) validateInstance(ctx context.Context, c echo.Context) (*api.Instance, error) {
+ instanceID, err := strconv.Atoi(c.Param("instanceID"))
+ if err != nil {
+ return nil, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("instanceID"))).SetInternal(err)
+ }
+
+ instance, err := s.store.GetInstanceByID(ctx, instanceID)
+ if err != nil {
+ return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance ID: %v", instanceID)).SetInternal(err)
+ }
+ if instance == nil {
+ return nil, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", instanceID))
+ }
+
+ return instance, nil
+}
+
+func (s *Server) findPGRoleByName(ctx context.Context, instance *api.Instance, roleName string) (*db.Role, error) {
+ role, err := func() (*db.Role, error) {
+ driver, err := s.dbFactory.GetAdminDatabaseDriver(ctx, instance, "" /* database name */)
+ if err != nil {
+ return nil, err
+ }
+ defer driver.Close(ctx)
+
+ role, err := driver.FindRole(ctx, roleName)
+ if err != nil {
+ return nil, err
+ }
+
+ return role, nil
+ }()
+ if err != nil {
+ return nil, echo.NewHTTPError(http.StatusInternalServerError, "Failed to query the role").SetInternal(err)
+ }
+
+ return role, nil
+}
+
func convertToOpenAPIInstance(instance *api.Instance) *openAPIV1.Instance {
dataSourceList := []*openAPIV1.DataSource{}
for _, dataSource := range instance.DataSourceList {