Skip to content

Commit

Permalink
Merge pull request #188 from civo/fix/default-instance-image
Browse files Browse the repository at this point in the history
fix the default instance diskimage by setting the highest available ubuntu version
  • Loading branch information
alessandroargentieri committed Mar 12, 2024
2 parents b9e232a + ad48842 commit 7bf7f85
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
29 changes: 29 additions & 0 deletions disk_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"errors"
"fmt"
"strings"

"golang.org/x/mod/semver"
)

// DiskImage represents a DiskImage for launching instances from
Expand Down Expand Up @@ -105,3 +107,30 @@ func (c *Client) GetDiskImageByName(name string) (*DiskImage, error) {

return nil, errors.New("diskimage not found")
}

// GetMostRecentDistro finds the highest version of a specified distro
func (c *Client) GetMostRecentDistro(name string) (*DiskImage, error) {
resp, err := c.ListDiskImages()
if err != nil {
return nil, decodeError(err)
}

var highestVersionDistro *DiskImage

for _, diskimage := range resp {
if strings.Contains(diskimage.Name, name) {
if highestVersionDistro == nil {
highestVersionDistro = &diskimage
} else {
if semver.Compare(highestVersionDistro.Version, diskimage.Version) < 0 {
highestVersionDistro = &diskimage
}
}
}
}
if highestVersionDistro == nil {
return nil, fmt.Errorf("%s image not found", name)
}

return highestVersionDistro, nil
}
17 changes: 17 additions & 0 deletions disk_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,20 @@ func TestGetDiskImageByName(t *testing.T) {
t.Errorf("Expected %s, got %s", "329d473e-f110-4852-b2fa-fe65aa6bff4a", got.ID)
}
}

func TestGetMostRecentDistro(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/disk_images": `[{ "ID": "329d473e-f110-4852-b2fa-fe65aa6bff4a", "Name": "ubuntu-bionic", "Version": "18.04", "State": "available", "Distribution": "ubuntu", "Description": "", "Label": "bionic" }, { "ID": "77bea4dd-bfd4-492c-823d-f92eb6dd962d", "Name": "ubuntu-focal", "Version": "20.04", "State": "available", "Distribution": "ubuntu", "Description": "", "Label": "focal" }]`,
})
defer server.Close()

got, err := client.GetMostRecentDistro("ubuntu")
if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}

if got.Name != "ubuntu-focal" {
t.Errorf("Expected %s, got %s", "ubuntu-focal", got.Name)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
golang.org/x/mod v0.16.0
golang.org/x/net v0.8.0 // indirect
golang.org/x/text v0.8.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand All @@ -64,7 +66,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
2 changes: 1 addition & 1 deletion instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (c *Client) NewInstanceConfig() (*InstanceConfig, error) {
return nil, decodeError(err)
}

diskimage, err := c.GetDiskImageByName("ubuntu-focal")
diskimage, err := c.GetMostRecentDistro("ubuntu")
if err != nil {
return nil, decodeError(err)
}
Expand Down

0 comments on commit 7bf7f85

Please sign in to comment.