Skip to content
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
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/jmattheis/goverter v1.9.3
github.com/prometheus/client_golang v1.23.2
github.com/stretchr/testify v1.11.1
github.com/vburenin/ifacemaker v1.3.0
github.com/vburenin/ifacemaker v1.3.1-0.20251209121141-a6d9756091ba
golang.org/x/crypto v0.46.0
golang.org/x/net v0.48.0
)
Expand All @@ -30,11 +30,11 @@ require (
github.com/prometheus/procfs v0.16.1 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
go.yaml.in/yaml/v2 v2.4.2 // indirect
golang.org/x/mod v0.30.0 // indirect
golang.org/x/mod v0.31.0 // indirect
golang.org/x/sync v0.19.0 // indirect
golang.org/x/sys v0.39.0 // indirect
golang.org/x/text v0.32.0 // indirect
golang.org/x/tools v0.39.0 // indirect
golang.org/x/tools v0.40.0 // indirect
google.golang.org/protobuf v1.36.8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDN
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/vburenin/ifacemaker v1.3.0 h1:X5//v/1tyORf5157wLATgP1wgquW3FUW91/OGHLRqGo=
github.com/vburenin/ifacemaker v1.3.0/go.mod h1:SxTD9m+6uBQyhd0aohV7R4iirO+l9mEoTn4nSe67vMs=
github.com/vburenin/ifacemaker v1.3.1-0.20251209121141-a6d9756091ba h1:rc8YUE3u6tgdBqjKvZCrCcqPxczqoFnu5oWofin92Yw=
github.com/vburenin/ifacemaker v1.3.1-0.20251209121141-a6d9756091ba/go.mod h1:Car3Ss6dMkORtaciIcBu5DlzKQaawLS5f4nogJjRHr0=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU=
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
golang.org/x/mod v0.30.0 h1:fDEXFVZ/fmCKProc/yAXXUijritrDzahmwwefnjoPFk=
golang.org/x/mod v0.30.0/go.mod h1:lAsf5O2EvJeSFMiBxXDki7sCgAxEUcZHXoXMKT4GJKc=
golang.org/x/mod v0.31.0 h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI=
golang.org/x/mod v0.31.0/go.mod h1:43JraMp9cGx1Rx3AqioxrbrhNsLl2l/iNAvuBkrezpg=
golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU=
golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY=
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
Expand All @@ -57,8 +57,8 @@ golang.org/x/term v0.38.0 h1:PQ5pkm/rLO6HnxFR7N2lJHOZX6Kez5Y1gDSJla6jo7Q=
golang.org/x/term v0.38.0/go.mod h1:bSEAKrOT1W+VSu9TSCMtoGEOUcKxOKgl3LE5QEF/xVg=
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
golang.org/x/tools v0.39.0 h1:ik4ho21kwuQln40uelmciQPp9SipgNDdrafrYA4TmQQ=
golang.org/x/tools v0.39.0/go.mod h1:JnefbkDPyD8UU2kI5fuf8ZX4/yUeh9W877ZeBONxUqQ=
golang.org/x/tools v0.40.0 h1:yLkxfA+Qnul4cs9QA3KnlFu0lVmd8JJfoq+E41uSutA=
golang.org/x/tools v0.40.0/go.mod h1:Ik/tzLRlbscWpqqMRjyWYDisX8bG13FrdXp3o4Sr9lc=
google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc=
google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
57 changes: 49 additions & 8 deletions hcloud/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (a *Action) Error() error {

// ActionClient is a client for the actions API.
type ActionClient struct {
action *ResourceActionClient
action *ResourceActionClient[noopResource]
}

// GetByID retrieves an action by its ID. If the action does not exist, nil is returned.
Expand Down Expand Up @@ -116,7 +116,7 @@ func (l ActionListOpts) values() url.Values {
return vals
}

// List returns a list of actions for a specific page.
// List returns a paginated list of actions.
//
// Please note that filters specified in opts are not taken into account
// when their value corresponds to their zero value or when they are empty.
Expand All @@ -143,12 +143,12 @@ func (c *ActionClient) AllWithOpts(ctx context.Context, opts ActionListOpts) ([]
}

// ResourceActionClient is a client for the actions API exposed by the resource.
type ResourceActionClient struct {
type ResourceActionClient[R actionSupporter] struct {
resource string
client *Client
}

func (c *ResourceActionClient) getBaseURL() string {
func (c *ResourceActionClient[R]) getBaseURL() string {
if c.resource == "" {
return ""
}
Expand All @@ -157,7 +157,7 @@ func (c *ResourceActionClient) getBaseURL() string {
}

// GetByID retrieves an action by its ID. If the action does not exist, nil is returned.
func (c *ResourceActionClient) GetByID(ctx context.Context, id int64) (*Action, *Response, error) {
func (c *ResourceActionClient[R]) GetByID(ctx context.Context, id int64) (*Action, *Response, error) {
opPath := c.getBaseURL() + "/actions/%d"
ctx = ctxutil.SetOpPath(ctx, opPath)

Expand All @@ -173,11 +173,11 @@ func (c *ResourceActionClient) GetByID(ctx context.Context, id int64) (*Action,
return ActionFromSchema(respBody.Action), resp, nil
}

// List returns a list of actions for a specific page.
// List returns a paginated list of actions.
//
// Please note that filters specified in opts are not taken into account
// when their value corresponds to their zero value or when they are empty.
func (c *ResourceActionClient) List(ctx context.Context, opts ActionListOpts) ([]*Action, *Response, error) {
func (c *ResourceActionClient[R]) List(ctx context.Context, opts ActionListOpts) ([]*Action, *Response, error) {
opPath := c.getBaseURL() + "/actions?%s"
ctx = ctxutil.SetOpPath(ctx, opPath)

Expand All @@ -192,7 +192,7 @@ func (c *ResourceActionClient) List(ctx context.Context, opts ActionListOpts) ([
}

// All returns all actions for the given options.
func (c *ResourceActionClient) All(ctx context.Context, opts ActionListOpts) ([]*Action, error) {
func (c *ResourceActionClient[R]) All(ctx context.Context, opts ActionListOpts) ([]*Action, error) {
if opts.ListOpts.PerPage == 0 {
opts.ListOpts.PerPage = 50
}
Expand All @@ -201,3 +201,44 @@ func (c *ResourceActionClient) All(ctx context.Context, opts ActionListOpts) ([]
return c.List(ctx, opts)
})
}

type actionSupporter interface {
pathID() (string, error)
}

// noopResource is used by the [ActionClient] to satisfy its underlying
// [ResourceActionClient] generic type.
type noopResource struct{}

func (noopResource) pathID() (string, error) { return "", nil }

// ListFor returns a paginated list of actions for the given Resource.
//
// Please note that filters specified in opts are not taken into account
// when their value corresponds to their zero value or when they are empty.
func (c *ResourceActionClient[R]) ListFor(ctx context.Context, resource R, opts ActionListOpts) ([]*Action, *Response, error) {
opPath := c.getBaseURL() + "/%s/actions?%s"
ctx = ctxutil.SetOpPath(ctx, opPath)

id, err := resource.pathID()
if err != nil {
return nil, nil, invalidArgument("resource", resource, err)
}

reqPath := fmt.Sprintf(opPath, id, opts.values().Encode())

respBody, resp, err := getRequest[schema.ActionListResponse](ctx, c.client, reqPath)
if err != nil {
return nil, resp, err
}

return allFromSchemaFunc(respBody.Actions, ActionFromSchema), resp, nil
}

// AllFor returns all actions for the given Resource.
func (c *ResourceActionClient[R]) AllFor(ctx context.Context, resource R, opts ActionListOpts) ([]*Action, error) {
return iterPages(func(page int) ([]*Action, *Response, error) {
opts.Page = page
return c.ListFor(ctx, resource, opts)
})
}
189 changes: 189 additions & 0 deletions hcloud/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package hcloud
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hetznercloud/hcloud-go/v2/hcloud/exp/mockutil"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)

Expand Down Expand Up @@ -330,3 +333,189 @@ func TestResourceActionClientAll(t *testing.T) {
t.Errorf("unexpected actions")
}
}

func TestResourceActionClientListFor(t *testing.T) {
t.Run("minimal", func(t *testing.T) {
ctx, server, client := makeTestUtils(t)

server.Expect([]mockutil.Request{
{
Method: "GET", Path: "/primary_ips/13/actions?",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772237 }
]
}`,
},
})

result, resp, err := client.PrimaryIP.Action.ListFor(ctx, &PrimaryIP{ID: 13}, ActionListOpts{})
require.NoError(t, err)
require.NotNil(t, resp)
require.Len(t, result, 1)
require.Equal(t, int64(1509772237), result[0].ID)
})

t.Run("full", func(t *testing.T) {
ctx, server, client := makeTestUtils(t)

server.Expect([]mockutil.Request{
{
Method: "GET", Path: "/primary_ips/13/actions?page=2&per_page=50&sort=asc%3Aid&status=running",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772237 }
]
}`,
},
})

result, resp, err := client.PrimaryIP.Action.ListFor(ctx, &PrimaryIP{ID: 13}, ActionListOpts{
Status: []ActionStatus{ActionStatusRunning},
Sort: []string{"asc:id"},
ListOpts: ListOpts{
Page: 2,
PerPage: 50,
},
})
require.NoError(t, err)
require.NotNil(t, resp)
require.Len(t, result, 1)
require.Equal(t, int64(1509772237), result[0].ID)
})

t.Run("string resource id", func(t *testing.T) {
ctx, server, client := makeTestUtils(t)

server.Expect([]mockutil.Request{
{
Method: "GET", Path: "/zones/example.org/actions?",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772237 }
]
}`,
},
})

result, resp, err := client.Zone.Action.ListFor(ctx, &Zone{Name: "example.org"}, ActionListOpts{})
require.NoError(t, err)
require.NotNil(t, resp)
require.Len(t, result, 1)
require.Equal(t, int64(1509772237), result[0].ID)
})

t.Run("invalid argument", func(t *testing.T) {
ctx, _, client := makeTestUtils(t)

result, resp, err := client.PrimaryIP.Action.ListFor(ctx, &PrimaryIP{ID: 0}, ActionListOpts{})
require.EqualError(t, err, "invalid argument 'resource' [*hcloud.PrimaryIP]: missing field [ID] in [*hcloud.PrimaryIP]")
require.Nil(t, result)
require.Nil(t, resp)
})
}

func TestResourceActionClientAllFor(t *testing.T) {
t.Run("minimal", func(t *testing.T) {
ctx, server, client := makeTestUtils(t)

server.Expect([]mockutil.Request{
{
Method: "GET", Path: "/primary_ips/13/actions?page=1",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772237 }
],
"meta": { "pagination": { "page": 1, "next_page": 2 }}
}`,
},
{
Method: "GET", Path: "/primary_ips/13/actions?page=2",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772238 }
],
"meta": { "pagination": { "page": 2 }}
}`,
},
})

result, err := client.PrimaryIP.Action.AllFor(ctx, &PrimaryIP{ID: 13}, ActionListOpts{})
require.NoError(t, err)
require.Len(t, result, 2)
require.Equal(t, int64(1509772237), result[0].ID)
require.Equal(t, int64(1509772238), result[1].ID)
})

t.Run("full", func(t *testing.T) {
ctx, server, client := makeTestUtils(t)

server.Expect([]mockutil.Request{
{
Method: "GET", Path: "/primary_ips/13/actions?page=1&per_page=50&sort=asc%3Aid&status=running",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772237 }
],
"meta": { "pagination": { "page": 1, "next_page": 2 }}
}`,
},
{
Method: "GET", Path: "/primary_ips/13/actions?page=2&per_page=50&sort=asc%3Aid&status=running",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772238 }
],
"meta": { "pagination": { "page": 2 }}
}`,
},
})

result, err := client.PrimaryIP.Action.AllFor(ctx, &PrimaryIP{ID: 13}, ActionListOpts{
Status: []ActionStatus{ActionStatusRunning},
Sort: []string{"asc:id"},
ListOpts: ListOpts{
PerPage: 50,
},
})
require.NoError(t, err)
require.Len(t, result, 2)
require.Equal(t, int64(1509772237), result[0].ID)
require.Equal(t, int64(1509772238), result[1].ID)
})
}

func ExampleResourceActionClient_ListFor() {
ctx := context.Background()
client := NewClient()

{
server := &Server{ID: 5425271}

// List actions for the server 5425271.
result, _, _ := client.Server.Action.ListFor(ctx, server, ActionListOpts{})
// Error handling skipped

for _, action := range result {
fmt.Println(action.ID, action.Command, action.Status)
}
}
{
zone := &Zone{Name: "example.org"}

// List actions for the zone "example.org".
result, _, _ := client.Zone.Action.ListFor(ctx, zone, ActionListOpts{})
// Error handling skipped

for _, action := range result {
fmt.Println(action.ID, action.Command, action.Status)
}
}
}
Loading
Loading