Skip to content

Commit

Permalink
feat: drop resp body from ReloadDeclarativeRawConfig signature (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
czeslavo committed Jun 25, 2024
1 parent c2f2a77 commit afe0c7b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Table of Contents

- [v0.57.0](#v0570)
- [v0.56.0](#v0560)
- [v0.55.0](#v0550)
- [v0.54.0](#v0540)
- [v0.53.0](#v0530)
Expand Down Expand Up @@ -67,6 +69,15 @@
- [0.2.0](#020)
- [0.1.0](#010)

## [v0.57.0]

> Release date: 2024/06/25
- **Breaking change** `ReloadDeclarativeRawConfig` now doesn't return a response body
as its first return value - the body can be obtained from `APIError` returned when
the response is not successful.
[#453](https://github.com/Kong/go-kong/pull/453)

## [v0.56.0]

> Release date: 2024/06/25
Expand Down Expand Up @@ -894,6 +905,8 @@ authentication credentials in Kong.
releases of Kong since every release of Kong is introducing breaking changes
to the Admin API.

[v0.57.0]: https://github.com/Kong/go-kong/compare/v0.56.0...v0.57.0
[v0.56.0]: https://github.com/Kong/go-kong/compare/v0.55.0...v0.56.0
[v0.55.0]: https://github.com/Kong/go-kong/compare/v0.54.0...v0.55.0
[v0.54.0]: https://github.com/Kong/go-kong/compare/v0.53.0...v0.54.0
[v0.53.0]: https://github.com/Kong/go-kong/compare/v0.52.0...v0.53.0
Expand Down
14 changes: 7 additions & 7 deletions kong/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,13 @@ func (c *Client) BaseRootURL() string {
// API endpoint using the provided reader which should contain the JSON
// serialized body that adheres to the configuration format specified at:
// https://docs.konghq.com/gateway/latest/production/deployment-topologies/db-less-and-declarative-config/#declarative-configuration-format
// It returns the response body and an error, if it encounters any.
// It returns APIError with a response body in case it receives a valid HTTP response with <200 or >=400 status codes.
func (c *Client) ReloadDeclarativeRawConfig(
ctx context.Context,
config io.Reader,
checkHash bool,
flattenErrors bool,
) ([]byte, error) {
) error {
type sendConfigParams struct {
CheckHash int `url:"check_hash,omitempty"`
FlattenErrors int `url:"flatten_errors,omitempty"`
Expand All @@ -496,22 +496,22 @@ func (c *Client) ReloadDeclarativeRawConfig(
config,
)
if err != nil {
return nil, fmt.Errorf("creating new HTTP request for /config: %w", err)
return fmt.Errorf("creating new HTTP request for /config: %w", err)
}

resp, err := c.DoRAW(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed posting new config to /config: %w", err)
return fmt.Errorf("failed posting new config to /config: %w", err)
}
defer resp.Body.Close()

b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("could not read /config %d status response body: %w", resp.StatusCode, err)
return fmt.Errorf("could not read /config %d status response body: %w", resp.StatusCode, err)
}
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
return b, NewAPIErrorWithRaw(resp.StatusCode, "failed posting new config to /config", b)
return NewAPIErrorWithRaw(resp.StatusCode, "failed posting new config to /config", b)
}

return b, nil
return nil
}
14 changes: 5 additions & 9 deletions kong/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,20 +361,16 @@ func TestReloadDeclarativeRawConfig(t *testing.T) {
b, err := json.Marshal(tt.config)
require.NoError(t, err)

body, err := client.ReloadDeclarativeRawConfig(ctx, bytes.NewBuffer(b), true, flattenErrors)
err = client.ReloadDeclarativeRawConfig(ctx, bytes.NewBuffer(b), true, flattenErrors)

if tt.wantErr {
assert.Error(t, err)
require.IsType(t, err, &APIError{}, "expected APIError")
apiErr := &APIError{}
assert.True(t, errors.As(err, &apiErr))
assert.NotEmpty(t, apiErr.Raw(), "expected non-empty response body in APIError")
} else {
assert.NoError(t, err)
}

// this is somewhat untrue: network or HTTP-level failures _can_ result in a nil response body. however,
// none of our test cases should cause network or HTTP-level failures, so fail if they do occur. if this
// _does_ encounter such a failure, we need to investigate and either update tests or fix some upstream bug
// if it's not some transient issue with the testing environment
require.NotNilf(t, body, "body was nil; should never be nil")
})
}
}
Expand All @@ -388,7 +384,7 @@ func TestReloadDeclarativeRawConfig_NetworkErrorDoesntReturnAPIError(t *testing.
require.NoError(t, err)
require.NotNil(t, client)

_, err = client.ReloadDeclarativeRawConfig(context.Background(), bytes.NewReader([]byte("dummy-config")), true, true)
err = client.ReloadDeclarativeRawConfig(context.Background(), bytes.NewReader([]byte("dummy-config")), true, true)
require.Error(t, err)
require.False(t, errors.Is(err, &APIError{}), "expected error to not be an APIError")
}
Expand Down

0 comments on commit afe0c7b

Please sign in to comment.