diff --git a/bare_metal_server.go b/bare_metal_server.go index d0b3936..82a1a44 100644 --- a/bare_metal_server.go +++ b/bare_metal_server.go @@ -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) @@ -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"` @@ -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"` @@ -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 @@ -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 { @@ -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) { @@ -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) @@ -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) @@ -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) diff --git a/bare_metal_server_test.go b/bare_metal_server_test.go index 122c9ee..25e6bd6 100644 --- a/bare_metal_server_test.go +++ b/bare_metal_server_test.go @@ -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", @@ -164,7 +164,7 @@ func TestBareMetalServerServiceHandler_Update(t *testing.T) { fmt.Fprint(writer) }) - options := &BareMetalReq{ + options := &BareMetalUpdate{ Label: "my new label", } @@ -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) + } +} diff --git a/block_storage.go b/block_storage.go index be5c692..5bf50ce 100644 --- a/block_storage.go +++ b/block_storage.go @@ -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 @@ -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 -} diff --git a/block_storage_test.go b/block_storage_test.go index 9743ded..9f7dfad 100644 --- a/block_storage_test.go +++ b/block_storage_test.go @@ -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) - } -} diff --git a/firewall_rule.go b/firewall_rule.go index 5d44994..d50bd87 100644 --- a/firewall_rule.go +++ b/firewall_rule.go @@ -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 } diff --git a/instance.go b/instance.go index 4e2cc62..4dc8169 100644 --- a/instance.go +++ b/instance.go @@ -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"` diff --git a/instance_test.go b/instance_test.go index ed88bfd..dee2303 100644 --- a/instance_test.go +++ b/instance_test.go @@ -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) }) @@ -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) }) @@ -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, diff --git a/regions.go b/regions.go index 77b31a7..20fba37 100644 --- a/regions.go +++ b/regions.go @@ -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) } @@ -35,7 +35,7 @@ type regionBase struct { Meta *Meta } -type planAvailability struct { +type PlanAvailability struct { AvailablePlans []string `json:"available_plans"` } @@ -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) @@ -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 } diff --git a/regions_test.go b/regions_test.go index a52eef9..4688552 100644 --- a/regions_test.go +++ b/regions_test.go @@ -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) } diff --git a/reserved_ip.go b/reserved_ip.go index 9ddf7b1..3e21faa 100644 --- a/reserved_ip.go +++ b/reserved_ip.go @@ -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 } @@ -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) @@ -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) @@ -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 } diff --git a/reserved_ip_test.go b/reserved_ip_test.go index bf6deef..0058553 100644 --- a/reserved_ip_test.go +++ b/reserved_ip_test.go @@ -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) } } @@ -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 {