Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Tenancy: add deployment configuration API and service. (#111)
spinningbot78bd5ce37a71
diff --git a/api/deploy.go b/api/deploy.gonew file mode 100644index 0000000000..4556e836fa--- /dev/null+++ b/api/deploy.go@@ -0,0 +1,104 @@+package api++import (+ "context"+)++// DeploymentConfig is the API message for deployment configurations.+type DeploymentConfig struct {+ ID int `jsonapi:"primary,deploymentConfig"`++ // Standard fields+ CreatorID int+ Creator *Principal `jsonapi:"attr,creator"`+ CreatedTs int64 `jsonapi:"attr,createdTs"`+ UpdaterID int+ Updater *Principal `jsonapi:"attr,updater"`+ UpdatedTs int64 `jsonapi:"attr,updatedTs"`++ // Related fields+ ProjectID int+ Project *Project `jsonapi:"relation,project"`++ // Domain specific fields+ Name string `jsonapi:"attr,name"`+ // Payload encapsulates DeploymentSchedule in json string format. We use json instead jsonapi because this configuration isn't queryable as HTTP format.+ Payload string `jsonapi:"attr,payload"`+}++// DeploymentSchedule is the API message for deployment schedule.+type DeploymentSchedule struct {+ Deployments []*Deployment `json:"deployments"`+}++// Deployment is the API message for deployment.+type Deployment struct {+ Spec *DeploymentSpec `json:"spec"`+}++// DeploymentSpec is the API message for deployment specification.+type DeploymentSpec struct {+ Selector *LabelSelector `json:"selector"`+}++// LabelSelector is the API message for label selector.+type LabelSelector struct {+ // MatchExpressions is a list of label selector requirements. The requirements are ANDed.+ MatchExpressions *LabelSelectorRequirement `json:"matchExpressions"`+}++// OperatorType is the type of label selector requirement operator.+// Valid operators are In, Exists.+// Note: NotIn and DoesNotExist are not supported initially.+type OperatorType string++const (+ // InOperatorType is the operator type for In.+ InOperatorType OperatorType = "In"+ // ExistsOperatorType is the operator type for Exists.+ ExistsOperatorType OperatorType = "Exists"+)++// LabelSelectorRequirement is the API message for label selector.+type LabelSelectorRequirement struct {+ // Key is the label key that the selector applies to.+ Key string `json:"key"`++ // Operator represents a key's relationship to a set of values.+ Operator OperatorType `json:"operator"`++ // Values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch.+ Values []string `json:"values"`+}++// DeploymentConfigFind is the find request for deployment configs.+type DeploymentConfigFind struct {+ ID *int++ // Related fields+ ProjectID *int+}++// DeploymentConfigUpsert is the message to upsert a deployment configuration.+// NOTE: We use PATCH for Upsert, this is inspired by https://google.aip.dev/134#patch-and-put+type DeploymentConfigUpsert struct {+ // Standard fields+ // Value is assigned from the jwt subject field passed by the client.+ // CreatorID is the ID of the creator.+ UpdaterID int++ // Related fields+ ProjectID *int `jsonapi:"attr,projectId"`++ // Domain specific fields+ Name string `jsonapi:"attr,name"`+ Payload string `jsonapi:"attr,payload"`+}++// DeploymentConfigService is the service for deployment configurations.+type DeploymentConfigService interface {+ // FindDeploymentConfig finds the deployment configuration in a project.+ FindDeploymentConfig(ctx context.Context, find *DeploymentConfigFind) (*DeploymentConfig, error)+ // UpsertDeploymentConfig upserts a deployment configuration to a project.+ UpsertDeploymentConfig(ctx context.Context, upsert *DeploymentConfigUpsert) (*DeploymentConfig, error)+}diff --git a/store/deploy.go b/store/deploy.gonew file mode 100644index 0000000000..2c30e6cff3--- /dev/null+++ b/store/deploy.go@@ -0,0 +1,137 @@+package store++import (+ "context"+ "fmt"++ "github.com/bytebase/bytebase/api"+ "github.com/bytebase/bytebase/common"+ "go.uber.org/zap"+)++var (+ _ api.DeploymentConfigService = (*DeploymentConfigService)(nil)+)++// DeploymentConfigService represents a service for managing deployment configurations.+type DeploymentConfigService struct {+ l *zap.Logger+ db *DB+}++// NewDeploymentConfigService returns a new instance of DeploymentConfigService.+func NewDeploymentConfigService(logger *zap.Logger, db *DB) *DeploymentConfigService {+ return &DeploymentConfigService{l: logger, db: db}+}++// FindDeploymentConfig finds the deployment configuration in a project.+func (s *DeploymentConfigService) FindDeploymentConfig(ctx context.Context, find *api.DeploymentConfigFind) (*api.DeploymentConfig, error) {+ tx, err := s.db.BeginTx(ctx, nil)+ if err != nil {+ return nil, FormatError(err)+ }+ defer tx.Rollback()++ rows, err := tx.QueryContext(ctx, `+ SELECT+ id,+ creator_id,+ created_ts,+ updater_id,+ updated_ts,+ project_id,+ name,+ config+ FROM deployment_config`,+ )+ if err != nil {+ return nil, FormatError(err)+ }+ defer rows.Close()++ // Iterate over result set and deserialize rows into list.+ var ret []*api.DeploymentConfig+ for rows.Next() {+ var cfg api.DeploymentConfig+ if err := rows.Scan(+ &cfg.ID,+ &cfg.CreatorID,+ &cfg.CreatedTs,+ &cfg.UpdaterID,+ &cfg.UpdatedTs,+ &cfg.ProjectID,+ &cfg.Name,+ &cfg.Payload,+ ); err != nil {+ return nil, FormatError(err)+ }++ ret = append(ret, &cfg)+ }+ if err := rows.Err(); err != nil {+ return nil, FormatError(err)+ }++ switch len(ret) {+ case 0:+ return nil, nil+ case 1:+ return ret[0], nil+ default:+ return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d deployment configurations with filter %+v, expect 1", len(ret), find)}+ }+}++// UpsertDeploymentConfig upserts a deployment configuration to a project.+func (s *DeploymentConfigService) UpsertDeploymentConfig(ctx context.Context, upsert *api.DeploymentConfigUpsert) (*api.DeploymentConfig, error) {+ tx, err := s.db.BeginTx(ctx, nil)+ if err != nil {+ return nil, FormatError(err)+ }+ defer tx.Rollback()++ row, err := tx.QueryContext(ctx, `+ INSERT INTO deployment_config (+ creator_id,+ updater_id,+ project_id,+ name,+ config+ )+ VALUES (?, ?, ?, ?, ?)+ ON CONFLICT(project_id) DO UPDATE SET+ creator_id = excluded.creator_id,+ updater_id = excluded.updater_id+ name = excluded.name,+ config = excluded.config+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, project_id, name, config+ `,+ upsert.UpdaterID,+ upsert.UpdaterID,+ upsert.ProjectID,+ upsert.Name,+ upsert.Payload,+ )++ if err != nil {+ return nil, FormatError(err)+ }+ defer row.Close()++ row.Next()+ var cfg api.DeploymentConfig+ if err := row.Scan(+ &cfg.ID,+ &cfg.CreatorID,+ &cfg.CreatedTs,+ &cfg.UpdaterID,+ &cfg.UpdatedTs,+ &cfg.ProjectID,+ &cfg.Name,+ &cfg.Payload,+ ); err != nil {+ return nil, FormatError(err)+ }++ return &cfg, nil+}diff --git a/store/policy.go b/store/policy.goindex 4ec15d7af0..312d0453ea 100644--- a/store/policy.go+++ b/store/policy.go@@ -11,7 +11,7 @@ import ()var (- _ api.EnvironmentService = (*EnvironmentService)(nil)+ _ api.PolicyService = (*PolicyService)(nil))// PolicyService represents a service for managing environment based policies.