Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 2 additions & 28 deletions pkg/client/helper.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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
Expand All @@ -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
}
60 changes: 25 additions & 35 deletions pkg/client/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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),
}

Expand All @@ -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),
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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 {
Expand Down
Loading