-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
323 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package kong | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
// AbstractConfigService handles Config in Kong. | ||
type AbstractConfigService interface { | ||
ReloadDeclarativeConfig(ctx context.Context, config io.Reader, checkHash bool) error | ||
ReloadDeclarativeConfigWithSchema(ctx context.Context, config Configuration, checkHash bool) error | ||
} | ||
|
||
// ConfigService handles Config in Kong. | ||
type ConfigService service | ||
|
||
// ReloadDeclarativeConfig sends out the specified config to configured Admin API endpoint | ||
func (c *ConfigService) ReloadDeclarativeConfig(ctx context.Context, config io.Reader, checkHash bool) 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 fmt.Errorf("creating new HTTP request for /config: %w", err) | ||
} | ||
|
||
resp, err := c.client.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 | ||
} | ||
|
||
// ReloadDeclarativeConfigWithSchema sends out the specified config to configured | ||
// Admin API endpoint using the provided reader which should contain. | ||
func (c *ConfigService) ReloadDeclarativeConfigWithSchema(ctx context.Context, config Configuration, checkHash bool) error { | ||
var b bytes.Buffer | ||
if err := json.NewEncoder(&b).Encode(config); err != nil { | ||
return fmt.Errorf("failed decoding config: %w", err) | ||
} | ||
|
||
return c.ReloadDeclarativeConfig(ctx, &b, checkHash) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package kong | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConfigService(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("with_schema/"+tt.name, func(t *testing.T) { | ||
if err := client.Configs.ReloadDeclarativeConfigWithSchema(context.Background(), tt.config, true); (err != nil) != tt.wantErr { | ||
t.Errorf("Client.SendConfig() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
|
||
testsWithReader := []struct { | ||
name string | ||
config io.Reader | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "basic config works", | ||
config: bytes.NewBufferString(`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`), | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range testsWithReader { | ||
client, err := NewTestClient(nil, nil) | ||
require.NoError(t, err) | ||
require.NotNil(t, client) | ||
|
||
tt := tt | ||
t.Run("with_reader/"+tt.name, func(t *testing.T) { | ||
if err := client.Configs.ReloadDeclarativeConfig(context.Background(), tt.config, true); (err != nil) != tt.wantErr { | ||
t.Errorf("Client.SendConfig() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package kong | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewRequestBody(t *testing.T) { | ||
t.Run("body can be string", func(t *testing.T) { | ||
cl, err := NewClient(nil, nil) | ||
require.NoError(t, err) | ||
|
||
body := `{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}` | ||
|
||
req, err := cl.NewRequest("POST", "/", nil, body) | ||
require.NoError(t, err) | ||
|
||
b, err := io.ReadAll(req.Body) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, | ||
`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`, | ||
string(b), | ||
) | ||
}) | ||
|
||
t.Run("body can be []byte", func(t *testing.T) { | ||
cl, err := NewClient(nil, nil) | ||
require.NoError(t, err) | ||
|
||
body := []byte(`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`) | ||
|
||
req, err := cl.NewRequest("POST", "/", nil, body) | ||
require.NoError(t, err) | ||
|
||
b, err := io.ReadAll(req.Body) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, | ||
`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`, | ||
string(b), | ||
) | ||
}) | ||
|
||
t.Run("body can be a bytes.Buffer", func(t *testing.T) { | ||
cl, err := NewClient(nil, nil) | ||
require.NoError(t, err) | ||
|
||
body := bytes.NewBufferString(`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`) | ||
|
||
req, err := cl.NewRequest("POST", "/", nil, body) | ||
require.NoError(t, err) | ||
|
||
b, err := io.ReadAll(req.Body) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, | ||
`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`, | ||
string(b), | ||
) | ||
}) | ||
|
||
t.Run("body can be a map", func(t *testing.T) { | ||
cl, err := NewClient(nil, nil) | ||
require.NoError(t, err) | ||
|
||
body := map[string]any{ | ||
"_format_version": "1.1", | ||
"services": []map[string]any{ | ||
{ | ||
"host": "example.com", | ||
"name": "foo", | ||
}, | ||
}, | ||
} | ||
|
||
req, err := cl.NewRequest("POST", "/", nil, body) | ||
require.NoError(t, err) | ||
|
||
b, err := io.ReadAll(req.Body) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, | ||
`{"_format_version":"1.1","services":[{"host":"example.com","name":"foo"}]}`, | ||
string(b), | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters