Skip to content

Commit

Permalink
Add get route for account bandwidth (#356)
Browse files Browse the repository at this point in the history
* Added implementation and test for `/account/bandwidth`

* Base type should be unexported

* Corrected abbreviations
  • Loading branch information
DazWilkin authored Feb 19, 2025
1 parent cb9fb65 commit d756953
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
44 changes: 44 additions & 0 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
// Link : https://www.vultr.com/api/#tag/account
type AccountService interface {
Get(ctx context.Context) (*Account, *http.Response, error)
GetBandwidth(ctx context.Context) (*AccountBandwidth, *http.Response, error)
}

// AccountServiceHandler handles interaction with the account methods for the Vultr API
Expand All @@ -30,6 +31,32 @@ type Account struct {
Email string `json:"email"`
ACL []string `json:"acls"`
}
type accountBandwidthBase struct {
Bandwidth *AccountBandwidth `json:"bandwidth"`
}

// Bandwidth represents a Vultr account bandwidth
type AccountBandwidth struct {
PreviousMonth AccountBandwidthPeriod `json:"previous_month"`
CurrentMonthToDate AccountBandwidthPeriod `json:"current_month_to_date"`
CurrentMonthProjected AccountBandwidthPeriod `json:"current_month_projected"`
}

// AccountBandwidthPeriod represents a Vultr account bandwidth period
type AccountBandwidthPeriod struct {
TimestampStart string `json:"timestamp_start"`
TimestampEnd string `json:"timestamp_end"`
GBIn int `json:"gb_in"`
GBOut int `json:"gb_out"`
TotalInstanceHours int `json:"total_instance_hours"`
TotalInstanceCount int `json:"total_instance_count"`
InstanceBandwidthCredits int `json:"instance_bandwidth_credits"`
FreeBandwidthCredits int `json:"free_bandwidth_credits"`
PurchasedBandwidthCredits int `json:"purchased_bandwidth_credits"`
Overage float32 `json:"overage"`
OverageUnitCost float32 `json:"overage_unit_cost"`
OverageCost float32 `json:"overage_cost"`
}

// Get Vultr account info
func (a *AccountServiceHandler) Get(ctx context.Context) (*Account, *http.Response, error) {
Expand All @@ -47,3 +74,20 @@ func (a *AccountServiceHandler) Get(ctx context.Context) (*Account, *http.Respon

return account.Account, resp, nil
}

// Get Vultr account bandwidth info
func (a *AccountServiceHandler) GetBandwidth(ctx context.Context) (*AccountBandwidth, *http.Response, error) {
uri := "/v2/account/bandwidth"
req, err := a.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}

accountBandwidth := new(accountBandwidthBase)
resp, err := a.client.DoWithContext(ctx, req, accountBandwidth)
if err != nil {
return nil, resp, err
}

return accountBandwidth.Bandwidth, resp, nil
}
112 changes: 112 additions & 0 deletions account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,115 @@ func TestAccountServiceHandler_Get(t *testing.T) {
t.Errorf("Account.Get returned %+v, expected %+v", account, expected)
}
}

func TestAccountServiceHandler_GetBandwidth(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/v2/account/bandwidth", func(w http.ResponseWriter, r *http.Request) {
response := `
{
"bandwidth": {
"previous_month": {
"timestamp_start": "1735689600",
"timestamp_end": "1738367999",
"gb_in": 0,
"gb_out": 0,
"total_instance_hours": 1,
"total_instance_count": 1,
"instance_bandwidth_credits": 1,
"free_bandwidth_credits": 2048,
"purchased_bandwidth_credits": 0,
"overage": 0,
"overage_unit_cost": 0.01,
"overage_cost": 0
},
"current_month_to_date": {
"timestamp_start": "1738368000",
"timestamp_end": "1739577600",
"gb_in": 0,
"gb_out": 0,
"total_instance_hours": 0,
"total_instance_count": 0,
"instance_bandwidth_credits": 0,
"free_bandwidth_credits": 2048,
"purchased_bandwidth_credits": 0,
"overage": 0,
"overage_unit_cost": 0.01,
"overage_cost": 0
},
"current_month_projected": {
"timestamp_start": "1738368000",
"timestamp_end": "1740787199",
"gb_in": 0,
"gb_out": 0,
"total_instance_hours": 0,
"total_instance_count": 0,
"instance_bandwidth_credits": 0,
"free_bandwidth_credits": 2048,
"purchased_bandwidth_credits": 0,
"overage": 0,
"overage_unit_cost": 0.01,
"overage_cost": 0
}
}
}
`

fmt.Fprint(w, response)
})

accountBandwidth, _, err := client.Account.GetBandwidth(ctx)
if err != nil {
t.Errorf("Account.GetBandwidth returned error: %v", err)
}

expected := &AccountBandwidth{
PreviousMonth: AccountBandwidthPeriod{
TimestampStart: "1735689600",
TimestampEnd: "1738367999",
GBIn: 0,
GBOut: 0,
TotalInstanceHours: 1,
TotalInstanceCount: 1,
InstanceBandwidthCredits: 1,
FreeBandwidthCredits: 2048,
PurchasedBandwidthCredits: 0,
Overage: 0,
OverageUnitCost: 0.01,
OverageCost: 0,
},
CurrentMonthToDate: AccountBandwidthPeriod{
TimestampStart: "1738368000",
TimestampEnd: "1739577600",
GBIn: 0,
GBOut: 0,
TotalInstanceHours: 0,
TotalInstanceCount: 0,
InstanceBandwidthCredits: 0,
FreeBandwidthCredits: 2048,
PurchasedBandwidthCredits: 0,
Overage: 0,
OverageUnitCost: 0.01,
OverageCost: 0,
},
CurrentMonthProjected: AccountBandwidthPeriod{
TimestampStart: "1738368000",
TimestampEnd: "1740787199",
GBIn: 0,
GBOut: 0,
TotalInstanceHours: 0,
TotalInstanceCount: 0,
InstanceBandwidthCredits: 0,
FreeBandwidthCredits: 2048,
PurchasedBandwidthCredits: 0,
Overage: 0,
OverageUnitCost: 0.01,
OverageCost: 0,
},
}

if !reflect.DeepEqual(accountBandwidth, expected) {
t.Errorf("Account.GetBandwidth returned %+v, expected %+v", accountBandwidth, expected)
}
}

0 comments on commit d756953

Please # to comment.