-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.go
162 lines (133 loc) · 4.36 KB
/
project.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
package api
import (
"context"
"regexp"
"strings"
"time"
"unicode/utf8"
"github.com/dustin/go-humanize"
"github.com/moonrhythm/validator"
)
type Project interface {
Create(ctx context.Context, m *ProjectCreate) (*Empty, error)
Get(ctx context.Context, m *ProjectGet) (*ProjectItem, error)
List(ctx context.Context, m *Empty) (*ProjectListResult, error)
Update(ctx context.Context, m *ProjectUpdate) (*Empty, error)
Delete(ctx context.Context, m *ProjectDelete) (*Empty, error)
Usage(ctx context.Context, m *ProjectUsage) (*ProjectUsageResult, error)
}
type ProjectCreate struct {
SID string `json:"sid" yaml:"sid"`
Name string `json:"name" yaml:"name"`
BillingAccount int64 `json:"billingAccount,string" yaml:"billingAccount"`
}
var (
ReValidSIDStr = `^[a-z][a-z0-9\-]*[^\-]$`
ReValidSID = regexp.MustCompile(ReValidSIDStr)
)
func (m *ProjectCreate) Valid() error {
m.SID = strings.TrimSpace(m.SID)
m.Name = strings.TrimSpace(m.Name)
v := validator.New()
if v.Must(m.SID != "", "sid required") {
v.Mustf(ReValidSID.MatchString(m.SID), "sid invalid %s", ReValidSIDStr)
cnt := utf8.RuneCountInString(m.SID)
v.Must(cnt >= 6 && cnt <= 32, "sid must have length between 6-32 characters")
}
v.Must(utf8.ValidString(m.Name), "name invalid")
cnt := utf8.RuneCountInString(m.Name)
v.Must(cnt >= 4 && cnt <= 64, "name must have length between 4-64 characters")
v.Must(m.BillingAccount > 0, "billing account required")
return WrapValidate(v)
}
type ProjectUpdate struct {
Project string `json:"project" yaml:"project"`
Name *string `json:"name" yaml:"name"`
BillingAccount *int64 `json:"billingAccount,string" yaml:"billingAccount"`
}
func (m *ProjectUpdate) Valid() error {
m.Project = strings.TrimSpace(m.Project)
v := validator.New()
v.Must(m.Project != "", "project required")
if m.Name != nil {
*m.Name = strings.TrimSpace(*m.Name)
v.Must(utf8.ValidString(*m.Name), "name invalid")
cnt := utf8.RuneCountInString(*m.Name)
v.Must(cnt >= 4 && cnt <= 64, "name must have length between 4-64 characters")
}
if m.BillingAccount != nil {
v.Must(*m.BillingAccount > 0, "billing account invalid")
}
return WrapValidate(v)
}
type ProjectGet struct {
Project string `json:"project" yaml:"project"`
}
type ProjectItem struct {
ID int64 `json:"id,string" yaml:"id"`
Project string `json:"project" yaml:"project"`
Name string `json:"name" yaml:"name"`
BillingAccount int64 `json:"billingAccount,string" yaml:"billingAccount"`
Quota ProjectQuota `json:"quota" yaml:"quota"`
Config ProjectConfig `json:"config" yaml:"config"`
CreatedAt time.Time `json:"createdAt" yaml:"createdAt"`
}
func (m *ProjectItem) Table() [][]string {
return [][]string{
{"PROJECT", "NAME", "AGE"},
{
m.Project,
m.Name,
age(m.CreatedAt),
},
}
}
type ProjectQuota struct {
Deployments int `json:"deployments" yaml:"deployments"`
DeploymentMaxReplicas int `json:"deploymentMaxReplicas" yaml:"deploymentMaxReplicas"`
}
type ProjectConfig struct {
DomainAllowDisableCDN bool `json:"domainAllowDisableCdn" yaml:"domainAllowDisableCDN"`
}
type ProjectListResult struct {
Items []*ProjectItem `json:"items" yaml:"items"`
}
func (m *ProjectListResult) Table() [][]string {
table := [][]string{
{"PROJECT", "NAME", "AGE"},
}
for _, x := range m.Items {
table = append(table, []string{
x.Project,
x.Name,
age(x.CreatedAt),
})
}
return table
}
type ProjectDelete struct {
Project string `json:"project" yaml:"project"`
}
type ProjectUsage struct {
Project string `json:"project" yaml:"project"`
}
type ProjectUsageResult struct {
CPUUsage float64 `json:"cpuUsage" yaml:"cpuUsage"`
CPU float64 `json:"cpu" yaml:"cpu"`
Memory float64 `json:"memory" yaml:"memory"`
Egress float64 `json:"egress" yaml:"egress"`
Disk float64 `json:"disk" yaml:"disk"`
Replica float64 `json:"replica" yaml:"replica"`
}
func (m *ProjectUsageResult) Table() [][]string {
table := [][]string{
{"RESOURCE", "USAGE"},
{"CPUUsage", humanize.CommafWithDigits(m.CPUUsage, 2)},
{"CPU", humanize.CommafWithDigits(m.CPU, 2)},
{"Memory", humanize.CommafWithDigits(m.Memory, 2)},
{"Egress", humanize.CommafWithDigits(m.Egress, 2)},
{"Disk", humanize.CommafWithDigits(m.Disk, 2)},
{"Replica", humanize.CommafWithDigits(m.Replica, 2)},
}
return table
}