Skip to content

Commit 08b3e9e

Browse files
authored
Add type to the instance size (#167)
* Add type to the instance size * Fix format in a test file Signed-off-by: Alejandro J. Nuñez Madrazo <[email protected]>
1 parent 69e1668 commit 08b3e9e

File tree

3 files changed

+61
-75
lines changed

3 files changed

+61
-75
lines changed

fake_client.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -203,22 +203,19 @@ func NewFakeClient() (*FakeClient, error) {
203203
},
204204
InstanceSizes: []InstanceSize{
205205
{
206-
ID: "g3.xsmall",
207-
Name: "Extra small",
206+
Name: "g3.xsmall",
208207
CPUCores: 1,
209208
RAMMegabytes: 1024,
210209
DiskGigabytes: 10,
211210
},
212211
{
213-
ID: "g3.small",
214-
Name: "Small",
212+
Name: "g3.small",
215213
CPUCores: 2,
216214
RAMMegabytes: 2048,
217215
DiskGigabytes: 20,
218216
},
219217
{
220-
ID: "g3.medium",
221-
Name: "Medium",
218+
Name: "g3.medium",
222219
CPUCores: 4,
223220
RAMMegabytes: 4096,
224221
DiskGigabytes: 40,
@@ -695,7 +692,7 @@ func (c *FakeClient) ListInstanceSizes() ([]InstanceSize, error) {
695692
// FindInstanceSizes implemented in a fake way for automated tests
696693
func (c *FakeClient) FindInstanceSizes(search string) (*InstanceSize, error) {
697694
for _, size := range c.InstanceSizes {
698-
if strings.Contains(size.Name, search) || size.ID == search {
695+
if strings.Contains(size.Name, search) {
699696
return &size, nil
700697
}
701698
}

instance_size.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99

1010
// InstanceSize represents an available size for instances to launch
1111
type InstanceSize struct {
12-
ID string `json:"id,omitempty"`
12+
Type string `json:"type,omitempty"`
1313
Name string `json:"name,omitempty"`
1414
NiceName string `json:"nice_name,omitempty"`
1515
CPUCores int `json:"cpu_cores,omitempty"`
16+
GPUCount int `json:"gpu_count,omitempty"`
17+
GPUType string `json:"gpu_type,omitempty"`
1618
RAMMegabytes int `json:"ram_mb,omitempty"`
1719
DiskGigabytes int `json:"disk_gb,omitempty"`
1820
TransferTerabytes int `json:"transfer_tb,omitempty"`
@@ -21,6 +23,7 @@ type InstanceSize struct {
2123
}
2224

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

4952
for _, value := range instanceSize {
50-
if value.Name == search || value.ID == search {
53+
if value.Name == search {
5154
exactMatch = true
5255
result = value
53-
} else if strings.Contains(value.Name, search) || strings.Contains(value.ID, search) {
56+
} else if strings.Contains(value.Name, search) {
5457
if !exactMatch {
5558
result = value
5659
partialMatchesCount++

instance_size_test.go

+51-65
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ func TestListInstanceSizes(t *testing.T) {
88
client, server, _ := NewClientForTesting(map[string]string{
99
"/v2/sizes": `[
1010
{
11-
"id": "d6b170f2-d2b3-4205-84c4-61898622393d",
12-
"name": "micro",
13-
"nice_name": "Micro",
14-
"cpu_cores": 1,
15-
"ram_mb": 1024,
16-
"disk_gb": 25,
17-
"description": "Micro - 1GB RAM, 1 CPU Core, 25GB SSD Disk",
18-
"selectable": true
19-
}
11+
"type": "Instance",
12+
"name": "g4s.xsmall",
13+
"nice_name": "xSmall - Standard",
14+
"cpu_cores": 1,
15+
"gpu_count": 0,
16+
"gpu_type": "",
17+
"ram_mb": 1024,
18+
"disk_gb": 25,
19+
"transfer_tb": 1,
20+
"description": "xSmall - Standard",
21+
"selectable": true
22+
}
2023
]
2124
`,
2225
})
@@ -27,17 +30,14 @@ func TestListInstanceSizes(t *testing.T) {
2730
t.Errorf("Request returned an error: %s", err)
2831
return
2932
}
30-
if got[0].ID != "d6b170f2-d2b3-4205-84c4-61898622393d" {
31-
t.Errorf("Expected %s, got %s", "d6b170f2-d2b3-4205-84c4-61898622393d", got[0].ID)
33+
if got[0].Name != "g4s.xsmall" {
34+
t.Errorf("Expected %s, got %s", "g4s.xsmall", got[0].Name)
3235
}
33-
if got[0].Name != "micro" {
34-
t.Errorf("Expected %s, got %s", "micro", got[0].Name)
36+
if got[0].Type != "Instance" {
37+
t.Errorf("Expected %s, got %s", "Instance", got[0].Type)
3538
}
36-
if got[0].NiceName != "Micro" {
37-
t.Errorf("Expected %s, got %s", "Micro", got[0].NiceName)
38-
}
39-
if got[0].NiceName != "Micro" {
40-
t.Errorf("Expected %s, got %s", "Micro", got[0].NiceName)
39+
if got[0].NiceName != "xSmall - Standard" {
40+
t.Errorf("Expected %s, got %s", "xSmall - Standard", got[0].NiceName)
4141
}
4242
if !got[0].Selectable {
4343
t.Errorf("Expected first result to be selectable")
@@ -51,66 +51,52 @@ func TestListInstanceSizes(t *testing.T) {
5151
if got[0].DiskGigabytes != 25 {
5252
t.Errorf("Expected %d, got %d", 25, got[0].DiskGigabytes)
5353
}
54-
if got[0].Description != "Micro - 1GB RAM, 1 CPU Core, 25GB SSD Disk" {
55-
t.Errorf("Expected %s, got %s", "Micro - 1GB RAM, 1 CPU Core, 25GB SSD Disk", got[0].Description)
54+
if got[0].Description != "xSmall - Standard" {
55+
t.Errorf("Expected %s, got %s", "xSmall - Standard", got[0].Description)
5656
}
5757
}
5858

5959
func TestFindInstanceSizes(t *testing.T) {
6060
client, server, _ := NewClientForTesting(map[string]string{
6161
"/v2/sizes": `[
6262
{
63-
"id": "d6b170f2-d2b3-4205-84c4-61898622393d",
64-
"name": "debian-10",
65-
"nice_name": "Debian 10",
66-
"cpu_cores": 1,
67-
"ram_mb": 1024,
68-
"disk_gb": 25,
69-
"description": "Micro - 1GB RAM, 1 CPU Core, 25GB SSD Disk",
70-
"selectable": true
71-
},
72-
{
73-
"id": "456780f2-d3b3-345-84c4-12345622393d",
74-
"name": "debian-9",
75-
"nice_name": "Debian 9",
76-
"cpu_cores": 1,
77-
"ram_mb": 2024,
78-
"disk_gb": 50,
79-
"description": "Debian - 2GB RAM, 1 CPU Core, 50GB SSD Disk",
80-
"selectable": true
81-
}
63+
"type": "Instance",
64+
"name": "g3.xsmall",
65+
"nice_name": "Extra Small",
66+
"cpu_cores": 1,
67+
"gpu_count": 0,
68+
"gpu_type": "",
69+
"ram_mb": 1024,
70+
"disk_gb": 25,
71+
"transfer_tb": 1,
72+
"description": "Extra Small",
73+
"selectable": true
74+
},
75+
{
76+
"type": "Instance",
77+
"name": "g3.small",
78+
"nice_name": "Small",
79+
"cpu_cores": 1,
80+
"gpu_count": 0,
81+
"gpu_type": "",
82+
"ram_mb": 2048,
83+
"disk_gb": 25,
84+
"transfer_tb": 2,
85+
"description": "Small",
86+
"selectable": true
87+
}
8288
]
8389
`,
8490
})
8591
defer server.Close()
8692

87-
got, _ := client.FindInstanceSizes("456780f2")
88-
if got.ID != "456780f2-d3b3-345-84c4-12345622393d" {
89-
t.Errorf("Expected %s, got %s", "456780f2-d3b3-345-84c4-12345622393d", got.ID)
90-
}
91-
92-
got, _ = client.FindInstanceSizes("d6b170")
93-
if got.ID != "d6b170f2-d2b3-4205-84c4-61898622393d" {
94-
t.Errorf("Expected %s, got %s", "d6b170f2-d2b3-4205-84c4-61898622393d", got.ID)
95-
}
96-
97-
got, _ = client.FindInstanceSizes("debian-1")
98-
if got.ID != "d6b170f2-d2b3-4205-84c4-61898622393d" {
99-
t.Errorf("Expected %s, got %s", "d6b170f2-d2b3-4205-84c4-61898622393d", got.ID)
100-
}
101-
102-
got, _ = client.FindInstanceSizes("debian-9")
103-
if got.ID != "456780f2-d3b3-345-84c4-12345622393d" {
104-
t.Errorf("Expected %s, got %s", "456780f2-d3b3-345-84c4-12345622393d", got.ID)
105-
}
106-
107-
_, err := client.FindInstanceSizes("debian")
108-
if err.Error() != "MultipleMatchesError: unable to find debian because there were multiple matches" {
109-
t.Errorf("Expected %s, got %s", "unable to find com because there were multiple matches", err.Error())
93+
got, _ := client.FindInstanceSizes("g3.small")
94+
if got.Name != "g3.small" {
95+
t.Errorf("Expected %s, got %s", "g3.small", got.Name)
11096
}
11197

112-
_, err = client.FindInstanceSizes("missing")
113-
if err.Error() != "ZeroMatchesError: unable to find missing, zero matches" {
114-
t.Errorf("Expected %s, got %s", "unable to find missing, zero matches", err.Error())
98+
got, _ = client.FindInstanceSizes("xsmall")
99+
if got.Name != "g3.xsmall" {
100+
t.Errorf("Expected %s, got %s", "g3.xsmall", got.Name)
115101
}
116102
}

0 commit comments

Comments
 (0)