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

Defines the Information Service on top of Kong's Root endpoint #65

Merged
merged 19 commits into from
Jul 1, 2021
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
2 changes: 2 additions & 0 deletions kong/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Client struct {
ACLs AbstractACLService
Oauth2Credentials AbstractOauth2Service
Tags AbstractTagService
Info AbstractInfoService

logger io.Writer
debug bool
Expand Down Expand Up @@ -149,6 +150,7 @@ func NewClient(baseURL *string, client *http.Client) (*Client, error) {

kong.Oauth2Credentials = (*Oauth2Service)(&kong.common)
kong.Tags = (*TagService)(&kong.common)
kong.Info = (*InfoService)(&kong.common)

kong.CustomEntities = (*CustomEntityService)(&kong.common)
kong.Registry = custom.NewDefaultRegistry()
Expand Down
47 changes: 47 additions & 0 deletions kong/information_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package kong

import (
"context"
"encoding/json"
)

// AbstractInfoService handles Kong's Information.
type AbstractInfoService interface {
// Get retrieves the general runtime information about the Kong gateway.
Get(ctx context.Context) (*Info, error)
}

type InfoService service

// IsInMemory check if Kong is in memory
func (r *RuntimeConfiguration) IsInMemory() bool {
return "off" == r.Database
}

// IsRBACEnabled check if RBAC are enabled
func (r *RuntimeConfiguration) IsRBACEnabled() bool {
return "on" == r.RBAC
}

// convert convert an object to another through json marshalling
// unmarshalling
func convert(from, to interface{}) error {
bytes, err := json.Marshal(from)
if err != nil {
return err
}
return json.Unmarshal(bytes, to)
}

// Get retrieves the high-level metadata of a Kong instance.
func (s *InfoService) Get(ctx context.Context) (*Info, error) {
information, err := s.client.Root(ctx)
if err != nil {
return nil, err
}
var info Info
if err := convert(information, &info); err != nil {
return nil, err
}
return &info, nil
}
49 changes: 49 additions & 0 deletions kong/information_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package kong
mmorel-35 marked this conversation as resolved.
Show resolved Hide resolved

import (
"reflect"
"testing"

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

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

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

info, err := client.Info.Get(defaultCtx)
assert.Nil(err)
assert.NotNil(info)
assert.NotNil(info.Version)
assert.NotNil(info.Configuration)
assert.NotNil(info.Configuration.Database)
}

func TestConvert(T *testing.T) {
assert := assert.New(T)
information := map[string]interface{}{
"version": "2.3.3.2-enterprise-edition",
"configuration": map[string]interface{}{
"portal": true,
"rbac": "on",
"database": "postgres",
},
}
expected := &Info{
Version: "2.3.3.2-enterprise-edition",
Configuration: &RuntimeConfiguration{
Portal: true,
RBAC: "on",
Database: "postgres",
},
}
var actual Info
err := convert(information, &actual)
assert.NoError(err)
assert.True(reflect.DeepEqual(expected, &actual))
assert.False(actual.Configuration.IsInMemory())
assert.True(actual.Configuration.IsRBACEnabled())
}
13 changes: 13 additions & 0 deletions kong/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,16 @@ type DeveloperRole struct {
ID *string `json:"id,omitempty" yaml:"id,omitempty"`
Name *string `json:"name,omitempty" yaml:"name,omitempty"`
}

// Info represents the information concerning Kong.
type Info struct {
Version string `json:"version,omitempty" yaml:"version,omitempty"`
Configuration *RuntimeConfiguration `json:"configuration,omitempty" yaml:"configuration,omitempty"`
}

// RuntimeConfiguration represents the runtime configuration of Kong.
type RuntimeConfiguration struct {
Database string `json:"database,omitempty" yaml:"database,omitempty"`
Portal bool `json:"portal,omitempty" yaml:"portal,omitempty"`
RBAC string `json:"rbac,omitempty" yaml:"rbac,omitempty"`
}