Skip to content

Commit b48f589

Browse files
committed
chore(logme): fix for create_test.go
1 parent 3711eff commit b48f589

5 files changed

Lines changed: 37 additions & 32 deletions

File tree

internal/cmd/logme/credentials/create/create.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,13 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
5151
if err != nil {
5252
return err
5353
}
54-
5554
// Configure API client
5655
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
5756
if err != nil {
5857
return err
5958
}
6059

61-
instanceLabel, err := logmeUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId)
60+
instanceLabel, err := logmeUtils.GetInstanceName(ctx, apiClient.DefaultAPI, model.ProjectId, model.Region, model.InstanceId)
6261
if err != nil {
6362
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
6463
instanceLabel = model.InstanceId
@@ -109,7 +108,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
109108
}
110109

111110
func buildRequest(ctx context.Context, model *inputModel, apiClient *logme.APIClient) logme.ApiCreateCredentialsRequest {
112-
req := apiClient.CreateCredentials(ctx, model.ProjectId, model.InstanceId)
111+
req := apiClient.DefaultAPI.CreateCredentials(ctx, model.ProjectId, model.Region, model.InstanceId)
113112
return req
114113
}
115114

@@ -118,26 +117,26 @@ func outputResult(p *print.Printer, outputFormat string, showPassword bool, inst
118117
return fmt.Errorf("credentials response is empty")
119118
}
120119

121-
if !showPassword && resp.HasRaw() && resp.Raw.Credentials != nil {
122-
resp.Raw.Credentials.Password = utils.Ptr("hidden")
120+
if !showPassword && resp.HasRaw() {
121+
resp.Raw.Credentials.Password = "hidden"
123122
}
124123

125124
return p.OutputResult(outputFormat, resp, func() error {
126-
p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, utils.PtrString(resp.Id))
125+
p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, resp.Id)
127126
// The username field cannot be set by the user so we only display it if it's not returned empty
128-
if resp.HasRaw() && resp.Raw.Credentials != nil {
129-
if username := resp.Raw.Credentials.Username; username != nil && *username != "" {
130-
p.Outputf("Username: %s\n", utils.PtrString(username))
127+
if resp.HasRaw() {
128+
if username := resp.Raw.Credentials.Username; username != "" {
129+
p.Outputf("Username: %s\n", username)
131130
}
132131
if !showPassword {
133132
p.Outputf("Password: <hidden>\n")
134133
} else {
135-
p.Outputf("Password: %s\n", utils.PtrString(resp.Raw.Credentials.Password))
134+
p.Outputf("Password: %s\n", resp.Raw.Credentials.Password)
136135
}
137-
p.Outputf("Host: %s\n", utils.PtrString(resp.Raw.Credentials.Host))
136+
p.Outputf("Host: %s\n", resp.Raw.Credentials.Host)
138137
p.Outputf("Port: %s\n", utils.PtrString(resp.Raw.Credentials.Port))
139138
}
140-
p.Outputf("URI: %s\n", utils.PtrString(resp.Uri))
139+
p.Outputf("URI: %s\n", resp.Uri)
141140
return nil
142141
})
143142
}

internal/cmd/logme/credentials/create/create_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
88
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
99
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
10+
loadbalancer "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api"
1011

1112
"github.com/google/go-cmp/cmp"
1213
"github.com/google/go-cmp/cmp/cmpopts"
@@ -15,18 +16,21 @@ import (
1516
)
1617

1718
var projectIdFlag = globalflags.ProjectIdFlag
19+
var regionFlag = globalflags.RegionFlag
1820

1921
type testCtxKey struct{}
2022

2123
var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
22-
var testClient = &logme.APIClient{}
24+
var testClient = &logme.APIClient{DefaultAPI: &logme.DefaultAPIService{}}
2325
var testProjectId = uuid.NewString()
2426
var testInstanceId = uuid.NewString()
27+
var testRegion = "region"
2528

2629
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
2730
flagValues := map[string]string{
28-
projectIdFlag: testProjectId,
29-
instanceIdFlag: testInstanceId,
31+
projectIdFlag: testProjectId,
32+
instanceIdFlag: testInstanceId,
33+
globalflags.RegionFlag: testRegion,
3034
}
3135
for _, mod := range mods {
3236
mod(flagValues)
@@ -39,6 +43,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
3943
GlobalFlagModel: &globalflags.GlobalFlagModel{
4044
ProjectId: testProjectId,
4145
Verbosity: globalflags.VerbosityDefault,
46+
Region: testRegion,
4247
},
4348
InstanceId: testInstanceId,
4449
}
@@ -49,7 +54,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
4954
}
5055

5156
func fixtureRequest(mods ...func(request *logme.ApiCreateCredentialsRequest)) logme.ApiCreateCredentialsRequest {
52-
request := testClient.CreateCredentials(testCtx, testProjectId, testInstanceId)
57+
request := testClient.DefaultAPI.CreateCredentials(testCtx, testProjectId, testRegion, testInstanceId)
5358
for _, mod := range mods {
5459
mod(&request)
5560
}
@@ -155,7 +160,8 @@ func TestBuildRequest(t *testing.T) {
155160

156161
diff := cmp.Diff(request, tt.expectedRequest,
157162
cmp.AllowUnexported(tt.expectedRequest),
158-
cmpopts.EquateComparable(testCtx),
163+
cmpopts.EquateComparable(testCtx, loadbalancer.DefaultAPIService{}),
164+
cmpopts.IgnoreFields(logme.ApiCreateCredentialsRequest{}, "ApiService"),
159165
)
160166
if diff != "" {
161167
t.Fatalf("Data does not match: %s", diff)

internal/cmd/logme/credentials/delete/delete.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
5656
return err
5757
}
5858

59-
instanceLabel, err := logmeUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId)
59+
instanceLabel, err := logmeUtils.GetInstanceName(ctx, apiClient.DefaultAPI, model.ProjectId, model.Region, model.InstanceId)
6060
if err != nil {
6161
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
6262
instanceLabel = model.InstanceId
6363
}
6464

65-
credentialsLabel, err := logmeUtils.GetCredentialsUsername(ctx, apiClient, model.ProjectId, model.InstanceId, model.CredentialsId)
65+
credentialsLabel, err := logmeUtils.GetCredentialsUsername(ctx, apiClient.DefaultAPI, model.ProjectId, model.Region, model.InstanceId, model.CredentialsId)
6666
if err != nil {
6767
params.Printer.Debug(print.ErrorLevel, "get credentials username: %v", err)
6868
credentialsLabel = model.CredentialsId
@@ -115,6 +115,6 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
115115
}
116116

117117
func buildRequest(ctx context.Context, model *inputModel, apiClient *logme.APIClient) logme.ApiDeleteCredentialsRequest {
118-
req := apiClient.DeleteCredentials(ctx, model.ProjectId, model.InstanceId, model.CredentialsId)
118+
req := apiClient.DefaultAPI.DeleteCredentials(ctx, model.ProjectId, model.Region, model.InstanceId, model.CredentialsId)
119119
return req
120120
}

internal/cmd/logme/credentials/describe/describe.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
9999
}
100100

101101
func buildRequest(ctx context.Context, model *inputModel, apiClient *logme.APIClient) logme.ApiGetCredentialsRequest {
102-
req := apiClient.GetCredentials(ctx, model.ProjectId, model.InstanceId, model.CredentialsId)
102+
req := apiClient.DefaultAPI.GetCredentials(ctx, model.ProjectId, model.Region, model.InstanceId, model.CredentialsId)
103103
return req
104104
}
105105

@@ -110,19 +110,19 @@ func outputResult(p *print.Printer, outputFormat string, credentials *logme.Cred
110110

111111
return p.OutputResult(outputFormat, credentials, func() error {
112112
table := tables.NewTable()
113-
table.AddRow("ID", utils.PtrString(credentials.Id))
113+
table.AddRow("ID", credentials.Id)
114114
table.AddSeparator()
115115
// The username field cannot be set by the user so we only display it if it's not returned empty
116116
if raw := credentials.Raw; raw != nil {
117-
if cred := raw.Credentials; cred != nil {
118-
if username := cred.Username; username != nil && *username != "" {
119-
table.AddRow("USERNAME", *username)
120-
table.AddSeparator()
121-
}
122-
table.AddRow("PASSWORD", utils.PtrString(cred.Password))
117+
cred := raw.Credentials
118+
119+
if username := cred.Username; username != "" {
120+
table.AddRow("USERNAME", username)
123121
table.AddSeparator()
124-
table.AddRow("URI", utils.PtrString(cred.Uri))
125122
}
123+
table.AddRow("PASSWORD", cred.Password)
124+
table.AddSeparator()
125+
table.AddRow("URI", utils.PtrString(cred.Uri))
126126
}
127127
err := table.Display(p)
128128
if err != nil {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ func LoadPlanId(planName, version string, offerings *logme.ListOfferingsResponse
6262
}
6363
}
6464

65-
func GetInstanceName(ctx context.Context, apiClient logme.DefaultAPI, projectId, instanceId, region string) (string, error) {
65+
func GetInstanceName(ctx context.Context, apiClient logme.DefaultAPI, projectId, region, instanceId string) (string, error) {
6666
resp, err := apiClient.GetInstance(ctx, projectId, region, instanceId).Execute()
6767
if err != nil {
6868
return "", fmt.Errorf("get LogMe instance: %w", err)
6969
}
7070
return resp.Name, nil
7171
}
7272

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()
73+
func GetCredentialsUsername(ctx context.Context, apiClient logme.DefaultAPI, projectId, region, instanceId, credentialsId string) (string, error) {
74+
resp, err := apiClient.GetCredentials(ctx, projectId, region, instanceId, credentialsId).Execute()
7575
if err != nil {
7676
return "", fmt.Errorf("get LogMe credentials: %w", err)
7777
}

0 commit comments

Comments
 (0)