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
8 changes: 7 additions & 1 deletion internal/cmd/server/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package create

import (
"context"
"encoding/base64"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/types"
Expand Down Expand Up @@ -275,6 +276,11 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateServerRequest {
req := apiClient.DefaultAPI.CreateServer(ctx, model.ProjectId, model.Region)

var userData *string
if model.UserData != nil {
userData = utils.Ptr(base64.StdEncoding.EncodeToString([]byte(*model.UserData)))
}

payload := iaas.CreateServerPayload{
Name: model.Name,
MachineType: model.MachineType,
Expand All @@ -285,7 +291,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
KeypairName: model.KeypairName,
SecurityGroups: model.SecurityGroups,
ServiceAccountMails: model.ServiceAccountMails,
UserData: model.UserData,
UserData: userData,
Volumes: model.Volumes,
Labels: model.Labels,
}
Expand Down
13 changes: 12 additions & 1 deletion internal/cmd/server/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func fixturePayload(mods ...func(payload *iaas.CreateServerPayload)) iaas.Create
KeypairName: utils.Ptr("test-keypair-name"),
SecurityGroups: []string{"test-security-groups"},
ServiceAccountMails: []string{"test-service-account"},
UserData: utils.Ptr("test-user-data"),
UserData: utils.Ptr("dGVzdC11c2VyLWRhdGE="),
Volumes: []string{testVolumeId},
BootVolume: &iaas.BootVolume{
PerformanceClass: utils.Ptr("test-perf-class"),
Expand Down Expand Up @@ -397,6 +397,17 @@ func TestBuildRequest(t *testing.T) {
*request = request.CreateServerPayload(payload)
}),
},
{
description: "with user data",
model: fixtureInputModel(func(model *inputModel) {
model.UserData = utils.Ptr("cloud-init data")
}),
expectedRequest: fixtureRequest(func(request *iaas.ApiCreateServerRequest) {
payload := fixturePayload()
payload.UserData = utils.Ptr("Y2xvdWQtaW5pdCBkYXRh")
*request = request.CreateServerPayload(payload)
}),
},
}

for _, tt := range tests {
Expand Down
7 changes: 1 addition & 6 deletions internal/cmd/server/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,7 @@ func outputResult(p *print.Printer, outputFormat string, server *iaas.Server) er

return nil
case print.YAMLOutputFormat:
// This is a temporary workaround to get the desired base64 encoded yaml output for userdata
// and will be replaced by a fix in the Go-SDK
// ref: https://jira.schwarz/browse/STACKITSDK-246
patchedServer := utils.ConvertToBase64PatchedServer(server)

details, err := yaml.MarshalWithOptions(patchedServer, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
details, err := yaml.MarshalWithOptions(server, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal server: %w", err)
}
Expand Down
7 changes: 1 addition & 6 deletions internal/cmd/server/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,7 @@ func outputResult(p *print.Printer, outputFormat, projectLabel string, servers [

return nil
case print.YAMLOutputFormat:
// This is a temporary workaround to get the desired base64 encoded yaml output for userdata
// and will be replaced by a fix in the Go-SDK
// ref: https://jira.schwarz/browse/STACKITSDK-246
patchedServers := utils.ConvertToBase64PatchedServers(servers)

details, err := yaml.MarshalWithOptions(patchedServers, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
details, err := yaml.MarshalWithOptions(servers, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal server: %w", err)
}
Expand Down
100 changes: 1 addition & 99 deletions internal/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/inhies/go-bytesize"
"github.com/spf13/cobra"
"github.com/spf13/viper"

sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api"

"github.com/stackitcloud/stackit-cli/internal/pkg/config"
)
Expand Down Expand Up @@ -165,104 +165,6 @@ func ConvertStringMapToInterfaceMap(m *map[string]string) *map[string]interface{
return &result
}

// Base64Bytes implements yaml.Marshaler to convert []byte to base64 strings
// ref: https://carlosbecker.com/posts/go-custom-marshaling
type Base64Bytes []byte

// MarshalYAML implements yaml.Marshaler
func (b Base64Bytes) MarshalYAML() (interface{}, error) {
if len(b) == 0 {
return "", nil
}
return base64.StdEncoding.EncodeToString(b), nil
}

type Base64PatchedServer struct {
Id *string `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
Status *string `json:"status,omitempty"`
AvailabilityZone *string `json:"availabilityZone,omitempty"`
BootVolume *iaas.BootVolume `json:"bootVolume,omitempty"`
CreatedAt *time.Time `json:"createdAt,omitempty"`
ErrorMessage *string `json:"errorMessage,omitempty"`
PowerStatus *string `json:"powerStatus,omitempty"`
AffinityGroup *string `json:"affinityGroup,omitempty"`
ImageId *string `json:"imageId,omitempty"`
KeypairName *string `json:"keypairName,omitempty"`
MachineType *string `json:"machineType,omitempty"`
Labels map[string]interface{} `json:"labels,omitempty"`
LaunchedAt *time.Time `json:"launchedAt,omitempty"`
MaintenanceWindow *iaas.ServerMaintenance `json:"maintenanceWindow,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Networking *iaas.ServerNetworking `json:"networking,omitempty"`
Nics []iaas.ServerNetwork `json:"nics,omitempty"`
SecurityGroups []string `json:"securityGroups,omitempty"`
ServiceAccountMails []string `json:"serviceAccountMails,omitempty"`
UpdatedAt *time.Time `json:"updatedAt,omitempty"`
UserData *Base64Bytes `json:"userData,omitempty"`
Volumes []string `json:"volumes,omitempty"`
Agent *iaas.ServerAgent `json:"agent,omitempty"`
}

// ConvertToBase64PatchedServer converts an iaas.Server to Base64PatchedServer
// This is a temporary workaround to get the desired base64 encoded yaml output for userdata
// and will be replaced by a fix in the Go-SDK
// ref: https://jira.schwarz/browse/STACKITSDK-246
func ConvertToBase64PatchedServer(server *iaas.Server) *Base64PatchedServer {
if server == nil {
return nil
}

var userData *Base64Bytes
if server.UserData != nil {
userData = Ptr(Base64Bytes(*server.UserData))
}

return &Base64PatchedServer{
Id: server.Id,
Name: &server.Name,
Status: server.Status,
AvailabilityZone: server.AvailabilityZone,
BootVolume: server.BootVolume,
CreatedAt: server.CreatedAt,
ErrorMessage: server.ErrorMessage,
PowerStatus: server.PowerStatus,
AffinityGroup: server.AffinityGroup,
ImageId: server.ImageId,
KeypairName: server.KeypairName,
MachineType: &server.MachineType,
Labels: server.Labels,
LaunchedAt: server.LaunchedAt,
MaintenanceWindow: server.MaintenanceWindow,
Metadata: server.Metadata,
Networking: server.Networking,
Nics: server.Nics,
SecurityGroups: server.SecurityGroups,
ServiceAccountMails: server.ServiceAccountMails,
UpdatedAt: server.UpdatedAt,
UserData: userData,
Volumes: server.Volumes,
Agent: server.Agent,
}
}

// ConvertToBase64PatchedServers converts a slice of iaas.Server to a slice of Base64PatchedServer
// This is a temporary workaround to get the desired base64 encoded yaml output for userdata
// and will be replaced by a fix in the Go-SDK
// ref: https://jira.schwarz/browse/STACKITSDK-246
func ConvertToBase64PatchedServers(servers []iaas.Server) []Base64PatchedServer {
if servers == nil {
return nil
}

result := make([]Base64PatchedServer, len(servers))
for i := range servers {
result[i] = *ConvertToBase64PatchedServer(&servers[i])
}

return result
}

// GetSliceFromPointer returns the value of a pointer to a slice of type T.
// If the pointer is nil, it returns an empty slice.
func GetSliceFromPointer[T any](s *[]T) []T {
Expand Down
Loading
Loading