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: add per resource list actions endpoints #521

Closed
wants to merge 1 commit into from
Closed
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
65 changes: 65 additions & 0 deletions hcloud/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ func (l ActionListOpts) values() url.Values {
return vals
}

// ResourceActionListOpts specifies options for listing actions.
type ResourceActionListOpts struct {
ListOpts
Status []ActionStatus
Sort []string
}

func (l ResourceActionListOpts) values() url.Values {
vals := l.ListOpts.Values()
for _, status := range l.Status {
vals.Add("status", string(status))
}
for _, sort := range l.Sort {
vals.Add("sort", sort)
}
return vals
}

// List returns a list of actions for a specific page.
//
// Please note that filters specified in opts are not taken into account
Expand Down Expand Up @@ -179,6 +197,33 @@ func (c *ResourceActionClient) List(ctx context.Context, opts ActionListOpts) ([
return actions, resp, nil
}

// ListForResource returns a list of actions of a resource for a specific page.
//
// 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) ListForResource(ctx context.Context, resourceID int64, opts ResourceActionListOpts) ([]*Action, *Response, error) {
req, err := c.client.NewRequest(
ctx,
"GET",
fmt.Sprintf("%s/%d/actions?%s", c.getBaseURL(), resourceID, opts.values().Encode()),
nil,
)
if err != nil {
return nil, nil, err
}

var body schema.ActionListResponse
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
actions := make([]*Action, 0, len(body.Actions))
for _, i := range body.Actions {
actions = append(actions, ActionFromSchema(i))
}
return actions, resp, nil
}

// All returns all actions for the given options.
func (c *ResourceActionClient) All(ctx context.Context, opts ActionListOpts) ([]*Action, error) {
allActions := []*Action{}
Expand All @@ -198,3 +243,23 @@ func (c *ResourceActionClient) All(ctx context.Context, opts ActionListOpts) ([]

return allActions, nil
}

// AllForResource returns all actions of a resource for the given options.
func (c *ResourceActionClient) AllForResource(ctx context.Context, resourceID int64, opts ResourceActionListOpts) ([]*Action, error) {
allActions := []*Action{}

err := c.client.all(func(page int) (*Response, error) {
opts.Page = page
actions, resp, err := c.ListForResource(ctx, resourceID, opts)
if err != nil {
return resp, err
}
allActions = append(allActions, actions...)
return resp, nil
})
if err != nil {
return nil, err
}

return allActions, nil
}
Loading