Skip to content

Commit 9c806bd

Browse files
committed
refactoring
1 parent 2bef334 commit 9c806bd

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

internal/cmd/volume/snapshot/create/create.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1515
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
1616
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
17+
iaasUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/utils"
1718
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
1819
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1920

@@ -71,12 +72,10 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
7172
}
7273

7374
// Get volume name for label
74-
volumeLabel := model.VolumeID
75-
volume, err := apiClient.GetVolume(ctx, model.ProjectId, model.VolumeID).Execute()
75+
volumeLabel, err := iaasUtils.GetVolumeName(ctx, apiClient, model.ProjectId, model.VolumeID)
7676
if err != nil {
7777
params.Printer.Debug(print.ErrorLevel, "get volume name: %v", err)
78-
} else if volume != nil && volume.Name != nil {
79-
volumeLabel = *volume.Name
78+
volumeLabel = model.VolumeID
8079
}
8180

8281
if !model.AssumeYes {
@@ -105,11 +104,11 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
105104
s.Stop()
106105
}
107106

107+
operationState := "Created"
108108
if model.Async {
109-
params.Printer.Outputf("Triggered snapshot of %q in %q. Snapshot ID: %s\n", volumeLabel, projectLabel, utils.PtrString(resp.Id))
110-
} else {
111-
params.Printer.Outputf("Created snapshot of %q in %q. Snapshot ID: %s\n", volumeLabel, projectLabel, utils.PtrString(resp.Id))
109+
operationState = "Triggered creation of"
112110
}
111+
params.Printer.Outputf("%s snapshot of %q in %q. Snapshot ID: %s\n", operationState, volumeLabel, projectLabel, utils.PtrString(resp.Id))
113112
return nil
114113
},
115114
}

internal/cmd/volume/snapshot/delete/delete.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1313
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1414
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
15+
iaasUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/utils"
1516
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
1617
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1718

@@ -53,12 +54,10 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5354
}
5455

5556
// Get snapshot name for label
56-
snapshotLabel := model.SnapshotId
57-
snapshot, err := apiClient.GetSnapshot(ctx, model.ProjectId, model.SnapshotId).Execute()
57+
snapshotLabel, err := iaasUtils.GetSnapshotName(ctx, apiClient, model.ProjectId, model.SnapshotId)
5858
if err != nil {
5959
params.Printer.Debug(print.ErrorLevel, "get snapshot name: %v", err)
60-
} else if snapshot != nil && snapshot.Name != nil {
61-
snapshotLabel = *snapshot.Name
60+
snapshotLabel = model.SnapshotId
6261
}
6362

6463
if !model.AssumeYes {
@@ -87,11 +86,11 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
8786
s.Stop()
8887
}
8988

89+
operationState := "Deleted"
9090
if model.Async {
91-
params.Printer.Outputf("Triggered deletion of snapshot %q\n", snapshotLabel)
92-
} else {
93-
params.Printer.Outputf("Deleted snapshot %q\n", snapshotLabel)
91+
operationState = "Triggered deletion of"
9492
}
93+
params.Printer.Outputf("%s snapshot %q\n", operationState, snapshotLabel)
9594
return nil
9695
},
9796
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ func GetSnapshotName(ctx context.Context, apiClient IaaSClient, projectId, snaps
177177
resp, err := apiClient.GetSnapshotExecute(ctx, projectId, snapshotId)
178178
if err != nil {
179179
return "", fmt.Errorf("get snapshot: %w", err)
180+
} else if resp == nil {
181+
return "", ErrResponseNil
182+
} else if resp.Name == nil {
183+
return "", ErrNameNil
180184
}
181185
return *resp.Name, nil
182186
}

0 commit comments

Comments
 (0)