-
Notifications
You must be signed in to change notification settings - Fork 20
/
dbaas.go
155 lines (129 loc) · 3.71 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package cmd
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"strconv"
"strings"
"github.com/exoscale/cli/table"
"github.com/mitchellh/go-wordwrap"
"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
}
// dbaasShowSettings outputs a table-formatted list of key/value settings.
func dbaasShowSettings(settings map[string]interface{}) {
t := table.NewTable(os.Stdout)
defer t.Render()
t.SetHeader([]string{"key", "type", "description"})
for k, v := range settings {
s, ok := v.(map[string]interface{})
if !ok {
continue
}
row := []string{k}
typ := "-"
if v, ok := s["type"]; ok {
typ = fmt.Sprint(v)
}
row = append(row, typ)
var description string
if v, ok := s["description"]; ok {
description = wordwrap.WrapString(v.(string), 50)
if v, ok := s["enum"]; ok {
description = fmt.Sprintf("%s\n * Supported values:\n%s", description, func() string {
values := make([]string, len(v.([]interface{})))
for i, val := range v.([]interface{}) {
values[i] = fmt.Sprintf(" - %v", val)
}
return strings.Join(values, "\n")
}())
}
min, hasMin := s["minimum"]
max, hasMax := s["maximum"]
if hasMin && hasMax {
description = fmt.Sprintf("%s\n * Minimum: %v / Maximum: %v", description, min, max)
}
if v, ok := s["default"]; ok {
description = fmt.Sprintf("%s\n * Default: %v", description, v)
}
if v, ok := s["example"]; ok {
description = fmt.Sprintf("%s\n * Example: %v", description, v)
}
}
row = append(row, description)
t.Append(row)
}
}