-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathvalidator.go
121 lines (93 loc) · 2.79 KB
/
validator.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package retrieval
import (
"errors"
"fmt"
"strings"
"github.com/robfig/cron/v3"
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/config"
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/logical"
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/physical"
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/snapshot"
)
// ValidateConfig validates retrieval configuration.
func ValidateConfig(cfg *config.Config) (*config.Config, error) {
retrievalCfg, err := formatJobsSpec(cfg)
if err != nil {
return nil, err
}
if err = validateRefreshTimetable(retrievalCfg); err != nil {
return nil, err
}
if err = validateStructure(retrievalCfg); err != nil {
return nil, err
}
return retrievalCfg, nil
}
// formatJobsSpec validates job list and enriches job specifications.
func formatJobsSpec(cfg *config.Config) (*config.Config, error) {
jobSpecs := make(map[string]config.JobSpec, len(cfg.Jobs))
undefinedJobs := []string{}
for _, jobName := range cfg.Jobs {
jobSpec, ok := cfg.JobsSpec[jobName]
if !ok {
undefinedJobs = append(undefinedJobs, jobName)
continue
}
jobSpec.Name = jobName
jobSpecs[jobName] = jobSpec
}
if len(undefinedJobs) > 0 {
return nil, fmt.Errorf("config contains jobs without specification: %s", strings.Join(undefinedJobs, ", "))
}
jobsCfg := &config.Config{
Refresh: cfg.Refresh,
Jobs: cfg.Jobs,
JobsSpec: jobSpecs,
}
return jobsCfg, nil
}
// validateStructure checks if the retrieval configuration is valid.
func validateStructure(r *config.Config) error {
if hasLogicalJob(r.JobsSpec) && hasPhysicalJob(r.JobsSpec) {
return errors.New("must not contain physical and logical jobs simultaneously")
}
return nil
}
func validateRefreshTimetable(r *config.Config) error {
if r.Refresh == nil || r.Refresh.Timetable == "" {
return nil
}
specParser := cron.NewParser(parseOption)
_, err := specParser.Parse(r.Refresh.Timetable)
if err != nil {
return fmt.Errorf("invalid timetable: %w", err)
}
return nil
}
func hasLogicalJob(jobSpecs map[string]config.JobSpec) bool {
if len(jobSpecs) == 0 {
return false
}
if _, hasLogicalDump := jobSpecs[logical.DumpJobType]; hasLogicalDump {
return true
}
if _, hasLogicalRestore := jobSpecs[logical.RestoreJobType]; hasLogicalRestore {
return true
}
if _, hasLogicalSnapshot := jobSpecs[snapshot.LogicalSnapshotType]; hasLogicalSnapshot {
return true
}
return false
}
func hasPhysicalJob(jobSpecs map[string]config.JobSpec) bool {
if len(jobSpecs) == 0 {
return false
}
if _, hasPhysicalRestore := jobSpecs[physical.RestoreJobType]; hasPhysicalRestore {
return true
}
if _, hasPhysicalSnapshot := jobSpecs[snapshot.PhysicalSnapshotType]; hasPhysicalSnapshot {
return true
}
return false
}