From ad4884298321e3e536bf200d4529222edce17b97 Mon Sep 17 00:00:00 2001 From: alessandroargentieri Date: Tue, 12 Mar 2024 13:36:15 +0100 Subject: [PATCH] fix the default instance diskimage by setting the highest available ubuntu version instead of the fixed ubuntu-focal Signed-off-by: alessandroargentieri --- disk_image.go | 29 +++++++++++++++++++++++++++++ disk_image_test.go | 17 +++++++++++++++++ go.mod | 1 + go.sum | 4 +++- instance.go | 2 +- 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/disk_image.go b/disk_image.go index aad3949..54bec45 100644 --- a/disk_image.go +++ b/disk_image.go @@ -6,6 +6,8 @@ import ( "errors" "fmt" "strings" + + "golang.org/x/mod/semver" ) // DiskImage represents a DiskImage for launching instances from @@ -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 +} diff --git a/disk_image_test.go b/disk_image_test.go index 405ea7f..4f474bf 100644 --- a/disk_image_test.go +++ b/disk_image_test.go @@ -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) + } +} diff --git a/go.mod b/go.mod index ddc86e8..574147e 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 31b5464..d112150 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/instance.go b/instance.go index d57f0ff..2b1b0df 100644 --- a/instance.go +++ b/instance.go @@ -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) }