1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
package kong
import (
"context"
"encoding/json"
"errors"
"fmt"
)
// RBACEndpointPermissionService handles RBACEndpointPermissions in Kong.
type RBACEndpointPermissionService service
// Create creates a RBACEndpointPermission in Kong.
func (s *RBACEndpointPermissionService) Create(ctx context.Context,
ep *RBACEndpointPermission) (*RBACEndpointPermission, error) {
if ep == nil {
return nil, errors.New("cannot create a nil endpointpermission")
}
if ep.Role == nil || ep.Role.ID == nil {
return nil, errors.New("cannot create endpoint permission with role or role id undefined")
}
method := "POST"
endpoint := fmt.Sprintf("/rbac/roles/%v/endpoints", *ep.Role.ID)
req, err := s.client.NewRequest(method, endpoint, nil, ep)
if err != nil {
return nil, err
}
var createdEndpointPermission RBACEndpointPermission
_, err = s.client.Do(ctx, req, &createdEndpointPermission)
if err != nil {
return nil, err
}
return &createdEndpointPermission, nil
}
// Get fetches a RBACEndpointPermission in Kong.
func (s *RBACEndpointPermissionService) Get(ctx context.Context,
roleNameOrID *string, workspaceNameOrID *string, endpointName *string) (*RBACEndpointPermission, error) {
if isEmptyString(endpointName) {
return nil, errors.New("endpointName cannot be nil for Get operation")
}
if *endpointName == "*" {
endpointName = String("/" + *endpointName)
}
endpoint := fmt.Sprintf("/rbac/roles/%v/endpoints/%v%v", *roleNameOrID, *workspaceNameOrID, *endpointName)
req, err := s.client.NewRequest("GET", endpoint, nil, nil)
if err != nil {
return nil, err
}
var EndpointPermission RBACEndpointPermission
_, err = s.client.Do(ctx, req, &EndpointPermission)
if err != nil {
return nil, err
}
return &EndpointPermission, nil
}
// Update updates a RBACEndpointPermission in Kong.
func (s *RBACEndpointPermissionService) Update(ctx context.Context,
ep *RBACEndpointPermission) (*RBACEndpointPermission, error) {
if ep == nil {
return nil, errors.New("cannot update a nil EndpointPermission")
}
if ep.Workspace == nil {
return nil, errors.New("cannot update an EndpointPermission with workspace as nil")
}
if ep.Role == nil || ep.Role.ID == nil {
return nil, errors.New("cannot create endpoint permission with role or role id undefined")
}
if isEmptyString(ep.Endpoint) {
return nil, errors.New("ID cannot be nil for Update operation")
}
endpoint := fmt.Sprintf("/rbac/roles/%v/endpoints/%v/%v",
*ep.Role.ID, *ep.Workspace, *ep.Endpoint)
req, err := s.client.NewRequest("PATCH", endpoint, nil, ep)
if err != nil {
return nil, err
}
var updatedEndpointPermission RBACEndpointPermission
_, err = s.client.Do(ctx, req, &updatedEndpointPermission)
if err != nil {
return nil, err
}
return &updatedEndpointPermission, nil
}
// Delete deletes a EndpointPermission in Kong
func (s *RBACEndpointPermissionService) Delete(ctx context.Context,
roleNameOrID *string, workspaceNameOrID *string, endpoint *string) error {
if endpoint == nil {
return errors.New("cannot update a nil EndpointPermission")
}
if workspaceNameOrID == nil {
return errors.New("cannot update an EndpointPermission with workspace as nil")
}
if roleNameOrID == nil {
return errors.New("cannot update an EndpointPermission with role as nil")
}
reqEndpoint := fmt.Sprintf("/rbac/roles/%v/endpoints/%v/%v",
*roleNameOrID, *workspaceNameOrID, *endpoint)
req, err := s.client.NewRequest("DELETE", reqEndpoint, nil, nil)
if err != nil {
return err
}
_, err = s.client.Do(ctx, req, nil)
return err
}
// ListAllForRole fetches a list of all RBACEndpointPermissions in Kong for a given role.
func (s *RBACEndpointPermissionService) ListAllForRole(ctx context.Context,
roleNameOrID *string) ([]*RBACEndpointPermission, error) {
data, _, err := s.client.list(ctx, fmt.Sprintf("/rbac/roles/%v/endpoints", *roleNameOrID), nil)
if err != nil {
return nil, err
}
var eps []*RBACEndpointPermission
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, err
}
var ep RBACEndpointPermission
err = json.Unmarshal(b, &ep)
if err != nil {
return nil, err
}
eps = append(eps, &ep)
}
return eps, nil
}
|