Skip to content

Commit 5447fbb

Browse files
committed
chore(logs): refactor instance create
1 parent 38ff1d3 commit 5447fbb

2 files changed

Lines changed: 49 additions & 19 deletions

File tree

internal/cmd/logs/instance/create/create.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package create
33
import (
44
"context"
55
"fmt"
6+
"math"
67

78
logs "github.com/stackitcloud/stackit-sdk-go/services/logs/v1api"
89

@@ -87,7 +88,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
8788
}
8889

8990
// Call API
90-
req := buildRequest(ctx, model, apiClient)
91+
req, err := buildRequest(ctx, model, apiClient)
9192
if err != nil {
9293
return err
9394
}
@@ -98,15 +99,15 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
9899
if resp == nil {
99100
return fmt.Errorf("create Logs instance: empty response from API")
100101
}
101-
if resp.Id == nil {
102+
if len(resp.Id) == 0 {
102103
return fmt.Errorf("create Logs instance: instance id missing in response")
103104
}
104-
instanceId := *resp.Id
105+
instanceId := resp.Id
105106

106107
// Wait for async operation, if async mode not enabled
107108
if !model.Async {
108109
err := spinner.Run(params.Printer, "Creating instance", func() error {
109-
_, err = wait.CreateLogsInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.Region, instanceId).WaitWithContext(ctx)
110+
_, err = wait.CreateLogsInstanceWaitHandler(ctx, apiClient.DefaultAPI, model.ProjectId, model.Region, instanceId).WaitWithContext(ctx)
110111
return err
111112
})
112113
if err != nil {
@@ -149,16 +150,25 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
149150
return &model, nil
150151
}
151152

152-
func buildRequest(ctx context.Context, model *inputModel, apiClient *logs.APIClient) logs.ApiCreateLogsInstanceRequest {
153-
req := apiClient.CreateLogsInstance(ctx, model.ProjectId, model.Region)
153+
func buildRequest(ctx context.Context, model *inputModel, apiClient *logs.APIClient) (logs.ApiCreateLogsInstanceRequest, error) {
154+
req := apiClient.DefaultAPI.CreateLogsInstance(ctx, model.ProjectId, model.Region)
155+
156+
var retentionDays int32
157+
if model.RetentionDays != nil {
158+
val := *model.RetentionDays
159+
if val < 0 || val > math.MaxInt32 {
160+
return req, fmt.Errorf("metrics frequency value %d overflows int32", val)
161+
}
162+
retentionDays = int32(val)
163+
}
154164

155165
req = req.CreateLogsInstancePayload(logs.CreateLogsInstancePayload{
156-
DisplayName: model.DisplayName,
166+
DisplayName: utils.PtrString(model.DisplayName),
157167
Description: model.Description,
158-
RetentionDays: model.RetentionDays,
159-
Acl: model.ACL,
168+
RetentionDays: retentionDays,
169+
Acl: utils.PtrValue(model.ACL),
160170
})
161-
return req
171+
return req, nil
162172
}
163173

164174
func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *logs.LogsInstance) error {
@@ -173,7 +183,7 @@ func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp
173183
if model.Async {
174184
operationState = "Triggered creation of"
175185
}
176-
p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, utils.PtrString(resp.Id))
186+
p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, resp.Id)
177187
return nil
178188
})
179189
}

internal/cmd/logs/instance/create/create_test.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type testCtxKey struct{}
3131

3232
var (
3333
testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
34-
testClient = &logs.APIClient{}
34+
testClient = &logs.APIClient{DefaultAPI: &logs.DefaultAPIService{}}
3535
testProjectId = uuid.NewString()
3636
)
3737

@@ -72,12 +72,12 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
7272

7373
// Request
7474
func fixtureRequest(mods ...func(request *logs.ApiCreateLogsInstanceRequest)) logs.ApiCreateLogsInstanceRequest {
75-
request := testClient.CreateLogsInstance(testCtx, testProjectId, testRegion)
75+
request := testClient.DefaultAPI.CreateLogsInstance(testCtx, testProjectId, testRegion)
7676
request = request.CreateLogsInstancePayload(logs.CreateLogsInstancePayload{
77-
DisplayName: utils.Ptr(testDisplayName),
77+
DisplayName: testDisplayName,
7878
Description: utils.Ptr(testDescription),
79-
RetentionDays: utils.Ptr(int64(testRetentionDays)),
80-
Acl: utils.Ptr([]string{testAcl}),
79+
RetentionDays: testRetentionDays,
80+
Acl: []string{testAcl},
8181
})
8282

8383
for _, mod := range mods {
@@ -166,11 +166,13 @@ func TestBuildRequest(t *testing.T) {
166166
description string
167167
model *inputModel
168168
expectedRequest logs.ApiCreateLogsInstanceRequest
169+
isValid bool
169170
}{
170171
{
171172
description: "base case",
172173
model: fixtureInputModel(),
173174
expectedRequest: fixtureRequest(),
175+
isValid: true,
174176
},
175177
{
176178
description: "no optional values",
@@ -179,20 +181,38 @@ func TestBuildRequest(t *testing.T) {
179181
model.ACL = nil
180182
}),
181183
expectedRequest: fixtureRequest().CreateLogsInstancePayload(logs.CreateLogsInstancePayload{
182-
DisplayName: utils.Ptr(testDisplayName),
183-
RetentionDays: utils.Ptr(int64(testRetentionDays)),
184+
DisplayName: testDisplayName,
185+
RetentionDays: int32(testRetentionDays),
184186
Description: nil,
185187
Acl: nil,
186188
}),
189+
isValid: true,
190+
},
191+
{
192+
description: "retention days not valid",
193+
model: fixtureInputModel(func(model *inputModel) {
194+
model.RetentionDays = utils.Int64Ptr(1 << 31)
195+
}),
196+
expectedRequest: fixtureRequest(),
197+
isValid: false,
187198
},
188199
}
189200

190201
for _, tt := range tests {
191202
t.Run(tt.description, func(t *testing.T) {
192-
request := buildRequest(testCtx, tt.model, testClient)
203+
request, err := buildRequest(testCtx, tt.model, testClient)
204+
205+
if err != nil {
206+
if !tt.isValid {
207+
return
208+
}
209+
t.Fatalf("error building request: %v", err)
210+
}
211+
193212
diff := cmp.Diff(tt.expectedRequest, request,
194213
cmp.AllowUnexported(tt.expectedRequest),
195214
cmpopts.EquateComparable(testCtx),
215+
cmpopts.IgnoreFields(logs.ApiCreateLogsInstanceRequest{}, "ApiService"),
196216
)
197217
if diff != "" {
198218
t.Fatalf("Data does not match: %s", diff)

0 commit comments

Comments
 (0)