-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Developer role support (#26)
- Loading branch information
Showing
5 changed files
with
274 additions
and
0 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
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 | ||
} |
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,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) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.