forked from Kong/go-kong
/
endpoint_permission_service.go
166 lines (148 loc) · 5.21 KB
/
endpoint_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package kong
import (
"context"
"encoding/json"
"fmt"
)
// AbstractRBACEndpointPermissionService handles RBACEndpointPermissions in Kong.
type AbstractRBACEndpointPermissionService interface {
// Create creates a RBACEndpointPermission in Kong.
Create(ctx context.Context, ep *RBACEndpointPermission) (*RBACEndpointPermission, error)
// Get fetches a RBACEndpointPermission in Kong.
Get(ctx context.Context, roleNameOrID *string, workspaceNameOrID *string,
endpointName *string) (*RBACEndpointPermission, error)
// Update updates a RBACEndpointPermission in Kong.
Update(ctx context.Context, ep *RBACEndpointPermission) (*RBACEndpointPermission, error)
// Delete deletes a EndpointPermission in Kong
Delete(ctx context.Context, roleNameOrID *string, workspaceNameOrID *string, endpoint *string) error
// ListAllForRole fetches a list of all RBACEndpointPermissions in Kong for a given role.
ListAllForRole(ctx context.Context, roleNameOrID *string) ([]*RBACEndpointPermission, error)
}
// 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, fmt.Errorf("cannot create a nil endpointpermission")
}
if ep.Role == nil || ep.Role.ID == nil {
return nil, fmt.Errorf("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, fmt.Errorf("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, fmt.Errorf("cannot update a nil EndpointPermission")
}
if ep.Workspace == nil {
return nil, fmt.Errorf("cannot update an EndpointPermission with workspace as nil")
}
if ep.Role == nil || ep.Role.ID == nil {
return nil, fmt.Errorf("cannot create endpoint permission with role or role id undefined")
}
if isEmptyString(ep.Endpoint) {
return nil, fmt.Errorf("ID cannot be nil for Update operation")
}
endpointName := ep.Endpoint
if *endpointName == "*" {
endpointName = String("/" + *endpointName)
}
endpoint := fmt.Sprintf("/rbac/roles/%v/endpoints/%v%v",
*ep.Role.ID, *ep.Workspace, *endpointName)
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, endpointName *string,
) error {
if endpointName == nil {
return fmt.Errorf("cannot update a nil EndpointPermission")
}
if workspaceNameOrID == nil {
return fmt.Errorf("cannot update an EndpointPermission with workspace as nil")
}
if roleNameOrID == nil {
return fmt.Errorf("cannot update an EndpointPermission with role as nil")
}
if *endpointName == "*" {
endpointName = String("/" + *endpointName)
}
reqEndpoint := fmt.Sprintf("/rbac/roles/%v/endpoints/%v/%v",
*roleNameOrID, *workspaceNameOrID, *endpointName)
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
}