Skip to content

Commit 9d8faaf

Browse files
refactor(serviceaccount): print custom messages instead of status codes (#413)
1 parent fdded4e commit 9d8faaf

File tree

7 files changed

+86
-11
lines changed

7 files changed

+86
-11
lines changed

cmd/rhoas/pkged.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

locales/cmd/serviceaccount/active.en.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ one = 'could not save credentials to file'
2424
description = 'Error message when service account is not found'
2525
one = 'service account with ID "{{.ID}}" not found'
2626

27+
[serviceAccount.common.error.internalServerError]
28+
one = 'internal server error'
29+
30+
[serviceAccount.common.error.forbidden]
31+
one = 'you are forbidden to {{.Operation}} this service account'
32+
other = 'you are forbidden to {{.Operation}} these service accounts'
33+
2734
[serviceAccount.common.log.info.credentialsSaved]
2835
description = 'Credentials file saved message'
2936
one = 'Credentials saved to {{.FilePath}}'

pkg/cmd/serviceaccount/create/create.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,22 @@ func runCreate(opts *Options) error {
135135
api := connection.API()
136136
a := api.Kafka().CreateServiceAccount(context.Background())
137137
a = a.ServiceAccountRequest(*serviceAccountPayload)
138-
serviceacct, _, apiErr := a.Execute()
138+
serviceacct, httpRes, apiErr := a.Execute()
139139

140140
if apiErr.Error() != "" {
141-
return fmt.Errorf("%v: %v", localizer.MustLocalizeFromID("serviceAccount.create.error.couldNotCreate"), apiErr)
141+
switch httpRes.StatusCode {
142+
case 403:
143+
return fmt.Errorf("%v: %w", localizer.MustLocalize(&localizer.Config{
144+
MessageID: "serviceAccount.common.error.forbidden",
145+
TemplateData: map[string]interface{}{
146+
"Operation": "create",
147+
},
148+
}), apiErr)
149+
case 500:
150+
return fmt.Errorf("%v: %w", localizer.MustLocalizeFromID("serviceAccount.common.error.internalServerError"), apiErr)
151+
default:
152+
return apiErr
153+
}
142154
}
143155

144156
logger.Info(localizer.MustLocalize(&localizer.Config{

pkg/cmd/serviceaccount/delete/delete.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,22 @@ func deleteServiceAccount(opts *Options) error {
123123
api := connection.API()
124124

125125
a := api.Kafka().DeleteServiceAccount(context.Background(), opts.id)
126-
_, _, apiErr := a.Execute()
126+
_, httpRes, apiErr := a.Execute()
127127

128128
if apiErr.Error() != "" {
129-
return fmt.Errorf("%v: %w", localizer.MustLocalizeFromID("serviceAccount.delete.error.unableToDelete"), apiErr)
129+
switch httpRes.StatusCode {
130+
case 403:
131+
return fmt.Errorf("%v: %w", localizer.MustLocalize(&localizer.Config{
132+
MessageID: "serviceAccount.common.error.forbidden",
133+
TemplateData: map[string]interface{}{
134+
"Operation": "delete",
135+
},
136+
}), apiErr)
137+
case 500:
138+
return fmt.Errorf("%v: %w", localizer.MustLocalizeFromID("serviceAccount.common.error.internalServerError"), apiErr)
139+
default:
140+
return apiErr
141+
}
130142
}
131143

132144
logger.Info(localizer.MustLocalizeFromID("serviceAccount.delete.log.info.deleteSuccess"))

pkg/cmd/serviceaccount/describe/describe.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,29 @@ func runDescribe(opts *Options) error {
6767
api := connection.API()
6868

6969
a := api.Kafka().GetServiceAccountById(context.Background(), opts.id)
70-
res, _, apiErr := a.Execute()
70+
res, httpRes, apiErr := a.Execute()
7171

7272
if apiErr.Error() != "" {
73-
return fmt.Errorf("%v: %w", localizer.MustLocalizeFromID("serviceAccount.describe.error.unableToDescribe"), apiErr)
73+
switch httpRes.StatusCode {
74+
case 404:
75+
return fmt.Errorf(localizer.MustLocalize(&localizer.Config{
76+
MessageID: "serviceAccount.common.error.notFoundError",
77+
TemplateData: map[string]interface{}{
78+
"ID": opts.id,
79+
},
80+
}))
81+
case 403:
82+
return fmt.Errorf("%v: %w", localizer.MustLocalize(&localizer.Config{
83+
MessageID: "serviceAccount.common.error.forbidden",
84+
TemplateData: map[string]interface{}{
85+
"Operation": "view",
86+
},
87+
}), apiErr)
88+
case 500:
89+
return fmt.Errorf("%v: %w", localizer.MustLocalizeFromID("serviceAccount.common.error.internalServerError"), apiErr)
90+
default:
91+
return apiErr
92+
}
7493
}
7594

7695
switch opts.outputFormat {

pkg/cmd/serviceaccount/list/list.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,23 @@ func runList(opts *Options) (err error) {
7979
api := connection.API()
8080

8181
a := api.Kafka().ListServiceAccounts(context.Background())
82-
res, _, apiErr := a.Execute()
82+
res, httpRes, apiErr := a.Execute()
8383

8484
if apiErr.Error() != "" {
85-
return fmt.Errorf("%v: %w", localizer.MustLocalizeFromID("serviceAccount.list.error.unableToList"), apiErr)
85+
switch httpRes.StatusCode {
86+
case 403:
87+
return fmt.Errorf("%v: %w", localizer.MustLocalize(&localizer.Config{
88+
MessageID: "serviceAccount.common.error.forbidden",
89+
PluralCount: 2,
90+
TemplateData: map[string]interface{}{
91+
"Operation": "list",
92+
},
93+
}), apiErr)
94+
case 500:
95+
return fmt.Errorf("%v: %w", localizer.MustLocalizeFromID("serviceAccount.common.error.internalServerError"), apiErr)
96+
default:
97+
return apiErr
98+
}
8699
}
87100

88101
serviceaccounts := res.GetItems()

pkg/cmd/serviceaccount/resetcredentials/reset_credentials.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,22 @@ func resetCredentials(name string, opts *Options) (*kasclient.ServiceAccount, er
212212
},
213213
}))
214214

215-
serviceacct, _, apiErr := api.Kafka().ResetServiceAccountCreds(context.Background(), opts.id).Execute()
215+
serviceacct, httpRes, apiErr := api.Kafka().ResetServiceAccountCreds(context.Background(), opts.id).Execute()
216216

217217
if apiErr.Error() != "" {
218-
return nil, apiErr
218+
switch httpRes.StatusCode {
219+
case 403:
220+
return nil, fmt.Errorf("%v: %w", localizer.MustLocalize(&localizer.Config{
221+
MessageID: "serviceAccount.common.error.forbidden",
222+
TemplateData: map[string]interface{}{
223+
"Operation": "update",
224+
},
225+
}), apiErr)
226+
case 500:
227+
return nil, fmt.Errorf("%v: %w", localizer.MustLocalizeFromID("serviceAccount.common.error.internalServerError"), apiErr)
228+
default:
229+
return nil, apiErr
230+
}
219231
}
220232

221233
return &serviceacct, nil

0 commit comments

Comments
 (0)