Skip to content

Commit

Permalink
Merge pull request #96 from vultr/v2-api-fixes
Browse files Browse the repository at this point in the history
V2 Beta changes
  • Loading branch information
ddymko authored Oct 17, 2020
2 parents 099001d + 88837ab commit a1b19d1
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 69 deletions.
47 changes: 37 additions & 10 deletions bare_metal_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ const bmPath = "/v2/bare-metals"

// BareMetalServerService is the interface to interact with the bare metal endpoints on the Vultr API
type BareMetalServerService interface {
Create(ctx context.Context, bmCreate *BareMetalReq) (*BareMetalServer, error)
Create(ctx context.Context, bmCreate *BareMetalCreate) (*BareMetalServer, error)
Get(ctx context.Context, serverID string) (*BareMetalServer, error)
Update(ctx context.Context, serverID string, bmReq *BareMetalReq) error
Update(ctx context.Context, serverID string, bmReq *BareMetalUpdate) error
Delete(ctx context.Context, serverID string) error
List(ctx context.Context, options *ListOptions) ([]BareMetalServer, *Meta, error)

GetBandwidth(ctx context.Context, serverID string) (*Bandwidth, error)
GetUserData(ctx context.Context, serverID string) (*UserData, error)

ListIPv4s(ctx context.Context, serverID string, options *ListOptions) ([]IPv4, *Meta, error)
ListIPv6s(ctx context.Context, serverID string, options *ListOptions) ([]IPv6, *Meta, error)
Expand Down Expand Up @@ -62,8 +63,8 @@ type BareMetalServer struct {
Features []string `json:"features"`
}

// BareMetalReq represents the optional parameters that can be set when creating or updating a bare metal server
type BareMetalReq struct {
// BareMetalCreate represents the optional parameters that can be set when creating a bare metal server
type BareMetalCreate struct {
Region string `json:"region,omitempty"`
Plan string `json:"plan,omitempty"`
OsID int `json:"os_id,omitempty"`
Expand All @@ -80,6 +81,16 @@ type BareMetalReq struct {
ReservedIPv4 string `json:"reserved_ipv4,omitempty"`
}

// BareMetalUpdate represents the optional parameters that can be set when updating a bare metal server
type BareMetalUpdate struct {
OsID int `json:"os_id,omitempty"`
EnableIPv6 bool `json:"enable_ipv6,omitempty"`
Label string `json:"label,omitempty"`
AppID int `json:"app_id,omitempty"`
UserData string `json:"user_data,omitempty"`
Tag string `json:"tag,omitempty"`
}

// BareMetalServerBandwidth represents bandwidth information for a bare metal server
type BareMetalServerBandwidth struct {
IncomingBytes int `json:"incoming_bytes"`
Expand All @@ -100,7 +111,7 @@ type BMBareMetalBase struct {
}

// Create a new bare metal server.
func (b *BareMetalServerServiceHandler) Create(ctx context.Context, bmCreate *BareMetalReq) (*BareMetalServer, error) {
func (b *BareMetalServerServiceHandler) Create(ctx context.Context, bmCreate *BareMetalCreate) (*BareMetalServer, error) {
req, err := b.client.NewRequest(ctx, http.MethodPost, bmPath, bmCreate)
if err != nil {
return nil, err
Expand Down Expand Up @@ -131,7 +142,7 @@ func (b *BareMetalServerServiceHandler) Get(ctx context.Context, serverID string
}

// Update will update the given bare metal. Empty values are ignored
func (b *BareMetalServerServiceHandler) Update(ctx context.Context, serverID string, bmReq *BareMetalReq) error {
func (b *BareMetalServerServiceHandler) Update(ctx context.Context, serverID string, bmReq *BareMetalUpdate) error {
uri := fmt.Sprintf("%s/%s", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodPatch, uri, bmReq)
if err != nil {
Expand Down Expand Up @@ -199,6 +210,22 @@ func (b *BareMetalServerServiceHandler) GetBandwidth(ctx context.Context, server
return bms, nil
}

// GetUserData from given bareMetal. The userdata returned will be in base64 encoding.
func (i *BareMetalServerServiceHandler) GetUserData(ctx context.Context, serverID string) (*UserData, error) {
uri := fmt.Sprintf("%s/%s/user-data", bmPath, serverID)
req, err := i.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}

userData := new(userDataBase)
if err = i.client.DoWithContext(ctx, req, userData); err != nil {
return nil, err
}

return userData.UserData, nil
}

// ListIPv4s will List the IPv4 information of a bare metal server.
// IP information is only available for bare metal servers in the "active" state.
func (b *BareMetalServerServiceHandler) ListIPv4s(ctx context.Context, serverID string, options *ListOptions) ([]IPv4, *Meta, error) {
Expand Down Expand Up @@ -299,7 +326,7 @@ func (b *BareMetalServerServiceHandler) Reinstall(ctx context.Context, serverID

// Start will start a list of bare metal servers the machine is already running, it will be restarted.
func (b *BareMetalServerServiceHandler) MassStart(ctx context.Context, serverList []string) error {
uri := fmt.Sprintf("%s/start", instancePath)
uri := fmt.Sprintf("%s/start", bmPath)

reqBody := RequestBody{"baremetal_ids": serverList}
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, reqBody)
Expand All @@ -316,7 +343,7 @@ func (b *BareMetalServerServiceHandler) MassStart(ctx context.Context, serverLis

// Halt will pause a list of bare metals.
func (b *BareMetalServerServiceHandler) MassHalt(ctx context.Context, serverList []string) error {
uri := fmt.Sprintf("%s/halt", instancePath)
uri := fmt.Sprintf("%s/halt", bmPath)

reqBody := RequestBody{"baremetal_ids": serverList}
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, reqBody)
Expand All @@ -331,9 +358,9 @@ func (b *BareMetalServerServiceHandler) MassHalt(ctx context.Context, serverList
return nil
}

// MassReboot reboots a list of instances.
// MassReboot reboots a list of bare metals.
func (b *BareMetalServerServiceHandler) MassReboot(ctx context.Context, serverList []string) error {
uri := fmt.Sprintf("%s/reboot", instancePath)
uri := fmt.Sprintf("%s/reboot", bmPath)

reqBody := RequestBody{"baremetal_ids": serverList}
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, reqBody)
Expand Down
25 changes: 23 additions & 2 deletions bare_metal_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestBareMetalServerServiceHandler_Create(t *testing.T) {
fmt.Fprint(writer, response)
})

options := &BareMetalReq{
options := &BareMetalCreate{
StartupScriptID: "1",
Region: "ewr",
Plan: "vbm-4c-32gb",
Expand Down Expand Up @@ -164,7 +164,7 @@ func TestBareMetalServerServiceHandler_Update(t *testing.T) {
fmt.Fprint(writer)
})

options := &BareMetalReq{
options := &BareMetalUpdate{
Label: "my new label",
}

Expand Down Expand Up @@ -429,3 +429,24 @@ func TestBareMetalServerServiceHandler_Reinstall(t *testing.T) {
t.Errorf("BareMetalServer.Reinstall returned %+v, expected %+v", err, nil)
}
}

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

mux.HandleFunc("/v2/bare-metals/dev-preview-abc123/user-data", func(writer http.ResponseWriter, request *http.Request) {
response := `{"user_data": {"data" : "ZWNobyBIZWxsbyBXb3JsZA=="}}`
fmt.Fprint(writer, response)
})

userData, err := client.BareMetalServer.GetUserData(ctx, "dev-preview-abc123")
if err != nil {
t.Errorf("BareMetalServer.GetUserData return %+v ", err)
}

expected := &UserData{Data: "ZWNobyBIZWxsbyBXb3JsZA=="}

if !reflect.DeepEqual(userData, expected) {
t.Errorf("BareMetalServer.GetUserData returned %+v, expected %+v", userData, expected)
}
}
18 changes: 0 additions & 18 deletions block_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type BlockStorageService interface {

Attach(ctx context.Context, blockID string, attach *BlockStorageAttach) error
Detach(ctx context.Context, blockID string, detach *BlockStorageDetach) error
Resize(ctx context.Context, blockID string, sizeGB int) error
}

// BlockStorageServiceHandler handles interaction with the block-storage methods for the Vultr API
Expand Down Expand Up @@ -196,20 +195,3 @@ func (b *BlockStorageServiceHandler) Detach(ctx context.Context, blockID string,

return nil
}

// Resize allows you to resize your Vultr block storage
func (b *BlockStorageServiceHandler) Resize(ctx context.Context, blockID string, sizeGB int) error {
uri := fmt.Sprintf("/v2/blocks/%s/resize", blockID)
body := &RequestBody{"size_gb": sizeGB}

req, err := b.client.NewRequest(ctx, http.MethodPost, uri, body)
if err != nil {
return err
}

if err = b.client.DoWithContext(ctx, req, nil); err != nil {
return err
}

return nil
}
14 changes: 0 additions & 14 deletions block_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,3 @@ func TestBlockStorageServiceHandler_Detach(t *testing.T) {
t.Errorf("BlockStorage.Detach returned %+v, expected %+v", err, nil)
}
}

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

mux.HandleFunc("/v2/blocks/123456/resize", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer)
})

err := client.BlockStorage.Resize(ctx, "123456", 50)
if err != nil {
t.Errorf("BlockStorage.Resize returned %+v, expected %+v", err, nil)
}
}
2 changes: 1 addition & 1 deletion firewall_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (f *FireWallRuleServiceHandler) Create(ctx context.Context, fwGroupID strin
func (f *FireWallRuleServiceHandler) Get(ctx context.Context, fwGroupID string, fwRuleID int) (*FirewallRule, error) {
uri := fmt.Sprintf("/v2/firewalls/%s/rules/%d", fwGroupID, fwRuleID)

req, err := f.client.NewRequest(ctx, http.MethodPost, uri, nil)
req, err := f.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ type backupScheduleBase struct {
type BackupSchedule struct {
Enabled bool `json:"enabled,omitempty"`
Type string `json:"type,omitempty"`
NextScheduleTimeUTC string `json:"next_run_utc,omitempty"`
NextScheduleTimeUTC string `json:"next_scheduled_time_utc,omitempty"`
Hour int `json:"hour,omitempty"`
Dow int `json:"dow,omitempty"`
Dom int `json:"dom,omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestServerServiceHandler_GetBackupSchedule(t *testing.T) {
defer teardown()

mux.HandleFunc("/v2/instances/dev-preview-abc123/backup-schedule", func(writer http.ResponseWriter, request *http.Request) {
response := `{"backup_schedule":{"enabled": true,"type": "weekly","next_run_utc": "2016-05-07 08:00:00","hour": 8,"dow": 6,"dom": 0}}`
response := `{"backup_schedule":{"enabled": true,"type": "weekly","next_scheduled_time_utc": "2016-05-07 08:00:00","hour": 8,"dow": 6,"dom": 0}}`
fmt.Fprint(writer, response)
})

Expand Down Expand Up @@ -40,7 +40,7 @@ func TestServerServiceHandler_SetBackupSchedule(t *testing.T) {
defer teardown()

mux.HandleFunc("/v2/instances/dev-preview-abc123/backup-schedule", func(writer http.ResponseWriter, request *http.Request) {
response := `{"backup_schedule":{"enabled": true,"type": "weekly","next_run_utc": "2016-05-07 08:00:00","hour": 22,"dow": 2,"dom": 3}}`
response := `{"backup_schedule":{"enabled": true,"type": "weekly","next_scheduled_time_utc": "2016-05-07 08:00:00","hour": 22,"dow": 2,"dom": 3}}`
fmt.Fprint(writer, response)
})

Expand Down Expand Up @@ -531,7 +531,7 @@ func TestServerServiceHandler_Create(t *testing.T) {
Hostname: "hostname-3000",
Tag: "tagger",
Label: "label-extreme",
SSHKeys: []string{"dev-preview-abc123", "dev-preview-abc124"},
SSHKeys: []string{"dev-preview-abc123", "dev-preview-abc124"},
ReservedIPv4: "63.209.35.79",
FirewallGroupID: "1234",
AppID: 1,
Expand Down
8 changes: 4 additions & 4 deletions regions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// RegionService is the interface to interact with Region endpoints on the Vultr API
type RegionService interface {
Availability(ctx context.Context, regionID string, planType string) (*planAvailability, error)
Availability(ctx context.Context, regionID string, planType string) (*PlanAvailability, error)
List(ctx context.Context, options *ListOptions) ([]Region, *Meta, error)
}

Expand All @@ -35,7 +35,7 @@ type regionBase struct {
Meta *Meta
}

type planAvailability struct {
type PlanAvailability struct {
AvailablePlans []string `json:"available_plans"`
}

Expand Down Expand Up @@ -64,7 +64,7 @@ func (r *RegionServiceHandler) List(ctx context.Context, options *ListOptions) (
}

// Availability retrieves a list of the plan IDs currently available for a given location.
func (r *RegionServiceHandler) Availability(ctx context.Context, regionID string, planType string) (*planAvailability, error) {
func (r *RegionServiceHandler) Availability(ctx context.Context, regionID string, planType string) (*PlanAvailability, error) {
uri := fmt.Sprintf("/v2/regions/%s/availability", regionID)

req, err := r.Client.NewRequest(ctx, http.MethodGet, uri, nil)
Expand All @@ -80,7 +80,7 @@ func (r *RegionServiceHandler) Availability(ctx context.Context, regionID string
req.URL.RawQuery = q.Encode()
}

plans := new(planAvailability)
plans := new(PlanAvailability)
if err = r.Client.DoWithContext(ctx, req, plans); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion regions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestRegionServiceHandler_Availability(t *testing.T) {
t.Errorf("Region.Availability returned error: %v", err)
}

expected := &planAvailability{AvailablePlans: []string{"vc2-1c-1gb", "vc2-1c-2gb", "vc2-2c-4gb"}}
expected := &PlanAvailability{AvailablePlans: []string{"vc2-1c-1gb", "vc2-1c-2gb", "vc2-2c-4gb"}}
if !reflect.DeepEqual(region, expected) {
t.Errorf("Region.Availability returned %+v, expected %+v", region, expected)
}
Expand Down
15 changes: 10 additions & 5 deletions reserved_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type ReservedIPService interface {
Delete(ctx context.Context, id string) error
List(ctx context.Context, options *ListOptions) ([]ReservedIP, *Meta, error)

Convert(ctx context.Context, ripConvert *ReservedIPReq) (*ReservedIP, error)
Attach(ctx context.Context, id string, ripAttach *ReservedIPReq) error
Convert(ctx context.Context, ripConvert *ReservedIPConvertReq) (*ReservedIP, error)
Attach(ctx context.Context, id, instance string) error
Detach(ctx context.Context, id string) error
}

Expand Down Expand Up @@ -56,6 +56,11 @@ type reservedIPBase struct {
ReservedIP *ReservedIP `json:"reserved_ip"`
}

type ReservedIPConvertReq struct {
IPAddress string `json:"ip_address,omitempty"`
Label string `json:"label,omitempty"`
}

// Create adds the specified reserved IP to your Vultr account
func (r *ReservedIPServiceHandler) Create(ctx context.Context, ripCreate *ReservedIPReq) (*ReservedIP, error) {
req, err := r.client.NewRequest(ctx, http.MethodPost, ripPath, ripCreate)
Expand Down Expand Up @@ -125,7 +130,7 @@ func (r *ReservedIPServiceHandler) List(ctx context.Context, options *ListOption
}

// Convert an existing IP on a subscription to a reserved IP.
func (r *ReservedIPServiceHandler) Convert(ctx context.Context, ripConvert *ReservedIPReq) (*ReservedIP, error) {
func (r *ReservedIPServiceHandler) Convert(ctx context.Context, ripConvert *ReservedIPConvertReq) (*ReservedIP, error) {
uri := fmt.Sprintf("%s/convert", ripPath)
req, err := r.client.NewRequest(ctx, http.MethodPost, uri, ripConvert)

Expand All @@ -142,9 +147,9 @@ func (r *ReservedIPServiceHandler) Convert(ctx context.Context, ripConvert *Rese
}

// Attach a reserved IP to an existing subscription
func (r *ReservedIPServiceHandler) Attach(ctx context.Context, id string, ripAttach *ReservedIPReq) error {
func (r *ReservedIPServiceHandler) Attach(ctx context.Context, id, instance string) error {
uri := fmt.Sprintf("%s/%s/attach", ripPath, id)
req, err := r.client.NewRequest(ctx, http.MethodPost, uri, ripAttach)
req, err := r.client.NewRequest(ctx, http.MethodPost, uri, instance)
if err != nil {
return err
}
Expand Down
14 changes: 4 additions & 10 deletions reserved_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ func TestReservedIPServiceHandler_Attach(t *testing.T) {
fmt.Fprint(writer)
})

options := &ReservedIPReq{
InstanceID: "1234",
}
err := client.ReservedIP.Attach(ctx, "12345", options)

if err != nil {
if err := client.ReservedIP.Attach(ctx, "12345", "1234"); err != nil {
t.Errorf("ReservedIP.Attach returned %+v, expected %+v", err, nil)
}
}
Expand All @@ -48,11 +43,10 @@ func TestReservedIPServiceHandler_Convert(t *testing.T) {
fmt.Fprint(writer, response)
})

options := &ReservedIPReq{
IPAddress: "111.111.111.111",
InstanceID: "1234",
options := &ReservedIPConvertReq{
IPAddress: "111.111.111.111",
Label: "my first reserved ip",
}

ip, err := client.ReservedIP.Convert(ctx, options)

if err != nil {
Expand Down

0 comments on commit a1b19d1

Please sign in to comment.