File: acl_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, trixie
  • size: 620 kB
  • sloc: sh: 18; makefile: 4
file content (146 lines) | stat: -rw-r--r-- 3,645 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
package kong

import (
	"context"
	"encoding/json"
)

// ACLService handles consumer ACL groups in Kong.
type ACLService service

// Create adds a consumer to an ACL group in Kong
// If an ID is specified, it will be used to
// create the group association in Kong, otherwise an ID
// is auto-generated.
func (s *ACLService) Create(ctx context.Context,
	consumerUsernameOrID *string, aclGroup *ACLGroup) (*ACLGroup, error) {

	cred, err := s.client.credentials.Create(ctx, "acl",
		consumerUsernameOrID, aclGroup)
	if err != nil {
		return nil, err
	}

	var createdACLGroup ACLGroup
	err = json.Unmarshal(cred, &createdACLGroup)
	if err != nil {
		return nil, err
	}

	return &createdACLGroup, nil
}

// Get fetches an ACL group for a consumer in Kong.
func (s *ACLService) Get(ctx context.Context,
	consumerUsernameOrID, groupOrID *string) (*ACLGroup, error) {

	cred, err := s.client.credentials.Get(ctx, "acl",
		consumerUsernameOrID, groupOrID)
	if err != nil {
		return nil, err
	}

	var aclGroup ACLGroup
	err = json.Unmarshal(cred, &aclGroup)
	if err != nil {
		return nil, err
	}

	return &aclGroup, nil
}

// Update updates an ACL group for a consumer in Kong
func (s *ACLService) Update(ctx context.Context,
	consumerUsernameOrID *string, aclGroup *ACLGroup) (*ACLGroup, error) {

	cred, err := s.client.credentials.Update(ctx, "acl",
		consumerUsernameOrID, aclGroup)
	if err != nil {
		return nil, err
	}

	var updatedACLGroup ACLGroup
	err = json.Unmarshal(cred, &updatedACLGroup)
	if err != nil {
		return nil, err
	}

	return &updatedACLGroup, nil
}

// Delete deletes an ACL group association for a consumer in Kong
func (s *ACLService) Delete(ctx context.Context,
	consumerUsernameOrID, groupOrID *string) error {
	return s.client.credentials.Delete(ctx, "acl",
		consumerUsernameOrID, groupOrID)
}

// List fetches a list of all ACL group and consumer associations in Kong.
// opt can be used to control pagination.
func (s *ACLService) List(ctx context.Context,
	opt *ListOpt) ([]*ACLGroup, *ListOpt, error) {
	data, next, err := s.client.list(ctx, "/acls", opt)
	if err != nil {
		return nil, nil, err
	}
	var aclGroups []*ACLGroup
	for _, object := range data {
		b, err := object.MarshalJSON()
		if err != nil {
			return nil, nil, err
		}
		var aclGroup ACLGroup
		err = json.Unmarshal(b, &aclGroup)
		if err != nil {
			return nil, nil, err
		}
		aclGroups = append(aclGroups, &aclGroup)
	}

	return aclGroups, next, nil
}

// ListAll fetches all all ACL group associations in Kong.
// This method can take a while if there
// a lot of ACLGroup associations are present.
func (s *ACLService) ListAll(ctx context.Context) ([]*ACLGroup, error) {
	var aclGroups, data []*ACLGroup
	var err error
	opt := &ListOpt{Size: pageSize}

	for opt != nil {
		data, opt, err = s.List(ctx, opt)
		if err != nil {
			return nil, err
		}
		aclGroups = append(aclGroups, data...)
	}
	return aclGroups, nil
}

// ListForConsumer fetches a list of ACL groups
// in Kong associated with a specific consumer.
// opt can be used to control pagination.
func (s *ACLService) ListForConsumer(ctx context.Context,
	consumerUsernameOrID *string, opt *ListOpt) ([]*ACLGroup, *ListOpt, error) {
	data, next, err := s.client.list(ctx,
		"/consumers/"+*consumerUsernameOrID+"/acls", opt)
	if err != nil {
		return nil, nil, err
	}
	var aclGroups []*ACLGroup
	for _, object := range data {
		b, err := object.MarshalJSON()
		if err != nil {
			return nil, nil, err
		}
		var aclGroup ACLGroup
		err = json.Unmarshal(b, &aclGroup)
		if err != nil {
			return nil, nil, err
		}
		aclGroups = append(aclGroups, &aclGroup)
	}

	return aclGroups, next, nil
}