Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type to the instance size #167

Merged
merged 2 commits into from
Oct 1, 2023
Merged
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
11 changes: 4 additions & 7 deletions fake_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,22 +203,19 @@ func NewFakeClient() (*FakeClient, error) {
},
InstanceSizes: []InstanceSize{
{
ID: "g3.xsmall",
Name: "Extra small",
Name: "g3.xsmall",
CPUCores: 1,
RAMMegabytes: 1024,
DiskGigabytes: 10,
},
{
ID: "g3.small",
Name: "Small",
Name: "g3.small",
CPUCores: 2,
RAMMegabytes: 2048,
DiskGigabytes: 20,
},
{
ID: "g3.medium",
Name: "Medium",
Name: "g3.medium",
CPUCores: 4,
RAMMegabytes: 4096,
DiskGigabytes: 40,
Expand Down Expand Up @@ -695,7 +692,7 @@ func (c *FakeClient) ListInstanceSizes() ([]InstanceSize, error) {
// FindInstanceSizes implemented in a fake way for automated tests
func (c *FakeClient) FindInstanceSizes(search string) (*InstanceSize, error) {
for _, size := range c.InstanceSizes {
if strings.Contains(size.Name, search) || size.ID == search {
if strings.Contains(size.Name, search) {
return &size, nil
}
}
Expand Down
9 changes: 6 additions & 3 deletions instance_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (

// InstanceSize represents an available size for instances to launch
type InstanceSize struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
NiceName string `json:"nice_name,omitempty"`
CPUCores int `json:"cpu_cores,omitempty"`
GPUCount int `json:"gpu_count,omitempty"`
GPUType string `json:"gpu_type,omitempty"`
RAMMegabytes int `json:"ram_mb,omitempty"`
DiskGigabytes int `json:"disk_gb,omitempty"`
TransferTerabytes int `json:"transfer_tb,omitempty"`
Expand All @@ -21,6 +23,7 @@ type InstanceSize struct {
}

// ListInstanceSizes returns all availble sizes of instances
// TODO: Rename to Size because this return all size (k8s, vm, database, kfaas)
func (c *Client) ListInstanceSizes() ([]InstanceSize, error) {
resp, err := c.SendGetRequest("/v2/sizes")
if err != nil {
Expand All @@ -47,10 +50,10 @@ func (c *Client) FindInstanceSizes(search string) (*InstanceSize, error) {
result := InstanceSize{}

for _, value := range instanceSize {
if value.Name == search || value.ID == search {
if value.Name == search {
exactMatch = true
result = value
} else if strings.Contains(value.Name, search) || strings.Contains(value.ID, search) {
} else if strings.Contains(value.Name, search) {
if !exactMatch {
result = value
partialMatchesCount++
Expand Down
116 changes: 51 additions & 65 deletions instance_size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ func TestListInstanceSizes(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/sizes": `[
{
"id": "d6b170f2-d2b3-4205-84c4-61898622393d",
"name": "micro",
"nice_name": "Micro",
"cpu_cores": 1,
"ram_mb": 1024,
"disk_gb": 25,
"description": "Micro - 1GB RAM, 1 CPU Core, 25GB SSD Disk",
"selectable": true
}
"type": "Instance",
"name": "g4s.xsmall",
"nice_name": "xSmall - Standard",
"cpu_cores": 1,
"gpu_count": 0,
"gpu_type": "",
"ram_mb": 1024,
"disk_gb": 25,
"transfer_tb": 1,
"description": "xSmall - Standard",
"selectable": true
}
]
`,
})
Expand All @@ -27,17 +30,14 @@ func TestListInstanceSizes(t *testing.T) {
t.Errorf("Request returned an error: %s", err)
return
}
if got[0].ID != "d6b170f2-d2b3-4205-84c4-61898622393d" {
t.Errorf("Expected %s, got %s", "d6b170f2-d2b3-4205-84c4-61898622393d", got[0].ID)
if got[0].Name != "g4s.xsmall" {
t.Errorf("Expected %s, got %s", "g4s.xsmall", got[0].Name)
}
if got[0].Name != "micro" {
t.Errorf("Expected %s, got %s", "micro", got[0].Name)
if got[0].Type != "Instance" {
t.Errorf("Expected %s, got %s", "Instance", got[0].Type)
}
if got[0].NiceName != "Micro" {
t.Errorf("Expected %s, got %s", "Micro", got[0].NiceName)
}
if got[0].NiceName != "Micro" {
t.Errorf("Expected %s, got %s", "Micro", got[0].NiceName)
if got[0].NiceName != "xSmall - Standard" {
t.Errorf("Expected %s, got %s", "xSmall - Standard", got[0].NiceName)
}
if !got[0].Selectable {
t.Errorf("Expected first result to be selectable")
Expand All @@ -51,66 +51,52 @@ func TestListInstanceSizes(t *testing.T) {
if got[0].DiskGigabytes != 25 {
t.Errorf("Expected %d, got %d", 25, got[0].DiskGigabytes)
}
if got[0].Description != "Micro - 1GB RAM, 1 CPU Core, 25GB SSD Disk" {
t.Errorf("Expected %s, got %s", "Micro - 1GB RAM, 1 CPU Core, 25GB SSD Disk", got[0].Description)
if got[0].Description != "xSmall - Standard" {
t.Errorf("Expected %s, got %s", "xSmall - Standard", got[0].Description)
}
}

func TestFindInstanceSizes(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/sizes": `[
{
"id": "d6b170f2-d2b3-4205-84c4-61898622393d",
"name": "debian-10",
"nice_name": "Debian 10",
"cpu_cores": 1,
"ram_mb": 1024,
"disk_gb": 25,
"description": "Micro - 1GB RAM, 1 CPU Core, 25GB SSD Disk",
"selectable": true
},
{
"id": "456780f2-d3b3-345-84c4-12345622393d",
"name": "debian-9",
"nice_name": "Debian 9",
"cpu_cores": 1,
"ram_mb": 2024,
"disk_gb": 50,
"description": "Debian - 2GB RAM, 1 CPU Core, 50GB SSD Disk",
"selectable": true
}
"type": "Instance",
"name": "g3.xsmall",
"nice_name": "Extra Small",
"cpu_cores": 1,
"gpu_count": 0,
"gpu_type": "",
"ram_mb": 1024,
"disk_gb": 25,
"transfer_tb": 1,
"description": "Extra Small",
"selectable": true
},
{
"type": "Instance",
"name": "g3.small",
"nice_name": "Small",
"cpu_cores": 1,
"gpu_count": 0,
"gpu_type": "",
"ram_mb": 2048,
"disk_gb": 25,
"transfer_tb": 2,
"description": "Small",
"selectable": true
}
]
`,
})
defer server.Close()

got, _ := client.FindInstanceSizes("456780f2")
if got.ID != "456780f2-d3b3-345-84c4-12345622393d" {
t.Errorf("Expected %s, got %s", "456780f2-d3b3-345-84c4-12345622393d", got.ID)
}

got, _ = client.FindInstanceSizes("d6b170")
if got.ID != "d6b170f2-d2b3-4205-84c4-61898622393d" {
t.Errorf("Expected %s, got %s", "d6b170f2-d2b3-4205-84c4-61898622393d", got.ID)
}

got, _ = client.FindInstanceSizes("debian-1")
if got.ID != "d6b170f2-d2b3-4205-84c4-61898622393d" {
t.Errorf("Expected %s, got %s", "d6b170f2-d2b3-4205-84c4-61898622393d", got.ID)
}

got, _ = client.FindInstanceSizes("debian-9")
if got.ID != "456780f2-d3b3-345-84c4-12345622393d" {
t.Errorf("Expected %s, got %s", "456780f2-d3b3-345-84c4-12345622393d", got.ID)
}

_, err := client.FindInstanceSizes("debian")
if err.Error() != "MultipleMatchesError: unable to find debian because there were multiple matches" {
t.Errorf("Expected %s, got %s", "unable to find com because there were multiple matches", err.Error())
got, _ := client.FindInstanceSizes("g3.small")
if got.Name != "g3.small" {
t.Errorf("Expected %s, got %s", "g3.small", got.Name)
}

_, err = client.FindInstanceSizes("missing")
if err.Error() != "ZeroMatchesError: unable to find missing, zero matches" {
t.Errorf("Expected %s, got %s", "unable to find missing, zero matches", err.Error())
got, _ = client.FindInstanceSizes("xsmall")
if got.Name != "g3.xsmall" {
t.Errorf("Expected %s, got %s", "g3.xsmall", got.Name)
}
}