forked from Kong/go-kong
/
entity_permission_service.go
152 lines (132 loc) · 4.46 KB
/
entity_permission_service.go
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
147
148
149
150
151
152
package kong
import (
"context"
"encoding/json"
"fmt"
)
// AbstractRBACEntityPermissionService handles RBACEntityPermissions in Kong.
type AbstractRBACEntityPermissionService interface {
// Create creates an RBACEntityPermission in Kong.
Create(ctx context.Context, ep *RBACEntityPermission) (*RBACEntityPermission, error)
// Get fetches an EntityPermission in Kong.
Get(ctx context.Context, roleNameOrID *string, entityName *string) (*RBACEntityPermission, error)
// Update updates an EntityPermission in Kong.
Update(ctx context.Context, ep *RBACEntityPermission) (*RBACEntityPermission, error)
// Delete deletes an EntityPermission in Kong
Delete(ctx context.Context, roleNameOrID *string, entityID *string) error
// ListAllForRole fetches a list of all RBACEntityPermissions in Kong for a given role.
ListAllForRole(ctx context.Context, roleNameOrID *string) ([]*RBACEntityPermission, error)
}
// RBACEntityPermissionService handles RBACEntityPermissions in Kong.
type RBACEntityPermissionService service
// Create creates an RBACEntityPermission in Kong.
func (s *RBACEntityPermissionService) Create(ctx context.Context,
ep *RBACEntityPermission,
) (*RBACEntityPermission, error) {
if ep == nil {
return nil, fmt.Errorf("cannot create a nil entitypermission")
}
if ep.Role == nil || ep.Role.ID == nil {
return nil, fmt.Errorf("cannot create entity permission with role or role id undefined")
}
method := "POST"
entity := fmt.Sprintf("/rbac/roles/%v/entities", *ep.Role.ID)
req, err := s.client.NewRequest(method, entity, nil, ep)
if err != nil {
return nil, err
}
var createdEntityPermission RBACEntityPermission
_, err = s.client.Do(ctx, req, &createdEntityPermission)
if err != nil {
return nil, err
}
return &createdEntityPermission, nil
}
// Get fetches an EntityPermission in Kong.
func (s *RBACEntityPermissionService) Get(ctx context.Context,
roleNameOrID *string, entityName *string,
) (*RBACEntityPermission, error) {
if isEmptyString(entityName) {
return nil, fmt.Errorf("entityName cannot be nil for Get operation")
}
entity := fmt.Sprintf("/rbac/roles/%v/entities/%v", *roleNameOrID, *entityName)
req, err := s.client.NewRequest("GET", entity, nil, nil)
if err != nil {
return nil, err
}
var EntityPermission RBACEntityPermission
_, err = s.client.Do(ctx, req, &EntityPermission)
if err != nil {
return nil, err
}
return &EntityPermission, nil
}
// Update updates an EntityPermission in Kong.
func (s *RBACEntityPermissionService) Update(ctx context.Context,
ep *RBACEntityPermission,
) (*RBACEntityPermission, error) {
if ep == nil {
return nil, fmt.Errorf("cannot update a nil EntityPermission")
}
if ep.Role == nil || ep.Role.ID == nil {
return nil, fmt.Errorf("cannot create entity permission with role or role id undefined")
}
if isEmptyString(ep.EntityID) {
return nil, fmt.Errorf("ID cannot be nil for Update operation")
}
entity := fmt.Sprintf("/rbac/roles/%v/entities/%v",
*ep.Role.ID, *ep.EntityID)
req, err := s.client.NewRequest("PATCH", entity, nil, ep)
if err != nil {
return nil, err
}
var updatedEntityPermission RBACEntityPermission
_, err = s.client.Do(ctx, req, &updatedEntityPermission)
if err != nil {
return nil, err
}
return &updatedEntityPermission, nil
}
// Delete deletes an EntityPermission in Kong
func (s *RBACEntityPermissionService) Delete(ctx context.Context,
roleNameOrID *string, entityID *string,
) error {
if roleNameOrID == nil {
return fmt.Errorf("cannot update an EntityPermission with role as nil")
}
if entityID == nil {
return fmt.Errorf("cannot update an EntityPermission with entity ID as nil")
}
endpoint := fmt.Sprintf("/rbac/roles/%v/entities/%v",
*roleNameOrID, *entityID)
req, err := s.client.NewRequest("DELETE", endpoint, nil, nil)
if err != nil {
return err
}
_, err = s.client.Do(ctx, req, nil)
return err
}
// ListAllForRole fetches a list of all RBACEntityPermissions in Kong for a given role.
func (s *RBACEntityPermissionService) ListAllForRole(ctx context.Context,
roleNameOrID *string,
) ([]*RBACEntityPermission, error) {
endpoint := fmt.Sprintf("/rbac/roles/%v/entities", *roleNameOrID)
data, _, err := s.client.list(ctx, endpoint, nil)
if err != nil {
return nil, err
}
var eps []*RBACEntityPermission
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, err
}
var ep RBACEntityPermission
err = json.Unmarshal(b, &ep)
if err != nil {
return nil, err
}
eps = append(eps, &ep)
}
return eps, nil
}