Skip to content

Commit 3711eff

Browse files
committed
chore(logme): refactor logme pkg
1 parent dc92a69 commit 3711eff

3 files changed

Lines changed: 40 additions & 44 deletions

File tree

internal/pkg/services/logme/client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ import (
1010
)
1111

1212
func ConfigureClient(p *print.Printer, cliVersion string) (*logme.APIClient, error) {
13-
return genericclient.ConfigureClientGeneric(p, cliVersion, viper.GetString(config.LogMeCustomEndpointKey), true, genericclient.CreateApiClient[*logme.APIClient](logme.NewAPIClient))
13+
return genericclient.ConfigureClientGeneric(p, cliVersion, viper.GetString(config.LogMeCustomEndpointKey), true, logme.NewAPIClient)
1414
}

internal/pkg/services/logme/utils/utils.go

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ const (
1515
)
1616

1717
func ValidatePlanId(planId string, offerings *logme.ListOfferingsResponse) error {
18-
for _, offer := range *offerings.Offerings {
19-
for _, plan := range *offer.Plans {
20-
if plan.Id != nil && strings.EqualFold(*plan.Id, planId) {
18+
for _, offer := range offerings.Offerings {
19+
for _, plan := range offer.Plans {
20+
if strings.EqualFold(plan.Id, planId) {
2121
return nil
2222
}
2323
}
@@ -33,21 +33,18 @@ func LoadPlanId(planName, version string, offerings *logme.ListOfferingsResponse
3333
availableVersions := ""
3434
availablePlanNames := ""
3535
isValidVersion := false
36-
for _, offer := range *offerings.Offerings {
37-
if !strings.EqualFold(*offer.Version, version) {
38-
availableVersions = fmt.Sprintf("%s\n- %s", availableVersions, *offer.Version)
36+
for _, offer := range offerings.Offerings {
37+
if !strings.EqualFold(offer.Version, version) {
38+
availableVersions = fmt.Sprintf("%s\n- %s", availableVersions, offer.Version)
3939
continue
4040
}
4141
isValidVersion = true
4242

43-
for _, plan := range *offer.Plans {
44-
if plan.Name == nil {
45-
continue
43+
for _, plan := range offer.Plans {
44+
if strings.EqualFold(plan.Name, planName) {
45+
return &plan.Id, nil
4646
}
47-
if strings.EqualFold(*plan.Name, planName) && plan.Id != nil {
48-
return plan.Id, nil
49-
}
50-
availablePlanNames = fmt.Sprintf("%s\n- %s", availablePlanNames, *plan.Name)
47+
availablePlanNames = fmt.Sprintf("%s\n- %s", availablePlanNames, plan.Name)
5148
}
5249
}
5350

@@ -65,23 +62,18 @@ func LoadPlanId(planName, version string, offerings *logme.ListOfferingsResponse
6562
}
6663
}
6764

68-
type LogMeClient interface {
69-
GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*logme.Instance, error)
70-
GetCredentialsExecute(ctx context.Context, projectId, instanceId, credentialsId string) (*logme.CredentialsResponse, error)
71-
}
72-
73-
func GetInstanceName(ctx context.Context, apiClient LogMeClient, projectId, instanceId string) (string, error) {
74-
resp, err := apiClient.GetInstanceExecute(ctx, projectId, instanceId)
65+
func GetInstanceName(ctx context.Context, apiClient logme.DefaultAPI, projectId, instanceId, region string) (string, error) {
66+
resp, err := apiClient.GetInstance(ctx, projectId, region, instanceId).Execute()
7567
if err != nil {
7668
return "", fmt.Errorf("get LogMe instance: %w", err)
7769
}
78-
return *resp.Name, nil
70+
return resp.Name, nil
7971
}
8072

81-
func GetCredentialsUsername(ctx context.Context, apiClient LogMeClient, projectId, instanceId, credentialsId string) (string, error) {
82-
resp, err := apiClient.GetCredentialsExecute(ctx, projectId, instanceId, credentialsId)
73+
func GetCredentialsUsername(ctx context.Context, apiClient logme.DefaultAPI, projectId, instanceId, credentialsId, region string) (string, error) {
74+
resp, err := apiClient.GetCredentials(ctx, projectId, instanceId, region, credentialsId).Execute()
8375
if err != nil {
8476
return "", fmt.Errorf("get LogMe credentials: %w", err)
8577
}
86-
return *resp.Raw.Credentials.Username, nil
78+
return resp.Raw.Credentials.Username, nil
8779
}

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,31 @@ var (
2020
const (
2121
testInstanceName = "instance"
2222
testCredentialsUsername = "username"
23+
testRegion = "region"
2324
)
2425

25-
type logMeClientMocked struct {
26+
type mockSettings struct {
2627
getInstanceFails bool
2728
getInstanceResp *logme.Instance
2829
getCredentialsFails bool
2930
getCredentialsResp *logme.CredentialsResponse
3031
}
3132

32-
func (m *logMeClientMocked) GetInstanceExecute(_ context.Context, _, _ string) (*logme.Instance, error) {
33-
if m.getInstanceFails {
34-
return nil, fmt.Errorf("could not get instance")
35-
}
36-
return m.getInstanceResp, nil
37-
}
38-
39-
func (m *logMeClientMocked) GetCredentialsExecute(_ context.Context, _, _, _ string) (*logme.CredentialsResponse, error) {
40-
if m.getCredentialsFails {
41-
return nil, fmt.Errorf("could not get user")
33+
func newAPIMock(s mockSettings) logme.DefaultAPI {
34+
return &logme.DefaultAPIServiceMock{
35+
GetInstanceExecuteMock: utils.Ptr(func(r logme.ApiGetInstanceRequest) (*logme.Instance, error) {
36+
if s.getInstanceFails {
37+
return nil, fmt.Errorf("could not get instance")
38+
}
39+
return s.getInstanceResp, nil
40+
}),
41+
GetCredentialsExecuteMock: utils.Ptr(func(r logme.ApiGetCredentialsRequest) (*logme.CredentialsResponse, error) {
42+
if s.getCredentialsFails {
43+
return nil, fmt.Errorf("could not get user")
44+
}
45+
return s.getCredentialsResp, nil
46+
}),
4247
}
43-
return m.getCredentialsResp, nil
4448
}
4549

4650
func TestGetInstanceName(t *testing.T) {
@@ -54,7 +58,7 @@ func TestGetInstanceName(t *testing.T) {
5458
{
5559
description: "base",
5660
getInstanceResp: &logme.Instance{
57-
Name: utils.Ptr(testInstanceName),
61+
Name: testInstanceName,
5862
},
5963
isValid: true,
6064
expectedOutput: testInstanceName,
@@ -68,12 +72,12 @@ func TestGetInstanceName(t *testing.T) {
6872

6973
for _, tt := range tests {
7074
t.Run(tt.description, func(t *testing.T) {
71-
client := &logMeClientMocked{
75+
client := mockSettings{
7276
getInstanceFails: tt.getInstanceFails,
7377
getInstanceResp: tt.getInstanceResp,
7478
}
7579

76-
output, err := GetInstanceName(context.Background(), client, testProjectId, testInstanceId)
80+
output, err := GetInstanceName(context.Background(), newAPIMock(client), testProjectId, testInstanceId, testRegion)
7781

7882
if tt.isValid && err != nil {
7983
t.Errorf("failed on valid input")
@@ -103,8 +107,8 @@ func TestGetCredentialsUsername(t *testing.T) {
103107
description: "base",
104108
getCredentialsResp: &logme.CredentialsResponse{
105109
Raw: &logme.RawCredentials{
106-
Credentials: &logme.Credentials{
107-
Username: utils.Ptr(testCredentialsUsername),
110+
Credentials: logme.Credentials{
111+
Username: testCredentialsUsername,
108112
},
109113
},
110114
},
@@ -120,12 +124,12 @@ func TestGetCredentialsUsername(t *testing.T) {
120124

121125
for _, tt := range tests {
122126
t.Run(tt.description, func(t *testing.T) {
123-
client := &logMeClientMocked{
127+
client := mockSettings{
124128
getCredentialsFails: tt.getCredentialsFails,
125129
getCredentialsResp: tt.getCredentialsResp,
126130
}
127131

128-
output, err := GetCredentialsUsername(context.Background(), client, testProjectId, testInstanceId, testCredentialsId)
132+
output, err := GetCredentialsUsername(context.Background(), newAPIMock(client), testProjectId, testInstanceId, testCredentialsId, testRegion)
129133

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

0 commit comments

Comments
 (0)