Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions vps/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,16 @@ func (r *Repository) GetOperatingSystems(vpsName string) ([]OperatingSystem, err
return response.OperatingSystems, err
}

// FilterOperatingSystems allows you to filter Operating Systems without needing a VPS
func (r *Repository) FilterOperatingSystems(productName string, addons []string) ([]OperatingSystem, error) {
var response operatingSystemsWrapper
requestBody := operatingSystemsRequest{ProductName: productName, Addons: addons}
restRequest := rest.Request{Endpoint: "/operating-systems", Body: &requestBody}
err := r.Client.Get(restRequest, &response)

return response.OperatingSystems, err
}

// InstallOperatingSystem allows you to install an operating system to a Vps,
// optionally you can specify a hostname and a base64InstallText,
// which would be the automatic installation configuration of your Vps
Expand Down
37 changes: 37 additions & 0 deletions vps/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,43 @@ func TestRepository_GetOperatingSystems(t *testing.T) {
assert.Equal(t, 1250, oses[0].Price)
}

func TestRepository_FilterOperatingSystems(t *testing.T) {
const apiResponse = `{
"operatingSystems": [
{
"name": "ubuntu-18.04",
"description": "Ubuntu 18.04 LTS",
"version": "18.04 LTS",
"price": 1250,
"installFlavours": [
"installer",
"preinstallable",
"cloudinit"
],
"licenses": []
}
]
}`
server := testutil.MockServer{T: t, ExpectedURL: "/operating-systems", ExpectedMethod: "GET", StatusCode: 200, Response: apiResponse, ExpectedRequest: `{"productName":"vps-bladevps-x8","addons":["vpsAddon-1-extra-cpu-core"]}`}
client, tearDown := server.GetClient()
defer tearDown()
repo := Repository{Client: *client}

oses, err := repo.FilterOperatingSystems("vps-bladevps-x8", []string{"vpsAddon-1-extra-cpu-core"})
require.NoError(t, err)
require.Equal(t, 1, len(oses))

assert.Equal(t, "ubuntu-18.04", oses[0].Name)
assert.Equal(t, "Ubuntu 18.04 LTS", oses[0].Description)
assert.Equal(t, false, oses[0].IsPreinstallableImage)
assert.Contains(t, oses[0].InstallFlavours, InstallFlavourInstaller)
assert.Contains(t, oses[0].InstallFlavours, InstallFlavourPreinstallable)
assert.Contains(t, oses[0].InstallFlavours, InstallFlavourCloudInit)
assert.Equal(t, "18.04 LTS", oses[0].Version)
assert.Equal(t, 1250, oses[0].Price)
assert.Equal(t, 0, len(oses[0].Licenses))
}

func TestRepository_InstallOperatingSystemOptionalFields(t *testing.T) {
const expectedRequest = `{"operatingSystemName":"ubuntu-18.04"}`
server := testutil.MockServer{T: t, ExpectedURL: "/vps/example-vps/operating-systems", ExpectedMethod: "POST", StatusCode: 201, ExpectedRequest: expectedRequest}
Expand Down
10 changes: 9 additions & 1 deletion vps/vps.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package vps

import (
"net"

"github.com/transip/gotransip/v6/ipaddress"
"github.com/transip/gotransip/v6/product"
"github.com/transip/gotransip/v6/rest"
"net"
)

// BackupStatus is one of the following strings
Expand Down Expand Up @@ -178,6 +179,13 @@ type upgradesWrapper struct {
Upgrades []product.Product `json:"upgrades"`
}

// operatingSystemsRequest struct contains a productName and an optional list of addons
// this is solely used for marshalling
type operatingSystemsRequest struct {
ProductName string `json:"productName"`
Addons []string `json:"addons,omitempty"`
}

// operatingSystemsWrapper struct contains a list with OperatingSystems in it,
// this is solely used for marshalling
type operatingSystemsWrapper struct {
Expand Down