Skip to content

Commit

Permalink
Implement server/listiso and server/mountiso
Browse files Browse the repository at this point in the history
Add functions for listing and mounting ISO files to Servers
  • Loading branch information
norrland committed Sep 26, 2024
1 parent 8465d31 commit 49993da
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## Unreleased
### Added
- Implemented `server/listiso` and `server/mountiso` endpoints.
### Changed
- Add `ISOFile` attribute in `ServerDetails`.

## [8.3.1] - 2024-09-19
### Changed
Expand Down
18 changes: 18 additions & 0 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,24 @@ func ExampleServerService_List() {
}
}

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

isos, _ := client.Servers.ListISOs(context.Background(), "wps123456")

for _, iso := range *isos {
fmt.Println(iso)
}
}

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

serverDetail, _ := client.Servers.MountISO(context.Background(), "wps123456", "OpenBSD/7.4/amd64/install74.iso")

fmt.Printf("ISO Mounted: %s\n", serverDetail.ISOFile)
}

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

Expand Down
28 changes: 28 additions & 0 deletions servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type ServerDetails struct {
IPList []ServerIP `json:"iplist"`
IsRunning bool `json:"isrunning"`
IsLocked bool `json:"islocked"`
ISOFile string `json:"isofile,omitempty"`
Platform string `json:"platform"`
Memory int `json:"memorysize"`
State string `json:"state"`
Expand Down Expand Up @@ -307,6 +308,33 @@ func (s *ServerService) PreviewCloudConfig(context context.Context, params Previ
return &data.Response.Cloudconfig, err
}

// ListISOs returns a list of ISO files available for `serverID`
func (s *ServerService) ListISOs(context context.Context, serverID string) (*[]string, error) {
data := struct {
Response struct {
IsoFiles []string
}
}{}
err := s.client.post(context, "server/listiso", &data, struct {
ServerID string `json:"serverid"`
}{serverID})
return &data.Response.IsoFiles, err
}

// MountISO mounts the isoFile to the server.
func (s *ServerService) MountISO(context context.Context, serverID string, isoFile string) (*ServerDetails, error) {
data := struct {
Response struct {
Server ServerDetails
}
}{}
err := s.client.post(context, "server/mountiso", &data, struct {
ServerID string `json:"serverid"`
ISOFile string `json:"isofile"`
}{serverID, isoFile})
return &data.Response.Server, err
}

// Templates lists all supported templates per platform
func (s *ServerService) Templates(context context.Context) (*ServerPlatformTemplates, error) {
data := struct {
Expand Down
29 changes: 29 additions & 0 deletions servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,35 @@ func TestServersStop(t *testing.T) {
assert.Equal(t, "server/stop", c.lastPath, "path used is correct")
}

func TestServersListISO(t *testing.T) {
c := &mockClient{body: `{"response":{ "isofiles": ["OpenBSD/7.4/amd64/cd74.iso", "OpenBSD/7.4/amd64/install74.iso"] }}`}

s := ServerService{client: c}

isos, _ := s.ListISOs(context.Background(), "wps123456")

assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "server/listiso", c.lastPath, "path used is correct")
assert.Equal(t, "OpenBSD/7.4/amd64/cd74.iso", (*isos)[0], "iso is correct")

}

func TestServersMountISO(t *testing.T) {
c := &mockClient{body: `{ "response": { "server": { "hostname": "my-server-123",
"bandwidth": 100,
"description": "MyServer",
"templatename": "None",
"isofile": "OpenBSD/7.4/amd64/cd74.iso"
} } }`}
s := ServerService{client: c}

detail, _ := s.MountISO(context.Background(), "wps123456", "OpenBSD/7.4/amd64/cd74.iso")

assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "server/mountiso", c.lastPath, "path used is correct")
assert.Equal(t, "OpenBSD/7.4/amd64/cd74.iso", detail.ISOFile, "iso is correct")
}

func TestServersTemplates(t *testing.T) {
c := &mockClient{body: `{"response":{ "templates": { "KVM": [{"id": "ac7c05f1-4cb6-4330-a0a2-d1f2e6244b21",
"name": "AlmaLinux 8", "minimumdisksize": 5, "minimummemorysize": 512, "operatingsystem": "linux", "platform": "KVM",
Expand Down

0 comments on commit 49993da

Please sign in to comment.