From 2328d04b8500a8967b753804c8e1002d858a595a Mon Sep 17 00:00:00 2001 From: Ludwig Bedacht Date: Mon, 15 Jan 2024 13:54:03 +0100 Subject: [PATCH] change api function to not return a pointer to a slice --- internal/ionoscloud/client.go | 2 +- internal/ionoscloud/client/client.go | 13 +++++-- internal/ionoscloud/clienttest/mock_client.go | 14 ++++---- internal/service/cloud/network.go | 4 +-- internal/service/cloud/request.go | 36 ------------------- 5 files changed, 20 insertions(+), 49 deletions(-) delete mode 100644 internal/service/cloud/request.go diff --git a/internal/ionoscloud/client.go b/internal/ionoscloud/client.go index cb43ee16..7b8bd18c 100644 --- a/internal/ionoscloud/client.go +++ b/internal/ionoscloud/client.go @@ -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) } diff --git a/internal/ionoscloud/client/client.go b/internal/ionoscloud/client/client.go index b21914d1..59d125f4 100644 --- a/internal/ionoscloud/client/client.go +++ b/internal/ionoscloud/client/client.go @@ -297,7 +297,7 @@ 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") } @@ -305,7 +305,8 @@ func (c *IonosCloudClient) GetRequests(ctx context.Context, method, path string) 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). @@ -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. diff --git a/internal/ionoscloud/clienttest/mock_client.go b/internal/ionoscloud/clienttest/mock_client.go index c8be0776..a9960347 100644 --- a/internal/ionoscloud/clienttest/mock_client.go +++ b/internal/ionoscloud/clienttest/mock_client.go @@ -570,19 +570,19 @@ func (_c *MockClient_GetLAN_Call) RunAndReturn(run func(context.Context, string, } // GetRequests provides a mock function with given fields: ctx, method, path -func (_m *MockClient) GetRequests(ctx context.Context, method string, path string) (*[]ionoscloud.Request, error) { +func (_m *MockClient) GetRequests(ctx context.Context, method string, path string) ([]ionoscloud.Request, error) { ret := _m.Called(ctx, method, path) - var r0 *[]ionoscloud.Request + var r0 []ionoscloud.Request var r1 error - if rf, ok := ret.Get(0).(func(context.Context, string, string) (*[]ionoscloud.Request, error)); ok { + if rf, ok := ret.Get(0).(func(context.Context, string, string) ([]ionoscloud.Request, error)); ok { return rf(ctx, method, path) } - if rf, ok := ret.Get(0).(func(context.Context, string, string) *[]ionoscloud.Request); ok { + if rf, ok := ret.Get(0).(func(context.Context, string, string) []ionoscloud.Request); ok { r0 = rf(ctx, method, path) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(*[]ionoscloud.Request) + r0 = ret.Get(0).([]ionoscloud.Request) } } @@ -615,12 +615,12 @@ func (_c *MockClient_GetRequests_Call) Run(run func(ctx context.Context, method return _c } -func (_c *MockClient_GetRequests_Call) Return(_a0 *[]ionoscloud.Request, _a1 error) *MockClient_GetRequests_Call { +func (_c *MockClient_GetRequests_Call) Return(_a0 []ionoscloud.Request, _a1 error) *MockClient_GetRequests_Call { _c.Call.Return(_a0, _a1) return _c } -func (_c *MockClient_GetRequests_Call) RunAndReturn(run func(context.Context, string, string) (*[]ionoscloud.Request, error)) *MockClient_GetRequests_Call { +func (_c *MockClient_GetRequests_Call) RunAndReturn(run func(context.Context, string, string) ([]ionoscloud.Request, error)) *MockClient_GetRequests_Call { _c.Call.Return(run) return _c } diff --git a/internal/service/cloud/network.go b/internal/service/cloud/network.go index d04f72d9..4ebe099b 100644 --- a/internal/service/cloud/network.go +++ b/internal/service/cloud/network.go @@ -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 { diff --git a/internal/service/cloud/request.go b/internal/service/cloud/request.go deleted file mode 100644 index 11b4fc97..00000000 --- a/internal/service/cloud/request.go +++ /dev/null @@ -1,36 +0,0 @@ -/* -Copyright 2024 IONOS Cloud. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package cloud - -import ( - "fmt" - - sdk "github.com/ionos-cloud/sdk-go/v6" -) - -func (s *Service) findRelatedRequests(method, resourcePath string) ([]sdk.Request, error) { - requests, err := s.api().GetRequests(s.ctx, method, resourcePath) - if err != nil { - return nil, fmt.Errorf("could not get requests: %w", err) - } - - if requests == nil { - return nil, nil - } - - return *requests, nil -}