-
Notifications
You must be signed in to change notification settings - Fork 20
/
dbaas.go
98 lines (82 loc) · 2.31 KB
/
dbaas.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
package cmd
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/xeipuuv/gojsonschema"
)
var dbServiceMaintenanceDOWs = []string{
"never",
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
"sunday",
}
var dbaasCmd = &cobra.Command{
Use: "dbaas",
Short: "Database as a Service management",
}
func init() {
RootCmd.AddCommand(dbaasCmd)
}
// parseDtabaseBackupSchedule parses a Database Service backup schedule value
// expressed in HH:MM format and returns the discrete values for hour and
// minute, or an error if the parsing failed.
func parseDatabaseBackupSchedule(v string) (int64, int64, error) {
parts := strings.Split(v, ":")
if len(parts) != 2 {
return 0, 0, fmt.Errorf("invalid value %q for backup schedule, expecting HH:MM", v)
}
backupHour, err := strconv.Atoi(parts[0])
if err != nil {
return 0, 0, fmt.Errorf("invalid value %q for backup schedule hour, must be between 0 and 23", v)
}
backupMinute, err := strconv.Atoi(parts[1])
if err != nil {
return 0, 0, fmt.Errorf("invalid value %q for backup schedule minute, must be between 0 and 59", v)
}
return int64(backupHour), int64(backupMinute), nil
}
// validateDatabaseServiceSettings validates user-provided JSON-formatted
// Database Service settings against a reference JSON Schema.
func validateDatabaseServiceSettings(in string, schema interface{}) (map[string]interface{}, error) {
var userSettings map[string]interface{}
if err := json.Unmarshal([]byte(in), &userSettings); err != nil {
return nil, fmt.Errorf("unable to unmarshal JSON: %w", err)
}
res, err := gojsonschema.Validate(
gojsonschema.NewGoLoader(schema),
gojsonschema.NewStringLoader(in),
)
if err != nil {
return nil, fmt.Errorf("unable to validate JSON Schema: %w", err)
}
if !res.Valid() {
return nil, errors.New(strings.Join(
func() []string {
errs := make([]string, len(res.Errors()))
for i, err := range res.Errors() {
errs[i] = err.String()
}
return errs
}(),
"\n",
))
}
return userSettings, nil
}
// redactDatabaseServiceURI returns a redacted version of the URI provided
// (i.e. masks potential password information).
func redactDatabaseServiceURI(u string) string {
if uri, err := url.Parse(u); err == nil {
return uri.Redacted()
}
return u
}