Skip to content

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

feat: impl environment setting (#15761)

p0nyebad42b6f58b
diff --git a/backend/api/v1/setting_service.go b/backend/api/v1/setting_service.go
index 42cd2e9453..2c715a239d 100644
--- a/backend/api/v1/setting_service.go
+++ b/backend/api/v1/setting_service.go
@@ -78,6 +78,7 @@ var whitelistSettings = []api.SettingName{
api.SettingSQLResultSizeLimit,
api.SettingSCIM,
api.SettingPasswordRestriction,
+ api.SettingEnvironment,
}
//go:embed mail_templates/testmail/template.html
@@ -591,6 +592,42 @@ func (s *SettingService) UpdateSetting(ctx context.Context, request *v1pb.Update
return nil, status.Errorf(codes.Internal, "failed to marshal setting for %s with error: %v", apiSettingName, err)
}
storeSettingValue = string(bytes)
+ case api.SettingEnvironment:
+ if serr := validateEnvironments(request.Setting.Value.GetEnvironmentSetting().GetEnvironments()); serr != nil {
+ return nil, serr.Err()
+ }
+ environmentSetting := &storepb.EnvironmentSetting{}
+ if err := convertProtoToProto(request.Setting.Value.GetEnvironmentSetting(), environmentSetting); err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to unmarshal setting value for %s with error: %v", apiSettingName, err)
+ }
+
+ oldEnvironmentSetting, err := s.store.GetEnvironmentSetting(ctx)
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to get old environment setting with error: %v", err)
+ }
+ newEnvIDMap := map[string]bool{}
+ for _, env := range environmentSetting.Environments {
+ newEnvIDMap[env.Id] = true
+ }
+ for _, env := range oldEnvironmentSetting.Environments {
+ if !newEnvIDMap[env.Id] {
+ // deleted
+ // check if instances are using the environments
+ count, err := s.store.CountInstance(ctx, &store.CountInstanceMessage{EnvironmentID: &env.Id})
+ if err != nil {
+ return nil, status.Error(codes.Internal, err.Error())
+ }
+ if count > 0 {
+ return nil, status.Errorf(codes.FailedPrecondition, "all instances in the environment %v should be deleted first", env.Id)
+ }
+ }
+ }
+
+ bytes, err := protojson.Marshal(environmentSetting)
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to marshal setting for %s with error: %v", apiSettingName, err)
+ }
+ storeSettingValue = string(bytes)
default:
storeSettingValue = request.Setting.Value.GetStringValue()
}
@@ -850,6 +887,19 @@ func (s *SettingService) convertToSettingMessage(ctx context.Context, setting *s
},
},
}, nil
+ case api.SettingEnvironment:
+ v1Value := new(v1pb.EnvironmentSetting)
+ if err := common.ProtojsonUnmarshaler.Unmarshal([]byte(setting.Value), v1Value); err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to unmarshal setting value for %s with error: %v", setting.Name, err)
+ }
+ return &v1pb.Setting{
+ Name: settingName,
+ Value: &v1pb.Value{
+ Value: &v1pb.Value_EnvironmentSetting{
+ EnvironmentSetting: v1Value,
+ },
+ },
+ }, nil
default:
return &v1pb.Setting{
Name: settingName,
@@ -1279,3 +1329,20 @@ func validateDomains(domains []string) error {
}
return nil
}
+
+func validateEnvironments(envs []*v1pb.EnvironmentSetting_Environment) *status.Status {
+ used := map[string]bool{}
+ for _, env := range envs {
+ if err := api.IsValidEnvironmentName(env.Title); err != nil {
+ return status.Newf(codes.InvalidArgument, "invalid environment title, error %v", err.Error())
+ }
+ if !isValidResourceID(env.Id) {
+ return status.Newf(codes.InvalidArgument, "invalid environment ID %v", env.Id)
+ }
+ if used[env.Id] {
+ return status.Newf(codes.InvalidArgument, "duplicate environment ID %v", env.Id)
+ }
+ used[env.Id] = true
+ }
+ return nil
+}
diff --git a/backend/legacyapi/setting.go b/backend/legacyapi/setting.go
index c8ef3311ce..0f0d559b1f 100644
--- a/backend/legacyapi/setting.go
+++ b/backend/legacyapi/setting.go
@@ -41,4 +41,6 @@ const (
SettingSCIM SettingName = "bb.workspace.scim"
// SettingPasswordRestriction is the setting name for password.
SettingPasswordRestriction SettingName = "bb.workspace.password-restriction"
+ // SettingEnvironment is the setting name for environments.
+ SettingEnvironment SettingName = "bb.workspace.environment"
)
diff --git a/backend/server/init.go b/backend/server/init.go
index 11f3389af2..fb4e6e1ff4 100644
--- a/backend/server/init.go
+++ b/backend/server/init.go
@@ -195,6 +195,29 @@ func (s *Server) getInitSetting(ctx context.Context) error {
return err
}
+ // Init workspace environment setting
+ environmentSettingValue, err := protojson.Marshal(&storepb.EnvironmentSetting{
+ Environments: []*storepb.EnvironmentSetting_Environment{
+ {
+ Title: "Test",
+ Id: "test",
+ },
+ {
+ Title: "Prod",
+ Id: "prod",
+ },
+ },
+ })
+ if err != nil {
+ return errors.Wrapf(err, "failed to marshal initial environment setting")
+ }
+ if _, _, err := s.store.CreateSettingIfNotExistV2(ctx, &store.SettingMessage{
+ Name: api.SettingEnvironment,
+ Value: string(environmentSettingValue),
+ }); err != nil {
+ return err
+ }
+
return nil
}
diff --git a/backend/store/setting.go b/backend/store/setting.go
index 8d075dcdb6..bc79cf10a8 100644
--- a/backend/store/setting.go
+++ b/backend/store/setting.go
@@ -197,6 +197,22 @@ func (s *Store) GetAISetting(ctx context.Context) (*storepb.AISetting, error) {
return aiSetting, nil
}
+func (s *Store) GetEnvironmentSetting(ctx context.Context) (*storepb.EnvironmentSetting, error) {
+ envSetting := &storepb.EnvironmentSetting{}
+ setting, err := s.GetSettingV2(ctx, api.SettingEnvironment)
+ if err != nil {
+ return nil, err
+ }
+ if setting == nil {
+ return envSetting, nil
+ }
+
+ if err := common.ProtojsonUnmarshaler.Unmarshal([]byte(setting.Value), envSetting); err != nil {
+ return nil, err
+ }
+ return envSetting, nil
+}
+
// DeleteCache deletes the cache.
func (s *Store) DeleteCache() {
s.settingCache.Purge()