Skip to content

Commit

Permalink
Version 1.1.0
Browse files Browse the repository at this point in the history
Added missing API endpoints

- Server Rebuild
- Server Ports
- Account Usage
- Account History
- Server Restart
- Server Rebuild
- Server Resize
- Server Protection
- Server Protection Ports
  • Loading branch information
BitLaunchIO committed Oct 16, 2020
1 parent cfeb91a commit 291f377
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 1 deletion.
75 changes: 75 additions & 0 deletions account.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gobitlaunch

import (
"fmt"
"time"
)

Expand All @@ -19,6 +20,38 @@ type Account struct {
NegativeAllowance int `json:"negativeAllowance"`
}

type usageData struct {
Description string `json:"description"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
Cost int `json:"cost"`
Hours int `json:"hours"`
Amount int `json:"amount"`
Type string `json:"type"`
}

// AccountUsage represents the usage data of a BitLaunch account
type AccountUsage struct {
Server []usageData `json:"serverUsage"`
Backup []usageData `json:"backupUsage"`
Bandwidth []usageData `json:"bandwidthUsage"`
Protection []usageData `json:"protectionUsage"`
TotalUSD int `json:"totalUsd"`
PrevMonth string `json:"prevMonth"`
ThisMonth string `json:"thisMonth"`
NextMonth string `json:"nextMonth"`
}

// AccountHistory represents the usage data of a BitLaunch account
type AccountHistory struct {
History []struct {
ID string `json:"id"`
Time time.Time `json:"time"`
Description string `json:"description"`
} `json:"history"`
Total int `json:"total"`
}

// AccountService manages account API actions
type AccountService struct {
client *Client
Expand All @@ -38,3 +71,45 @@ func (as *AccountService) Show() (*Account, error) {

return account, nil
}

// Usage shows the account usage
func (as *AccountService) Usage(filter ...string) (*AccountUsage, error) {
var search string

if len(filter) > 1 {
return nil, fmt.Errorf("Too many arguments")
} else if len(filter) == 0 {
search = "latest"
} else {
search = filter[0]
}

req, err := as.client.NewRequest("GET", "/usage?period="+search, nil)
if err != nil {
return nil, err
}

usage := &AccountUsage{}
if err := as.client.DoRequest(req, usage); err != nil {
return nil, err
}

return usage, nil
}

// History shows the account history/activity
func (as *AccountService) History(page, perPage int) (*AccountHistory, error) {
q := fmt.Sprintf("?page=%d&items=%d", page, perPage)

req, err := as.client.NewRequest("GET", "/security/history"+q, nil)
if err != nil {
return nil, err
}

history := &AccountHistory{}
if err := as.client.DoRequest(req, history); err != nil {
return nil, err
}

return history, nil
}
2 changes: 1 addition & 1 deletion gobitlaunch.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

const (
version = "1.0.0"
version = "1.1.0"
userAgent = "gobitlaunch/" + version
maxRateLimit = 900 * time.Millisecond
retryLimit = 3
Expand Down
128 changes: 128 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import (
"time"
)

// Ports represents a port slice object
type Ports struct {
PortNumber int `json:"portNumber"`
Protocol string `json:"protocol"`
}

// Server represents a server
type Server struct {
ID string `json:"id"`
Expand All @@ -26,6 +32,15 @@ type Server struct {
Version string `json:"version"`
Abuse bool `json:"abuse"`
DiskGB int `json:"diskGB"`
Protection struct {
Enabled bool `json:"enabled"`
Proxy struct {
IP string `json:"ip"`
Region string `json:"region"`
Ports []Ports `json:"ports"`
Target string `json:"target"`
} `json:"proxy"`
} `json:"protection"`
}

// CreateServerOptions defines options for creating a new server
Expand All @@ -40,6 +55,12 @@ type CreateServerOptions struct {
InitScript string `json:"initscript"`
}

// RebuildOptions defines options for rebuilding a server
type RebuildOptions struct {
ID string `json:"hostImageID"`
Description string `json:"imageDescription"`
}

// ServerService manages server API actions
type ServerService struct {
client *Client
Expand Down Expand Up @@ -111,3 +132,110 @@ func (ss *ServerService) Destroy(id string) error {

return nil
}

// Rebuild server
func (ss *ServerService) Rebuild(id string, opts *RebuildOptions) error {
b, err := json.Marshal(opts)
if err != nil {
return err
}
req, err := ss.client.NewRequest("POST", "/servers/"+id+"/rebuild", b)
if err != nil {
return err
}

if err := ss.client.DoRequest(req, nil); err != nil {
return err
}

return nil
}

// Resize server
func (ss *ServerService) Resize(id string, size string) error {
b, err := json.Marshal(map[string]string{
"size": size,
})
if err != nil {
return err
}
req, err := ss.client.NewRequest("POST", "/servers/"+id+"/resize", b)
if err != nil {
return err
}

if err := ss.client.DoRequest(req, nil); err != nil {
return err
}

return nil
}

// Restart server
func (ss *ServerService) Restart(id string) error {
req, err := ss.client.NewRequest("POST", "/servers/"+id+"/restart", nil)
if err != nil {
return err
}

if err := ss.client.DoRequest(req, nil); err != nil {
return err
}

return nil
}

// Protection is to enable/disable DDoS protection on a server
func (ss *ServerService) Protection(id string, enabled bool) (*Server, error) {
opts := struct {
Enabled bool `json:"enable"`
Region string `json:"region"`
}{
Enabled: enabled,
Region: func() string {
if enabled {
return "bvm-lux"
}
return ""
}(),
}

b, err := json.Marshal(opts)
if err != nil {
return nil, err
}

req, err := ss.client.NewRequest("POST", "/servers/"+id+"/protection", b)
if err != nil {
return nil, err
}

s := Server{}

if err := ss.client.DoRequest(req, &s); err != nil {
return nil, err
}

return &s, nil
}

// SetPorts sets the enabled ports on a server for DDoS protection (Needs Protection enabled)
func (ss *ServerService) SetPorts(id string, ports *[]Ports) (*Server, error) {
b, err := json.Marshal(ports)
if err != nil {
return nil, err
}

req, err := ss.client.NewRequest("POST", "/servers/"+id+"/protection/ports", b)
if err != nil {
return nil, err
}

s := Server{}

if err := ss.client.DoRequest(req, &s); err != nil {
return nil, err
}

return &s, nil
}

0 comments on commit 291f377

Please # to comment.