From 4d72ff071a1442cbaf2766b2614f5d997363ed4f Mon Sep 17 00:00:00 2001 From: Felix Breuer Date: Fri, 23 Jan 2026 11:51:27 +0100 Subject: [PATCH] remove-pointer-functions Signed-off-by: Felix Breuer --- pkg/client/helper.go | 30 +------- pkg/client/sdk.go | 60 ++++++--------- pkg/client/sdk_test.go | 168 ++--------------------------------------- 3 files changed, 34 insertions(+), 224 deletions(-) diff --git a/pkg/client/helper.go b/pkg/client/helper.go index 4ca29941..5b543944 100644 --- a/pkg/client/helper.go +++ b/pkg/client/helper.go @@ -1,15 +1,7 @@ package client -// ptr returns a pointer to the given value -// This helper is needed because the STACKIT SDK uses pointers for optional fields -func ptr[T any](v T) *T { - return &v -} - // convertLabelsToSDK converts map[string]string to *map[string]interface{} for SDK -// -//nolint:gocritic // SDK requires *map -func convertLabelsToSDK(labels map[string]string) *map[string]interface{} { +func convertLabelsToSDK(labels map[string]string) map[string]interface{} { if labels == nil { return nil } @@ -18,7 +10,7 @@ func convertLabelsToSDK(labels map[string]string) *map[string]interface{} { for k, v := range labels { result[k] = v } - return &result + return result } // convertLabelsFromSDK converts *map[string]interface{} from SDK to map[string]string @@ -37,21 +29,3 @@ func convertLabelsFromSDK(labels *map[string]interface{}) map[string]string { } return result } - -// convertStringSliceToSDK converts []string to *[]string for SDK -func convertStringSliceToSDK(slice []string) *[]string { - if slice == nil { - return nil - } - return &slice -} - -// convertMetadataToSDK converts map[string]interface{} to *map[string]interface{} for SDK -// -//nolint:gocritic // SDK requires *map -func convertMetadataToSDK(metadata map[string]interface{}) *map[string]interface{} { - if metadata == nil { - return nil - } - return &metadata -} diff --git a/pkg/client/sdk.go b/pkg/client/sdk.go index 4584d665..c4694c30 100644 --- a/pkg/client/sdk.go +++ b/pkg/client/sdk.go @@ -87,19 +87,18 @@ func createIAASClient(serviceAccountKey string) (*iaas.APIClient, error) { //nolint:gocyclo // TODO: refactor func (c *SdkStackitClient) CreateServer(ctx context.Context, projectID, region string, req *CreateServerRequest) (*Server, error) { // Convert our request to SDK payload - payload := &iaas.CreateServerPayload{ - Name: ptr(req.Name), - MachineType: ptr(req.MachineType), - } + payload := &iaas.CreateServerPayload{} + payload.SetName(req.Name) + payload.SetMachineType(req.MachineType) // ImageID (optional - can be nil if booting from snapshot/volume) if req.ImageID != "" { - payload.ImageId = ptr(req.ImageID) + payload.SetImageId(req.ImageID) } // Labels if req.Labels != nil { - payload.Labels = convertLabelsToSDK(req.Labels) + payload.SetLabels(convertLabelsToSDK(req.Labels)) } // Networking - Required in v2 API, SDK uses union type: either NetworkId OR NicIds @@ -134,13 +133,12 @@ func (c *SdkStackitClient) CreateServer(ctx context.Context, projectID, region s // Security Groups if len(req.SecurityGroups) > 0 { - payload.SecurityGroups = convertStringSliceToSDK(req.SecurityGroups) + payload.SetSecurityGroups(req.SecurityGroups) } // UserData - SDK expects *[]byte (base64-encoded bytes) if req.UserData != "" { - userDataBytes := []byte(req.UserData) - payload.SetUserData(userDataBytes) + payload.SetUserData([]byte(req.UserData)) } // Boot Volume @@ -164,27 +162,27 @@ func (c *SdkStackitClient) CreateServer(ctx context.Context, projectID, region s // Volumes if len(req.Volumes) > 0 { - payload.Volumes = convertStringSliceToSDK(req.Volumes) + payload.SetVolumes(req.Volumes) } // KeypairName if req.KeypairName != "" { - payload.KeypairName = ptr(req.KeypairName) + payload.SetKeypairName(req.KeypairName) } // AvailabilityZone if req.AvailabilityZone != "" { - payload.AvailabilityZone = ptr(req.AvailabilityZone) + payload.SetAvailabilityZone(req.AvailabilityZone) } // AffinityGroup if req.AffinityGroup != "" { - payload.AffinityGroup = ptr(req.AffinityGroup) + payload.SetAffinityGroup(req.AffinityGroup) } // ServiceAccountMails if len(req.ServiceAccountMails) > 0 { - payload.ServiceAccountMails = convertStringSliceToSDK(req.ServiceAccountMails) + payload.SetServiceAccountMails(req.ServiceAccountMails) } // Agent @@ -196,7 +194,7 @@ func (c *SdkStackitClient) CreateServer(ctx context.Context, projectID, region s // Metadata if req.Metadata != nil { - payload.Metadata = convertMetadataToSDK(req.Metadata) + payload.SetMetadata(req.Metadata) } // Call SDK using the stored client @@ -209,9 +207,9 @@ func (c *SdkStackitClient) CreateServer(ctx context.Context, projectID, region s // Convert SDK server to our Server type server := &Server{ - ID: getStringValue(sdkServer.Id), - Name: getStringValue(sdkServer.Name), - Status: getStringValue(sdkServer.Status), + ID: sdkServer.GetId(), + Name: sdkServer.GetName(), + Status: sdkServer.GetStatus(), Labels: convertLabelsFromSDK(sdkServer.Labels), } @@ -231,9 +229,9 @@ func (c *SdkStackitClient) GetServer(ctx context.Context, projectID, region, ser // Convert SDK server to our Server type server := &Server{ - ID: getStringValue(sdkServer.Id), - Name: getStringValue(sdkServer.Name), - Status: getStringValue(sdkServer.Status), + ID: sdkServer.GetId(), + Name: sdkServer.GetName(), + Status: sdkServer.GetStatus(), Labels: convertLabelsFromSDK(sdkServer.Labels), } @@ -286,9 +284,9 @@ func (c *SdkStackitClient) ListServers(ctx context.Context, projectID, region st sdkServer := &(*sdkResponse.Items)[i] server := &Server{ - ID: getStringValue(sdkServer.Id), - Name: getStringValue(sdkServer.Name), - Status: getStringValue(sdkServer.Status), + ID: sdkServer.GetId(), + Name: sdkServer.GetName(), + Status: sdkServer.GetStatus(), Labels: convertLabelsFromSDK(sdkServer.Labels), } servers = append(servers, server) @@ -321,7 +319,7 @@ func (c *SdkStackitClient) UpdateNIC(ctx context.Context, projectID, region, net for i, addr := range allowedAddresses { addresses[i] = iaas.AllowedAddressesInner{ - String: ptr(addr), + String: &addr, } } @@ -354,20 +352,12 @@ func convertSDKNICtoNIC(nic *iaas.NIC) *NIC { } return &NIC{ - ID: getStringValue(nic.Id), - NetworkID: getStringValue(nic.NetworkId), + ID: nic.GetId(), + NetworkID: nic.GetNetworkId(), AllowedAddresses: addresses, } } -// getStringValue safely dereferences a string pointer, returning empty string if nil -func getStringValue(s *string) string { - if s == nil { - return "" - } - return *s -} - // isNotFoundError checks if an error is a 404 Not Found error from the SDK func isNotFoundError(err error) bool { if err == nil { diff --git a/pkg/client/sdk_test.go b/pkg/client/sdk_test.go index 40f9da68..6e46fd04 100644 --- a/pkg/client/sdk_test.go +++ b/pkg/client/sdk_test.go @@ -12,32 +12,6 @@ import ( var _ = Describe("SDK Client Helpers", func() { - Describe("getStringValue", func() { - Context("with valid pointer", func() { - It("should return string value", func() { - value := "test-string" - result := getStringValue(&value) - - Expect(result).To(Equal("test-string")) - }) - - It("should return empty string when pointer is to empty string", func() { - value := "" - result := getStringValue(&value) - - Expect(result).To(Equal("")) - }) - }) - - Context("with nil pointer", func() { - It("should return empty string when pointer is nil", func() { - result := getStringValue(nil) - - Expect(result).To(Equal("")) - }) - }) - }) - Describe("isNotFoundError", func() { Context("with SDK 404 errors", func() { It("should detect GenericOpenAPIError with 404 status code", func() { @@ -115,40 +89,6 @@ var _ = Describe("SDK Client Helpers", func() { var _ = Describe("SDK Type Conversion Helpers", func() { - Describe("ptr", func() { - It("should create pointer to string", func() { - value := "test" - result := ptr(value) - - Expect(result).NotTo(BeNil()) - Expect(*result).To(Equal("test")) - }) - - It("should create pointer to int", func() { - value := 42 - result := ptr(value) - - Expect(result).NotTo(BeNil()) - Expect(*result).To(Equal(42)) - }) - - It("should create pointer to bool", func() { - value := true - result := ptr(value) - - Expect(result).NotTo(BeNil()) - Expect(*result).To(BeTrue()) - }) - - It("should create pointer to empty string", func() { - value := "" - result := ptr(value) - - Expect(result).NotTo(BeNil()) - Expect(*result).To(Equal("")) - }) - }) - Describe("convertLabelsToSDK", func() { Context("with valid labels", func() { It("should convert labels map to SDK format", func() { @@ -160,9 +100,9 @@ var _ = Describe("SDK Type Conversion Helpers", func() { result := convertLabelsToSDK(labels) Expect(result).NotTo(BeNil()) - Expect(*result).To(HaveLen(2)) - Expect((*result)["app"]).To(Equal("web-server")) - Expect((*result)["team"]).To(Equal("platform")) + Expect(result).To(HaveLen(2)) + Expect(result["app"]).To(Equal("web-server")) + Expect(result["team"]).To(Equal("platform")) }) It("should convert empty labels map", func() { @@ -171,7 +111,7 @@ var _ = Describe("SDK Type Conversion Helpers", func() { result := convertLabelsToSDK(labels) Expect(result).NotTo(BeNil()) - Expect(*result).To(BeEmpty()) + Expect(result).To(BeEmpty()) }) It("should convert labels with special characters", func() { @@ -183,9 +123,9 @@ var _ = Describe("SDK Type Conversion Helpers", func() { result := convertLabelsToSDK(labels) Expect(result).NotTo(BeNil()) - Expect(*result).To(HaveLen(2)) - Expect((*result)["mcm.gardener.cloud/machine"]).To(Equal("test-machine")) - Expect((*result)["kubernetes.io/role"]).To(Equal("node")) + Expect(result).To(HaveLen(2)) + Expect(result["mcm.gardener.cloud/machine"]).To(Equal("test-machine")) + Expect(result["kubernetes.io/role"]).To(Equal("node")) }) }) @@ -265,100 +205,6 @@ var _ = Describe("SDK Type Conversion Helpers", func() { }) }) - Describe("convertStringSliceToSDK", func() { - Context("with valid slice", func() { - It("should convert string slice to pointer", func() { - slice := []string{"value1", "value2", "value3"} - - result := convertStringSliceToSDK(slice) - - Expect(result).NotTo(BeNil()) - Expect(*result).To(HaveLen(3)) - Expect(*result).To(Equal([]string{"value1", "value2", "value3"})) - }) - - It("should convert empty slice", func() { - slice := []string{} - - result := convertStringSliceToSDK(slice) - - Expect(result).NotTo(BeNil()) - Expect(*result).To(BeEmpty()) - }) - - It("should convert slice with single item", func() { - slice := []string{"only-item"} - - result := convertStringSliceToSDK(slice) - - Expect(result).NotTo(BeNil()) - Expect(*result).To(HaveLen(1)) - Expect((*result)[0]).To(Equal("only-item")) - }) - }) - - Context("with nil slice", func() { - It("should return nil for nil slice", func() { - result := convertStringSliceToSDK(nil) - - Expect(result).To(BeNil()) - }) - }) - }) - - Describe("convertMetadataToSDK", func() { - Context("with valid metadata", func() { - It("should convert metadata map to pointer", func() { - metadata := map[string]interface{}{ - "key1": "value1", - "key2": 42, - "key3": true, - } - - result := convertMetadataToSDK(metadata) - - Expect(result).NotTo(BeNil()) - Expect(*result).To(HaveLen(3)) - Expect((*result)["key1"]).To(Equal("value1")) - Expect((*result)["key2"]).To(Equal(42)) - Expect((*result)["key3"]).To(BeTrue()) - }) - - It("should convert empty metadata map", func() { - metadata := map[string]interface{}{} - - result := convertMetadataToSDK(metadata) - - Expect(result).NotTo(BeNil()) - Expect(*result).To(BeEmpty()) - }) - - It("should convert nested metadata objects", func() { - metadata := map[string]interface{}{ - "config": map[string]interface{}{ - "nested": "value", - }, - "list": []string{"item1", "item2"}, - } - - result := convertMetadataToSDK(metadata) - - Expect(result).NotTo(BeNil()) - Expect(*result).To(HaveLen(2)) - Expect((*result)["config"]).To(BeAssignableToTypeOf(map[string]interface{}{})) - Expect((*result)["list"]).To(BeAssignableToTypeOf([]string{})) - }) - }) - - Context("with nil metadata", func() { - It("should return nil for nil metadata", func() { - result := convertMetadataToSDK(nil) - - Expect(result).To(BeNil()) - }) - }) - }) - Describe("NewStackitClient", func() { Context("with STACKIT_NO_AUTH enabled", func() { It("should create client successfully without authentication", func() {