/
handlers.go
69 lines (60 loc) · 2.19 KB
/
handlers.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
package handlers
import (
"fmt"
"time"
"github.com/gorilla/mux"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/common/orchestration"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/process"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/storage"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type Handler interface {
AttachRoutes(router *mux.Router)
}
type handler struct {
handlers []Handler
}
func NewOrchestrationHandler(db storage.BrokerStorage, kymaQueue *process.Queue, clusterQueue *process.Queue, defaultMaxPage int, log logrus.FieldLogger) Handler {
return &handler{
handlers: []Handler{
NewKymaHandler(db.Orchestrations(), kymaQueue, log),
NewClusterHandler(db.Orchestrations(), clusterQueue, log),
NewOrchestrationStatusHandler(db.Operations(), db.Orchestrations(), db.RuntimeStates(), kymaQueue, clusterQueue, defaultMaxPage, log),
},
}
}
func (h *handler) AttachRoutes(router *mux.Router) {
for _, handler := range h.handlers {
handler.AttachRoutes(router)
}
}
func validateTarget(spec orchestration.TargetSpec) error {
if spec.Include == nil || len(spec.Include) == 0 {
return errors.New("targets.include array must be not empty")
}
return nil
}
// ValidateDeprecatedParameters cheks if `maintenanceWindow` parameter is used as schedule.
func ValidateDeprecatedParameters(params orchestration.Parameters) error {
if params.Strategy.Schedule == string(orchestration.MaintenanceWindow) {
return fmt.Errorf("{\"strategy\":{\"schedule\": \"maintenanceWindow\"} is deprecated use {\"strategy\":{\"MaintenanceWindow\": true} instead")
}
return nil
}
// ValidateScheduleParameter cheks if the schedule parameter is valid.
func ValidateScheduleParameter(params *orchestration.Parameters) error {
switch params.Strategy.Schedule {
case "immediate":
case "now":
params.Strategy.ScheduleTime = time.Now()
default:
parsedTime, err := time.Parse(time.RFC3339, params.Strategy.Schedule)
if err == nil {
params.Strategy.ScheduleTime = parsedTime
} else {
return fmt.Errorf("the schedule filed does not contain 'imediate'/'now' nor is a date: %w", err)
}
}
return nil
}