Skip to content

Commit 654269f

Browse files
authored
remove-pointer-functions (#92)
Signed-off-by: Felix Breuer <f.breuer94@gmail.com>
1 parent ad869cc commit 654269f

File tree

3 files changed

+34
-224
lines changed

3 files changed

+34
-224
lines changed

pkg/client/helper.go

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
package client
22

3-
// ptr returns a pointer to the given value
4-
// This helper is needed because the STACKIT SDK uses pointers for optional fields
5-
func ptr[T any](v T) *T {
6-
return &v
7-
}
8-
93
// convertLabelsToSDK converts map[string]string to *map[string]interface{} for SDK
10-
//
11-
//nolint:gocritic // SDK requires *map
12-
func convertLabelsToSDK(labels map[string]string) *map[string]interface{} {
4+
func convertLabelsToSDK(labels map[string]string) map[string]interface{} {
135
if labels == nil {
146
return nil
157
}
@@ -18,7 +10,7 @@ func convertLabelsToSDK(labels map[string]string) *map[string]interface{} {
1810
for k, v := range labels {
1911
result[k] = v
2012
}
21-
return &result
13+
return result
2214
}
2315

2416
// convertLabelsFromSDK converts *map[string]interface{} from SDK to map[string]string
@@ -37,21 +29,3 @@ func convertLabelsFromSDK(labels *map[string]interface{}) map[string]string {
3729
}
3830
return result
3931
}
40-
41-
// convertStringSliceToSDK converts []string to *[]string for SDK
42-
func convertStringSliceToSDK(slice []string) *[]string {
43-
if slice == nil {
44-
return nil
45-
}
46-
return &slice
47-
}
48-
49-
// convertMetadataToSDK converts map[string]interface{} to *map[string]interface{} for SDK
50-
//
51-
//nolint:gocritic // SDK requires *map
52-
func convertMetadataToSDK(metadata map[string]interface{}) *map[string]interface{} {
53-
if metadata == nil {
54-
return nil
55-
}
56-
return &metadata
57-
}

pkg/client/sdk.go

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,18 @@ func createIAASClient(serviceAccountKey string) (*iaas.APIClient, error) {
8787
//nolint:gocyclo // TODO: refactor
8888
func (c *SdkStackitClient) CreateServer(ctx context.Context, projectID, region string, req *CreateServerRequest) (*Server, error) {
8989
// Convert our request to SDK payload
90-
payload := &iaas.CreateServerPayload{
91-
Name: ptr(req.Name),
92-
MachineType: ptr(req.MachineType),
93-
}
90+
payload := &iaas.CreateServerPayload{}
91+
payload.SetName(req.Name)
92+
payload.SetMachineType(req.MachineType)
9493

9594
// ImageID (optional - can be nil if booting from snapshot/volume)
9695
if req.ImageID != "" {
97-
payload.ImageId = ptr(req.ImageID)
96+
payload.SetImageId(req.ImageID)
9897
}
9998

10099
// Labels
101100
if req.Labels != nil {
102-
payload.Labels = convertLabelsToSDK(req.Labels)
101+
payload.SetLabels(convertLabelsToSDK(req.Labels))
103102
}
104103

105104
// 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
134133

135134
// Security Groups
136135
if len(req.SecurityGroups) > 0 {
137-
payload.SecurityGroups = convertStringSliceToSDK(req.SecurityGroups)
136+
payload.SetSecurityGroups(req.SecurityGroups)
138137
}
139138

140139
// UserData - SDK expects *[]byte (base64-encoded bytes)
141140
if req.UserData != "" {
142-
userDataBytes := []byte(req.UserData)
143-
payload.SetUserData(userDataBytes)
141+
payload.SetUserData([]byte(req.UserData))
144142
}
145143

146144
// Boot Volume
@@ -164,27 +162,27 @@ func (c *SdkStackitClient) CreateServer(ctx context.Context, projectID, region s
164162

165163
// Volumes
166164
if len(req.Volumes) > 0 {
167-
payload.Volumes = convertStringSliceToSDK(req.Volumes)
165+
payload.SetVolumes(req.Volumes)
168166
}
169167

170168
// KeypairName
171169
if req.KeypairName != "" {
172-
payload.KeypairName = ptr(req.KeypairName)
170+
payload.SetKeypairName(req.KeypairName)
173171
}
174172

175173
// AvailabilityZone
176174
if req.AvailabilityZone != "" {
177-
payload.AvailabilityZone = ptr(req.AvailabilityZone)
175+
payload.SetAvailabilityZone(req.AvailabilityZone)
178176
}
179177

180178
// AffinityGroup
181179
if req.AffinityGroup != "" {
182-
payload.AffinityGroup = ptr(req.AffinityGroup)
180+
payload.SetAffinityGroup(req.AffinityGroup)
183181
}
184182

185183
// ServiceAccountMails
186184
if len(req.ServiceAccountMails) > 0 {
187-
payload.ServiceAccountMails = convertStringSliceToSDK(req.ServiceAccountMails)
185+
payload.SetServiceAccountMails(req.ServiceAccountMails)
188186
}
189187

190188
// Agent
@@ -196,7 +194,7 @@ func (c *SdkStackitClient) CreateServer(ctx context.Context, projectID, region s
196194

197195
// Metadata
198196
if req.Metadata != nil {
199-
payload.Metadata = convertMetadataToSDK(req.Metadata)
197+
payload.SetMetadata(req.Metadata)
200198
}
201199

202200
// Call SDK using the stored client
@@ -209,9 +207,9 @@ func (c *SdkStackitClient) CreateServer(ctx context.Context, projectID, region s
209207

210208
// Convert SDK server to our Server type
211209
server := &Server{
212-
ID: getStringValue(sdkServer.Id),
213-
Name: getStringValue(sdkServer.Name),
214-
Status: getStringValue(sdkServer.Status),
210+
ID: sdkServer.GetId(),
211+
Name: sdkServer.GetName(),
212+
Status: sdkServer.GetStatus(),
215213
Labels: convertLabelsFromSDK(sdkServer.Labels),
216214
}
217215

@@ -231,9 +229,9 @@ func (c *SdkStackitClient) GetServer(ctx context.Context, projectID, region, ser
231229

232230
// Convert SDK server to our Server type
233231
server := &Server{
234-
ID: getStringValue(sdkServer.Id),
235-
Name: getStringValue(sdkServer.Name),
236-
Status: getStringValue(sdkServer.Status),
232+
ID: sdkServer.GetId(),
233+
Name: sdkServer.GetName(),
234+
Status: sdkServer.GetStatus(),
237235
Labels: convertLabelsFromSDK(sdkServer.Labels),
238236
}
239237

@@ -286,9 +284,9 @@ func (c *SdkStackitClient) ListServers(ctx context.Context, projectID, region st
286284
sdkServer := &(*sdkResponse.Items)[i]
287285

288286
server := &Server{
289-
ID: getStringValue(sdkServer.Id),
290-
Name: getStringValue(sdkServer.Name),
291-
Status: getStringValue(sdkServer.Status),
287+
ID: sdkServer.GetId(),
288+
Name: sdkServer.GetName(),
289+
Status: sdkServer.GetStatus(),
292290
Labels: convertLabelsFromSDK(sdkServer.Labels),
293291
}
294292
servers = append(servers, server)
@@ -321,7 +319,7 @@ func (c *SdkStackitClient) UpdateNIC(ctx context.Context, projectID, region, net
321319

322320
for i, addr := range allowedAddresses {
323321
addresses[i] = iaas.AllowedAddressesInner{
324-
String: ptr(addr),
322+
String: &addr,
325323
}
326324
}
327325

@@ -354,20 +352,12 @@ func convertSDKNICtoNIC(nic *iaas.NIC) *NIC {
354352
}
355353

356354
return &NIC{
357-
ID: getStringValue(nic.Id),
358-
NetworkID: getStringValue(nic.NetworkId),
355+
ID: nic.GetId(),
356+
NetworkID: nic.GetNetworkId(),
359357
AllowedAddresses: addresses,
360358
}
361359
}
362360

363-
// getStringValue safely dereferences a string pointer, returning empty string if nil
364-
func getStringValue(s *string) string {
365-
if s == nil {
366-
return ""
367-
}
368-
return *s
369-
}
370-
371361
// isNotFoundError checks if an error is a 404 Not Found error from the SDK
372362
func isNotFoundError(err error) bool {
373363
if err == nil {

0 commit comments

Comments
 (0)