File: entity_permission_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 (140 lines) | stat: -rw-r--r-- 3,717 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
package kong

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

// 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, errors.New("cannot create a nil entitypermission")
	}
	if ep.Role == nil || ep.Role.ID == nil {
		return nil, errors.New("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, errors.New("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, errors.New("cannot update a nil EntityPermission")
	}

	if ep.Role == nil || ep.Role.ID == nil {
		return nil, errors.New("cannot create entity permission with role or role id undefined")
	}

	if isEmptyString(ep.EntityID) {
		return nil, errors.New("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 errors.New("cannot update an EntityPermission with role as nil")
	}
	if entityID == nil {
		return errors.New("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
}