Skip to content

refactor(cli): add ConvertStringMapToInterfaceMap util function #790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 13, 2025
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
10 changes: 1 addition & 9 deletions internal/cmd/image/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,18 +340,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
}

func createPayload(_ context.Context, model *inputModel) iaas.CreateImagePayload {
var labelsMap *map[string]any
if model.Labels != nil && len(*model.Labels) > 0 {
// convert map[string]string to map[string]interface{}
labelsMap = utils.Ptr(map[string]interface{}{})
for k, v := range *model.Labels {
(*labelsMap)[k] = v
}
}
payload := iaas.CreateImagePayload{
DiskFormat: &model.DiskFormat,
Name: &model.Name,
Labels: labelsMap,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
MinDiskSize: model.MinDiskSize,
MinRam: model.MinRam,
Protected: model.Protected,
Expand Down
11 changes: 2 additions & 9 deletions internal/cmd/image/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,10 @@ func parseInput(p *print.Printer, cmd *cobra.Command, cliArgs []string) (*inputM
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdateImageRequest {
request := apiClient.UpdateImage(ctx, model.ProjectId, model.Id)
payload := iaas.NewUpdateImagePayload()
var labelsMap *map[string]any
if model.Labels != nil && len(*model.Labels) > 0 {
// convert map[string]string to map[string]interface{}
labelsMap = utils.Ptr(map[string]interface{}{})
for k, v := range *model.Labels {
(*labelsMap)[k] = v
}
}

// Config *ImageConfig `json:"config,omitempty"`
payload.DiskFormat = model.DiskFormat
payload.Labels = labelsMap
payload.Labels = utils.ConvertStringMapToInterfaceMap(model.Labels)
payload.MinDiskSize = model.MinDiskSize
payload.MinRam = model.MinRam
payload.Name = model.Name
Expand Down
11 changes: 1 addition & 10 deletions internal/cmd/key-pair/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateKeyPairRequest {
req := apiClient.CreateKeyPair(ctx)

var labelsMap *map[string]interface{}
if model.Labels != nil && len(*model.Labels) > 0 {
// convert map[string]string to map[string]interface{}
labelsMap = utils.Ptr(map[string]interface{}{})
for k, v := range *model.Labels {
(*labelsMap)[k] = v
}
}

payload := iaas.CreateKeyPairPayload{
Name: model.Name,
Labels: labelsMap,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
PublicKey: model.PublicKey,
}

Expand Down
10 changes: 1 addition & 9 deletions internal/cmd/key-pair/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,8 @@ func configureFlags(cmd *cobra.Command) {
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdateKeyPairRequest {
req := apiClient.UpdateKeyPair(ctx, *model.KeyPairName)

var labelsMap *map[string]interface{}
if model.Labels != nil && len(*model.Labels) > 0 {
// convert map[string]string to map[string]interface{}
labelsMap = utils.Ptr(map[string]interface{}{})
for k, v := range *model.Labels {
(*labelsMap)[k] = v
}
}
payload := iaas.UpdateKeyPairPayload{
Labels: labelsMap,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
}
return req.UpdateKeyPairPayload(payload)
}
Expand Down
11 changes: 1 addition & 10 deletions internal/cmd/network-area/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
}
}

var labelsMap *map[string]interface{}
if model.Labels != nil && len(*model.Labels) > 0 {
// convert map[string]string to map[string]interface{}
labelsMap = utils.Ptr(map[string]interface{}{})
for k, v := range *model.Labels {
(*labelsMap)[k] = v
}
}

payload := iaas.CreateNetworkAreaPayload{
Name: model.Name,
Labels: labelsMap,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
AddressFamily: &iaas.CreateAreaAddressFamily{
Ipv4: &iaas.CreateAreaIPv4{
DefaultNameservers: model.DnsNameServers,
Expand Down
11 changes: 1 addition & 10 deletions internal/cmd/network-area/route/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,12 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateNetworkAreaRouteRequest {
req := apiClient.CreateNetworkAreaRoute(ctx, *model.OrganizationId, *model.NetworkAreaId)

var labelsMap *map[string]interface{}
if model.Labels != nil && len(*model.Labels) > 0 {
// convert map[string]string to map[string]interface{}
labelsMap = utils.Ptr(map[string]interface{}{})
for k, v := range *model.Labels {
(*labelsMap)[k] = v
}
}

payload := iaas.CreateNetworkAreaRoutePayload{
Ipv4: &[]iaas.Route{
{
Prefix: model.Prefix,
Nexthop: model.Nexthop,
Labels: labelsMap,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
},
},
}
Expand Down
8 changes: 1 addition & 7 deletions internal/cmd/network-area/route/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,8 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdateNetworkAreaRouteRequest {
req := apiClient.UpdateNetworkAreaRoute(ctx, *model.OrganizationId, *model.NetworkAreaId, model.RouteId)

// convert map[string]string to map[string]interface{}
labelsMap := make(map[string]interface{})
for k, v := range *model.Labels {
labelsMap[k] = v
}

payload := iaas.UpdateNetworkAreaRoutePayload{
Labels: &labelsMap,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
}
req = req.UpdateNetworkAreaRoutePayload(payload)

Expand Down
11 changes: 1 addition & 10 deletions internal/cmd/network-area/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiPartialUpdateNetworkAreaRequest {
req := apiClient.PartialUpdateNetworkArea(ctx, *model.OrganizationId, model.AreaId)

var labelsMap *map[string]interface{}
if model.Labels != nil && len(*model.Labels) > 0 {
// convert map[string]string to map[string]interface{}
labelsMap = utils.Ptr(map[string]interface{}{})
for k, v := range *model.Labels {
(*labelsMap)[k] = v
}
}

payload := iaas.PartialUpdateNetworkAreaPayload{
Name: model.Name,
Labels: labelsMap,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
AddressFamily: &iaas.UpdateAreaAddressFamily{
Ipv4: &iaas.UpdateAreaIPv4{
DefaultNameservers: model.DnsNameServers,
Expand Down
12 changes: 1 addition & 11 deletions internal/cmd/network-interface/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,21 +207,11 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateNicRequest {
req := apiClient.CreateNic(ctx, model.ProjectId, *model.NetworkId)

var labelsMap *map[string]interface{}
if model.Labels != nil && len(*model.Labels) > 0 {
// convert map[string]string to map[string]interface{}
convertedMap := make(map[string]interface{}, len(*model.Labels))
for k, v := range *model.Labels {
convertedMap[k] = v
}
labelsMap = &convertedMap
}

payload := iaas.CreateNicPayload{
AllowedAddresses: model.AllowedAddresses,
Ipv4: model.Ipv4,
Ipv6: model.Ipv6,
Labels: labelsMap,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
Name: model.Name,
NicSecurity: model.NicSecurity,
SecurityGroups: model.SecurityGroups,
Expand Down
12 changes: 1 addition & 11 deletions internal/cmd/network-interface/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdateNicRequest {
req := apiClient.UpdateNic(ctx, model.ProjectId, *model.NetworkId, model.NicId)

var labelsMap *map[string]interface{}
if model.Labels != nil && len(*model.Labels) > 0 {
// convert map[string]string to map[string]interface{}
convertedMap := make(map[string]interface{}, len(*model.Labels))
for k, v := range *model.Labels {
convertedMap[k] = v
}
labelsMap = &convertedMap
}

payload := iaas.UpdateNicPayload{
AllowedAddresses: model.AllowedAddresses,
Labels: labelsMap,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
Name: model.Name,
NicSecurity: model.NicSecurity,
SecurityGroups: model.SecurityGroups,
Expand Down
11 changes: 1 addition & 10 deletions internal/cmd/network/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
}
}

var labelsMap *map[string]interface{}
if model.Labels != nil && len(*model.Labels) > 0 {
// convert map[string]string to map[string]interface{}
labelsMap = utils.Ptr(map[string]interface{}{})
for k, v := range *model.Labels {
(*labelsMap)[k] = v
}
}

routed := true
if model.NonRouted {
routed = false
}

payload := iaas.CreateNetworkPayload{
Name: model.Name,
Labels: labelsMap,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
Routed: &routed,
}

Expand Down
11 changes: 1 addition & 10 deletions internal/cmd/network/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,6 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
req := apiClient.PartialUpdateNetwork(ctx, model.ProjectId, model.NetworkId)
addressFamily := &iaas.UpdateNetworkAddressFamily{}

var labelsMap *map[string]interface{}
if model.Labels != nil && len(*model.Labels) > 0 {
// convert map[string]string to map[string]interface{}
labelsMap = utils.Ptr(map[string]interface{}{})
for k, v := range *model.Labels {
(*labelsMap)[k] = v
}
}

if model.IPv6DnsNameServers != nil || model.NoIPv6Gateway || model.IPv6Gateway != nil {
addressFamily.Ipv6 = &iaas.UpdateNetworkIPv6Body{
Nameservers: model.IPv6DnsNameServers,
Expand All @@ -214,7 +205,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli

payload := iaas.PartialUpdateNetworkPayload{
Name: model.Name,
Labels: labelsMap,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
}

if addressFamily.Ipv4 != nil || addressFamily.Ipv6 != nil {
Expand Down
11 changes: 1 addition & 10 deletions internal/cmd/public-ip/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreatePublicIPRequest {
req := apiClient.CreatePublicIP(ctx, model.ProjectId)

var labelsMap *map[string]interface{}
if model.Labels != nil && len(*model.Labels) > 0 {
// convert map[string]string to map[string]interface{}
labelsMap = utils.Ptr(map[string]interface{}{})
for k, v := range *model.Labels {
(*labelsMap)[k] = v
}
}

payload := iaas.CreatePublicIPPayload{
NetworkInterface: iaas.NewNullableString(model.AssociatedResourceId),
Labels: labelsMap,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
}

return req.CreatePublicIPPayload(payload)
Expand Down
11 changes: 1 addition & 10 deletions internal/cmd/public-ip/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,8 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdatePublicIPRequest {
req := apiClient.UpdatePublicIP(ctx, model.ProjectId, model.PublicIpId)

var labelsMap *map[string]interface{}
if model.Labels != nil && len(*model.Labels) > 0 {
// convert map[string]string to map[string]interface{}
labelsMap = utils.Ptr(map[string]interface{}{})
for k, v := range *model.Labels {
(*labelsMap)[k] = v
}
}

payload := iaas.UpdatePublicIPPayload{
Labels: labelsMap,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
}

return req.UpdatePublicIPPayload(payload)
Expand Down
10 changes: 1 addition & 9 deletions internal/cmd/security-group/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateSecurityGroupRequest {
request := apiClient.CreateSecurityGroup(ctx, model.ProjectId)

var labelsMap *map[string]any
if model.Labels != nil && len(*model.Labels) > 0 {
// convert map[string]string to map[string]interface{}
labelsMap = utils.Ptr(map[string]interface{}{})
for k, v := range *model.Labels {
(*labelsMap)[k] = v
}
}
payload := iaas.CreateSecurityGroupPayload{
Description: model.Description,
Labels: labelsMap,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
Name: model.Name,
Stateful: model.Stateful,
}
Expand Down
10 changes: 1 addition & 9 deletions internal/cmd/security-group/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
request := apiClient.UpdateSecurityGroup(ctx, model.ProjectId, model.SecurityGroupId)
payload := iaas.NewUpdateSecurityGroupPayload()
payload.Description = model.Description
var labelsMap *map[string]any
if model.Labels != nil && len(*model.Labels) > 0 {
// convert map[string]string to map[string]interface{}
labelsMap = utils.Ptr(map[string]interface{}{})
for k, v := range *model.Labels {
(*labelsMap)[k] = v
}
}
payload.Labels = labelsMap
payload.Labels = utils.ConvertStringMapToInterfaceMap(model.Labels)
payload.Name = model.Name
request = request.UpdateSecurityGroupPayload(*payload)

Expand Down
10 changes: 1 addition & 9 deletions internal/cmd/server/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,6 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {

func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateServerRequest {
req := apiClient.CreateServer(ctx, model.ProjectId)
var labelsMap *map[string]interface{}
if model.Labels != nil && len(*model.Labels) > 0 {
// convert map[string]string to map[string]interface{}
labelsMap = utils.Ptr(map[string]interface{}{})
for k, v := range *model.Labels {
(*labelsMap)[k] = v
}
}

var userData *[]byte
if model.UserData != nil {
Expand All @@ -307,7 +299,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
ServiceAccountMails: model.ServiceAccountMails,
UserData: userData,
Volumes: model.Volumes,
Labels: labelsMap,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
}

if model.BootVolumePerformanceClass != nil || model.BootVolumeSize != nil || model.BootVolumeDeleteOnTermination != nil || model.BootVolumeSourceId != nil || model.BootVolumeSourceType != nil {
Expand Down
11 changes: 1 addition & 10 deletions internal/cmd/server/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdateServerRequest {
req := apiClient.UpdateServer(ctx, model.ProjectId, model.ServerId)

var labelsMap *map[string]interface{}
if model.Labels != nil && len(*model.Labels) > 0 {
// convert map[string]string to map[string]interface{}
labelsMap = utils.Ptr(map[string]interface{}{})
for k, v := range *model.Labels {
(*labelsMap)[k] = v
}
}

payload := iaas.UpdateServerPayload{
Name: model.Name,
Labels: labelsMap,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
}

return req.UpdateServerPayload(payload)
Expand Down
15 changes: 15 additions & 0 deletions internal/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,18 @@ func Base64Encode(message []byte) string {
func UserAgentConfigOption(cliVersion string) sdkConfig.ConfigurationOption {
return sdkConfig.WithUserAgent(fmt.Sprintf("stackit-cli/%s", cliVersion))
}

// ConvertStringMapToInterfaceMap converts a map[string]string to a pointer to map[string]interface{}.
// Returns nil if the input map is empty.
//
//nolint:gocritic // Linter wants to have a non-pointer type for the map, but this would mean a nil check has to be done before every usage of this func.
func ConvertStringMapToInterfaceMap(m *map[string]string) *map[string]interface{} {
if m == nil || len(*m) == 0 {
return nil
}
result := make(map[string]interface{}, len(*m))
for k, v := range *m {
result[k] = v
}
return &result
}
Loading
Loading