diff --git a/internal/server/handlers/backup.go b/internal/server/handlers/backup.go index 0bd91e2a..5f17e242 100644 --- a/internal/server/handlers/backup.go +++ b/internal/server/handlers/backup.go @@ -103,7 +103,7 @@ func (s *Service) readAllBackups(w http.ResponseWriter, r *http.Request, isFullB return } - response, err := dto.Serialize(dto.ConvertBackupDetailsMap(backups, s.config.GetBackupConfig()), dto.JSON) + response, err := dto.Serialize(dto.ConvertBackupDetailsMap(backups, s.config.BackupConfigCopy()), dto.JSON) if err != nil { hLogger.Error("failed to marshal backup list", slog.Any("error", err), @@ -169,7 +169,7 @@ func (s *Service) readBackupsForRoutine(w http.ResponseWriter, r *http.Request, http.Error(w, "failed to retrieve backup list: "+err.Error(), http.StatusInternalServerError) return } - backupConfig := s.config.GetBackupConfig() + backupConfig := s.config.BackupConfigCopy() backupDetails := dto.ConvertModelsToDTO(backups, func(m *model.BackupDetails) *dto.BackupDetails { return dto.NewBackupDetailsFromModel(m, backupConfig) }) diff --git a/internal/server/handlers/config.go b/internal/server/handlers/config.go index 2b18179f..9f251b44 100644 --- a/internal/server/handlers/config.go +++ b/internal/server/handlers/config.go @@ -88,7 +88,7 @@ func (s *Service) updateConfig(w http.ResponseWriter, r *http.Request) { } err = s.changeConfig(r.Context(), func(config *model.Config) error { - config.SetBackupConfig(newConfigModel.GetBackupConfig()) + config.SetBackupConfig(newConfigModel.BackupConfigCopy()) return nil }) @@ -135,7 +135,7 @@ func (s *Service) ApplyConfig(w http.ResponseWriter, r *http.Request) { return } - backupConfig := config.GetBackupConfig() + backupConfig := config.BackupConfigCopy() s.config.SetBackupConfig(backupConfig) err = s.configApplier.ApplyNewRoutines(r.Context(), backupConfig.BackupRoutines) diff --git a/internal/server/handlers/config_cluster.go b/internal/server/handlers/config_cluster.go index 16f9e95a..a9dc67b3 100644 --- a/internal/server/handlers/config_cluster.go +++ b/internal/server/handlers/config_cluster.go @@ -89,7 +89,7 @@ func (s *Service) addAerospikeCluster(w http.ResponseWriter, r *http.Request) { func (s *Service) ReadAerospikeClusters(w http.ResponseWriter, _ *http.Request) { hLogger := s.logger.With(slog.String("handler", "ReadAerospikeClusters")) - backupConfig := s.config.GetBackupConfig() + backupConfig := s.config.BackupConfigCopy() clusters := backupConfig.AerospikeClusters toDTO := dto.ConvertModelMapToDTO(clusters, func(m *model.AerospikeCluster) *dto.AerospikeCluster { return dto.NewClusterFromModel(m, backupConfig) @@ -134,7 +134,7 @@ func (s *Service) readAerospikeCluster(w http.ResponseWriter, r *http.Request) { http.Error(w, clusterNameNotSpecifiedMsg, http.StatusBadRequest) return } - backupConfig := s.config.GetBackupConfig() + backupConfig := s.config.BackupConfigCopy() cluster, ok := backupConfig.AerospikeClusters[clusterName] if !ok { hLogger.Error("cluster not found", @@ -197,7 +197,7 @@ func (s *Service) updateAerospikeCluster(w http.ResponseWriter, r *http.Request) return } - err = s.nsValidator.ValidateRoutines(cluster, s.config.GetBackupConfig().BackupRoutines) + err = s.nsValidator.ValidateRoutines(cluster, s.config.BackupConfigCopy().BackupRoutines) if err != nil { hLogger.Error("cluster namespace validation failed", slog.String("name", clusterName), diff --git a/internal/server/handlers/config_policy.go b/internal/server/handlers/config_policy.go index 8a11c031..b8a17264 100644 --- a/internal/server/handlers/config_policy.go +++ b/internal/server/handlers/config_policy.go @@ -81,7 +81,7 @@ func (s *Service) addPolicy(w http.ResponseWriter, r *http.Request) { func (s *Service) ReadPolicies(w http.ResponseWriter, _ *http.Request) { hLogger := s.logger.With(slog.String("handler", "ReadPolicies")) - policies := dto.ConvertModelMapToDTO(s.config.GetBackupConfig().BackupPolicies, dto.NewBackupPolicyFromModel) + policies := dto.ConvertModelMapToDTO(s.config.BackupConfigCopy().BackupPolicies, dto.NewBackupPolicyFromModel) jsonResponse, err := dto.Serialize(policies, dto.JSON) if err != nil { hLogger.Error("failed to marshal backup policies", @@ -121,7 +121,7 @@ func (s *Service) readPolicy(w http.ResponseWriter, r *http.Request) { http.Error(w, policyNameNotSpecifiedMsg, http.StatusBadRequest) return } - policy, ok := s.config.GetBackupConfig().BackupPolicies[policyName] + policy, ok := s.config.BackupConfigCopy().BackupPolicies[policyName] if !ok { hLogger.Error("policy not found") http.Error(w, fmt.Sprintf("policy %s could not be found", policyName), http.StatusNotFound) diff --git a/internal/server/handlers/config_routine.go b/internal/server/handlers/config_routine.go index 6574f67b..52722de3 100644 --- a/internal/server/handlers/config_routine.go +++ b/internal/server/handlers/config_routine.go @@ -55,7 +55,7 @@ func (s *Service) addRoutine(w http.ResponseWriter, r *http.Request) { http.Error(w, routineNameNotSpecifiedMsg, http.StatusBadRequest) return } - toModel, err := newRoutine.ToModel(s.config.GetBackupConfig(), s.nsValidator) + toModel, err := newRoutine.ToModel(s.config.BackupConfigCopy(), s.nsValidator) if err != nil { hLogger.Error("failed to create routine", slog.String("name", name), @@ -188,7 +188,7 @@ func (s *Service) updateRoutine(w http.ResponseWriter, r *http.Request) { return } - toModel, err := updatedRoutine.ToModel(s.config.GetBackupConfig(), s.nsValidator) + toModel, err := updatedRoutine.ToModel(s.config.BackupConfigCopy(), s.nsValidator) if err != nil { hLogger.Error("failed to create routine", slog.String("name", name), diff --git a/internal/server/handlers/config_storage.go b/internal/server/handlers/config_storage.go index 5e56aba5..a7ccb8df 100644 --- a/internal/server/handlers/config_storage.go +++ b/internal/server/handlers/config_storage.go @@ -82,7 +82,7 @@ func (s *Service) addStorage(w http.ResponseWriter, r *http.Request) { func (s *Service) ReadAllStorage(w http.ResponseWriter, _ *http.Request) { hLogger := s.logger.With(slog.String("handler", "ReadAllStorage")) - backupConfig := s.config.GetBackupConfig() + backupConfig := s.config.BackupConfigCopy() toDTO := dto.ConvertStorageMapToDTO(backupConfig.Storage, backupConfig) jsonResponse, err := dto.Serialize(toDTO, dto.JSON) if err != nil { @@ -123,7 +123,7 @@ func (s *Service) readStorage(w http.ResponseWriter, r *http.Request) { http.Error(w, storageNameNotSpecifiedMsg, http.StatusBadRequest) return } - backupConfig := s.config.GetBackupConfig() + backupConfig := s.config.BackupConfigCopy() storage, ok := backupConfig.Storage[storageName] if !ok { http.Error(w, fmt.Sprintf("Storage %s could not be found", storageName), http.StatusNotFound) diff --git a/pkg/dto/backup_routine.go b/pkg/dto/backup_routine.go index 73bde8d1..80e9099c 100644 --- a/pkg/dto/backup_routine.go +++ b/pkg/dto/backup_routine.go @@ -158,7 +158,7 @@ func NewRoutineFromModel(m *model.BackupRoutine, config *model.Config) *BackupRo } b := &BackupRoutine{} - b.fromModel(m, config.GetBackupConfig()) + b.fromModel(m, config.BackupConfigCopy()) return b } diff --git a/pkg/dto/config.go b/pkg/dto/config.go index d9ffdb97..570aaa85 100644 --- a/pkg/dto/config.go +++ b/pkg/dto/config.go @@ -33,7 +33,7 @@ func NewConfigFromModel(m *model.Config) *Config { func (c *Config) fromModel(m *model.Config) { c.ServiceConfig.fromModel(&m.ServiceConfig) - backupConfig := m.GetBackupConfig() + backupConfig := m.BackupConfigCopy() c.AerospikeClusters = make(map[string]*AerospikeCluster) for name, a := range backupConfig.AerospikeClusters { @@ -173,7 +173,7 @@ func (c *Config) ToModel(nsValidator aerospike.NamespaceValidator) (*model.Confi } } - backupConfig := modelConfig.GetBackupConfig() + backupConfig := modelConfig.BackupConfigCopy() // routines must be added after storage, secret agents and policies. for k, v := range c.BackupRoutines { toModel, err := v.ToModel(backupConfig, nsValidator) diff --git a/pkg/model/config.go b/pkg/model/config.go index 22b93d5f..78457d2a 100644 --- a/pkg/model/config.go +++ b/pkg/model/config.go @@ -74,7 +74,7 @@ var ( ErrInUse = fmt.Errorf("item is in use") ) -func (c *Config) GetBackupConfig() *BackupConfig { +func (c *Config) BackupConfigCopy() *BackupConfig { c.RLock() defer c.RUnlock() return c.backupConfig.copy()