Skip to content

Commit

Permalink
chore: split api and remote api
Browse files Browse the repository at this point in the history
  • Loading branch information
avallete committed Oct 11, 2024
1 parent 15ff758 commit 23173ec
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
23 changes: 13 additions & 10 deletions pkg/config/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import (
)

type (
api struct {
Enabled bool `toml:"enabled"`
Image string `toml:"-"`
KongImage string `toml:"-"`
Port uint16 `toml:"port"`
RemoteApi struct {
Schemas []string `toml:"schemas"`
ExtraSearchPath []string `toml:"extra_search_path"`
MaxRows uint `toml:"max_rows"`
Tls tlsKong `toml:"tls"`
}
api struct {
RemoteApi
Enabled bool `toml:"enabled"`
Image string `toml:"-"`
KongImage string `toml:"-"`
Port uint16 `toml:"port"`
Tls tlsKong `toml:"tls"`
// TODO: replace [auth|studio].api_url
ExternalUrl string `toml:"external_url"`
}
Expand All @@ -25,7 +28,7 @@ type (
}
)

func (a *api) ToUpdatePostgrestConfigBody() v1API.UpdatePostgrestConfigBody {
func (a *RemoteApi) ToUpdatePostgrestConfigBody() v1API.UpdatePostgrestConfigBody {
body := v1API.UpdatePostgrestConfigBody{}

// Convert Schemas to a comma-separated string
Expand All @@ -50,7 +53,7 @@ func (a *api) ToUpdatePostgrestConfigBody() v1API.UpdatePostgrestConfigBody {
return body
}

func (a *api) FromRemoteApiConfig(remoteConfig v1API.PostgrestConfigWithJWTSecretResponse) api {
func (a *RemoteApi) fromRemoteApiConfig(remoteConfig v1API.PostgrestConfigWithJWTSecretResponse) RemoteApi {
result := *a
// Update Schemas if present in remoteConfig
result.Schemas = strings.Split(remoteConfig.DbSchema, ",")
Expand All @@ -64,9 +67,9 @@ func (a *api) FromRemoteApiConfig(remoteConfig v1API.PostgrestConfigWithJWTSecre
return result
}

func (a *api) DiffWithRemote(remoteConfig v1API.PostgrestConfigWithJWTSecretResponse) []byte {
func (a *RemoteApi) DiffWithRemote(remoteConfig v1API.PostgrestConfigWithJWTSecretResponse) []byte {
// Convert the config values into easily comparable remoteConfig values
currentValue := ToTomlBytes(a)
remoteCompare := ToTomlBytes(a.FromRemoteApiConfig(remoteConfig))
remoteCompare := ToTomlBytes(a.fromRemoteApiConfig(remoteConfig))
return Diff("remote[api]", remoteCompare, "local[api]", currentValue)
}
8 changes: 4 additions & 4 deletions pkg/config/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestApiToUpdatePostgrestConfigBody(t *testing.T) {
t.Run("converts all fields correctly", func(t *testing.T) {
api := &api{
api := &RemoteApi{
Schemas: []string{"public", "private"},
ExtraSearchPath: []string{"extensions", "public"},
MaxRows: 1000,
Expand All @@ -23,7 +23,7 @@ func TestApiToUpdatePostgrestConfigBody(t *testing.T) {
})

t.Run("handles empty fields", func(t *testing.T) {
api := &api{}
api := &RemoteApi{}

body := api.ToUpdatePostgrestConfigBody()

Expand All @@ -35,7 +35,7 @@ func TestApiToUpdatePostgrestConfigBody(t *testing.T) {

func TestApiDiffWithRemote(t *testing.T) {
t.Run("detects differences", func(t *testing.T) {
api := &api{
api := &RemoteApi{
Schemas: []string{"public", "private"},
ExtraSearchPath: []string{"extensions", "public"},
MaxRows: 1000,
Expand All @@ -58,7 +58,7 @@ func TestApiDiffWithRemote(t *testing.T) {
})

t.Run("handles no differences", func(t *testing.T) {
api := &api{
api := &RemoteApi{
Schemas: []string{"public"},
ExtraSearchPath: []string{"public"},
MaxRows: 500,
Expand Down
9 changes: 9 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,15 @@ func (a *auth) ResolveJWKS(ctx context.Context) (string, error) {
return string(jwksEncoded), nil
}

// Retrieve the final base config to use taking into account the remotes override
func (c *config) GetRemoteOverride(remotes_name string) (overrideConfig baseConfig, overrideExist bool) {
overrideConfig, exist := c.Remotes[remotes_name]
if exist {
return overrideConfig, true
}
return c.baseConfig, false
}

func ToTomlBytes(config any) []byte {
var buf bytes.Buffer
enc := toml.NewEncoder(&buf)
Expand Down

0 comments on commit 23173ec

Please sign in to comment.