account.go raw
1 package govultr
2
3 import (
4 "context"
5 "net/http"
6 )
7
8 // AccountService is the interface to interact with Accounts endpoint on the Vultr API
9 // Link : https://www.vultr.com/api/#tag/account
10 type AccountService interface {
11 Get(ctx context.Context) (*Account, *http.Response, error)
12 GetBandwidth(ctx context.Context) (*AccountBandwidth, *http.Response, error)
13 }
14
15 // AccountServiceHandler handles interaction with the account methods for the Vultr API
16 type AccountServiceHandler struct {
17 client *Client
18 }
19
20 type accountBase struct {
21 Account *Account `json:"account"`
22 }
23
24 // Account represents a Vultr account
25 type Account struct {
26 Balance float32 `json:"balance"`
27 PendingCharges float32 `json:"pending_charges"`
28 LastPaymentDate string `json:"last_payment_date"`
29 LastPaymentAmount float32 `json:"last_payment_amount"`
30 Name string `json:"name"`
31 Email string `json:"email"`
32 ACL []string `json:"acls"`
33 }
34
35 type accountBandwidthBase struct {
36 Bandwidth *AccountBandwidth `json:"bandwidth"`
37 }
38
39 // Bandwidth represents a Vultr account bandwidth
40 type AccountBandwidth struct {
41 PreviousMonth AccountBandwidthPeriod `json:"previous_month"`
42 CurrentMonthToDate AccountBandwidthPeriod `json:"current_month_to_date"`
43 CurrentMonthProjected AccountBandwidthPeriod `json:"current_month_projected"`
44 }
45
46 // AccountBandwidthPeriod represents a Vultr account bandwidth period
47 type AccountBandwidthPeriod struct {
48 TimestampStart string `json:"timestamp_start"`
49 TimestampEnd string `json:"timestamp_end"`
50 GBIn int `json:"gb_in"`
51 GBOut int `json:"gb_out"`
52 TotalInstanceHours int `json:"total_instance_hours"`
53 TotalInstanceCount int `json:"total_instance_count"`
54 InstanceBandwidthCredits int `json:"instance_bandwidth_credits"`
55 FreeBandwidthCredits int `json:"free_bandwidth_credits"`
56 PurchasedBandwidthCredits int `json:"purchased_bandwidth_credits"`
57 Overage float32 `json:"overage"`
58 OverageUnitCost float32 `json:"overage_unit_cost"`
59 OverageCost float32 `json:"overage_cost"`
60 }
61
62 // Get Vultr account info
63 func (a *AccountServiceHandler) Get(ctx context.Context) (*Account, *http.Response, error) {
64 uri := "/v2/account"
65 req, err := a.client.NewRequest(ctx, http.MethodGet, uri, nil)
66 if err != nil {
67 return nil, nil, err
68 }
69
70 account := new(accountBase)
71 resp, err := a.client.DoWithContext(ctx, req, account)
72 if err != nil {
73 return nil, resp, err
74 }
75
76 return account.Account, resp, nil
77 }
78
79 // Get Vultr account bandwidth info
80 func (a *AccountServiceHandler) GetBandwidth(ctx context.Context) (*AccountBandwidth, *http.Response, error) {
81 uri := "/v2/account/bandwidth"
82 req, err := a.client.NewRequest(ctx, http.MethodGet, uri, nil)
83 if err != nil {
84 return nil, nil, err
85 }
86
87 accountBandwidth := new(accountBandwidthBase)
88 resp, err := a.client.DoWithContext(ctx, req, accountBandwidth)
89 if err != nil {
90 return nil, resp, err
91 }
92
93 return accountBandwidth.Bandwidth, resp, nil
94 }
95