Skip to content

Commit d814048

Browse files
committed
add temporary types and function to handle base64byte conversion of server struct
1 parent f30f187 commit d814048

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

internal/pkg/utils/utils.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/spf13/viper"
1414
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
1515
sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
16+
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1617
)
1718

1819
// Ptr Returns the pointer to any type T
@@ -153,3 +154,79 @@ func ConvertStringMapToInterfaceMap(m *map[string]string) *map[string]interface{
153154
}
154155
return &result
155156
}
157+
158+
// Base64Bytes implements yaml.Marshaler to convert []byte to base64 strings
159+
// ref: https://carlosbecker.com/posts/go-custom-marshaling
160+
type Base64Bytes []byte
161+
162+
// MarshalYAML implements yaml.Marshaler
163+
func (b Base64Bytes) MarshalYAML() (interface{}, error) {
164+
if len(b) == 0 {
165+
return "", nil
166+
}
167+
return base64.StdEncoding.EncodeToString(b), nil
168+
}
169+
170+
type Base64PatchedServer struct {
171+
Id *string `json:"id,omitempty"`
172+
Name *string `json:"name,omitempty"`
173+
Status *string `json:"status,omitempty"`
174+
AvailabilityZone *string `json:"availabilityZone,omitempty"`
175+
BootVolume *iaas.CreateServerPayloadBootVolume `json:"bootVolume,omitempty"`
176+
CreatedAt *time.Time `json:"createdAt,omitempty"`
177+
ErrorMessage *string `json:"errorMessage,omitempty"`
178+
PowerStatus *string `json:"powerStatus,omitempty"`
179+
AffinityGroup *string `json:"affinityGroup,omitempty"`
180+
ImageId *string `json:"imageId,omitempty"`
181+
KeypairName *string `json:"keypairName,omitempty"`
182+
MachineType *string `json:"machineType,omitempty"`
183+
Labels *map[string]interface{} `json:"labels,omitempty"`
184+
LaunchedAt *time.Time `json:"launchedAt,omitempty"`
185+
MaintenanceWindow *iaas.ServerMaintenance `json:"maintenanceWindow,omitempty"`
186+
Metadata *map[string]interface{} `json:"metadata,omitempty"`
187+
Networking *iaas.CreateServerPayloadNetworking `json:"networking,omitempty"`
188+
Nics *[]iaas.ServerNetwork `json:"nics,omitempty"`
189+
SecurityGroups *[]string `json:"securityGroups,omitempty"`
190+
ServiceAccountMails *[]string `json:"serviceAccountMails,omitempty"`
191+
UpdatedAt *time.Time `json:"updatedAt,omitempty"`
192+
UserData *Base64Bytes `json:"userData,omitempty"`
193+
Volumes *[]string `json:"volumes,omitempty"`
194+
}
195+
196+
// ConvertToBase64PatchedServer converts an iaas.Server to Base64PatchedServer
197+
func ConvertToBase64PatchedServer(server *iaas.Server) *Base64PatchedServer {
198+
if server == nil {
199+
return nil
200+
}
201+
202+
var userData *Base64Bytes
203+
if server.UserData != nil {
204+
userData = Ptr(Base64Bytes(*server.UserData))
205+
}
206+
207+
return &Base64PatchedServer{
208+
Id: server.Id,
209+
Name: server.Name,
210+
Status: server.Status,
211+
AvailabilityZone: server.AvailabilityZone,
212+
BootVolume: server.BootVolume,
213+
CreatedAt: server.CreatedAt,
214+
ErrorMessage: server.ErrorMessage,
215+
PowerStatus: server.PowerStatus,
216+
AffinityGroup: server.AffinityGroup,
217+
ImageId: server.ImageId,
218+
KeypairName: server.KeypairName,
219+
MachineType: server.MachineType,
220+
Labels: server.Labels,
221+
LaunchedAt: server.LaunchedAt,
222+
MaintenanceWindow: server.MaintenanceWindow,
223+
Metadata: server.Metadata,
224+
Networking: server.Networking,
225+
Nics: server.Nics,
226+
SecurityGroups: server.SecurityGroups,
227+
ServiceAccountMails: server.ServiceAccountMails,
228+
UpdatedAt: server.UpdatedAt,
229+
UserData: userData,
230+
Volumes: server.Volumes,
231+
}
232+
}

0 commit comments

Comments
 (0)