Skip to content

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

refactor: add PolicyRaw to represent store model for Policy (#911)

Yilong Libc804f1e83ec
diff --git a/api/policy.go b/api/policy.go
index 670a6c3290..53c1834dff 100644
--- a/api/policy.go
+++ b/api/policy.go
@@ -42,6 +42,48 @@ var (
}
)
+// PolicyRaw is the store model for an Policy.
+// Fields have exactly the same meanings as Policy.
+type PolicyRaw struct {
+ ID int
+
+ // Standard fields
+ RowStatus RowStatus
+ CreatorID int
+ CreatedTs int64
+ UpdaterID int
+ UpdatedTs int64
+
+ // Related fields
+ EnvironmentID int
+
+ // Domain specific fields
+ Type PolicyType
+ Payload string
+}
+
+// ToPolicy creates an instance of Policy based on the PolicyRaw.
+// This is intended to be called when we need to compose an Policy relationship.
+func (raw *PolicyRaw) ToPolicy() *Policy {
+ return &Policy{
+ ID: raw.ID,
+
+ // Standard fields
+ RowStatus: raw.RowStatus,
+ CreatorID: raw.CreatorID,
+ CreatedTs: raw.CreatedTs,
+ UpdaterID: raw.UpdaterID,
+ UpdatedTs: raw.UpdatedTs,
+
+ // Related fields
+ EnvironmentID: raw.EnvironmentID,
+
+ // Domain specific fields
+ Type: raw.Type,
+ Payload: raw.Payload,
+ }
+}
+
// Policy is the API message for a policy.
type Policy struct {
ID int `jsonapi:"primary,policy"`
@@ -93,8 +135,8 @@ type PolicyUpsert struct {
// PolicyService is the backend for policies.
type PolicyService interface {
- FindPolicy(ctx context.Context, find *PolicyFind) (*Policy, error)
- UpsertPolicy(ctx context.Context, upsert *PolicyUpsert) (*Policy, error)
+ FindPolicy(ctx context.Context, find *PolicyFind) (*PolicyRaw, error)
+ UpsertPolicy(ctx context.Context, upsert *PolicyUpsert) (*PolicyRaw, error)
GetBackupPlanPolicy(ctx context.Context, environmentID int) (*BackupPlanPolicy, error)
GetPipelineApprovalPolicy(ctx context.Context, environmentID int) (*PipelineApprovalPolicy, error)
}
diff --git a/server/policy.go b/server/policy.go
index ec1c00aff5..d29f1f3333 100644
--- a/server/policy.go
+++ b/server/policy.go
@@ -56,13 +56,14 @@ func (s *Server) registerPolicyRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusForbidden, err.Error()).SetInternal(err)
}
- policy, err := s.PolicyService.UpsertPolicy(ctx, policyUpsert)
+ policyRaw, err := s.PolicyService.UpsertPolicy(ctx, policyUpsert)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to set policy for type %q", pType)).SetInternal(err)
}
- if err := s.composePolicyRelationship(ctx, policy); err != nil {
- return err
+ policy, err := s.composePolicyRelationship(ctx, policyRaw)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to compose policy relationship for type %q", pType)).SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
@@ -86,12 +87,13 @@ func (s *Server) registerPolicyRoutes(g *echo.Group) {
policyFind.Type = &pType
policyFind.EnvironmentID = &environmentID
- policy, err := s.PolicyService.FindPolicy(ctx, policyFind)
+ policyRaw, err := s.PolicyService.FindPolicy(ctx, policyFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to get policy for type %q", pType)).SetInternal(err)
}
- if err := s.composePolicyRelationship(ctx, policy); err != nil {
+ policy, err := s.composePolicyRelationship(ctx, policyRaw)
+ if err != nil {
return err
}
@@ -103,23 +105,26 @@ func (s *Server) registerPolicyRoutes(g *echo.Group) {
})
}
-func (s *Server) composePolicyRelationship(ctx context.Context, policy *api.Policy) error {
- var err error
+func (s *Server) composePolicyRelationship(ctx context.Context, raw *api.PolicyRaw) (*api.Policy, error) {
+ policy := raw.ToPolicy()
- policy.Creator, err = s.composePrincipalByID(ctx, policy.CreatorID)
+ creator, err := s.composePrincipalByID(ctx, policy.CreatorID)
if err != nil {
- return err
+ return nil, err
}
+ policy.Creator = creator
- policy.Updater, err = s.composePrincipalByID(ctx, policy.UpdaterID)
+ updater, err := s.composePrincipalByID(ctx, policy.UpdaterID)
if err != nil {
- return err
+ return nil, err
}
+ policy.Updater = updater
- policy.Environment, err = s.composeEnvironmentByID(ctx, policy.EnvironmentID)
+ env, err := s.composeEnvironmentByID(ctx, policy.EnvironmentID)
if err != nil {
- return err
+ return nil, err
}
+ policy.Environment = env
- return nil
+ return policy, nil
}
diff --git a/store/policy.go b/store/policy.go
index 12de8b611d..67e1f33fdf 100644
--- a/store/policy.go
+++ b/store/policy.go
@@ -30,7 +30,7 @@ func NewPolicyService(logger *zap.Logger, db *DB, cache api.CacheService) *Polic
// FindPolicy finds the policy for an environment.
// Returns ECONFLICT if finding more than 1 matching records.
-func (s *PolicyService) FindPolicy(ctx context.Context, find *api.PolicyFind) (*api.Policy, error) {
+func (s *PolicyService) FindPolicy(ctx context.Context, find *api.PolicyFind) (*api.PolicyRaw, error) {
// Validate policy type existence.
if find.Type != nil && *find.Type != "" {
if err := api.ValidatePolicy(*find.Type, ""); err != nil {
@@ -43,23 +43,23 @@ func (s *PolicyService) FindPolicy(ctx context.Context, find *api.PolicyFind) (*
}
defer tx.PTx.Rollback()
- list, err := s.findPolicy(ctx, tx.PTx, find)
- var ret *api.Policy
+ policyRawList, err := s.findPolicy(ctx, tx.PTx, find)
+ var ret *api.PolicyRaw
if err != nil {
return nil, err
}
- if len(list) == 0 {
- ret = &api.Policy{
+ if len(policyRawList) == 0 {
+ ret = &api.PolicyRaw{
CreatorID: api.SystemBotID,
UpdaterID: api.SystemBotID,
EnvironmentID: *find.EnvironmentID,
Type: *find.Type,
}
- } else if len(list) > 1 {
- return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d policy with filter %+v, expect 1. ", len(list), find)}
+ } else if len(policyRawList) > 1 {
+ return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d policy with filter %+v, expect 1. ", len(policyRawList), find)}
} else {
- ret = list[0]
+ ret = policyRawList[0]
}
if ret.Payload == "" {
@@ -73,7 +73,7 @@ func (s *PolicyService) FindPolicy(ctx context.Context, find *api.PolicyFind) (*
return ret, nil
}
-func (s *PolicyService) findPolicy(ctx context.Context, tx *sql.Tx, find *api.PolicyFind) ([]*api.Policy, error) {
+func (s *PolicyService) findPolicy(ctx context.Context, tx *sql.Tx, find *api.PolicyFind) ([]*api.PolicyRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.ID; v != nil {
@@ -105,34 +105,34 @@ func (s *PolicyService) findPolicy(ctx context.Context, tx *sql.Tx, find *api.Po
}
defer rows.Close()
- // Iterate over result set and deserialize rows into policyList.
- var policyList []*api.Policy
+ // Iterate over result set and deserialize rows into policyRawList.
+ var policyRawList []*api.PolicyRaw
for rows.Next() {
- var policy api.Policy
+ var policyRaw api.PolicyRaw
if err := rows.Scan(
- &policy.ID,
- &policy.CreatorID,
- &policy.CreatedTs,
- &policy.UpdaterID,
- &policy.UpdatedTs,
- &policy.EnvironmentID,
- &policy.Type,
- &policy.Payload,
+ &policyRaw.ID,
+ &policyRaw.CreatorID,
+ &policyRaw.CreatedTs,
+ &policyRaw.UpdaterID,
+ &policyRaw.UpdatedTs,
+ &policyRaw.EnvironmentID,
+ &policyRaw.Type,
+ &policyRaw.Payload,
); err != nil {
return nil, FormatError(err)
}
- policyList = append(policyList, &policy)
+ policyRawList = append(policyRawList, &policyRaw)
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
}
- return policyList, nil
+ return policyRawList, nil
}
// UpsertPolicy sets a policy for an environment.
-func (s *PolicyService) UpsertPolicy(ctx context.Context, upsert *api.PolicyUpsert) (*api.Policy, error) {
+func (s *PolicyService) UpsertPolicy(ctx context.Context, upsert *api.PolicyUpsert) (*api.PolicyRaw, error) {
// Validate policy.
if upsert.Type != "" {
if err := api.ValidatePolicy(upsert.Type, upsert.Payload); err != nil {
@@ -158,7 +158,7 @@ func (s *PolicyService) UpsertPolicy(ctx context.Context, upsert *api.PolicyUpse
}
// upsertPolicy updates an existing policy.
-func (s *PolicyService) upsertPolicy(ctx context.Context, tx *sql.Tx, upsert *api.PolicyUpsert) (*api.Policy, error) {
+func (s *PolicyService) upsertPolicy(ctx context.Context, tx *sql.Tx, upsert *api.PolicyUpsert) (*api.PolicyRaw, error) {
// Upsert row into policy.
if upsert.Payload == "" {
upsert.Payload = "{}"
@@ -189,21 +189,21 @@ func (s *PolicyService) upsertPolicy(ctx context.Context, tx *sql.Tx, upsert *ap
defer row.Close()
row.Next()
- var policy api.Policy
+ var policyRaw api.PolicyRaw
if err := row.Scan(
- &policy.ID,
- &policy.CreatorID,
- &policy.CreatedTs,
- &policy.UpdaterID,
- &policy.UpdatedTs,
- &policy.EnvironmentID,
- &policy.Type,
- &policy.Payload,
+ &policyRaw.ID,
+ &policyRaw.CreatorID,
+ &policyRaw.CreatedTs,
+ &policyRaw.UpdaterID,
+ &policyRaw.UpdatedTs,
+ &policyRaw.EnvironmentID,
+ &policyRaw.Type,
+ &policyRaw.Payload,
); err != nil {
return nil, FormatError(err)
}
- return &policy, nil
+ return &policyRaw, nil
}
// GetBackupPlanPolicy will get the backup plan policy for an environment.