-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathaccount.go
177 lines (158 loc) · 5.35 KB
/
account.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
// This file is part of gobizfly
package gobizfly
import (
"context"
"encoding/json"
"net/http"
"strings"
)
const (
regionsPath = "/regions"
usersInfoPath = "/user"
)
type accountService struct {
client *Client
}
var _ AccountService = (*accountService)(nil)
type AccountService interface {
ListRegion(ctx context.Context) (*Regions, error)
GetRegion(ctx context.Context, regionName string) (*Region, error)
GetUserInfo(ctx context.Context) (*User, error)
}
type Region struct {
Active bool `json:"active"`
Icon string `json:"icon"`
Name string `json:"name"`
Order int `json:"order"`
RegionName string `json:"region_name"`
ShortName string `json:"short_name"`
Zones []AvailabilityZone `json:"zones"`
}
type AvailabilityZone struct {
Active bool `json:"active"`
Icon string `json:"icon"`
Name string `json:"name"`
Order int `json:"order"`
ShortName string `json:"short_name"`
}
type Regions struct {
HN Region `json:"HN"`
HCM Region `json:"HCM"`
}
type UserRegion struct {
Name string `json:"name"`
Code string `json:"code"`
ShortName string `json:"short_name"`
}
type User struct {
Service string `json:"service"`
URLType string `json:"url_type"`
Origin string `json:"origin"`
ClientType string `json:"client_type"`
BillingBalance int `json:"billing_balance"`
Balances map[string]float32 `json:"balances"`
PaymentMethod string `json:"payment_method"`
BillingAccID string `json:"billing_acc_id"`
Debit bool `json:"debit"`
Email string `json:"email"`
Phone string `json:"phone"`
FullName string `json:"full_name"`
VerifiedEmail bool `json:"verified_email"`
VerifiedPhone bool `json:"verified_phone"`
LoginAlert bool `json:"login_alert"`
VerifiedPayment bool `json:"verified_payment"`
LastRegion string `json:"last_region"`
LastProject string `json:"last_project"`
Type string `json:"type"`
OTP bool `json:"otp"`
Services []Service `json:"services"`
Whitelist []string `json:"whitelist"`
Expires string `json:"expires"`
TenantID string `json:"tenant_id"`
TenantName string `json:"tenant_name"`
KsUserID string `json:"ks_user_id"`
IAM IAM `json:"iam"`
Domains []string `json:"domains"`
PaymentType string `json:"payment_type"`
DOB string `json:"dob"`
Gender string `json:"_gender"`
Trial Trial `json:"trial"`
HasExpiredInvoice bool `json:"has_expired_invoice"`
NegativeBalance bool `json:"negative_balance"`
Promotion []string `json:"promotion"`
UserRegions []UserRegion `json:"user_regions"`
}
type IAM struct {
Expire string `json:"expire"`
TenantID string `json:"tenant_id"`
TenantName string `json:"tenant_name"`
VerifiedPhone bool `json:"verified_phone"`
VerifiedEmail bool `json:"verified_email"`
VerifiedPayment bool `json:"verified_payment"`
}
type Trial struct {
StartedAt string `json:"started_at"`
ExpiredAt string `json:"expired_at"`
Active bool `json:"active"`
Enable bool `json:"enable"`
ServiceLevel int `json:"service_level"`
}
func (a accountService) resourceRegionPath() string {
return regionsPath
}
func (a accountService) resourceUserInfo() string {
return usersInfoPath
}
func (a accountService) itemPath(name string) string {
return strings.Join([]string{regionsPath, name}, "/")
}
func (a accountService) ListRegion(ctx context.Context) (*Regions, error) {
req, err := a.client.NewRequest(ctx, http.MethodGet, accountName, a.resourceRegionPath(), nil)
if err != nil {
return nil, err
}
var regions *Regions
resp, err := a.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(®ions); err != nil {
return nil, err
}
return regions, nil
}
func (a accountService) GetRegion(ctx context.Context, regionName string) (*Region, error) {
req, err := a.client.NewRequest(ctx, http.MethodGet, accountName, a.itemPath(regionName), nil)
if err != nil {
return nil, err
}
var region *Region
resp, err := a.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(®ion); err != nil {
return nil, err
}
return region, nil
}
func (a accountService) GetUserInfo(ctx context.Context) (*User, error) {
req, err := a.client.NewRequest(ctx, http.MethodGet, accountName, a.resourceUserInfo(), nil)
if err != nil {
return nil, err
}
resp, err := a.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data struct {
User *User `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data.User, nil
}