Skip to content

Commit

Permalink
Implement ServerDisks
Browse files Browse the repository at this point in the history
* ServerDisks doc_test
* Server add `AdditionalDisks` in `ServerDetails`
  • Loading branch information
norrland committed Nov 30, 2023
1 parent f9c08e9 commit b79d699
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## Unreleased

### Added
- ServerDisks - Implement `/serverdisk` endpoint.

## [8.0.0] - 2023-11-20
### Changed
- **BREAKING** - Server Templates cost float64
Expand Down
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Client struct {
LoadBalancers *LoadBalancerService
ObjectStorages *ObjectStorageService
Servers *ServerService
ServerDisks *ServerDisksService
Networks *NetworkService
NetworkAdapters *NetworkAdapterService
}
Expand All @@ -59,6 +60,7 @@ func NewClient(project, apiKey, userAgent string) *Client {
c.LoadBalancers = &LoadBalancerService{client: c}
c.ObjectStorages = &ObjectStorageService{client: c}
c.Servers = &ServerService{client: c}
c.ServerDisks = &ServerDisksService{client: c}
c.Networks = &NetworkService{client: c}
c.NetworkAdapters = &NetworkAdapterService{client: c}

Expand Down
75 changes: 75 additions & 0 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,81 @@ func ExampleServerService_PreviewCloudConfig() {
fmt.Println(preview.Context.Users[0].Username)
fmt.Printf("Number of balloons: %f\n", preview.Context.Params["balloon"])
}

func ExampleServerDisksService_Create() {
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

params := glesys.CreateServerDiskParams{
ServerID: "wps123456",
SizeInGIB: 125,
Name: "Extra disk",
}

_, _ = client.ServerDisks.Create(context.Background(), params)

server, _ := client.Servers.Details(context.Background(), "wps123456")

for _, disk := range server.AdditionalDisks {
fmt.Printf("Disk%d: %s - %d\n", disk.SCSIID, disk.Name, disk.SizeInGIB)
}
}

func ExampleServerDisksService_Delete() {
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

diskid := "abc1234-zxy987-cccbbbaaa"
err := client.ServerDisks.Delete(context.Background(), diskid)

if err != nil {
fmt.Println("Error deleting disk", err)
}
}

func ExampleServerDisksService_UpdateName() {
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

params := glesys.EditServerDiskParams{
ID: "abc1234-zxy987-cccbbbaaa",
Name: "new name",
}

_, err := client.ServerDisks.UpdateName(context.Background(), params)

if err != nil {
fmt.Println("Error renaming disk", err)
}
}

func ExampleServerDisksService_Reconfigure() {
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

params := glesys.EditServerDiskParams{
ID: "abc1234-zxy987-cccbbbaaa",
SizeInGIB: 120,
}

_, err := client.ServerDisks.Reconfigure(context.Background(), params)

if err != nil {
fmt.Println("Error updating disk size", err)
}
}

func ExampleServerDisksService_Limits() {
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

serverid := "wps123456"
limits, _ := client.ServerDisks.Limits(context.Background(), serverid)

fmt.Printf("Limits (%s): Min size: %d, Max size %d, Current number of disks: %d, Max number of disks: %d\n",
serverid,
limits.MinSizeInGIB,
limits.MaxSizeInGIB,
limits.CurrentNumDisks,
limits.MaxNumDisks,
)
}

func ExampleObjectStorageService_CreateInstance() {
client := glesys.NewClient("CL12345", "your-api-key", "my-application/0.0.1")

Expand Down
92 changes: 92 additions & 0 deletions serverdisks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package glesys

import "context"

// ServerDisksService provides functions to interact with serverdisks
type ServerDisksService struct {
client clientInterface
}

// CreateServerDiskParams specifies the details for a new serverdisk
type CreateServerDiskParams struct {
Name string `json:"name"`
ServerID string `json:"serverid"`
SizeInGIB int `json:"sizeingib"`
}

// ServerDiskDetails represents any extra disks for a server
type ServerDiskDetails struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
SizeInGIB int `json:"sizeingib"`
SCSIID int `json:"scsiid"`
}

// ServerDiskReconfigureParams parameters for updating a ServerDisk
type EditServerDiskParams struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
SizeInGIB int `json:"sizeingib,omitempty"`
}

// ServerDiskLimitsDetails represents the disk limits for a server
type ServerDiskLimitsDetails struct {
MinSizeInGIB int `json:"minsizeingib"`
MaxSizeInGIB int `json:"maxsizeingib"`
MaxNumDisks int `json:"maxnumdisks"`
CurrentNumDisks int `json:"currentnumdisks"`
}

// Create - Creates an additional serverdisk using CreateServerDiskParams
func (s *ServerDisksService) Create(context context.Context, params CreateServerDiskParams) (*ServerDiskDetails, error) {
data := struct {
Response struct {
Disk ServerDiskDetails
}
}{}
err := s.client.post(context, "serverdisk/create", &data, params)
return &data.Response.Disk, err
}

// UpdateName - Modifies a serverdisk name using EditServerDiskParams
func (s *ServerDisksService) UpdateName(context context.Context, params EditServerDiskParams) (*ServerDiskDetails, error) {
data := struct {
Response struct {
Disk ServerDiskDetails
}
}{}
err := s.client.post(context, "serverdisk/updatename", &data, params)
return &data.Response.Disk, err
}

// Reconfigure - Modifies a serverdisk using EditServerDiskParams
func (s *ServerDisksService) Reconfigure(context context.Context, params EditServerDiskParams) (*ServerDiskDetails, error) {
data := struct {
Response struct {
Disk ServerDiskDetails
}
}{}
err := s.client.post(context, "serverdisk/reconfigure", &data, params)
return &data.Response.Disk, err
}

// Delete - deletes a serverdisk
func (s *ServerDisksService) Delete(context context.Context, diskID string) error {
return s.client.post(context, "serverdisk/delete", nil, struct {
DiskID string `json:"id"`
}{diskID})
}

// Limits - retrieve serverdisk limits for a specific server
func (s *ServerDisksService) Limits(context context.Context, serverID string) (*ServerDiskLimitsDetails, error) {
data := struct {
Response struct {
Limits ServerDiskLimitsDetails
}
}{}
err := s.client.post(context, "serverdisk/limits", &data,
struct {
ServerID string `json:"serverid"`
}{serverID})
return &data.Response.Limits, err
}
95 changes: 95 additions & 0 deletions serverdisks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package glesys

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestServerDisk_Create(t *testing.T) {
c := &mockClient{body: `{ "response": { "disk": { "id": "aaaaa-bbbbbb-cccccc",
"sizeingib": 200,
"name": "Diskett",
"scsiid": 1
}}}`}
s := ServerDisksService{client: c}

params := CreateServerDiskParams{
ServerID: "wps123456",
Name: "Diskett",
SizeInGIB: 200,
}

disk, _ := s.Create(context.Background(), params)

assert.Equal(t, "serverdisk/create", c.lastPath, "correct path is used")
assert.Equal(t, 200, disk.SizeInGIB, "size is correct")
assert.Equal(t, "Diskett", disk.Name, "correct name variable")
}

func TestServerDisk_UpdateName(t *testing.T) {
c := &mockClient{body: `{ "response": { "disk": { "id": "aaaaa-bbbbbb-cccccc",
"sizeingib": 200,
"name": "Extradisk",
"scsiid": 1
}}}`}
s := ServerDisksService{client: c}

params := EditServerDiskParams{
ID: "aaaaa-bbbbbb-cccccc",
Name: "Extradisk",
}

disk, _ := s.UpdateName(context.Background(), params)

assert.Equal(t, "serverdisk/updatename", c.lastPath, "correct path is used")
assert.Equal(t, 200, disk.SizeInGIB, "size is correct")
assert.Equal(t, "Extradisk", disk.Name, "correct name variable")
}

func TestServerDisk_Reconfigure(t *testing.T) {
c := &mockClient{body: `{ "response": { "disk": { "id": "aaaaa-bbbbbb-cccccc",
"sizeingib": 250,
"name": "Diskett",
"scsiid": 1
}}}`}
s := ServerDisksService{client: c}

params := EditServerDiskParams{
ID: "aaaaa-bbbbbb-cccccc",
SizeInGIB: 250,
}

disk, _ := s.Reconfigure(context.Background(), params)

assert.Equal(t, "serverdisk/reconfigure", c.lastPath, "correct path is used")
assert.Equal(t, 250, disk.SizeInGIB, "size is correct")
assert.Equal(t, "Diskett", disk.Name, "correct name variable")
}

func TestServerDisk_Delete(t *testing.T) {
c := &mockClient{}
s := ServerDisksService{client: c}

id := "aaaaa-bbbbbb-cccccc"

_ = s.Delete(context.Background(), id)

assert.Equal(t, "serverdisk/delete", c.lastPath, "correct path is used")
}

func TestServerDisk_Limits(t *testing.T) {
c := &mockClient{body: `{ "response": { "limits": { "minsizeingib": 10,
"maxsizeingib": 1024,
"currentnumdisks": 1,
"maxnumdisks": 3
}}}`}
s := ServerDisksService{client: c}

limits, _ := s.Limits(context.Background(), "wps12345")

assert.Equal(t, "serverdisk/limits", c.lastPath, "correct path is used")
assert.Equal(t, 1024, limits.MaxSizeInGIB, "max size is correct")
assert.Equal(t, 3, limits.MaxNumDisks, "max number of disks correct")
}
1 change: 1 addition & 0 deletions servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type User struct {

// ServerDetails is a more complete representation of a server
type ServerDetails struct {
AdditionalDisks []ServerDiskDetails `json:"additionaldisks,omitempty"`
CPU int `json:"cpucores"`
Backup ServerBackupDetails `json:"backup,omitempty"`
Bandwidth int `json:"bandwidth"`
Expand Down

0 comments on commit b79d699

Please sign in to comment.