-
Notifications
You must be signed in to change notification settings - Fork 1
/
account_calls.go
242 lines (210 loc) · 6.25 KB
/
account_calls.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package lib
import (
"encoding/json"
"github.com/BytemarkHosting/bytemark-client/lib/billing"
"github.com/BytemarkHosting/bytemark-client/lib/brain"
"github.com/BytemarkHosting/bytemark-client/util/log"
)
/*func (c *Client) RegisterAccount() {
}*/
// getBillingAccount gets the billing account with the given name.
// Due to the way the billing API is implemented this is done by grabbing them all and looping *shrug*
func (c *bytemarkClient) getBillingAccount(name string) (account *billing.Account, err error) {
accounts, err := c.getBillingAccounts()
if err != nil {
return
}
for _, account = range accounts {
if account.Name == name {
return
}
}
account = nil
return
}
// getBillingAccounts returns all the billing accounts the currently logged in user can see.
func (c *bytemarkClient) getBillingAccounts() (accounts []*billing.Account, err error) {
if c.urls.Billing == "" {
return make([]*billing.Account, 0), nil
}
req, err := c.BuildRequest("GET", BillingEndpoint, "/api/v1/accounts")
if err != nil {
return
}
oldfile := log.LogFile
log.LogFile = nil
_, _, err = req.Run(nil, &accounts)
log.LogFile = oldfile
return
}
// getBrainAccount gets the brain account with the given name.
func (c *bytemarkClient) getBrainAccount(name string) (account *brain.Account, err error) {
err = c.EnsureAccountName(&name)
if err != nil {
return
}
account = new(brain.Account)
req, err := c.BuildRequest("GET", BrainEndpoint, "/accounts/%s?view=overview&include_deleted=true", name)
if err != nil {
return nil, err
}
_, _, err = req.Run(nil, account)
return
}
/*
// CreateAccount creates a new account with you as the owner.
func (c *bytemarkClient) CreateAccount(account *Account) (newAccount *Account, err error) {
// TODO(telyn): implement
return nil, nil
}*/
// RegisterNewAccount registers a new account with bmbilling. This will create a new user for the owner.
// If you would like an extra account attached to your regular user, use CreateAccount
func (c *bytemarkClient) RegisterNewAccount(acc *Account) (newAcc *Account, err error) {
req, err := c.BuildRequestNoAuth("POST", BillingEndpoint, "/api/v1/accounts")
if err != nil {
return nil, err
}
// prevent password & card reference from being written to debug log
// this is a bit of a sledgehammer
// TODO make it not a sledgehammer somehow
oldfile := log.LogFile
log.LogFile = nil
outputBillingAcc := billing.Account{}
status, _, err := req.MarshalAndRun(acc.billingAccount(), &outputBillingAcc)
if err != nil {
if _, ok := err.(*json.InvalidUnmarshalError); !ok {
return newAcc, err
}
}
newAcc = &Account{}
newAcc.fillBilling(&outputBillingAcc)
log.LogFile = oldfile
if status == 202 {
return newAcc, AccountCreationDeferredError{}
}
return newAcc, err
}
// GetAccount takes an account name or ID and returns a filled-out Account object
func (c *bytemarkClient) GetAccount(name string) (account *Account, err error) {
if name == "" {
return c.GetDefaultAccount()
}
billingAccount, err := c.getBillingAccount(name)
if err != nil {
return nil, err
}
brainAccount, err := c.getBrainAccount(name)
if err != nil {
return nil, err
}
account = new(Account)
account.fillBrain(brainAccount)
account.fillBilling(billingAccount)
return
}
func (c *bytemarkClient) getBrainAccounts() (accounts []*brain.Account, err error) {
accounts = make([]*brain.Account, 1)
req, err := c.BuildRequest("GET", BrainEndpoint, "/accounts?view=overview&include_deleted=true")
if err != nil {
return
}
_, _, err = req.Run(nil, &accounts)
if err != nil {
return
}
return
}
// getDefaultBillingAccount gets the default billing account for this user.
// DANGER DANGER the returned account can be nil when there's also no error. (this means there's no billing accounts at all)
func (c *bytemarkClient) getDefaultBillingAccount() (*billing.Account, error) {
if c.urls.Billing == "https://int.bigv.io" {
return &billing.Account{Name: "bytemark"}, nil
}
billAccs, err := c.getBillingAccounts()
if err != nil {
return nil, err
}
if len(billAccs) == 0 {
return nil, nil
}
return billAccs[0], nil
}
// GetDefaultAccount gets the account *most likely* to be your default account.
// This is the first account returned by the billing endpoint,
// with the brain's data for it attached. Fingers crossed.
// Returns the default billing account with NoDefaultAccountError if there's
// not a bigv_subscription_account on the billing account, and returns nil
func (c *bytemarkClient) GetDefaultAccount() (acc *Account, err error) {
acc = new(Account)
acc.IsDefaultAccount = true
billAcc, err := c.getDefaultBillingAccount()
log.Debugf(log.LvlMisc, "billAcc: %#v, err: %v\r\n", billAcc, err)
if err != nil {
return nil, err
}
if billAcc != nil {
acc.fillBilling(billAcc)
brainAcc, err := c.getBrainAccount(billAcc.Name)
if err != nil {
return nil, err
}
acc.fillBrain(brainAcc)
} else {
brainAcc, err := c.getBrainAccounts()
if err != nil {
return nil, err
}
acc.fillBrain(brainAcc[0])
}
return
}
// Gets all Accounts you can see, merging data from both the brain and the billing
func (c *bytemarkClient) GetAccounts() (accounts []*Account, err error) {
byName := make(map[string]*Account)
billingAccounts, err := c.getBillingAccounts()
if err != nil {
return
}
brainAccounts, err := c.getBrainAccounts()
if err != nil {
return
}
for _, b := range brainAccounts {
if byName[b.Name] == nil {
byName[b.Name] = new(Account)
}
byName[b.Name].fillBrain(b)
}
for _, b := range billingAccounts {
if byName[b.Name] == nil {
byName[b.Name] = new(Account)
}
byName[b.Name].fillBilling(b)
}
for _, a := range byName {
accounts = append(accounts, a)
}
return
}
// Overview is a combination of a user's default account, their username, and all the accounts they have access to see.
type Overview struct {
DefaultAccount *Account
Username string
Accounts []*Account
}
// GetOverview gets an Overview for everything the user can access at bytemark
func (c *bytemarkClient) GetOverview() (*Overview, error) {
o := new(Overview)
acc, err := c.GetDefaultAccount()
if err != nil {
return nil, err
}
accs, err := c.GetAccounts()
if err != nil {
return nil, err
}
o.DefaultAccount = acc
o.Accounts = accs
o.Username = c.authSession.Username
return o, nil
}