Skip to content

Commit 1e76138

Browse files
committed
add ConvertStringMapToInterfaceMap util function and refactor conversions
1 parent 2c00ad0 commit 1e76138

File tree

19 files changed

+32
-173
lines changed

19 files changed

+32
-173
lines changed

internal/cmd/image/create/create.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,18 +340,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
340340
}
341341

342342
func createPayload(_ context.Context, model *inputModel) iaas.CreateImagePayload {
343-
var labelsMap *map[string]any
344-
if model.Labels != nil && len(*model.Labels) > 0 {
345-
// convert map[string]string to map[string]interface{}
346-
labelsMap = utils.Ptr(map[string]interface{}{})
347-
for k, v := range *model.Labels {
348-
(*labelsMap)[k] = v
349-
}
350-
}
351343
payload := iaas.CreateImagePayload{
352344
DiskFormat: &model.DiskFormat,
353345
Name: &model.Name,
354-
Labels: labelsMap,
346+
Labels: utils.ConvertStringMapToInterfaceMap(*model.Labels),
355347
MinDiskSize: model.MinDiskSize,
356348
MinRam: model.MinRam,
357349
Protected: model.Protected,

internal/cmd/image/update/update.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,10 @@ func parseInput(p *print.Printer, cmd *cobra.Command, cliArgs []string) (*inputM
243243
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdateImageRequest {
244244
request := apiClient.UpdateImage(ctx, model.ProjectId, model.Id)
245245
payload := iaas.NewUpdateImagePayload()
246-
var labelsMap *map[string]any
247-
if model.Labels != nil && len(*model.Labels) > 0 {
248-
// convert map[string]string to map[string]interface{}
249-
labelsMap = utils.Ptr(map[string]interface{}{})
250-
for k, v := range *model.Labels {
251-
(*labelsMap)[k] = v
252-
}
253-
}
246+
254247
// Config *ImageConfig `json:"config,omitempty"`
255248
payload.DiskFormat = model.DiskFormat
256-
payload.Labels = labelsMap
249+
payload.Labels = utils.ConvertStringMapToInterfaceMap(*model.Labels)
257250
payload.MinDiskSize = model.MinDiskSize
258251
payload.MinRam = model.MinRam
259252
payload.Name = model.Name

internal/cmd/key-pair/create/create.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
124124
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateKeyPairRequest {
125125
req := apiClient.CreateKeyPair(ctx)
126126

127-
var labelsMap *map[string]interface{}
128-
if model.Labels != nil && len(*model.Labels) > 0 {
129-
// convert map[string]string to map[string]interface{}
130-
labelsMap = utils.Ptr(map[string]interface{}{})
131-
for k, v := range *model.Labels {
132-
(*labelsMap)[k] = v
133-
}
134-
}
135-
136127
payload := iaas.CreateKeyPairPayload{
137128
Name: model.Name,
138-
Labels: labelsMap,
129+
Labels: utils.ConvertStringMapToInterfaceMap(*model.Labels),
139130
PublicKey: model.PublicKey,
140131
}
141132

internal/cmd/key-pair/update/update.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,8 @@ func configureFlags(cmd *cobra.Command) {
8787
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdateKeyPairRequest {
8888
req := apiClient.UpdateKeyPair(ctx, *model.KeyPairName)
8989

90-
var labelsMap *map[string]interface{}
91-
if model.Labels != nil && len(*model.Labels) > 0 {
92-
// convert map[string]string to map[string]interface{}
93-
labelsMap = utils.Ptr(map[string]interface{}{})
94-
for k, v := range *model.Labels {
95-
(*labelsMap)[k] = v
96-
}
97-
}
9890
payload := iaas.UpdateKeyPairPayload{
99-
Labels: labelsMap,
91+
Labels: utils.ConvertStringMapToInterfaceMap(*model.Labels),
10092
}
10193
return req.UpdateKeyPairPayload(payload)
10294
}

internal/cmd/network-area/create/create.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
172172
}
173173
}
174174

175-
var labelsMap *map[string]interface{}
176-
if model.Labels != nil && len(*model.Labels) > 0 {
177-
// convert map[string]string to map[string]interface{}
178-
labelsMap = utils.Ptr(map[string]interface{}{})
179-
for k, v := range *model.Labels {
180-
(*labelsMap)[k] = v
181-
}
182-
}
183-
184175
payload := iaas.CreateNetworkAreaPayload{
185176
Name: model.Name,
186-
Labels: labelsMap,
177+
Labels: utils.ConvertStringMapToInterfaceMap(*model.Labels),
187178
AddressFamily: &iaas.CreateAreaAddressFamily{
188179
Ipv4: &iaas.CreateAreaIPv4{
189180
DefaultNameservers: model.DnsNameServers,

internal/cmd/network-area/route/create/create.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,12 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
147147
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateNetworkAreaRouteRequest {
148148
req := apiClient.CreateNetworkAreaRoute(ctx, *model.OrganizationId, *model.NetworkAreaId)
149149

150-
var labelsMap *map[string]interface{}
151-
if model.Labels != nil && len(*model.Labels) > 0 {
152-
// convert map[string]string to map[string]interface{}
153-
labelsMap = utils.Ptr(map[string]interface{}{})
154-
for k, v := range *model.Labels {
155-
(*labelsMap)[k] = v
156-
}
157-
}
158-
159150
payload := iaas.CreateNetworkAreaRoutePayload{
160151
Ipv4: &[]iaas.Route{
161152
{
162153
Prefix: model.Prefix,
163154
Nexthop: model.Nexthop,
164-
Labels: labelsMap,
155+
Labels: utils.ConvertStringMapToInterfaceMap(*model.Labels),
165156
},
166157
},
167158
}

internal/cmd/network-area/route/update/update.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,8 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
130130
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdateNetworkAreaRouteRequest {
131131
req := apiClient.UpdateNetworkAreaRoute(ctx, *model.OrganizationId, *model.NetworkAreaId, model.RouteId)
132132

133-
// convert map[string]string to map[string]interface{}
134-
labelsMap := make(map[string]interface{})
135-
for k, v := range *model.Labels {
136-
labelsMap[k] = v
137-
}
138-
139133
payload := iaas.UpdateNetworkAreaRoutePayload{
140-
Labels: &labelsMap,
134+
Labels: utils.ConvertStringMapToInterfaceMap(*model.Labels),
141135
}
142136
req = req.UpdateNetworkAreaRoutePayload(payload)
143137

internal/cmd/network-area/update/update.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
153153
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiPartialUpdateNetworkAreaRequest {
154154
req := apiClient.PartialUpdateNetworkArea(ctx, *model.OrganizationId, model.AreaId)
155155

156-
var labelsMap *map[string]interface{}
157-
if model.Labels != nil && len(*model.Labels) > 0 {
158-
// convert map[string]string to map[string]interface{}
159-
labelsMap = utils.Ptr(map[string]interface{}{})
160-
for k, v := range *model.Labels {
161-
(*labelsMap)[k] = v
162-
}
163-
}
164-
165156
payload := iaas.PartialUpdateNetworkAreaPayload{
166157
Name: model.Name,
167-
Labels: labelsMap,
158+
Labels: utils.ConvertStringMapToInterfaceMap(*model.Labels),
168159
AddressFamily: &iaas.UpdateAreaAddressFamily{
169160
Ipv4: &iaas.UpdateAreaIPv4{
170161
DefaultNameservers: model.DnsNameServers,

internal/cmd/network-interface/create/create.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,11 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
207207
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateNicRequest {
208208
req := apiClient.CreateNic(ctx, model.ProjectId, *model.NetworkId)
209209

210-
var labelsMap *map[string]interface{}
211-
if model.Labels != nil && len(*model.Labels) > 0 {
212-
// convert map[string]string to map[string]interface{}
213-
convertedMap := make(map[string]interface{}, len(*model.Labels))
214-
for k, v := range *model.Labels {
215-
convertedMap[k] = v
216-
}
217-
labelsMap = &convertedMap
218-
}
219-
220210
payload := iaas.CreateNicPayload{
221211
AllowedAddresses: model.AllowedAddresses,
222212
Ipv4: model.Ipv4,
223213
Ipv6: model.Ipv6,
224-
Labels: labelsMap,
214+
Labels: utils.ConvertStringMapToInterfaceMap(*model.Labels),
225215
Name: model.Name,
226216
NicSecurity: model.NicSecurity,
227217
SecurityGroups: model.SecurityGroups,

internal/cmd/network-interface/update/update.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
199199
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdateNicRequest {
200200
req := apiClient.UpdateNic(ctx, model.ProjectId, *model.NetworkId, model.NicId)
201201

202-
var labelsMap *map[string]interface{}
203-
if model.Labels != nil && len(*model.Labels) > 0 {
204-
// convert map[string]string to map[string]interface{}
205-
convertedMap := make(map[string]interface{}, len(*model.Labels))
206-
for k, v := range *model.Labels {
207-
convertedMap[k] = v
208-
}
209-
labelsMap = &convertedMap
210-
}
211-
212202
payload := iaas.UpdateNicPayload{
213203
AllowedAddresses: model.AllowedAddresses,
214-
Labels: labelsMap,
204+
Labels: utils.ConvertStringMapToInterfaceMap(*model.Labels),
215205
Name: model.Name,
216206
NicSecurity: model.NicSecurity,
217207
SecurityGroups: model.SecurityGroups,

internal/cmd/network/create/create.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,23 +229,14 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
229229
}
230230
}
231231

232-
var labelsMap *map[string]interface{}
233-
if model.Labels != nil && len(*model.Labels) > 0 {
234-
// convert map[string]string to map[string]interface{}
235-
labelsMap = utils.Ptr(map[string]interface{}{})
236-
for k, v := range *model.Labels {
237-
(*labelsMap)[k] = v
238-
}
239-
}
240-
241232
routed := true
242233
if model.NonRouted {
243234
routed = false
244235
}
245236

246237
payload := iaas.CreateNetworkPayload{
247238
Name: model.Name,
248-
Labels: labelsMap,
239+
Labels: utils.ConvertStringMapToInterfaceMap(*model.Labels),
249240
Routed: &routed,
250241
}
251242

internal/cmd/network/update/update.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,6 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
179179
req := apiClient.PartialUpdateNetwork(ctx, model.ProjectId, model.NetworkId)
180180
addressFamily := &iaas.UpdateNetworkAddressFamily{}
181181

182-
var labelsMap *map[string]interface{}
183-
if model.Labels != nil && len(*model.Labels) > 0 {
184-
// convert map[string]string to map[string]interface{}
185-
labelsMap = utils.Ptr(map[string]interface{}{})
186-
for k, v := range *model.Labels {
187-
(*labelsMap)[k] = v
188-
}
189-
}
190-
191182
if model.IPv6DnsNameServers != nil || model.NoIPv6Gateway || model.IPv6Gateway != nil {
192183
addressFamily.Ipv6 = &iaas.UpdateNetworkIPv6Body{
193184
Nameservers: model.IPv6DnsNameServers,
@@ -214,7 +205,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
214205

215206
payload := iaas.PartialUpdateNetworkPayload{
216207
Name: model.Name,
217-
Labels: labelsMap,
208+
Labels: utils.ConvertStringMapToInterfaceMap(*model.Labels),
218209
}
219210

220211
if addressFamily.Ipv4 != nil || addressFamily.Ipv6 != nil {

internal/cmd/public-ip/create/create.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
126126
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreatePublicIPRequest {
127127
req := apiClient.CreatePublicIP(ctx, model.ProjectId)
128128

129-
var labelsMap *map[string]interface{}
130-
if model.Labels != nil && len(*model.Labels) > 0 {
131-
// convert map[string]string to map[string]interface{}
132-
labelsMap = utils.Ptr(map[string]interface{}{})
133-
for k, v := range *model.Labels {
134-
(*labelsMap)[k] = v
135-
}
136-
}
137-
138129
payload := iaas.CreatePublicIPPayload{
139130
NetworkInterface: iaas.NewNullableString(model.AssociatedResourceId),
140-
Labels: labelsMap,
131+
Labels: utils.ConvertStringMapToInterfaceMap(*model.Labels),
141132
}
142133

143134
return req.CreatePublicIPPayload(payload)

internal/cmd/public-ip/update/update.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,8 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
130130
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdatePublicIPRequest {
131131
req := apiClient.UpdatePublicIP(ctx, model.ProjectId, model.PublicIpId)
132132

133-
var labelsMap *map[string]interface{}
134-
if model.Labels != nil && len(*model.Labels) > 0 {
135-
// convert map[string]string to map[string]interface{}
136-
labelsMap = utils.Ptr(map[string]interface{}{})
137-
for k, v := range *model.Labels {
138-
(*labelsMap)[k] = v
139-
}
140-
}
141-
142133
payload := iaas.UpdatePublicIPPayload{
143-
Labels: labelsMap,
134+
Labels: utils.ConvertStringMapToInterfaceMap(*model.Labels),
144135
}
145136

146137
return req.UpdatePublicIPPayload(payload)

internal/cmd/security-group/create/create.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
127127
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateSecurityGroupRequest {
128128
request := apiClient.CreateSecurityGroup(ctx, model.ProjectId)
129129

130-
var labelsMap *map[string]any
131-
if model.Labels != nil && len(*model.Labels) > 0 {
132-
// convert map[string]string to map[string]interface{}
133-
labelsMap = utils.Ptr(map[string]interface{}{})
134-
for k, v := range *model.Labels {
135-
(*labelsMap)[k] = v
136-
}
137-
}
138130
payload := iaas.CreateSecurityGroupPayload{
139131
Description: model.Description,
140-
Labels: labelsMap,
132+
Labels: utils.ConvertStringMapToInterfaceMap(*model.Labels),
141133
Name: model.Name,
142134
Stateful: model.Stateful,
143135
}

internal/cmd/security-group/update/update.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
135135
request := apiClient.UpdateSecurityGroup(ctx, model.ProjectId, model.SecurityGroupId)
136136
payload := iaas.NewUpdateSecurityGroupPayload()
137137
payload.Description = model.Description
138-
var labelsMap *map[string]any
139-
if model.Labels != nil && len(*model.Labels) > 0 {
140-
// convert map[string]string to map[string]interface{}
141-
labelsMap = utils.Ptr(map[string]interface{}{})
142-
for k, v := range *model.Labels {
143-
(*labelsMap)[k] = v
144-
}
145-
}
146-
payload.Labels = labelsMap
138+
payload.Labels = utils.ConvertStringMapToInterfaceMap(*model.Labels)
147139
payload.Name = model.Name
148140
request = request.UpdateSecurityGroupPayload(*payload)
149141

internal/cmd/server/create/create.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,6 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
281281

282282
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateServerRequest {
283283
req := apiClient.CreateServer(ctx, model.ProjectId)
284-
var labelsMap *map[string]interface{}
285-
if model.Labels != nil && len(*model.Labels) > 0 {
286-
// convert map[string]string to map[string]interface{}
287-
labelsMap = utils.Ptr(map[string]interface{}{})
288-
for k, v := range *model.Labels {
289-
(*labelsMap)[k] = v
290-
}
291-
}
292284

293285
var userData *[]byte
294286
if model.UserData != nil {
@@ -307,7 +299,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
307299
ServiceAccountMails: model.ServiceAccountMails,
308300
UserData: userData,
309301
Volumes: model.Volumes,
310-
Labels: labelsMap,
302+
Labels: utils.ConvertStringMapToInterfaceMap(*model.Labels),
311303
}
312304

313305
if model.BootVolumePerformanceClass != nil || model.BootVolumeSize != nil || model.BootVolumeDeleteOnTermination != nil || model.BootVolumeSourceId != nil || model.BootVolumeSourceType != nil {

internal/cmd/server/update/update.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
129129
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdateServerRequest {
130130
req := apiClient.UpdateServer(ctx, model.ProjectId, model.ServerId)
131131

132-
var labelsMap *map[string]interface{}
133-
if model.Labels != nil && len(*model.Labels) > 0 {
134-
// convert map[string]string to map[string]interface{}
135-
labelsMap = utils.Ptr(map[string]interface{}{})
136-
for k, v := range *model.Labels {
137-
(*labelsMap)[k] = v
138-
}
139-
}
140-
141132
payload := iaas.UpdateServerPayload{
142133
Name: model.Name,
143-
Labels: labelsMap,
134+
Labels: utils.ConvertStringMapToInterfaceMap(*model.Labels),
144135
}
145136

146137
return req.UpdateServerPayload(payload)

internal/pkg/utils/utils.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,16 @@ func Base64Encode(message []byte) string {
129129
func UserAgentConfigOption(cliVersion string) sdkConfig.ConfigurationOption {
130130
return sdkConfig.WithUserAgent(fmt.Sprintf("stackit-cli/%s", cliVersion))
131131
}
132+
133+
// ConvertStringMapToInterfaceMap converts a map[string]string to a pointer to map[string]interface{}.
134+
// Returns nil if the input map is empty.
135+
func ConvertStringMapToInterfaceMap(m map[string]string) *map[string]interface{} {
136+
if len(m) == 0 {
137+
return nil
138+
}
139+
result := make(map[string]interface{}, len(m))
140+
for k, v := range m {
141+
result[k] = v
142+
}
143+
return &result
144+
}

0 commit comments

Comments
 (0)