Skip to content

Commit 9fc85c7

Browse files
committed
refactor duplicate code with util func ConvertStringMapToInterfaceMap
1 parent 40b4145 commit 9fc85c7

File tree

4 files changed

+14
-50
lines changed

4 files changed

+14
-50
lines changed

internal/cmd/volume/backup/create/create.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -181,24 +181,16 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
181181
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateBackupRequest {
182182
req := apiClient.CreateBackup(ctx, model.ProjectId)
183183

184-
// Convert map[string]string to map[string]interface{}
185-
var labelsMap *map[string]interface{}
186-
if len(model.Labels) > 0 {
187-
labelsMap = utils.Ptr(map[string]interface{}{})
188-
for k, v := range model.Labels {
189-
(*labelsMap)[k] = v
190-
}
191-
}
192-
193-
createPayload := iaas.NewCreateBackupPayloadWithDefaults()
194-
createPayload.Name = model.Name
195-
createPayload.Labels = labelsMap
196-
createPayload.Source = &iaas.BackupSource{
197-
Id: &model.SourceID,
198-
Type: &model.SourceType,
184+
payload := iaas.CreateBackupPayload{
185+
Name: model.Name,
186+
Labels: utils.ConvertStringMapToInterfaceMap(utils.Ptr(model.Labels)),
187+
Source: &iaas.BackupSource{
188+
Id: &model.SourceID,
189+
Type: &model.SourceType,
190+
},
199191
}
200192

201-
return req.CreateBackupPayload(*createPayload)
193+
return req.CreateBackupPayload(payload)
202194
}
203195

204196
func outputResult(p *print.Printer, outputFormat string, async bool, sourceLabel, projectLabel string, resp *iaas.Backup) error {

internal/cmd/volume/backup/update/update.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,12 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
130130
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdateBackupRequest {
131131
req := apiClient.UpdateBackup(ctx, model.ProjectId, model.BackupId)
132132

133-
updatePayload := iaas.NewUpdateBackupPayloadWithDefaults()
134-
if model.Name != nil {
135-
updatePayload.Name = model.Name
133+
payload := iaas.UpdateBackupPayload{
134+
Name: model.Name,
135+
Labels: utils.ConvertStringMapToInterfaceMap(utils.Ptr(model.Labels)),
136136
}
137137

138-
// Convert map[string]string to map[string]interface{}
139-
var labelsMap *map[string]interface{}
140-
if len(model.Labels) > 0 {
141-
labelsMap = utils.Ptr(map[string]interface{}{})
142-
for k, v := range model.Labels {
143-
(*labelsMap)[k] = v
144-
}
145-
}
146-
updatePayload.Labels = labelsMap
147-
148-
req = req.UpdateBackupPayload(*updatePayload)
138+
req = req.UpdateBackupPayload(payload)
149139
return req
150140
}
151141

internal/cmd/volume/create/create.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,11 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
174174
Type: model.SourceType,
175175
}
176176

177-
var labelsMap *map[string]interface{}
178-
if model.Labels != nil && len(*model.Labels) > 0 {
179-
// convert map[string]string to map[string]interface{}
180-
labelsMap = utils.Ptr(map[string]interface{}{})
181-
for k, v := range *model.Labels {
182-
(*labelsMap)[k] = v
183-
}
184-
}
185-
186177
payload := iaas.CreateVolumePayload{
187178
AvailabilityZone: model.AvailabilityZone,
188179
Name: model.Name,
189180
Description: model.Description,
190-
Labels: labelsMap,
181+
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
191182
PerformanceClass: model.PerformanceClass,
192183
Size: model.Size,
193184
}

internal/cmd/volume/update/update.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,10 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
135135
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdateVolumeRequest {
136136
req := apiClient.UpdateVolume(ctx, model.ProjectId, model.VolumeId)
137137

138-
var labelsMap *map[string]interface{}
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-
147138
payload := iaas.UpdateVolumePayload{
148139
Name: model.Name,
149140
Description: model.Description,
150-
Labels: labelsMap,
141+
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
151142
}
152143

153144
return req.UpdateVolumePayload(payload)

0 commit comments

Comments
 (0)