Skip to content

Commit

Permalink
rpk: rename httpError for httpResponseError
Browse files Browse the repository at this point in the history
We want to make sure that we are only processing
and logging a response from admin API
  • Loading branch information
r-vasquez committed May 24, 2022
1 parent 4b4ec2c commit 3f81cb7
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func needsRestart(clusterStatus admin.ConfigStatusResponse) bool {
func tryMapErrorToCondition(
err error,
) (*redpandav1alpha1.ClusterCondition, error) {
var httpErr *admin.HTTPError
var httpErr *admin.HTTPResponseError
if errors.As(err, &httpErr) {
if httpErr.Response != nil && httpErr.Response.StatusCode == http.StatusBadRequest {
return &redpandav1alpha1.ClusterCondition{
Expand Down
2 changes: 1 addition & 1 deletion src/go/k8s/controllers/redpanda/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (m *mockAdminAPI) PatchClusterConfig(
}
invalidRequest := len(newInvalid)+len(newUnknown) > 0
if m.directValidation && invalidRequest {
return admin.ClusterConfigWriteResult{}, &admin.HTTPError{
return admin.ClusterConfigWriteResult{}, &admin.HTTPResponseError{
Method: http.MethodPut,
URL: "/v1/cluster_config",
Response: &http.Response{
Expand Down
8 changes: 4 additions & 4 deletions src/go/rpk/pkg/api/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
// ErrNoAdminAPILeader happen when there's no leader for the Admin API.
var ErrNoAdminAPILeader = errors.New("no Admin API leader found")

type HTTPError struct {
type HTTPResponseError struct {
Method string
URL string
Response *http.Response
Expand Down Expand Up @@ -549,19 +549,19 @@ func (a *AdminAPI) sendAndReceive(
if err != nil {
return nil, fmt.Errorf("request %s %s failed: %s, unable to read body: %w", method, url, status, err)
}
return nil, &HTTPError{Response: res, Body: resBody}
return nil, &HTTPResponseError{Response: res, Body: resBody}
}

return res, nil
}

func (he HTTPError) DecodeGenericErrorBody() (GenericErrorBody, error) {
func (he HTTPResponseError) DecodeGenericErrorBody() (GenericErrorBody, error) {
var resp GenericErrorBody
err := json.Unmarshal(he.Body, &resp)
return resp, err
}

func (he HTTPError) Error() string {
func (he HTTPResponseError) Error() string {
return fmt.Sprintf("request %s %s failed: %s, body: %q",
he.Method, he.URL, http.StatusText(he.Response.StatusCode), he.Body)
}
7 changes: 3 additions & 4 deletions src/go/rpk/pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func sendMetricsToURL(b metricsBody, url string, conf config.Config) error {
if err != nil {
return err
}
return sendRequest(bs, http.MethodPost, url, conf)
return sendRequest(bs, url, conf)
}

func sendEnvironmentToURL(
Expand All @@ -183,10 +183,10 @@ func sendEnvironmentToURL(
if err != nil {
return err
}
return sendRequest(bs, http.MethodPost, url, conf)
return sendRequest(bs, url, conf)
}

func sendRequest(body []byte, method, url string, conf config.Config) error {
func sendRequest(body []byte, url string, conf config.Config) error {
if !conf.Rpk.EnableUsageStats {
log.Debug("Sending usage stats is disabled.")
return nil
Expand All @@ -200,7 +200,6 @@ func sendRequest(body []byte, method, url string, conf config.Config) error {
if err != nil {
return err
}
log.Debugf("%s '%s' body='%s'", method, url, body)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions src/go/rpk/pkg/cli/cmd/cluster/config/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func importConfig(

// PUT to admin API
result, err := client.PatchClusterConfig(upsert, remove)
if he := (*admin.HTTPError)(nil); errors.As(err, &he) {
if he := (*admin.HTTPResponseError)(nil); errors.As(err, &he) {
// Special case 400 (validation) errors with friendly output
// about which configuration properties were invalid.
if he.Response.StatusCode == 400 {
Expand All @@ -176,7 +176,7 @@ func importConfig(
}

func formatValidationError(
err error, httpErr *admin.HTTPError,
err error, httpErr *admin.HTTPResponseError,
) (string, error) {
// Output structured validation errors from server
var validationErrs map[string]string
Expand Down
2 changes: 1 addition & 1 deletion src/go/rpk/pkg/cli/cmd/cluster/config/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ If an empty string is given as the value, the property is reset to its default.`
}

result, err := client.PatchClusterConfig(upsert, remove)
if he := (*admin.HTTPError)(nil); errors.As(err, &he) {
if he := (*admin.HTTPResponseError)(nil); errors.As(err, &he) {
// Special case 400 (validation) errors with friendly output
// about which configuration properties were invalid.
if he.Response.StatusCode == 400 {
Expand Down
2 changes: 1 addition & 1 deletion src/go/rpk/pkg/cli/cmd/cluster/maintenance/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func newDisableCommand(fs afero.Fs) *cobra.Command {
out.MaybeDie(err, "unable to initialize admin client: %v", err)

err = client.DisableMaintenanceMode(nodeID)
if he := (*admin.HTTPError)(nil); errors.As(err, &he) {
if he := (*admin.HTTPResponseError)(nil); errors.As(err, &he) {
if he.Response.StatusCode == 404 {
body, bodyErr := he.DecodeGenericErrorBody()
if bodyErr == nil {
Expand Down
2 changes: 1 addition & 1 deletion src/go/rpk/pkg/cli/cmd/cluster/maintenance/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ node exists that is already in maintenance mode then an error will be returned.
out.MaybeDie(err, "unable to initialize admin client: %v", err)

err = client.EnableMaintenanceMode(nodeID)
var he *admin.HTTPError
var he *admin.HTTPResponseError
if errors.As(err, &he) {
if he.Response.StatusCode == 404 {
body, bodyErr := he.DecodeGenericErrorBody()
Expand Down

0 comments on commit 3f81cb7

Please sign in to comment.