Skip to content

Commit 3a5ac24

Browse files
authored
Onboard Secrets Manager (ACL): describe command (#168)
* Onboard Secrets Manager (ACLs): describe command * update json output * improve code to print ACLs
1 parent 503cec8 commit 3a5ac24

2 files changed

Lines changed: 75 additions & 11 deletions

File tree

internal/cmd/secrets-manager/instance/describe/describe.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"strings"
78

89
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
910
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
@@ -52,14 +53,21 @@ func NewCmd() *cobra.Command {
5253
return err
5354
}
5455

55-
// Call API
56-
req := buildRequest(ctx, model, apiClient)
57-
resp, err := req.Execute()
56+
// Call API to get instance details
57+
req := buildGetInstanceRequest(ctx, model, apiClient)
58+
instance, err := req.Execute()
5859
if err != nil {
5960
return fmt.Errorf("read Secrets Manager instance: %w", err)
6061
}
6162

62-
return outputResult(cmd, model.OutputFormat, resp)
63+
// Call API to get instance acls
64+
listACLsReq := buildListACLsRequest(ctx, model, apiClient)
65+
aclList, err := listACLsReq.Execute()
66+
if err != nil {
67+
return fmt.Errorf("read Secrets Manager instance ACLs: %w", err)
68+
}
69+
70+
return outputResult(cmd, model.OutputFormat, instance, aclList)
6371
},
6472
}
6573
return cmd
@@ -79,12 +87,17 @@ func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
7987
}, nil
8088
}
8189

82-
func buildRequest(ctx context.Context, model *inputModel, apiClient *secretsmanager.APIClient) secretsmanager.ApiGetInstanceRequest {
90+
func buildGetInstanceRequest(ctx context.Context, model *inputModel, apiClient *secretsmanager.APIClient) secretsmanager.ApiGetInstanceRequest {
8391
req := apiClient.GetInstance(ctx, model.ProjectId, model.InstanceId)
8492
return req
8593
}
8694

87-
func outputResult(cmd *cobra.Command, outputFormat string, instance *secretsmanager.Instance) error {
95+
func buildListACLsRequest(ctx context.Context, model *inputModel, apiClient *secretsmanager.APIClient) secretsmanager.ApiListACLsRequest {
96+
req := apiClient.ListACLs(ctx, model.ProjectId, model.InstanceId)
97+
return req
98+
}
99+
100+
func outputResult(cmd *cobra.Command, outputFormat string, instance *secretsmanager.Instance, aclList *secretsmanager.AclList) error {
88101
switch outputFormat {
89102
case globalflags.PrettyOutputFormat:
90103

@@ -101,14 +114,29 @@ func outputResult(cmd *cobra.Command, outputFormat string, instance *secretsmana
101114
table.AddSeparator()
102115
table.AddRow("CREATION DATE", *instance.CreationStartDate)
103116
table.AddSeparator()
117+
// Only show ACL if it's present and not empty
118+
if aclList != nil && aclList.Acls != nil && len(*aclList.Acls) > 0 {
119+
var cidrs []string
120+
121+
for _, acl := range *aclList.Acls {
122+
cidrs = append(cidrs, *acl.Cidr)
123+
}
124+
125+
table.AddRow("ACL", strings.Join(cidrs, ","))
126+
}
104127
err := table.Display(cmd)
105128
if err != nil {
106129
return fmt.Errorf("render table: %w", err)
107130
}
108131

109132
return nil
110133
default:
111-
details, err := json.MarshalIndent(instance, "", " ")
134+
output := struct {
135+
*secretsmanager.Instance
136+
*secretsmanager.AclList
137+
}{instance, aclList}
138+
139+
details, err := json.MarshalIndent(output, "", " ")
112140
if err != nil {
113141
return fmt.Errorf("marshal Secrets Manager instance: %w", err)
114142
}

internal/cmd/secrets-manager/instance/describe/describe_test.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,22 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
5454
return model
5555
}
5656

57-
func fixtureRequest(mods ...func(request *secretsmanager.ApiGetInstanceRequest)) secretsmanager.ApiGetInstanceRequest {
57+
func fixtureGetInstanceRequest(mods ...func(request *secretsmanager.ApiGetInstanceRequest)) secretsmanager.ApiGetInstanceRequest {
5858
request := testClient.GetInstance(testCtx, testProjectId, testInstanceId)
5959
for _, mod := range mods {
6060
mod(&request)
6161
}
6262
return request
6363
}
6464

65+
func fixtureListACLsRequest(mods ...func(request *secretsmanager.ApiListACLsRequest)) secretsmanager.ApiListACLsRequest {
66+
request := testClient.ListACLs(testCtx, testProjectId, testInstanceId)
67+
for _, mod := range mods {
68+
mod(&request)
69+
}
70+
return request
71+
}
72+
6573
func TestParseInput(t *testing.T) {
6674
tests := []struct {
6775
description string
@@ -186,7 +194,7 @@ func TestParseInput(t *testing.T) {
186194
}
187195
}
188196

189-
func TestBuildRequest(t *testing.T) {
197+
func TestBuildGetInstanceRequest(t *testing.T) {
190198
tests := []struct {
191199
description string
192200
model *inputModel
@@ -195,13 +203,41 @@ func TestBuildRequest(t *testing.T) {
195203
{
196204
description: "base",
197205
model: fixtureInputModel(),
198-
expectedRequest: fixtureRequest(),
206+
expectedRequest: fixtureGetInstanceRequest(),
207+
},
208+
}
209+
210+
for _, tt := range tests {
211+
t.Run(tt.description, func(t *testing.T) {
212+
request := buildGetInstanceRequest(testCtx, tt.model, testClient)
213+
214+
diff := cmp.Diff(request, tt.expectedRequest,
215+
cmp.AllowUnexported(tt.expectedRequest),
216+
cmpopts.EquateComparable(testCtx),
217+
)
218+
if diff != "" {
219+
t.Fatalf("Data does not match: %s", diff)
220+
}
221+
})
222+
}
223+
}
224+
225+
func TestBuildGetACLsRequest(t *testing.T) {
226+
tests := []struct {
227+
description string
228+
model *inputModel
229+
expectedRequest secretsmanager.ApiListACLsRequest
230+
}{
231+
{
232+
description: "base",
233+
model: fixtureInputModel(),
234+
expectedRequest: fixtureListACLsRequest(),
199235
},
200236
}
201237

202238
for _, tt := range tests {
203239
t.Run(tt.description, func(t *testing.T) {
204-
request := buildRequest(testCtx, tt.model, testClient)
240+
request := buildListACLsRequest(testCtx, tt.model, testClient)
205241

206242
diff := cmp.Diff(request, tt.expectedRequest,
207243
cmp.AllowUnexported(tt.expectedRequest),

0 commit comments

Comments
 (0)