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 879
/
cluster_backup_strategy.go
56 lines (49 loc) · 1.7 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
package repository
import (
"github.com/KubeOperator/KubeOperator/pkg/db"
"github.com/KubeOperator/KubeOperator/pkg/model"
"github.com/jinzhu/gorm"
)
type ClusterBackupStrategyRepository interface {
Get(clusterName string) (*model.ClusterBackupStrategy, error)
Save(clusterBackupStrategy *model.ClusterBackupStrategy) error
List() ([]model.ClusterBackupStrategy, error)
}
type clusterBackupStrategyRepository struct {
clusterRepository ClusterRepository
}
func NewClusterBackupStrategyRepository() ClusterBackupStrategyRepository {
return &clusterBackupStrategyRepository{
clusterRepository: NewClusterRepository(),
}
}
func (c clusterBackupStrategyRepository) Get(clusterName string) (*model.ClusterBackupStrategy, error) {
var clusterBackupStrategy model.ClusterBackupStrategy
cluster, err := c.clusterRepository.Get(clusterName)
if err != nil {
return nil, err
}
if err := db.DB.Where("cluster_id = ?", cluster.ID).Preload("BackupAccount").First(&clusterBackupStrategy).Error; err != nil {
if gorm.IsRecordNotFoundError(err) {
return &clusterBackupStrategy, nil
} else {
return nil, err
}
}
return &clusterBackupStrategy, nil
}
func (c clusterBackupStrategyRepository) Save(clusterBackupStrategy *model.ClusterBackupStrategy) error {
if db.DB.NewRecord(clusterBackupStrategy) {
return db.DB.Create(clusterBackupStrategy).Error
} else {
return db.DB.Save(&clusterBackupStrategy).Error
}
}
func (c clusterBackupStrategyRepository) List() ([]model.ClusterBackupStrategy, error) {
var clusterBackupStrategies []model.ClusterBackupStrategy
err := db.DB.Order("created_at desc").Find(&clusterBackupStrategies).Error
if err != nil {
return nil, err
}
return clusterBackupStrategies, err
}