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

feat(iso): extend ISOClient by AllWithOpts method #254

Merged
merged 1 commit into from
May 5, 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
8 changes: 5 additions & 3 deletions hcloud/iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,12 @@ func (c *ISOClient) List(ctx context.Context, opts ISOListOpts) ([]*ISO, *Respon

// All returns all ISOs.
func (c *ISOClient) All(ctx context.Context) ([]*ISO, error) {
allISOs := []*ISO{}
return c.AllWithOpts(ctx, ISOListOpts{ListOpts: ListOpts{PerPage: 50}})
}

opts := ISOListOpts{}
opts.PerPage = 50
// AllWithOpts returns all ISOs for the given options.
func (c *ISOClient) AllWithOpts(ctx context.Context, opts ISOListOpts) ([]*ISO, error) {
allISOs := make([]*ISO, 0)

err := c.client.all(func(page int) (*Response, error) {
opts.Page = page
Expand Down
61 changes: 61 additions & 0 deletions hcloud/iso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,65 @@ func TestISOClient(t *testing.T) {
t.Errorf("unexpected isos")
}
})

t.Run("AllWithOpts", func(t *testing.T) {
env := newTestEnv()
defer env.Teardown()

env.Mux.HandleFunc("/isos", func(w http.ResponseWriter, r *http.Request) {
if name := r.URL.Query().Get("name"); name != "my-iso" {
t.Errorf("unexpected name: %s", name)
}
w.Header().Set("Content-Type", "application/json")
var isos []schema.ISO
var meta schema.Meta
if name := r.URL.Query().Get("page"); name == "1" {
isos = []schema.ISO{
{ID: 1},
{ID: 2},
}
meta = schema.Meta{
Pagination: &schema.MetaPagination{
Page: 1,
NextPage: 2,
LastPage: 2,
PerPage: 2,
TotalEntries: 3,
},
}
} else {
isos = []schema.ISO{
{ID: 3},
}
meta = schema.Meta{
Pagination: &schema.MetaPagination{
Page: 2,
LastPage: 2,
PerPage: 2,
TotalEntries: 3,
},
}
}
json.NewEncoder(w).Encode(struct {
ISOs []schema.ISO `json:"isos"`
Meta schema.Meta `json:"meta"`
}{
ISOs: isos,
Meta: meta,
})
})

ctx := context.Background()
opts := ISOListOpts{Name: "my-iso"}
isos, err := env.Client.ISO.AllWithOpts(ctx, opts)
if err != nil {
t.Fatal(err)
}
if len(isos) != 3 {
t.Fatalf("expected 3 isos; got %d", len(isos))
}
if isos[0].ID != 1 || isos[1].ID != 2 || isos[2].ID != 3 {
t.Errorf("unexpected isos")
}
})
}