-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbilling.go
174 lines (141 loc) · 5.42 KB
/
billing.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
package api
import (
"context"
"strings"
"unicode/utf8"
"github.com/moonrhythm/validator"
)
type Billing interface {
Create(ctx context.Context, m *BillingCreate) (*BillingCreateResult, error)
List(ctx context.Context, m *Empty) (*BillingListResult, error)
Delete(ctx context.Context, m *BillingDelete) (*Empty, error)
Get(ctx context.Context, m *BillingGet) (*BillingItem, error)
Update(ctx context.Context, m *BillingUpdate) (*Empty, error)
Report(ctx context.Context, m *BillingReport) (*BillingReportResult, error)
SKUs(ctx context.Context, m *Empty) (*BillingSKUs, error)
Project(ctx context.Context, m *BillingProject) (*BillingProjectResult, error)
}
type BillingCreate struct {
Name string `json:"name" yaml:"name"`
TaxID string `json:"taxId" yaml:"taxId"`
TaxName string `json:"taxName" yaml:"taxName"`
TaxAddress string `json:"taxAddress" yaml:"taxAddress"`
}
func (m *BillingCreate) Valid() error {
m.Name = strings.TrimSpace(m.Name)
m.TaxID = strings.TrimSpace(m.TaxID)
m.TaxName = strings.TrimSpace(m.TaxName)
m.TaxAddress = strings.TrimSpace(m.TaxAddress)
v := validator.New()
if ok := v.Must(m.Name != "", "name required"); ok {
cnt := utf8.RuneCountInString(m.Name)
v.Mustf(cnt >= MinNameLength && cnt <= MaxNameLength, "name must have length between %d-%d characters", MinNameLength, MaxNameLength)
}
v.Must(m.TaxID != "", "tax id required")
v.Must(utf8.RuneCountInString(m.TaxID) < 100, "tax id too long")
v.Must(m.TaxName != "", "tax name required")
v.Must(utf8.RuneCountInString(m.TaxName) < 200, "tax name too long")
v.Must(m.TaxAddress != "", "tax address required")
v.Must(utf8.RuneCountInString(m.TaxAddress) < 500, "tax address too long")
return WrapValidate(v)
}
type BillingCreateResult struct {
ID int64 `json:"id,string" yaml:"id"`
}
type BillingListResult struct {
Items []*BillingItem `json:"items" yaml:"items"`
}
type BillingDelete struct {
ID int64 `json:"id,string" yaml:"id"`
}
func (m *BillingDelete) Valid() error {
v := validator.New()
v.Must(m.ID > 0, "id required")
return WrapValidate(v)
}
type BillingGet struct {
ID int64 `json:"id,string" yaml:"id"`
}
func (m *BillingGet) Valid() error {
v := validator.New()
v.Must(m.ID > 0, "id required")
return WrapValidate(v)
}
type BillingItem struct {
ID int64 `json:"id,string" yaml:"id"`
Name string `json:"name" yaml:"name"`
TaxID string `json:"taxId" yaml:"taxId"`
TaxName string `json:"taxName" yaml:"taxName"`
TaxAddress string `json:"taxAddress" yaml:"taxAddress"`
Active bool `json:"active" yaml:"active"`
}
type BillingUpdate struct {
ID int64 `json:"id,string" yaml:"id"`
Name string `json:"name" yaml:"name"`
TaxID string `json:"taxId" yaml:"taxId"`
TaxName string `json:"taxName" yaml:"taxName"`
TaxAddress string `json:"taxAddress" yaml:"taxAddress"`
}
func (m *BillingUpdate) Valid() error {
m.Name = strings.TrimSpace(m.Name)
m.TaxID = strings.TrimSpace(m.TaxID)
m.TaxName = strings.TrimSpace(m.TaxName)
m.TaxAddress = strings.TrimSpace(m.TaxAddress)
v := validator.New()
if ok := v.Must(m.Name != "", "name required"); ok {
cnt := utf8.RuneCountInString(m.Name)
v.Mustf(cnt >= MinNameLength && cnt <= MaxNameLength, "name must have length between %d-%d characters", MinNameLength, MaxNameLength)
}
v.Must(m.TaxID != "", "tax id required")
v.Must(utf8.RuneCountInString(m.TaxID) < 100, "tax id too long")
v.Must(m.TaxName != "", "tax name required")
v.Must(utf8.RuneCountInString(m.TaxName) < 200, "tax name too long")
v.Must(m.TaxAddress != "", "tax address required")
v.Must(utf8.RuneCountInString(m.TaxAddress) < 500, "tax address too long")
return WrapValidate(v)
}
type BillingReport struct {
ID int64 `json:"id,string" yaml:"id"`
Range string `json:"range" yaml:"range"`
ProjectSIDs []string `json:"projectSids" yaml:"projectSids"`
}
type BillingReportListItem struct {
ProjectSID string `json:"projectSid" yaml:"projectSid"`
Name string `json:"name" yaml:"name"`
UsageValue float64 `json:"usageValue" yaml:"usageValue"`
BillingValue float64 `json:"billingValue" yaml:"billingValue"`
}
type BillingReportChartSeries struct {
Name string `json:"name" yaml:"name"`
Data []float64 `json:"data" yaml:"data"`
}
type BillingReportChart struct {
Categories []string `json:"categories" yaml:"categories"`
Series []*BillingReportChartSeries `json:"series" yaml:"series"`
}
type ReportProjectListItem struct {
SID string `json:"sid" yaml:"sid"`
Name string `json:"name" yaml:"name"`
}
type BillingReportResult struct {
Range string `json:"range" yaml:"range"`
List []*BillingReportListItem `json:"list" yaml:"list"`
Chart *BillingReportChart `json:"chart" yaml:"chart"`
ProjectList []*ReportProjectListItem `json:"projectList" yaml:"projectList"`
ProjectSIDs []string `json:"projectSids" yaml:"projectSids"`
}
type BillingSKUs 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"`
DomainCDN float64 `json:"domainCdn" yaml:"domainCdn"`
}
type BillingProject struct {
Project string `json:"project" yaml:"project"`
}
type BillingProjectResult struct {
Price float64 `json:"price" yaml:"price"`
}