Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add SchemaService and FillEntityDefaults utility #119

Merged
merged 11 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/google/go-cmp v0.5.7
github.com/google/go-querystring v1.1.0
github.com/google/uuid v1.3.0
github.com/imdario/mergo v0.3.12
github.com/mitchellh/mapstructure v1.4.3
github.com/stretchr/testify v1.7.0
github.com/tidwall/gjson v1.13.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/googleapis/gnostic v0.5.1/go.mod h1:6U4PtQXGIEt/Z3h5MAT7FNofLnw9vXk2cUuW7uA/OeU=
github.com/googleapis/gnostic v0.5.5/go.mod h1:7+EbHbldMins07ALC74bsA81Ovc97DwqyJO1AENw9kA=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand Down
4 changes: 4 additions & 0 deletions kong/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type Client struct {
Tags AbstractTagService
Info AbstractInfoService

Schemas AbstractSchemaService

logger io.Writer
debug bool
CustomEntities AbstractCustomEntityService
Expand Down Expand Up @@ -150,6 +152,8 @@ func NewClient(baseURL *string, client *http.Client) (*Client, error) {
kong.MTLSAuths = (*MTLSAuthService)(&kong.common)
kong.ACLs = (*ACLService)(&kong.common)

kong.Schemas = (*SchemaService)(&kong.common)

kong.Oauth2Credentials = (*Oauth2Service)(&kong.common)
kong.Tags = (*TagService)(&kong.common)
kong.Info = (*InfoService)(&kong.common)
Expand Down
12 changes: 6 additions & 6 deletions kong/plugin_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ type AbstractPluginService interface {
// GetSchema retrieves the config schema of a plugin.
//
// Deprecated: Use GetFullSchema instead.
GetSchema(ctx context.Context, pluginName *string) (map[string]interface{}, error)
GetSchema(ctx context.Context, pluginName *string) (Schema, error)
// GetFullSchema retrieves the full schema of a plugin.
// This makes the use of `/schemas` endpoint in Kong.
GetFullSchema(ctx context.Context, pluginName *string) (map[string]interface{}, error)
GetFullSchema(ctx context.Context, pluginName *string) (Schema, error)
}

// PluginService handles Plugins in Kong.
type PluginService service

// GetFullSchema retrieves the full schema of a plugin.
func (s *PluginService) GetFullSchema(ctx context.Context,
pluginName *string) (map[string]interface{}, error) {
pluginName *string) (Schema, error) {
if isEmptyString(pluginName) {
return nil, fmt.Errorf("pluginName cannot be empty")
}
Expand All @@ -52,7 +52,7 @@ func (s *PluginService) GetFullSchema(ctx context.Context,
if err != nil {
return nil, err
}
var schema map[string]interface{}
var schema Schema
_, err = s.client.Do(ctx, req, &schema)
if err != nil {
return nil, err
Expand All @@ -64,7 +64,7 @@ func (s *PluginService) GetFullSchema(ctx context.Context,
//
// Deprecated: Use GetPluginSchema instead
func (s *PluginService) GetSchema(ctx context.Context,
pluginName *string) (map[string]interface{}, error) {
pluginName *string) (Schema, error) {
if isEmptyString(pluginName) {
return nil, fmt.Errorf("pluginName cannot be empty")
}
Expand All @@ -73,7 +73,7 @@ func (s *PluginService) GetSchema(ctx context.Context,
if err != nil {
return nil, err
}
var schema map[string]interface{}
var schema Schema
_, err = s.client.Do(ctx, req, &schema)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion kong/plugin_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func TestPluginListAllForEntityEndpoint(T *testing.T) {
assert.Nil(client.Services.Delete(defaultCtx, createdService.ID))
}

func TestGetFullSchema(T *testing.T) {
func TestPluginGetFullSchema(T *testing.T) {
assert := assert.New(T)

client, err := NewTestClient(nil, nil)
Expand Down
32 changes: 32 additions & 0 deletions kong/schema_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package kong

import (
"context"
"fmt"
)

// AbstractSchemaService handles schemas in Kong.
type AbstractSchemaService interface {
// Get fetches an entity schema from Kong.
Get(ctx context.Context, entity string) (Schema, error)
}

// SchemaService handles schemas in Kong.
type SchemaService service

// Schema represents an entity schema in Kong.
type Schema map[string]interface{}

// Get retrieves the full schema of kong entities.
func (s *SchemaService) Get(ctx context.Context, entity string) (Schema, error) {
req, err := s.client.NewRequest("GET", fmt.Sprintf("/schemas/%s", entity), nil, nil)
if err != nil {
return nil, err
}
var schema Schema
_, err = s.client.Do(ctx, req, &schema)
if err != nil {
return nil, err
}
return schema, nil
}
34 changes: 34 additions & 0 deletions kong/schema_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package kong

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSchemaService(T *testing.T) {
assert := assert.New(T)

client, err := NewTestClient(nil, nil)
assert.Nil(err)
assert.NotNil(client)

entities := []string{
"services",
"routes",
"targets",
"upstreams",
"plugins",
"ca_certificates",
"certificates",
"consumers",
"snis",
"tags",
}
for _, entity := range entities {
schema, err := client.Schemas.Get(defaultCtx, entity)
_, ok := schema["fields"]
assert.True(ok)
assert.Nil(err)
}
}
120 changes: 119 additions & 1 deletion kong/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/blang/semver/v4"
"github.com/imdario/mergo"
"github.com/tidwall/gjson"
)

Expand All @@ -31,6 +32,11 @@ func Int(i int) *int {
return &i
}

// Float64 returns a pointer to f.
func Float64(f float64) *float64 {
return &f
}

func isEmptyString(s *string) bool {
return s == nil || strings.TrimSpace(*s) == ""
}
Expand Down Expand Up @@ -208,9 +214,121 @@ func fillConfigRecord(schema gjson.Result, config Configuration) Configuration {
return res
}

// flattenDefaultsSchema gets an arbitrarily nested and structured entity schema
// and flattens it, turning it into a map that can be more easily unmarshalled
// into proper entity objects.
//
// Sample input:
// {
// "fields": [
// {
// "algorithm": {
// "default": "round-robin",
// "one_of": ["consistent-hashing", "least-connections", "round-robin"],
// "type": "string"
// }
// }, {
// "hash_on": {
// "default": "none",
// "one_of": ["none", "consumer", "ip", "header", "cookie"],
// "type": "string"
// }
// }, {
// "hash_fallback": {
// "default": "none",
// "one_of": ["none", "consumer", "ip", "header", "cookie"],
// "type": "string"
// }
// },
// ...
// }
//
// Sample output:
// {
// "algorithm": "round-robin",
// "hash_on": "none",
// "hash_fallback": "none",
// ...
// }
func flattenDefaultsSchema(schema gjson.Result) Schema {
value := schema.Get("fields")
results := Schema{}

value.ForEach(func(key, value gjson.Result) bool {
// get the key name
ms := value.Map()
fname := ""
for k := range ms {
fname = k
break
}

ftype := value.Get(fname + ".type")
if ftype.String() == "record" {
newSubConfig := flattenDefaultsSchema(value.Get(fname))
results[fname] = newSubConfig
return true
}
value = value.Get(fname + ".default")
if value.Exists() {
results[fname] = value.Value()
} else {
results[fname] = nil
}
return true
})

return results
}

func getDefaultsObj(schema Schema) ([]byte, error) {
jsonSchema, err := json.Marshal(&schema)
if err != nil {
return nil, err
}
gjsonSchema := gjson.ParseBytes((jsonSchema))
defaults := flattenDefaultsSchema(gjsonSchema)
jsonSchemaWithDefaults, err := json.Marshal(&defaults)
if err != nil {
return nil, err
}
GGabriele marked this conversation as resolved.
Show resolved Hide resolved
return jsonSchemaWithDefaults, nil
}

// FillEntityDefaults ingests entities' defaults from their schema.
func FillEntityDefaults(entity interface{}, schema Schema) error {
if schema == nil {
return fmt.Errorf("filling defaults for '%T': provided schema is nil", entity)
}
var tmpEntity interface{}
switch entity.(type) {
case *Target:
tmpEntity = &Target{}
case *Service:
tmpEntity = &Service{}
case *Route:
tmpEntity = &Route{}
case *Upstream:
tmpEntity = &Upstream{}
GGabriele marked this conversation as resolved.
Show resolved Hide resolved
default:
return fmt.Errorf("unsupported entity: '%T'", entity)
}
defaults, err := getDefaultsObj(schema)
GGabriele marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("parse schema for defaults: %v", err)
}
if err := json.Unmarshal(defaults, &tmpEntity); err != nil {
return fmt.Errorf("unmarshal entity with defaults: %v", err)
}
if err := mergo.Merge(entity, tmpEntity); err != nil {
return fmt.Errorf("merge entity with its defaults: %v", err)
}
return nil
}

// FillPluginsDefaults ingests plugin's defaults from its schema.
// Takes in a plugin struct and mutate it in place.
func FillPluginsDefaults(plugin *Plugin, schema map[string]interface{}) error {
func FillPluginsDefaults(plugin *Plugin, schema Schema) error {
GGabriele marked this conversation as resolved.
Show resolved Hide resolved
jsonb, err := json.Marshal(&schema)
if err != nil {
return err
Expand Down
Loading