Skip to content

Commit c8a96fd

Browse files
committed
add util function, refactor
1 parent d00c465 commit c8a96fd

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1616
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
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/spinner"
1920
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
2021

@@ -79,18 +80,18 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
7980
// Get source name for label (use ID if name not available)
8081
sourceLabel := model.SourceID
8182
if model.SourceType == "volume" {
82-
volume, err := apiClient.GetVolume(ctx, model.ProjectId, model.SourceID).Execute()
83+
name, err := iaasutils.GetVolumeName(ctx, apiClient, model.ProjectId, model.SourceID)
8384
if err != nil {
8485
params.Printer.Debug(print.ErrorLevel, "get volume name: %v", err)
85-
} else if volume != nil && volume.Name != nil {
86-
sourceLabel = *volume.Name
86+
} else if name != "" {
87+
sourceLabel = name
8788
}
8889
} else if model.SourceType == "snapshot" {
89-
snapshot, err := apiClient.GetSnapshot(ctx, model.ProjectId, model.SourceID).Execute()
90+
name, err := iaasutils.GetSnapshotName(ctx, apiClient, model.ProjectId, model.SourceID)
9091
if err != nil {
9192
params.Printer.Debug(print.ErrorLevel, "get snapshot name: %v", err)
92-
} else if snapshot != nil && snapshot.Name != nil {
93-
sourceLabel = *snapshot.Name
93+
} else if name != "" {
94+
sourceLabel = name
9495
}
9596
}
9697

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type IaaSClient interface {
1919
GetNetworkAreaRangeExecute(ctx context.Context, organizationId, areaId, networkRangeId string) (*iaas.NetworkRange, error)
2020
GetImageExecute(ctx context.Context, projectId string, imageId string) (*iaas.Image, error)
2121
GetAffinityGroupExecute(ctx context.Context, projectId string, affinityGroupId string) (*iaas.AffinityGroup, error)
22+
GetSnapshotExecute(ctx context.Context, projectId, snapshotId string) (*iaas.Snapshot, error)
2223
}
2324

2425
func GetSecurityGroupRuleName(ctx context.Context, apiClient IaaSClient, projectId, securityGroupRuleId, securityGroupId string) (string, error) {
@@ -141,3 +142,11 @@ func GetAffinityGroupName(ctx context.Context, apiClient IaaSClient, projectId,
141142
}
142143
return *resp.Name, nil
143144
}
145+
146+
func GetSnapshotName(ctx context.Context, apiClient IaaSClient, projectId, snapshotId string) (string, error) {
147+
resp, err := apiClient.GetSnapshotExecute(ctx, projectId, snapshotId)
148+
if err != nil {
149+
return "", fmt.Errorf("get snapshot: %w", err)
150+
}
151+
return *resp.Name, nil
152+
}

0 commit comments

Comments
 (0)