Skip to content

Commit e5cbece

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

File tree

7 files changed

+97
-21
lines changed

7 files changed

+97
-21
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+
iaasUtils "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 := iaasUtils.GetSecurityGroupName(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/list/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/goccy/go-yaml"
99
"github.com/spf13/cobra"
10-
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/security-group/utils"
1110
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1211
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
1312
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
@@ -17,6 +16,7 @@ import (
1716
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
1817
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
1918
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
19+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
2020
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
2121
)
2222

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66

77
"github.com/spf13/cobra"
8-
cmd_utils "github.com/stackitcloud/stackit-cli/internal/cmd/beta/security-group/utils"
98
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
109
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
1110
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
@@ -14,6 +13,7 @@ import (
1413
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1514
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
1615
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
16+
iaasUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/utils"
1717
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1818
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
1919
)
@@ -63,8 +63,14 @@ func NewCmd(p *print.Printer) *cobra.Command {
6363
projectLabel = model.ProjectId
6464
}
6565

66+
groupLabel, err := iaasUtils.GetSecurityGroupName(ctx, apiClient, model.ProjectId, model.SecurityGroupId)
67+
if err != nil {
68+
p.Warn("cannot retrieve groupname: %v", err)
69+
groupLabel = model.SecurityGroupId
70+
}
71+
6672
if !model.AssumeYes {
67-
prompt := fmt.Sprintf("Are you sure you want to update the security group %q?", model.SecurityGroupId)
73+
prompt := fmt.Sprintf("Are you sure you want to update the security group %q?", groupLabel)
6874
err = p.PromptForConfirmation(prompt)
6975
if err != nil {
7076
return err
@@ -78,7 +84,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
7884
if err != nil {
7985
return fmt.Errorf("update security group: %w", err)
8086
}
81-
p.Info("Updated security group \"%v\" for %q\n", cmd_utils.PtrString(resp.Name), projectLabel)
87+
p.Info("Updated security group \"%v\" for %q\n", utils.PtrString(resp.Name), projectLabel)
8288

8389
return nil
8490
},

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

Lines changed: 0 additions & 12 deletions
This file was deleted.

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+
// GetSecurityGroupName retrieves the name of a security group for a project
104+
func GetSecurityGroupName(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 TestSecurityGetGroupName(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 := GetSecurityGroupName(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+
}

internal/pkg/utils/utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ func Ptr[T any](v T) *T {
1616
return &v
1717
}
1818

19+
// PtrString creates a string representation of a passed object pointer or returns
20+
// an empty string, if the passed object is _nil_.
21+
func PtrString[T any](t *T) string {
22+
if t != nil {
23+
return fmt.Sprintf("%v", *t)
24+
}
25+
return ""
26+
}
27+
1928
// Int64Ptr returns a pointer to an int64
2029
// Needed because the Ptr function only returns pointer to int
2130
func Int64Ptr(i int64) *int64 {

0 commit comments

Comments
 (0)