Skip to content

Commit

Permalink
feat(exp): add actionutil.AllForResource
Browse files Browse the repository at this point in the history
Allow the users to fetch actions related to a specific resource.
  • Loading branch information
jooola committed Aug 28, 2024
1 parent 743eabb commit 824cb91
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
28 changes: 27 additions & 1 deletion hcloud/exp/actionutil/actions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package actionutil

import "github.com/hetznercloud/hcloud-go/v2/hcloud"
import (
"context"
"slices"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

// AppendNext return the action and the next actions in a new slice.
func AppendNext(action *hcloud.Action, nextActions []*hcloud.Action) []*hcloud.Action {
Expand All @@ -9,3 +14,24 @@ func AppendNext(action *hcloud.Action, nextActions []*hcloud.Action) []*hcloud.A
all = append(all, nextActions...)
return all
}

func AllForResource(
ctx context.Context,
actionClient *hcloud.ResourceActionClient,
opts hcloud.ActionListOpts,
resourceType hcloud.ActionResourceType,
resourceID int64,
) ([]*hcloud.Action, error) {
actions, err := actionClient.All(ctx, opts)
if err != nil {
return nil, err
}

actions = slices.Clip(slices.DeleteFunc(actions, func(action *hcloud.Action) bool {
return !slices.ContainsFunc(action.Resources, func(resource *hcloud.ActionResource) bool {
return resource.Type == resourceType && resource.ID == resourceID
})
}))

return actions, nil
}
32 changes: 32 additions & 0 deletions hcloud/exp/actionutil/actions_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package actionutil

import (
"context"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"

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

func TestAppendNext(t *testing.T) {
Expand All @@ -16,3 +20,31 @@ func TestAppendNext(t *testing.T) {

assert.Equal(t, []*hcloud.Action{{ID: 1}, {ID: 2}, {ID: 3}}, actions)
}

func TestAllForResource(t *testing.T) {
ctx := context.Background()

server := httptest.NewServer(mockutil.Handler(t, []mockutil.Request{
{
Method: "GET", Path: "/firewalls/actions?page=1&status=running",
Status: 200,
JSON: schema.ActionListResponse{
Actions: []schema.Action{
{Resources: []schema.ActionResourceReference{{Type: "server", ID: 8}}},
{Resources: []schema.ActionResourceReference{{Type: "server", ID: 8}, {Type: "firewall", ID: 10}}},
{Resources: []schema.ActionResourceReference{{Type: "server", ID: 10}}},
{Resources: []schema.ActionResourceReference{{Type: "firewall", ID: 8}}},
},
},
},
}))
client := hcloud.NewClient(hcloud.WithEndpoint(server.URL))

actions, err := AllForResource(ctx,
client.Firewall.Action,
hcloud.ActionListOpts{Status: []hcloud.ActionStatus{hcloud.ActionStatusRunning}},
hcloud.ActionResourceTypeServer, 8,
)
assert.NoError(t, err)
assert.Len(t, actions, 2)
}

0 comments on commit 824cb91

Please sign in to comment.