Skip to content

Commit

Permalink
change api function to not return a pointer to a slice
Browse files Browse the repository at this point in the history
  • Loading branch information
lubedacht committed Jan 15, 2024
1 parent 92dab43 commit 2328d04
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 49 deletions.
2 changes: 1 addition & 1 deletion internal/ionoscloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ type Client interface {
// WaitForRequest waits for the completion of the provided request, return an error if it fails.
WaitForRequest(ctx context.Context, requestURL string) error
// GetRequests returns the requests made in the last 24 hours that match the provided method and path.
GetRequests(ctx context.Context, method, path string) (*[]ionoscloud.Request, error)
GetRequests(ctx context.Context, method, path string) ([]ionoscloud.Request, error)
}
13 changes: 10 additions & 3 deletions internal/ionoscloud/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,16 @@ func (c *IonosCloudClient) CheckRequestStatus(ctx context.Context, requestURL st
}

// GetRequests returns the requests made in the last 24 hours that match the provided method and path.
func (c *IonosCloudClient) GetRequests(ctx context.Context, method, path string) (*[]sdk.Request, error) {
func (c *IonosCloudClient) GetRequests(ctx context.Context, method, path string) ([]sdk.Request, error) {
if path == "" {
return nil, errors.New("path needs to be provided")
}
if method == "" {
return nil, errors.New("method needs to be provided")
}

lookback := time.Now().Add(-24 * time.Hour).Format(time.DateTime)
const defaultLookbackTime = 24 * time.Hour
lookback := time.Now().Add(-defaultLookbackTime).Format(time.DateTime)
reqs, _, err := c.API.RequestsApi.RequestsGet(ctx).
Depth(3).
FilterMethod(method).
Expand All @@ -315,13 +316,19 @@ func (c *IonosCloudClient) GetRequests(ctx context.Context, method, path string)
if err != nil {
return nil, fmt.Errorf("failed to get requests: %w", err)
}
if reqs.Items == nil {
// NOTE(lubedacht): This shouldn't happen, but we shouldn't deref
// a pointer without a nil check
return nil, nil
}

items := *reqs.Items
slices.SortFunc(items, func(a, b sdk.Request) int {
// We invert the value to sort in descending order
return -a.Metadata.CreatedDate.Compare(b.Metadata.CreatedDate.Time)
})

return &items, nil
return items, nil
}

// WaitForRequest waits for the completion of the provided request, return an error if it fails.
Expand Down
14 changes: 7 additions & 7 deletions internal/ionoscloud/clienttest/mock_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/service/cloud/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ func (s *Service) checkForPendingLANRequest(method string, lanID string) (status
}

lanPath := path.Join("datacenters", s.dataCenterID(), "lan")
requests, err := s.findRelatedRequests(method, lanPath)
requests, err := s.api().GetRequests(s.ctx, method, lanPath)
if err != nil {
return "", err
return "", fmt.Errorf("could not get requests: %w", err)
}

for _, r := range requests {
Expand Down
36 changes: 0 additions & 36 deletions internal/service/cloud/request.go

This file was deleted.

0 comments on commit 2328d04

Please sign in to comment.