This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 875
/
cluster_backup_strategy.go
93 lines (83 loc) · 2.86 KB
/
cluster_backup_strategy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package service
import (
"errors"
"github.com/KubeOperator/KubeOperator/pkg/db"
"github.com/KubeOperator/KubeOperator/pkg/dto"
"github.com/KubeOperator/KubeOperator/pkg/model"
"github.com/KubeOperator/KubeOperator/pkg/repository"
"github.com/jinzhu/gorm"
)
var (
UpdateError = "BACKUP_FILES_NOT_NULL"
)
type CLusterBackupStrategyService interface {
Get(clusterName string) (*dto.ClusterBackupStrategy, error)
Save(creation dto.ClusterBackupStrategyRequest) (*dto.ClusterBackupStrategy, error)
}
type cLusterBackupStrategyService struct {
clusterBackupStrategyRepo repository.ClusterBackupStrategyRepository
clusterRepo repository.ClusterRepository
backupAccountService BackupAccountService
clusterBackupFileRepository repository.ClusterBackupFileRepository
}
func NewCLusterBackupStrategyService() CLusterBackupStrategyService {
return &cLusterBackupStrategyService{
clusterBackupStrategyRepo: repository.NewClusterBackupStrategyRepository(),
clusterRepo: repository.NewClusterRepository(),
backupAccountService: NewBackupAccountService(),
clusterBackupFileRepository: repository.NewClusterBackupFileRepository(),
}
}
func (c cLusterBackupStrategyService) Get(clusterName string) (*dto.ClusterBackupStrategy, error) {
var clusterBackupStrategyDTO dto.ClusterBackupStrategy
mo, err := c.clusterBackupStrategyRepo.Get(clusterName)
if err != nil {
return nil, err
}
clusterBackupStrategyDTO = dto.ClusterBackupStrategy{
ClusterBackupStrategy: *mo,
BackupAccountName: mo.BackupAccount.Name,
ClusterName: clusterName,
}
return &clusterBackupStrategyDTO, nil
}
func (c cLusterBackupStrategyService) Save(creation dto.ClusterBackupStrategyRequest) (*dto.ClusterBackupStrategy, error) {
backupAccount, err := c.backupAccountService.Get(creation.BackupAccountName)
if err != nil {
return nil, err
}
cluster, err := c.clusterRepo.Get(creation.ClusterName)
if err != nil {
return nil, err
}
var id string
old, err := c.Get(creation.ClusterName)
if err != nil {
return nil, err
} else {
id = old.ID
if old.BackupAccountID != backupAccount.ID {
var backupFiles []model.ClusterBackupFile
err := db.DB.Where("cluster_backup_strategy_id = ? AND cluster_id = ?", id, cluster.ID).Find(&backupFiles).Error
if err != nil && !gorm.IsRecordNotFoundError(err) {
return nil, err
}
if len(backupFiles) > 0 {
return nil, errors.New(UpdateError)
}
}
}
clusterBackupStrategy := model.ClusterBackupStrategy{
ID: id,
ClusterID: cluster.ID,
Cron: creation.Cron,
Status: creation.Status,
BackupAccountID: backupAccount.ID,
SaveNum: creation.SaveNum,
}
err = c.clusterBackupStrategyRepo.Save(&clusterBackupStrategy)
if err != nil {
return nil, err
}
return &dto.ClusterBackupStrategy{ClusterBackupStrategy: clusterBackupStrategy}, err
}