File: admin_service.go

package info (click to toggle)
golang-github-kong-go-kong 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 620 kB
  • sloc: sh: 18; makefile: 4
file content (314 lines) | stat: -rw-r--r-- 7,796 bytes parent folder | download
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package kong

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"strings"
)

// AdminService handles Admins in Kong.
type AdminService service

// Invite creates an Admin in Kong.
func (s *AdminService) Invite(ctx context.Context,
	admin *Admin) (*Admin, error) {

	if admin == nil {
		return nil, errors.New("cannot create a nil admin")
	}

	endpoint := "/admins"
	method := "POST"
	req, err := s.client.NewRequest(method, endpoint, nil, admin)
	if err != nil {
		return nil, err
	}

	var createdAdmin struct {
		Admin Admin `json:"admin,omitempty" yaml:"admin,omitempty"`
	}
	_, err = s.client.Do(ctx, req, &createdAdmin)
	if err != nil {
		return nil, err
	}
	return &createdAdmin.Admin, nil
}

// Create aliases the Invite function as it performs
// essentially the same operation.
func (s *AdminService) Create(ctx context.Context,
	admin *Admin) (*Admin, error) {
	return s.Invite(ctx, admin)
}

// Get fetches a Admin in Kong.
func (s *AdminService) Get(ctx context.Context,
	nameOrID *string) (*Admin, error) {

	if isEmptyString(nameOrID) {
		return nil, errors.New("nameOrID cannot be nil for Get operation")
	}

	endpoint := fmt.Sprintf("/admins/%v", *nameOrID)

	req, err := s.client.NewRequest("GET", endpoint, nil, nil)
	if err != nil {
		return nil, err
	}

	var Admin Admin
	_, err = s.client.Do(ctx, req, &Admin)
	if err != nil {
		return nil, err
	}
	return &Admin, nil
}

// GenerateRegisterURL fetches an Admin in Kong
// and returns a unique registration URL for the Admin
func (s *AdminService) GenerateRegisterURL(ctx context.Context,
	nameOrID *string) (*Admin, error) {

	if isEmptyString(nameOrID) {
		return nil, errors.New("nameOrID cannot be nil for Get operation")
	}

	endpoint := fmt.Sprintf("/admins/%v?generate_register_url=true", *nameOrID)

	req, err := s.client.NewRequest("GET", endpoint, nil, nil)
	if err != nil {
		return nil, err
	}

	var Admin Admin
	_, err = s.client.Do(ctx, req, &Admin)
	if err != nil {
		return nil, err
	}
	return &Admin, nil
}

// Update updates an Admin in Kong.
func (s *AdminService) Update(ctx context.Context,
	admin *Admin) (*Admin, error) {

	if admin == nil {
		return nil, errors.New("cannot update a nil Admin")
	}

	if isEmptyString(admin.ID) {
		return nil, errors.New("ID cannot be nil for Update operation")
	}

	endpoint := fmt.Sprintf("/admins/%v", *admin.ID)
	req, err := s.client.NewRequest("PATCH", endpoint, nil, admin)
	if err != nil {
		return nil, err
	}

	var updatedAdmin Admin
	_, err = s.client.Do(ctx, req, &updatedAdmin)
	if err != nil {
		return nil, err
	}
	return &updatedAdmin, nil
}

// Delete deletes an Admin in Kong
func (s *AdminService) Delete(ctx context.Context,
	AdminOrID *string) error {

	if isEmptyString(AdminOrID) {
		return errors.New("AdminOrID cannot be nil for Delete operation")
	}

	endpoint := fmt.Sprintf("/admins/%v", *AdminOrID)
	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 Admins in Kong.
func (s *AdminService) List(ctx context.Context,
	opt *ListOpt) ([]*Admin, *ListOpt, error) {

	data, next, err := s.client.list(ctx, "/admins/", opt)
	if err != nil {
		return nil, nil, err
	}
	var admins []*Admin
	for _, object := range data {
		b, err := object.MarshalJSON()
		if err != nil {
			return nil, nil, err
		}
		var admin Admin
		err = json.Unmarshal(b, &admin)
		if err != nil {
			return nil, nil, err
		}
		admins = append(admins, &admin)
	}

	return admins, next, nil
}

// RegisterCredentials registers credentials for existing Kong Admins
func (s *AdminService) RegisterCredentials(ctx context.Context,
	admin *Admin) error {

	if admin == nil {
		return errors.New("cannot register credentials for a nil Admin")
	}

	if isEmptyString(admin.Username) {
		return errors.New("Username cannot be nil for a registration operation")
	}
	if isEmptyString(admin.Email) {
		return errors.New("Email cannot be nil for a registration operation")
	}
	if isEmptyString(admin.Password) {
		return errors.New("Password cannot be nil for a registration operation")
	}

	req, err := s.client.NewRequest("POST", "/admins/register", nil, admin)
	if err != nil {
		return err
	}

	_, err = s.client.Do(ctx, req, nil)
	if err != nil {
		return err
	}
	return nil
}

// ListWorkspaces lists the workspaces associated with an admin
func (s *AdminService) ListWorkspaces(ctx context.Context,
	emailOrID *string) ([]*Workspace, error) {
	endpoint := fmt.Sprintf("/admins/%v/workspaces", *emailOrID)
	req, err := s.client.NewRequest("GET", endpoint, nil, nil)
	if err != nil {
		return nil, err
	}
	var workspaces []*Workspace
	_, err = s.client.Do(ctx, req, &workspaces)
	if err != nil {
		return nil, fmt.Errorf("error updating admin workspaces: %v", err)
	}
	return workspaces, nil
}

// ListRoles returns a slice of Kong RBAC roles associated with an Admin.
func (s *AdminService) ListRoles(ctx context.Context,
	emailOrID *string, opt *ListOpt) ([]*RBACRole, error) {

	endpoint := fmt.Sprintf("/admins/%v/roles", *emailOrID)
	req, err := s.client.NewRequest("GET", endpoint, nil, nil)
	if err != nil {
		return nil, err
	}

	var listRoles struct {
		Roles []*RBACRole `json:"roles,omitempty" yaml:"roles,omitempty"`
	}
	_, err = s.client.Do(ctx, req, &listRoles)
	if err != nil {
		return nil, fmt.Errorf("error listing admin roles: %v", err)
	}

	return listRoles.Roles, nil
}

// UpdateRoles creates or updates roles associated with an Admin
func (s *AdminService) UpdateRoles(ctx context.Context,
	emailOrID *string, roles []*RBACRole) ([]*RBACRole, error) {

	var updateRoles struct {
		NameOrID *string `json:"name_or_id,omitempty" yaml:"name_or_id,omitempty"`
		Roles    *string `json:"roles,omitempty" yaml:"roles,omitempty"`
	}

	// Flatten roles
	var r []string
	for _, role := range roles {
		r = append(r, *role.Name)
	}
	updateRoles.NameOrID = emailOrID
	updateRoles.Roles = String(strings.Join(r, ","))

	endpoint := fmt.Sprintf("/admins/%v/roles", *emailOrID)
	req, err := s.client.NewRequest("POST", endpoint, nil, updateRoles)
	if err != nil {
		return nil, err
	}
	var listRoles struct {
		Roles []*RBACRole `json:"roles,omitempty" yaml:"roles,omitempty"`
	}
	_, err = s.client.Do(ctx, req, &listRoles)
	if err != nil {
		return nil, fmt.Errorf("error updating admin roles: %v", err)
	}
	return listRoles.Roles, nil
}

// DeleteRoles deletes roles associated with an Admin
func (s *AdminService) DeleteRoles(ctx context.Context,
	emailOrID *string, roles []*RBACRole) error {

	var updateRoles struct {
		NameOrID *string `json:"name_or_id,omitempty" yaml:"name_or_id,omitempty"`
		Roles    *string `json:"roles,omitempty" yaml:"roles,omitempty"`
	}

	// Flatten roles
	var r []string
	for _, role := range roles {
		r = append(r, *role.Name)
	}
	updateRoles.NameOrID = emailOrID
	updateRoles.Roles = String(strings.Join(r, ","))

	endpoint := fmt.Sprintf("/admins/%v/roles", *emailOrID)
	req, err := s.client.NewRequest("DELETE", endpoint, nil, updateRoles)
	if err != nil {
		return err
	}

	_, err = s.client.Do(ctx, req, nil)
	if err != nil {
		return fmt.Errorf("error deleting admin roles: %v", err)
	}

	return nil
}

// GetConsumer fetches the Consumer that gets generated for an Admin when
// the Admin is created.
func (s *AdminService) GetConsumer(ctx context.Context,
	emailOrID *string) (*Consumer, error) {

	if isEmptyString(emailOrID) {
		return nil, errors.New("emailOrID cannot be nil for GetConsumer operation")
	}

	endpoint := fmt.Sprintf("/admins/%v/consumer", *emailOrID)

	req, err := s.client.NewRequest("GET", endpoint, nil, nil)
	if err != nil {
		return nil, err
	}

	var consumer Consumer
	_, err = s.client.Do(ctx, req, &consumer)
	if err != nil {
		return nil, err
	}
	return &consumer, nil
}