Skip to content

Commit

Permalink
feat: add Developer role support (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmorel-35 authored Mar 22, 2021
1 parent f75ccd0 commit 77590ab
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 0 deletions.
143 changes: 143 additions & 0 deletions kong/developer_role_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package kong

import (
"context"
"encoding/json"
"errors"
"fmt"
)

// AbstractDeveloperRoleService handles Developer Roles in Kong.
type AbstractDeveloperRoleService interface {
// Create creates a Developer Role in Kong.
Create(ctx context.Context, role *DeveloperRole) (*DeveloperRole, error)
// Get fetches a Developer Role in Kong.
Get(ctx context.Context, nameOrID *string) (*DeveloperRole, error)
// Update updates a Developer Role in Kong.
Update(ctx context.Context, role *DeveloperRole) (*DeveloperRole, error)
// Delete deletes a Developer Role in Kong
Delete(ctx context.Context, RoleOrID *string) error
// List fetches a list of all Developer Roles in Kong.
List(ctx context.Context) ([]*DeveloperRole, error)
}

// DeveloperRoleService handles Developer Roles in Kong.
type DeveloperRoleService service

// Create creates a Developer Role in Kong.
func (s *DeveloperRoleService) Create(ctx context.Context,
role *DeveloperRole) (*DeveloperRole, error) {

if role == nil {
return nil, errors.New("cannot create a nil role")
}

endpoint := "/developers/roles"
method := "POST"
if role.ID != nil {
endpoint = endpoint + "/" + *role.ID
method = "PUT"
}
req, err := s.client.NewRequest(method, endpoint, nil, role)

if err != nil {
return nil, err
}

var createdRole DeveloperRole
_, err = s.client.Do(ctx, req, &createdRole)
if err != nil {
return nil, err
}
return &createdRole, nil
}

// Get fetches a Developer Role in Kong.
func (s *DeveloperRoleService) Get(ctx context.Context,
nameOrID *string) (*DeveloperRole, error) {

if isEmptyString(nameOrID) {
return nil, errors.New("nameOrID cannot be nil for Get operation")
}

endpoint := fmt.Sprintf("/developers/roles/%v", *nameOrID)
req, err := s.client.NewRequest("GET", endpoint, nil, nil)
if err != nil {
return nil, err
}

var Role DeveloperRole
_, err = s.client.Do(ctx, req, &Role)
if err != nil {
return nil, err
}
return &Role, nil
}

// Update updates a Developer Role in Kong.
func (s *DeveloperRoleService) Update(ctx context.Context,
role *DeveloperRole) (*DeveloperRole, error) {

if role == nil {
return nil, errors.New("cannot update a nil Role")
}

if isEmptyString(role.ID) {
return nil, errors.New("ID cannot be nil for Update operation")
}

endpoint := fmt.Sprintf("/developers/roles/%v", *role.ID)
req, err := s.client.NewRequest("PATCH", endpoint, nil, role)
if err != nil {
return nil, err
}

var updatedRole DeveloperRole
_, err = s.client.Do(ctx, req, &updatedRole)
if err != nil {
return nil, err
}
return &updatedRole, nil
}

// Delete deletes a Developer Role in Kong
func (s *DeveloperRoleService) Delete(ctx context.Context,
RoleOrID *string) error {

if isEmptyString(RoleOrID) {
return errors.New("RoleOrID cannot be nil for Delete operation")
}

endpoint := fmt.Sprintf("/developers/roles/%v", *RoleOrID)
req, err := s.client.NewRequest("DELETE", endpoint, nil, nil)
if err != nil {
return err
}

_, err = s.client.Do(ctx, req, nil)
return err
}

// List fetches a list of all Developer Roles in Kong.
func (s *DeveloperRoleService) List(ctx context.Context) ([]*DeveloperRole, error) {

data, _, err := s.client.list(ctx, "/developers/roles/", nil)
if err != nil {
return nil, err
}
var roles []*DeveloperRole
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, err
}
var role DeveloperRole
err = json.Unmarshal(b, &role)
if err != nil {
return nil, err
}
roles = append(roles, &role)
}

return roles, nil
}
84 changes: 84 additions & 0 deletions kong/developer_role_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package kong

import (
"testing"

uuid "github.com/satori/go.uuid"
"github.com/stretchr/testify/assert"
)

func TestDeveloperRoleService(T *testing.T) {
runWhenEnterprise(T, ">=0.33.0", true)
assert := assert.New(T)

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

role := &DeveloperRole{
Name: String("roleA"),
}

createdRole, err := client.DeveloperRoles.Create(defaultCtx, role)
assert.Nil(err)
assert.NotNil(createdRole)

role, err = client.DeveloperRoles.Get(defaultCtx, createdRole.ID)
assert.Nil(err)
assert.NotNil(role)

role.Comment = String("new comment")
role, err = client.DeveloperRoles.Update(defaultCtx, role)
assert.Nil(err)
assert.NotNil(role)
assert.Equal("roleA", *role.Name)

err = client.DeveloperRoles.Delete(defaultCtx, createdRole.ID)
assert.Nil(err)

// ID can be specified
id := uuid.NewV4().String()
role = &DeveloperRole{
Name: String("teamB"),
ID: String(id),
}

createdRole, err = client.DeveloperRoles.Create(defaultCtx, role)
assert.Nil(err)
assert.NotNil(createdRole)
assert.Equal(id, *createdRole.ID)

err = client.DeveloperRoles.Delete(defaultCtx, createdRole.ID)
assert.Nil(err)
}
func TestDeveloperRoleServiceList(T *testing.T) {
runWhenEnterprise(T, ">=0.33.0", true)
assert := assert.New(T)

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

roleA := &DeveloperRole{
Name: String("roleA"),
}
roleB := &DeveloperRole{
Name: String("roleB"),
}

createdRoleA, err := client.DeveloperRoles.Create(defaultCtx, roleA)
assert.Nil(err)
createdRoleB, err := client.DeveloperRoles.Create(defaultCtx, roleB)
assert.Nil(err)

roles, err := client.DeveloperRoles.List(defaultCtx)
assert.Nil(err)
assert.NotNil(roles)
// Counts default roles (super-admin, admin, read-only)
assert.Equal(5, len(roles))

err = client.DeveloperRoles.Delete(defaultCtx, createdRoleA.ID)
assert.Nil(err)
err = client.DeveloperRoles.Delete(defaultCtx, createdRoleB.ID)
assert.Nil(err)
}
2 changes: 2 additions & 0 deletions kong/kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Client struct {
baseURL string
common service
Consumers AbstractConsumerService
DeveloperRoles AbstractDeveloperRoleService
Services AbstractSvcService
Routes AbstractRouteService
CACertificates AbstractCACertificateService
Expand Down Expand Up @@ -103,6 +104,7 @@ func NewClient(baseURL *string, client *http.Client) (*Client, error) {

kong.common.client = kong
kong.Consumers = (*ConsumerService)(&kong.common)
kong.DeveloperRoles = (*DeveloperRoleService)(&kong.common)
kong.Services = (*Svcservice)(&kong.common)
kong.Routes = (*RouteService)(&kong.common)
kong.Plugins = (*PluginService)(&kong.common)
Expand Down
9 changes: 9 additions & 0 deletions kong/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,12 @@ type RBACPermissionsList struct {
Endpoints map[string]interface{} `json:"endpoints,omitempty" yaml:"endpoints,omitempty"`
Entities map[string]interface{} `json:"entities,omitempty" yaml:"entities,omitempty"`
}

// DeveloperRole represents an Developer Role in Kong.
// +k8s:deepcopy-gen=true
type DeveloperRole struct {
Comment *string `json:"comment,omitempty" yaml:"comment,omitempty"`
CreatedAt *int `json:"created_at,omitempty" yaml:"created_at,omitempty"`
ID *string `json:"id,omitempty" yaml:"id,omitempty"`
Name *string `json:"name,omitempty" yaml:"name,omitempty"`
}
36 changes: 36 additions & 0 deletions kong/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 77590ab

Please sign in to comment.