-
Notifications
You must be signed in to change notification settings - Fork 41
/
rbac_role_service.go
162 lines (142 loc) · 3.77 KB
/
rbac_role_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
package kong
import (
"context"
"encoding/json"
"fmt"
)
// AbstractRBACRoleService handles Roles in Kong.
type AbstractRBACRoleService interface {
// Create creates a Role in Kong.
Create(ctx context.Context, role *RBACRole) (*RBACRole, error)
// Get fetches a Role in Kong.
Get(ctx context.Context, nameOrID *string) (*RBACRole, error)
// Update updates a Role in Kong.
Update(ctx context.Context, role *RBACRole) (*RBACRole, error)
// Delete deletes a Role in Kong
Delete(ctx context.Context, RoleOrID *string) error
// List fetches a list of Roles in Kong.
List(ctx context.Context, opt *ListOpt) ([]*RBACRole, *ListOpt, error)
// List fetches a list of all Roles in Kong.
ListAll(ctx context.Context) ([]*RBACRole, error)
}
// RBACRoleService handles Roles in Kong.
type RBACRoleService service
// Create creates a Role in Kong.
func (s *RBACRoleService) Create(ctx context.Context,
role *RBACRole,
) (*RBACRole, error) {
if role == nil {
return nil, fmt.Errorf("cannot create a nil role")
}
endpoint := "/rbac/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 RBACRole
_, err = s.client.Do(ctx, req, &createdRole)
if err != nil {
return nil, err
}
return &createdRole, nil
}
// Get fetches a Role in Kong.
func (s *RBACRoleService) Get(ctx context.Context,
nameOrID *string,
) (*RBACRole, error) {
if isEmptyString(nameOrID) {
return nil, fmt.Errorf("nameOrID cannot be nil for Get operation")
}
endpoint := fmt.Sprintf("/rbac/roles/%v", *nameOrID)
req, err := s.client.NewRequest("GET", endpoint, nil, nil)
if err != nil {
return nil, err
}
var Role RBACRole
_, err = s.client.Do(ctx, req, &Role)
if err != nil {
return nil, err
}
return &Role, nil
}
// Update updates a Role in Kong.
func (s *RBACRoleService) Update(ctx context.Context,
role *RBACRole,
) (*RBACRole, error) {
if role == nil {
return nil, fmt.Errorf("cannot update a nil Role")
}
if isEmptyString(role.ID) {
return nil, fmt.Errorf("ID cannot be nil for Update operation")
}
endpoint := fmt.Sprintf("/rbac/roles/%v", *role.ID)
req, err := s.client.NewRequest("PATCH", endpoint, nil, role)
if err != nil {
return nil, err
}
var updatedRole RBACRole
_, err = s.client.Do(ctx, req, &updatedRole)
if err != nil {
return nil, err
}
return &updatedRole, nil
}
// Delete deletes a Role in Kong
func (s *RBACRoleService) Delete(ctx context.Context,
RoleOrID *string,
) error {
if isEmptyString(RoleOrID) {
return fmt.Errorf("RoleOrID cannot be nil for Delete operation")
}
endpoint := fmt.Sprintf("/rbac/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 Roles in Kong.
func (s *RBACRoleService) List(ctx context.Context,
opt *ListOpt,
) ([]*RBACRole, *ListOpt, error) {
data, next, err := s.client.list(ctx, "/rbac/roles/", opt)
if err != nil {
return nil, nil, err
}
var roles []*RBACRole
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, nil, err
}
var role RBACRole
err = json.Unmarshal(b, &role)
if err != nil {
return nil, nil, err
}
roles = append(roles, &role)
}
return roles, next, nil
}
// ListAll fetches all Roles in Kong.
// This method can take a while if there
// a lot of Roles present.
func (s *RBACRoleService) ListAll(ctx context.Context) ([]*RBACRole, error) {
var roles, data []*RBACRole
var err error
opt := &ListOpt{Size: pageSize}
for opt != nil {
data, opt, err = s.List(ctx, opt)
if err != nil {
return nil, err
}
roles = append(roles, data...)
}
return roles, nil
}