-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk.go
226 lines (185 loc) · 6.02 KB
/
disk.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package api
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/moonrhythm/validator"
)
type Disk interface {
Create(ctx context.Context, m *DiskCreate) (*Empty, error)
Get(ctx context.Context, m *DiskGet) (*DiskItem, error)
List(ctx context.Context, m *DiskList) (*DiskListResult, error)
Update(ctx context.Context, m *DiskUpdate) (*Empty, error)
Delete(ctx context.Context, m *DiskDelete) (*Empty, error)
Metrics(ctx context.Context, m *DiskMetrics) (*DiskMetricsResult, error)
}
type DiskCreate struct {
Location string `json:"location" yaml:"location"`
Project string `json:"project" yaml:"project"`
Name string `json:"name" yaml:"name"`
Size int64 `json:"size" yaml:"size"`
}
func (m *DiskCreate) Valid() error {
m.Name = strings.TrimSpace(m.Name)
v := validator.New()
v.Must(m.Location != "", "location required")
v.Must(m.Project != "", "project required")
v.Must(ReValidName.MatchString(m.Name), "name invalid "+ReValidNameStr)
{
cnt := utf8.RuneCountInString(m.Name)
v.Mustf(cnt >= MinNameLength && cnt <= MaxNameLength, "name must have length between %d-%d characters", MinNameLength, MaxNameLength)
}
v.Must(m.Size >= 1, "minimum disk size 1 Gi")
v.Mustf(m.Size <= DiskMaxSize, "maximum disk size %d Gi", DiskMaxSize)
return WrapValidate(v)
}
type DiskUpdate struct {
Location string `json:"location" yaml:"location"`
Project string `json:"project" yaml:"project"`
Name string `json:"name" yaml:"name"`
Size int64 `json:"size" yaml:"size"`
}
func (m *DiskUpdate) Valid() error {
m.Name = strings.TrimSpace(m.Name)
v := validator.New()
v.Must(m.Location != "", "location required")
v.Must(m.Project != "", "project required")
v.Must(ReValidName.MatchString(m.Name), "name invalid "+ReValidNameStr)
{
cnt := utf8.RuneCountInString(m.Name)
v.Mustf(cnt >= MinNameLength && cnt <= MaxNameLength, "name must have length between %d-%d characters", MinNameLength, MaxNameLength)
}
v.Must(m.Size >= 1, "minimum disk size 1 Gi")
v.Mustf(m.Size <= DiskMaxSize, "maximum disk size %d Gi", DiskMaxSize)
return WrapValidate(v)
}
type DiskGet struct {
Location string `json:"location" yaml:"location"`
Project string `json:"project" yaml:"project"`
Name string `json:"name" yaml:"name"`
}
func (m *DiskGet) Valid() error {
v := validator.New()
v.Must(m.Location != "", "location required")
v.Must(m.Project != "", "project required")
v.Must(m.Name != "", "name required")
return WrapValidate(v)
}
type DiskList struct {
Location string `json:"location" yaml:"location"` // optional
Project string `json:"project" yaml:"project"`
}
func (m *DiskList) Valid() error {
v := validator.New()
v.Must(m.Project != "", "project required")
return WrapValidate(v)
}
type DiskListResult struct {
Items []*DiskItem `json:"items" yaml:"items"`
}
func (m *DiskListResult) Table() [][]string {
table := [][]string{
{"NAME", "SIZE", "LOCATION", "AGE"},
}
for _, x := range m.Items {
table = append(table, []string{
x.Name,
strconv.FormatInt(x.Size, 10) + "Gi",
x.Location,
age(x.CreatedAt),
})
}
return table
}
type DiskItem struct {
Project string `json:"project" yaml:"project"`
Location string `json:"location" yaml:"location"`
Name string `json:"name" yaml:"name"`
Size int64 `json:"size" yaml:"size"`
Status Status `json:"status" yaml:"status"`
Action string `json:"action" yaml:"action"`
CreatedAt time.Time `json:"createdAt" yaml:"createdAt"`
CreatedBy string `json:"createdBy" yaml:"createdBy"`
SuccessAt time.Time `json:"successAt" yaml:"successAt"`
}
func (m *DiskItem) Table() [][]string {
table := [][]string{
{"NAME", "SIZE", "LOCATION", "AGE"},
{
m.Name,
strconv.FormatInt(m.Size, 10) + "Gi",
m.Location,
age(m.CreatedAt),
},
}
return table
}
type DiskDelete struct {
Location string `json:"location" yaml:"location"`
Project string `json:"project" yaml:"project"`
Name string `json:"name" yaml:"name"`
}
func (m *DiskDelete) Valid() error {
m.Name = strings.TrimSpace(m.Name)
v := validator.New()
v.Must(m.Location != "", "location required")
v.Must(m.Project != "", "project required")
v.Must(ReValidName.MatchString(m.Name), "name invalid "+ReValidNameStr)
if cnt := utf8.RuneCountInString(m.Name); cnt > MaxNameLength {
return fmt.Errorf("name invalid")
}
return WrapValidate(v)
}
type DiskMetricsTimeRange string
const (
DiskMetricsTimeRange1h = "1h"
DiskMetricsTimeRange6h = "6h"
DiskMetricsTimeRange12h = "12h"
DiskMetricsTimeRange1d = "1d"
DiskMetricsTimeRange2d = "2d"
DiskMetricsTimeRange7d = "7d"
DiskMetricsTimeRange30d = "30d"
)
var allDiskMetricsTimeRange = []DiskMetricsTimeRange{
DiskMetricsTimeRange1h,
DiskMetricsTimeRange6h,
DiskMetricsTimeRange12h,
DiskMetricsTimeRange1d,
DiskMetricsTimeRange2d,
DiskMetricsTimeRange7d,
DiskMetricsTimeRange30d,
}
var validDiskMetricsTimeRange = func() map[DiskMetricsTimeRange]bool {
m := map[DiskMetricsTimeRange]bool{}
for _, t := range allDiskMetricsTimeRange {
m[t] = true
}
return m
}()
type DiskMetrics struct {
Location string `json:"location" yaml:"location"`
Project string `json:"project" yaml:"project"`
Name string `json:"name" yaml:"name"`
TimeRange DiskMetricsTimeRange `json:"timeRange" yaml:"timeRange"`
}
func (m *DiskMetrics) Valid() error {
m.Name = strings.TrimSpace(m.Name)
v := validator.New()
v.Must(m.Location != "", "location required")
v.Must(ReValidName.MatchString(m.Name), "name invalid "+ReValidNameStr)
v.Mustf(utf8.RuneCountInString(m.Name) <= MaxNameLength, "name must have length less then %d characters", MaxNameLength)
v.Must(m.Project != "", "project required")
v.Must(validDiskMetricsTimeRange[m.TimeRange], "timeRange invalid")
return WrapValidate(v)
}
type DiskMetricsResult struct {
Usage []*DiskMetricsLine `json:"usage" yaml:"usage"`
Size []*DiskMetricsLine `json:"size" yaml:"size"`
}
type DiskMetricsLine struct {
Name string `json:"name" yaml:"name"`
Points [][2]float64 `json:"points" yaml:"points"`
}