Skip to content

Commit

Permalink
feat: add Client.SendConfig()
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Dec 14, 2022
1 parent cbe9681 commit 528d319
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
34 changes: 34 additions & 0 deletions kong/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,37 @@ func (c *Client) RootJSON(ctx context.Context) ([]byte, error) {

return body, nil
}

// SendConfig sends out the specified config to configured Admin API endpoint
func (c *Client) SendConfig(ctx context.Context, config Configuration) error {
type sendConfigParams struct {
CheckHash int `url:"check_hash"`
}
req, err := c.NewRequest("POST", "/config", sendConfigParams{CheckHash: 1}, config)
if err != nil {
return fmt.Errorf("creating new HTTP request for /config: %w", err)
}

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

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

return fmt.Errorf(
"failed posting new config to /config: got status code %d, body: %s",
resp.StatusCode, b,
)
}

return nil
}
70 changes: 70 additions & 0 deletions kong/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,73 @@ func TestTestWorkspace(T *testing.T) {
assert.NoError(err)
assert.Equal(currWorkspace.Config, origWorkspace.Config)
}

func TestClient_SendConfig(t *testing.T) {
RunWhenDbMode(t, "off")

tests := []struct {
name string
config Configuration
wantErr bool
}{
{
name: "basic config works",
config: Configuration{
"_format_version": "1.1",
"services": []Configuration{
{
"host": "mockbin.com",
"port": 443,
"protocol": "https",
"routes": []Configuration{
{"paths": []string{"/"}},
},
},
},
},
wantErr: false,
},
{
name: "missing _format_version fails",
config: Configuration{
"services": []Configuration{
{
"host": "mockbin.com",
"port": 443,
"protocol": "https",
"routes": []Configuration{
{"paths": []string{"/"}},
},
},
},
},
wantErr: true,
},
{
name: "invalid config fails",
config: Configuration{
"dummy_key": []Configuration{
{
"host": "mockbin.com",
"port": 443,
"protocol": "https",
},
},
},
wantErr: true,
},
}

for _, tt := range tests {
client, err := NewTestClient(nil, nil)
require.NoError(t, err)
require.NotNil(t, client)

tt := tt
t.Run(tt.name, func(t *testing.T) {
if err := client.SendConfig(context.Background(), tt.config); (err != nil) != tt.wantErr {
t.Errorf("Client.SendConfig() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
39 changes: 39 additions & 0 deletions kong/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,42 @@ func NewTestClient(baseURL *string, client *http.Client) (*Client, error) {
}
return NewClient(baseURL, client)
}

func RunWhenDbMode(t *testing.T, dbmode string) {
client, err := NewTestClient(nil, nil)
if err != nil {
t.Error(err)
}
info, err := client.Root(defaultCtx)
if err != nil {
t.Error(err)
}

config, ok := info["configuration"]
if !ok {
t.Logf("failed to find 'configuration' config key in kong confiration")
t.Skip()
}

configuration, ok := config.(map[string]any)
if !ok {
t.Logf("'configuration' key is not a map but %T", config)
t.Skip()
}

dbConfig, ok := configuration["database"]
if !ok {
t.Logf("failed to find 'database' config key in kong confiration")
t.Skip()
}

dbMode, ok := dbConfig.(string)
if !ok {
t.Logf("'database' config key is not a string but %T", dbConfig)
t.Skip()
}

if dbMode != dbmode {
t.Skip()
}
}

0 comments on commit 528d319

Please sign in to comment.