Skip to content

Commit 8ef4d89

Browse files
committed
chore(logs): refactor utils
1 parent 5447fbb commit 8ef4d89

2 files changed

Lines changed: 27 additions & 50 deletions

File tree

internal/pkg/services/logs/utils/utils.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,24 @@ import (
1010

1111
var (
1212
ErrResponseNil = errors.New("response is nil")
13-
ErrNameNil = errors.New("display name is nil")
1413
)
1514

16-
type LogsClient interface {
17-
GetLogsInstanceExecute(ctx context.Context, projectId, regionId, instanceId string) (*logs.LogsInstance, error)
18-
GetAccessTokenExecute(ctx context.Context, projectId string, regionId string, instanceId string, tId string) (*logs.AccessToken, error)
19-
}
20-
21-
func GetInstanceName(ctx context.Context, apiClient LogsClient, projectId, regionId, instanceId string) (string, error) {
22-
resp, err := apiClient.GetLogsInstanceExecute(ctx, projectId, regionId, instanceId)
15+
func GetInstanceName(ctx context.Context, apiClient logs.DefaultAPI, projectId, regionId, instanceId string) (string, error) {
16+
resp, err := apiClient.GetLogsInstance(ctx, projectId, regionId, instanceId).Execute()
2317
if err != nil {
2418
return "", fmt.Errorf("get Logs instance: %w", err)
2519
} else if resp == nil {
2620
return "", ErrResponseNil
27-
} else if resp.DisplayName == nil {
28-
return "", ErrNameNil
2921
}
30-
return *resp.DisplayName, nil
22+
return resp.DisplayName, nil
3123
}
3224

33-
func GetAccessTokenName(ctx context.Context, apiClient LogsClient, projectId, regionId, instanceId, accessTokenId string) (string, error) {
34-
resp, err := apiClient.GetAccessTokenExecute(ctx, projectId, regionId, instanceId, accessTokenId)
25+
func GetAccessTokenName(ctx context.Context, apiClient logs.DefaultAPI, projectId, regionId, instanceId, accessTokenId string) (string, error) {
26+
resp, err := apiClient.GetAccessToken(ctx, projectId, regionId, instanceId, accessTokenId).Execute()
3527
if err != nil {
3628
return "", fmt.Errorf("get Logs access token: %w", err)
3729
} else if resp == nil {
3830
return "", ErrResponseNil
39-
} else if resp.DisplayName == nil {
40-
return "", ErrNameNil
4131
}
42-
return *resp.DisplayName, nil
32+
return resp.DisplayName, nil
4333
}

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

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,28 @@ const (
2323
testRegion = "eu01"
2424
)
2525

26-
type logsClientMocked struct {
26+
type mockSettings struct {
2727
getInstanceFails bool
2828
getInstanceResp *logs.LogsInstance
2929
getAccessTokenFails bool
3030
getAccessTokenResp *logs.AccessToken
3131
}
3232

33-
func (m *logsClientMocked) GetLogsInstanceExecute(_ context.Context, _, _, _ string) (*logs.LogsInstance, error) {
34-
if m.getInstanceFails {
35-
return nil, fmt.Errorf("could not get instance")
36-
}
37-
return m.getInstanceResp, nil
38-
}
39-
40-
func (m *logsClientMocked) GetAccessTokenExecute(_ context.Context, _, _, _, _ string) (*logs.AccessToken, error) {
41-
if m.getAccessTokenFails {
42-
return nil, fmt.Errorf("could not get access token")
33+
func newAPIMock(s mockSettings) logs.DefaultAPI {
34+
return &logs.DefaultAPIServiceMock{
35+
GetLogsInstanceExecuteMock: utils.Ptr(func(_ logs.ApiGetLogsInstanceRequest) (*logs.LogsInstance, error) {
36+
if s.getInstanceFails {
37+
return nil, fmt.Errorf("could not get instance")
38+
}
39+
return s.getInstanceResp, nil
40+
}),
41+
GetAccessTokenExecuteMock: utils.Ptr(func(_ logs.ApiGetAccessTokenRequest) (*logs.AccessToken, error) {
42+
if s.getAccessTokenFails {
43+
return nil, fmt.Errorf("could not get access token")
44+
}
45+
return s.getAccessTokenResp, nil
46+
}),
4347
}
44-
return m.getAccessTokenResp, nil
4548
}
4649

4750
func TestGetInstanceName(t *testing.T) {
@@ -55,7 +58,7 @@ func TestGetInstanceName(t *testing.T) {
5558
{
5659
description: "base",
5760
getInstanceResp: &logs.LogsInstance{
58-
DisplayName: utils.Ptr(testInstanceName),
61+
DisplayName: testInstanceName,
5962
},
6063
isValid: true,
6164
expectedOutput: testInstanceName,
@@ -71,24 +74,16 @@ func TestGetInstanceName(t *testing.T) {
7174
getInstanceResp: nil,
7275
isValid: false,
7376
},
74-
{
75-
description: "name in response is nil",
76-
getInstanceFails: false,
77-
getInstanceResp: &logs.LogsInstance{
78-
DisplayName: nil,
79-
},
80-
isValid: false,
81-
},
8277
}
8378

8479
for _, tt := range tests {
8580
t.Run(tt.description, func(t *testing.T) {
86-
client := &logsClientMocked{
81+
client := mockSettings{
8782
getInstanceFails: tt.getInstanceFails,
8883
getInstanceResp: tt.getInstanceResp,
8984
}
9085

91-
output, err := GetInstanceName(context.Background(), client, testProjectId, testRegion, testInstanceId)
86+
output, err := GetInstanceName(context.Background(), newAPIMock(client), testProjectId, testRegion, testInstanceId)
9287

9388
if tt.isValid && err != nil {
9489
t.Errorf("failed on valid input")
@@ -117,7 +112,7 @@ func TestGetAccessTokenName(t *testing.T) {
117112
{
118113
description: "base",
119114
getAccessTokenResp: &logs.AccessToken{
120-
DisplayName: utils.Ptr(testInstanceName),
115+
DisplayName: testInstanceName,
121116
},
122117
isValid: true,
123118
expectedOutput: testInstanceName,
@@ -133,24 +128,16 @@ func TestGetAccessTokenName(t *testing.T) {
133128
getAccessTokenResp: nil,
134129
isValid: false,
135130
},
136-
{
137-
description: "name in response is nil",
138-
getAccessTokenFails: false,
139-
getAccessTokenResp: &logs.AccessToken{
140-
DisplayName: nil,
141-
},
142-
isValid: false,
143-
},
144131
}
145132

146133
for _, tt := range tests {
147134
t.Run(tt.description, func(t *testing.T) {
148-
client := &logsClientMocked{
135+
client := mockSettings{
149136
getAccessTokenFails: tt.getAccessTokenFails,
150137
getAccessTokenResp: tt.getAccessTokenResp,
151138
}
152139

153-
output, err := GetAccessTokenName(context.Background(), client, testProjectId, testRegion, testInstanceId, testAccessTokenId)
140+
output, err := GetAccessTokenName(context.Background(), newAPIMock(client), testProjectId, testRegion, testInstanceId, testAccessTokenId)
154141

155142
if tt.isValid && err != nil {
156143
t.Errorf("failed on valid input")

0 commit comments

Comments
 (0)