Skip to content

Commit a333dc3

Browse files
committed
refactoring
1 parent c8a96fd commit a333dc3

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

internal/cmd/volume/backup/delete/delete.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
1616
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1717

18+
iaasutils "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/utils"
1819
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1920
"github.com/stackitcloud/stackit-sdk-go/services/iaas/wait"
2021
)
@@ -51,14 +52,10 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5152
return err
5253
}
5354

54-
backup, err := apiClient.GetBackup(ctx, model.ProjectId, model.BackupId).Execute()
55+
backupLabel, err := iaasutils.GetBackupName(ctx, apiClient, model.ProjectId, model.BackupId)
5556
if err != nil {
5657
params.Printer.Debug(print.ErrorLevel, "get backup name: %v", err)
5758
}
58-
backupLabel := model.BackupId
59-
if backup != nil && backup.Name != nil {
60-
backupLabel = *backup.Name
61-
}
6259

6360
if !model.AssumeYes {
6461
prompt := fmt.Sprintf("Are you sure you want to delete backup %q? (This cannot be undone)", backupLabel)

internal/cmd/volume/backup/restore/restore.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
1616
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1717

18+
iaasutils "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/utils"
1819
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1920
"github.com/stackitcloud/stackit-sdk-go/services/iaas/wait"
2021
)
@@ -51,25 +52,21 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5152
return err
5253
}
5354

54-
// Get backup details for labels
55-
backup, err := apiClient.GetBackup(ctx, model.ProjectId, model.BackupId).Execute()
55+
backupLabel, err := iaasutils.GetBackupName(ctx, apiClient, model.ProjectId, model.BackupId)
5656
if err != nil {
5757
params.Printer.Debug(print.ErrorLevel, "get backup details: %v", err)
5858
}
59-
backupLabel := model.BackupId
60-
if backup != nil && backup.Name != nil {
61-
backupLabel = *backup.Name
62-
}
6359

6460
// Get source details for labels
6561
var sourceLabel string
66-
if backup != nil && backup.VolumeId != nil {
62+
backup, err := apiClient.GetBackup(ctx, model.ProjectId, model.BackupId).Execute()
63+
if err == nil && backup != nil && backup.VolumeId != nil {
6764
sourceLabel = *backup.VolumeId
68-
volume, err := apiClient.GetVolume(ctx, model.ProjectId, *backup.VolumeId).Execute()
65+
name, err := iaasutils.GetVolumeName(ctx, apiClient, model.ProjectId, *backup.VolumeId)
6966
if err != nil {
7067
params.Printer.Debug(print.ErrorLevel, "get volume details: %v", err)
71-
} else if volume.Name != nil {
72-
sourceLabel = *volume.Name
68+
} else if name != "" {
69+
sourceLabel = name
7370
}
7471
}
7572

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1616
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1717
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
18+
iaasutils "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/utils"
1819
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1920

2021
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
@@ -54,6 +55,17 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5455
return err
5556
}
5657

58+
// Configure API client
59+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
60+
if err != nil {
61+
return err
62+
}
63+
64+
backupLabel, err := iaasutils.GetBackupName(ctx, apiClient, model.ProjectId, model.BackupId)
65+
if err != nil {
66+
params.Printer.Debug(print.ErrorLevel, "get backup name: %v", err)
67+
}
68+
5769
if !model.AssumeYes {
5870
prompt := fmt.Sprintf("Are you sure you want to update backup %q? (This cannot be undone)", model.BackupId)
5971
err = params.Printer.PromptForConfirmation(prompt)
@@ -62,25 +74,13 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6274
}
6375
}
6476

65-
// Configure API client
66-
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
67-
if err != nil {
68-
return err
69-
}
70-
7177
// Call API
7278
req := buildRequest(ctx, model, apiClient)
7379
resp, err := req.Execute()
7480
if err != nil {
7581
return fmt.Errorf("update backup: %w", err)
7682
}
7783

78-
// Get backup label (use ID if name not available)
79-
backupLabel := model.BackupId
80-
if resp.Name != nil {
81-
backupLabel = *resp.Name
82-
}
83-
8484
return outputResult(params.Printer, model.OutputFormat, backupLabel, resp)
8585
},
8686
}

internal/pkg/services/iaas/utils/utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type IaaSClient interface {
2020
GetImageExecute(ctx context.Context, projectId string, imageId string) (*iaas.Image, error)
2121
GetAffinityGroupExecute(ctx context.Context, projectId string, affinityGroupId string) (*iaas.AffinityGroup, error)
2222
GetSnapshotExecute(ctx context.Context, projectId, snapshotId string) (*iaas.Snapshot, error)
23+
GetBackupExecute(ctx context.Context, projectId, backupId string) (*iaas.Backup, error)
2324
}
2425

2526
func GetSecurityGroupRuleName(ctx context.Context, apiClient IaaSClient, projectId, securityGroupRuleId, securityGroupId string) (string, error) {
@@ -150,3 +151,14 @@ func GetSnapshotName(ctx context.Context, apiClient IaaSClient, projectId, snaps
150151
}
151152
return *resp.Name, nil
152153
}
154+
155+
func GetBackupName(ctx context.Context, apiClient IaaSClient, projectId, backupId string) (string, error) {
156+
resp, err := apiClient.GetBackupExecute(ctx, projectId, backupId)
157+
if err != nil {
158+
return backupId, fmt.Errorf("get backup: %w", err)
159+
}
160+
if resp != nil && resp.Name != nil {
161+
return *resp.Name, nil
162+
}
163+
return backupId, nil
164+
}

0 commit comments

Comments
 (0)