Skip to content

Commit 0c7d0bd

Browse files
committed
feature: implemented review suggestions
1 parent a6338e3 commit 0c7d0bd

File tree

4 files changed

+86
-6
lines changed

4 files changed

+86
-6
lines changed

internal/cmd/beta/security-group/delete/delete.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1313
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
1414
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
15+
iaas_utils "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/utils"
1516
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1617
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1718
)
@@ -47,16 +48,18 @@ func NewCmd(p *print.Printer) *cobra.Command {
4748

4849
projectLabel, err := projectname.GetProjectName(ctx, p, cmd)
4950
if err != nil {
50-
return fmt.Errorf("get project name: %w", err)
51+
p.Debug(print.ErrorLevel, "get project name: %v", err)
52+
projectLabel = model.ProjectId
5153
}
5254

53-
securityGroupResp, err := apiClient.GetSecurityGroup(ctx, model.ProjectId, model.SecurityGroupId).Execute()
55+
groupLabel, err := iaas_utils.GetGroupName(ctx, apiClient, model.ProjectId, model.SecurityGroupId)
5456
if err != nil {
55-
return fmt.Errorf("get security group %q: %w", model.SecurityGroupId, err)
57+
p.Warn("get security group name: %v", err)
58+
groupLabel = model.SecurityGroupId
5659
}
5760

5861
if !model.AssumeYes {
59-
prompt := fmt.Sprintf("Are you sure you want to delete the security group %q for %q?", *securityGroupResp.Name, projectLabel)
62+
prompt := fmt.Sprintf("Are you sure you want to delete the security group %q for %q?", groupLabel, projectLabel)
6063
err = p.PromptForConfirmation(prompt)
6164
if err != nil {
6265
return err
@@ -69,7 +72,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
6972
if err := request.Execute(); err != nil {
7073
return fmt.Errorf("delete security group: %w", err)
7174
}
72-
p.Info("Deleted security group %q for %q\n", *securityGroupResp.Name, projectLabel)
75+
p.Info("Deleted security group %q for %q\n", groupLabel, projectLabel)
7376

7477
return nil
7578
},

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

Lines changed: 8 additions & 1 deletion
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+
iaas_utils "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/utils"
1718
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1819
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1920
)
@@ -63,8 +64,14 @@ func NewCmd(p *print.Printer) *cobra.Command {
6364
projectLabel = model.ProjectId
6465
}
6566

67+
groupLabel, err := iaas_utils.GetGroupName(ctx, apiClient, model.ProjectId, model.SecurityGroupId)
68+
if err != nil {
69+
p.Warn("cannot retrieve groupname: %v", err)
70+
groupLabel = model.SecurityGroupId
71+
}
72+
6673
if !model.AssumeYes {
67-
prompt := fmt.Sprintf("Are you sure you want to update the security group %q?", model.SecurityGroupId)
74+
prompt := fmt.Sprintf("Are you sure you want to update the security group %q?", groupLabel)
6875
err = p.PromptForConfirmation(prompt)
6976
if err != nil {
7077
return err

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type IaaSClient interface {
1515
GetNetworkAreaExecute(ctx context.Context, organizationId, areaId string) (*iaas.NetworkArea, error)
1616
ListNetworkAreaProjectsExecute(ctx context.Context, organizationId, areaId string) (*iaas.ProjectListResponse, error)
1717
GetNetworkAreaRangeExecute(ctx context.Context, organizationId, areaId, networkRangeId string) (*iaas.NetworkRange, error)
18+
GetSecurityGroupExecute(ctx context.Context, projectId string, securityGroupId string) (*iaas.SecurityGroup, error)
1819
}
1920

2021
func GetPublicIP(ctx context.Context, apiClient IaaSClient, projectId, publicIpId string) (ip, associatedResource string, err error) {
@@ -98,3 +99,12 @@ func GetNetworkRangeFromAPIResponse(prefix string, networkRanges *[]iaas.Network
9899
}
99100
return iaas.NetworkRange{}, fmt.Errorf("new network range not found in API response")
100101
}
102+
103+
// GetGroupName retrieves the name of a security group for a project
104+
func GetGroupName(ctx context.Context, apiClient IaaSClient, projectId string, securityGroupId string) (string, error) {
105+
group, err := apiClient.GetSecurityGroupExecute(ctx, projectId, securityGroupId)
106+
if err != nil {
107+
return "", fmt.Errorf("get security group: %v", err)
108+
}
109+
return *group.Name, nil
110+
}

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"reflect"
77
"testing"
88

9+
"github.com/google/uuid"
910
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1011
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1112
)
@@ -25,6 +26,8 @@ type IaaSClientMocked struct {
2526
GetAttachedProjectsResp *iaas.ProjectListResponse
2627
GetNetworkAreaRangeFails bool
2728
GetNetworkAreaRangeResp *iaas.NetworkRange
29+
GetSecurityGroupFails bool
30+
ApiGetSecurityGroupResp *iaas.SecurityGroup
2831
}
2932

3033
func (m *IaaSClientMocked) GetPublicIPExecute(_ context.Context, _, _ string) (*iaas.PublicIp, error) {
@@ -76,6 +79,13 @@ func (m *IaaSClientMocked) GetNetworkAreaRangeExecute(_ context.Context, _, _, _
7679
return m.GetNetworkAreaRangeResp, nil
7780
}
7881

82+
func (m *IaaSClientMocked) GetSecurityGroupExecute(ctx context.Context, projectId string, securityGroupId string) (*iaas.SecurityGroup, error) {
83+
if m.GetSecurityGroupFails {
84+
return nil, fmt.Errorf("could not get security group")
85+
}
86+
return m.ApiGetSecurityGroupResp, nil
87+
}
88+
7989
func TestGetPublicIp(t *testing.T) {
8090
type args struct {
8191
getPublicIpFails bool
@@ -551,3 +561,53 @@ func TestGetNetworkRangeFromAPIResponse(t *testing.T) {
551561
})
552562
}
553563
}
564+
565+
func TestGetGroupName(t *testing.T) {
566+
testProjectId := uuid.NewString()
567+
type args struct {
568+
securityGroupId string
569+
}
570+
tests := []struct {
571+
name string
572+
args args
573+
getGroupResp *iaas.SecurityGroup
574+
want string
575+
wantErr bool
576+
}{
577+
{
578+
name: "base",
579+
args: args{
580+
securityGroupId: uuid.NewString(),
581+
},
582+
getGroupResp: &iaas.SecurityGroup{
583+
Name: utils.Ptr("test-group"),
584+
},
585+
want: "test-group",
586+
wantErr: false,
587+
},
588+
{
589+
name: "get fails",
590+
args: args{
591+
securityGroupId: uuid.NewString(),
592+
},
593+
wantErr: true,
594+
},
595+
}
596+
for _, tt := range tests {
597+
t.Run(tt.name, func(t *testing.T) {
598+
m := &IaaSClientMocked{
599+
GetSecurityGroupFails: tt.wantErr,
600+
ApiGetSecurityGroupResp: tt.getGroupResp,
601+
}
602+
603+
got, err := GetGroupName(context.Background(), m, testProjectId, tt.args.securityGroupId)
604+
if (err != nil) != tt.wantErr {
605+
t.Errorf("GetGroupName() error = %v, wantErr %v", err, tt.wantErr)
606+
return
607+
}
608+
if got != tt.want {
609+
t.Errorf("GetGroupName() = %v, want %v", got, tt.want)
610+
}
611+
})
612+
}
613+
}

0 commit comments

Comments
 (0)