Skip to content

Commit 75b6ace

Browse files
committed
add iso
1 parent 8f35cd3 commit 75b6ace

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

hcloud/iso.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ func (c *ISOClient) GetByName(ctx context.Context, name string) (*ISO, *Response
7171
// Get retrieves an ISO by its ID if the input can be parsed as an integer, otherwise it retrieves an ISO by its name.
7272
func (c *ISOClient) Get(ctx context.Context, idOrName string) (*ISO, *Response, error) {
7373
if id, err := strconv.ParseInt(idOrName, 10, 64); err == nil {
74-
return c.GetByID(ctx, id)
74+
iso, res, err := c.GetByID(ctx, id)
75+
if iso != nil || err != nil {
76+
return iso, res, err
77+
}
7578
}
7679
return c.GetByName(ctx, idOrName)
7780
}

hcloud/iso_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,47 @@ func TestISOClient(t *testing.T) {
134134
})
135135
})
136136

137+
t.Run("GetByNumericName", func(t *testing.T) {
138+
env := newTestEnv()
139+
defer env.Teardown()
140+
141+
env.Mux.HandleFunc("/isos/123", func(w http.ResponseWriter, r *http.Request) {
142+
w.Header().Set("Content-Type", "application/json")
143+
w.WriteHeader(http.StatusNotFound)
144+
json.NewEncoder(w).Encode(schema.ErrorResponse{
145+
Error: schema.Error{
146+
Code: string(ErrorCodeNotFound),
147+
},
148+
})
149+
})
150+
151+
env.Mux.HandleFunc("/isos", func(w http.ResponseWriter, r *http.Request) {
152+
if r.URL.RawQuery != "name=123" {
153+
t.Fatal("missing name query")
154+
}
155+
json.NewEncoder(w).Encode(schema.ISOListResponse{
156+
ISOs: []schema.ISO{
157+
{
158+
ID: 1,
159+
},
160+
},
161+
})
162+
})
163+
164+
ctx := context.Background()
165+
166+
iso, _, err := env.Client.ISO.Get(ctx, "123")
167+
if err != nil {
168+
t.Fatal(err)
169+
}
170+
if iso == nil {
171+
t.Fatal("no iso")
172+
}
173+
if iso.ID != 1 {
174+
t.Errorf("unexpected iso ID: %v", iso.ID)
175+
}
176+
})
177+
137178
t.Run("GetByName (not found)", func(t *testing.T) {
138179
env := newTestEnv()
139180
defer env.Teardown()

0 commit comments

Comments
 (0)