From 2f15d71c95c84eef4104b1af2506fece4c79daa2 Mon Sep 17 00:00:00 2001 From: Travis Raines <571832+rainest@users.noreply.github.com> Date: Thu, 2 Feb 2023 14:15:01 -0800 Subject: [PATCH] chore: refactor declarative config functionality Remove the config service and wrap its functionality into the Kong client. --- CHANGELOG.md | 11 ++++++-- kong/client.go | 43 ++++++++++++++++++++++++++++- kong/config_service.go | 62 ------------------------------------------ 3 files changed, 50 insertions(+), 66 deletions(-) delete mode 100644 kong/config_service.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 6600dea87..a1693ab90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,9 +50,14 @@ > Release date: 2023/02/03 -- **Breaking change** to `ConfigService.ReloadDeclarativeRawConfig()`. Its - response signature is now `([]byte, error)`. The byte slice is the config - response body. The error is unchanged. +- **Breaking change:** the `ConfigService` is now directly embedded in the + `kong.Client`. Configurations are collections of entities, not entities + themselves, so they do not fit with other go-kong services. +- **Breaking change:** `ReloadDeclarativeRawConfig()` (formerly part of + `ConfigService`, now part of `kong.Client`) now has the response signature + `([]byte, error)` instead of `error`. The byte slice is the config response + body. The error is unchanged. + [#273](https://github.com/Kong/go-kong/pull/273) ## [v0.36.0] diff --git a/kong/client.go b/kong/client.go index dbde0ab86..76d5f459c 100644 --- a/kong/client.go +++ b/kong/client.go @@ -39,7 +39,6 @@ type Client struct { workspace string // Do not access directly. Use Workspace()/SetWorkspace(). workspaceLock sync.RWMutex // Synchronizes access to workspace. common service - Configs AbstractConfigService ConsumerGroupConsumers AbstractConsumerGroupConsumerService ConsumerGroups AbstractConsumerGroupService Consumers AbstractConsumerService @@ -378,3 +377,45 @@ func (c *Client) RootJSON(ctx context.Context) ([]byte, error) { func (c *Client) BaseRootURL() string { return c.baseRootURL } + +// ReloadDeclarativeRawConfig sends out the specified config to configured Admin +// 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. +func (c *Client) ReloadDeclarativeRawConfig( + ctx context.Context, + config io.Reader, + checkHash bool, +) ([]byte, error) { + type sendConfigParams struct { + CheckHash int `url:"check_hash"` + } + var checkHashI int + if checkHash { + checkHashI = 1 + } + req, err := c.NewRequest("POST", "/config", sendConfigParams{CheckHash: checkHashI}, config) + if err != nil { + return []byte{}, fmt.Errorf("creating new HTTP request for /config: %w", err) + } + + resp, err := c.DoRAW(ctx, req) + if err != nil { + return []byte{}, fmt.Errorf("failed posting new config to /config: %w", err) + } + defer resp.Body.Close() + + var b []byte + if resp.StatusCode < 200 || resp.StatusCode >= 400 { + b, err = io.ReadAll(resp.Body) + if err != nil { + return nil, + fmt.Errorf(`failed posting new config to /config: got status code %d + (and failed to read the response body): %w`, + resp.StatusCode, err) + } + } + + return b, nil +} diff --git a/kong/config_service.go b/kong/config_service.go deleted file mode 100644 index 5bc693165..000000000 --- a/kong/config_service.go +++ /dev/null @@ -1,62 +0,0 @@ -package kong - -import ( - "context" - "fmt" - "io" -) - -// AbstractConfigService handles Config in Kong. -type AbstractConfigService interface { - // ReloadDeclarativeRawConfig sends out the specified config to configured Admin - // 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. - ReloadDeclarativeRawConfig(ctx context.Context, config io.Reader, checkHash bool) ([]byte, error) -} - -// ConfigService handles Config in Kong. -type ConfigService service - -// ReloadDeclarativeRawConfig sends out the specified config to configured Admin -// 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. -func (c *ConfigService) ReloadDeclarativeRawConfig( - ctx context.Context, - config io.Reader, - checkHash bool, -) ([]byte, error) { - type sendConfigParams struct { - CheckHash int `url:"check_hash"` - } - var checkHashI int - if checkHash { - checkHashI = 1 - } - req, err := c.client.NewRequest("POST", "/config", sendConfigParams{CheckHash: checkHashI}, config) - if err != nil { - return []byte{}, fmt.Errorf("creating new HTTP request for /config: %w", err) - } - - resp, err := c.client.DoRAW(ctx, req) - if err != nil { - return []byte{}, fmt.Errorf("failed posting new config to /config: %w", err) - } - defer resp.Body.Close() - - var b []byte - if resp.StatusCode < 200 || resp.StatusCode >= 400 { - b, err = io.ReadAll(resp.Body) - if err != nil { - return nil, - fmt.Errorf(`failed posting new config to /config: got status code %d - (and failed to read the response body): %w`, - resp.StatusCode, err) - } - } - - return b, nil -}