From a8d582b236e1d2e426668f288f2dd9d93b53572b Mon Sep 17 00:00:00 2001 From: Lukas Hoehl Date: Tue, 4 Nov 2025 14:45:09 +0100 Subject: [PATCH] idea request wrapper Signed-off-by: Lukas Hoehl --- pkg/stackit/client.go | 27 +++++++++++++++++++++ pkg/stackit/snapshots.go | 51 ++++++++++++++++++++++------------------ 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/pkg/stackit/client.go b/pkg/stackit/client.go index b08a942e..329002a3 100644 --- a/pkg/stackit/client.go +++ b/pkg/stackit/client.go @@ -26,6 +26,7 @@ import ( sdkconfig "github.com/stackitcloud/stackit-sdk-go/core/config" oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" + "github.com/stackitcloud/stackit-sdk-go/core/runtime" "github.com/stackitcloud/stackit-sdk-go/services/iaas" "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer" "gopkg.in/gcfg.v1" @@ -36,6 +37,9 @@ import ( "github.com/spf13/pflag" "k8s.io/klog/v2" + sdkWait "github.com/stackitcloud/stackit-sdk-go/services/iaas/wait" + + stackiterrors "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/errors" "github.com/stackitcloud/cloud-provider-stackit/pkg/stackit/metadata" ) @@ -214,3 +218,26 @@ func isOpenAPINotFound(err error) bool { func IsNotFound(err error) bool { return errors.Is(err, ErrorNotFound) } + +func request(ctx context.Context, reqFunc func(ctx context.Context) error) error { + _, err := request2(ctx, func(ctx context.Context) (any, error) { + err := reqFunc(ctx) + return nil, err + }) + return err +} + +func request2[T any](ctx context.Context, req2Func func(ctx context.Context) (T, error)) (T, error) { + var httpResp *http.Response + ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(ctx, &httpResp) + + resp, err := req2Func(ctxWithHTTPResp) + if err != nil { + if httpResp != nil { + reqID := httpResp.Header.Get(sdkWait.XRequestIDHeader) + return resp, stackiterrors.WrapErrorWithResponseID(err, reqID) + } + return resp, err + } + return resp, nil +} diff --git a/pkg/stackit/snapshots.go b/pkg/stackit/snapshots.go index 94ada6f0..d9dc5410 100644 --- a/pkg/stackit/snapshots.go +++ b/pkg/stackit/snapshots.go @@ -33,18 +33,21 @@ func (os *iaasClient) CreateSnapshot(ctx context.Context, name, volID string, ta if tags != nil { opts.Labels = ptr.To(map[string]interface{}(labelsFromTags(tags))) } - var httpResp *http.Response - ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(ctx, &httpResp) - snapshot, err := os.iaas.CreateSnapshot(ctxWithHTTPResp, os.projectID, os.region).CreateSnapshotPayload(opts).Execute() - if err != nil { - if httpResp != nil { - reqID := httpResp.Header.Get(sdkWait.XRequestIDHeader) - return nil, stackiterrors.WrapErrorWithResponseID(err, reqID) - } - return nil, err - } - - return snapshot, nil + return request2(ctx, func(ctx context.Context) (*iaas.Snapshot, error) { + return os.iaas.CreateSnapshot(ctx, os.projectID, os.region).CreateSnapshotPayload(opts).Execute() + }) + // var httpResp *http.Response + // ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(ctx, &httpResp) + // snapshot, err := os.iaas.CreateSnapshot(ctxWithHTTPResp, os.projectID, os.region).CreateSnapshotPayload(opts).Execute() + // if err != nil { + // if httpResp != nil { + // reqID := httpResp.Header.Get(sdkWait.XRequestIDHeader) + // return nil, stackiterrors.WrapErrorWithResponseID(err, reqID) + // } + // return nil, err + // } + // + // return snapshot, nil } func (os *iaasClient) ListSnapshots(ctx context.Context, filters map[string]string) ([]iaas.Snapshot, string, error) { @@ -65,17 +68,19 @@ func (os *iaasClient) ListSnapshots(ctx context.Context, filters map[string]stri } func (os *iaasClient) DeleteSnapshot(ctx context.Context, snapID string) error { - var httpResp *http.Response - ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(ctx, &httpResp) - err := os.iaas.DeleteSnapshotExecute(ctxWithHTTPResp, os.projectID, os.region, snapID) - if err != nil { - if httpResp != nil { - reqID := httpResp.Header.Get(sdkWait.XRequestIDHeader) - return stackiterrors.WrapErrorWithResponseID(err, reqID) - } - return err - } - return nil + // var httpResp *http.Response + // ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(ctx, &httpResp) + return request(ctx, func(ctx context.Context) error { + return os.iaas.DeleteSnapshotExecute(ctx, os.projectID, os.region, snapID) + }) + // if err != nil { + // if httpResp != nil { + // reqID := httpResp.Header.Get(sdkWait.XRequestIDHeader) + // return stackiterrors.WrapErrorWithResponseID(err, reqID) + // } + // return err + // } + // return nil } func (os *iaasClient) GetSnapshotByID(ctx context.Context, snapshotID string) (*iaas.Snapshot, error) {