forked from Kong/go-kong
/
basic_auth_service.go
169 lines (150 loc) · 4.83 KB
/
basic_auth_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
167
168
169
package kong
import (
"context"
"encoding/json"
)
// AbstractBasicAuthService handles basic-auth credentials in Kong.
type AbstractBasicAuthService interface {
// Create creates a basic-auth credential in Kong
// is auto-generated.
Create(ctx context.Context, consumerUsernameOrID *string, basicAuth *BasicAuth) (*BasicAuth, error)
// Get fetches a basic-auth credential from Kong.
Get(ctx context.Context, consumerUsernameOrID, usernameOrID *string) (*BasicAuth, error)
// Update updates a basic-auth credential in Kong
Update(ctx context.Context, consumerUsernameOrID *string, basicAuth *BasicAuth) (*BasicAuth, error)
// Delete deletes a basic-auth credential in Kong
Delete(ctx context.Context, consumerUsernameOrID, usernameOrID *string) error
// List fetches a list of basic-auth credentials in Kong.
List(ctx context.Context, opt *ListOpt) ([]*BasicAuth, *ListOpt, error)
// ListAll fetches all basic-auth credentials in Kong.
ListAll(ctx context.Context) ([]*BasicAuth, error)
// ListForConsumer fetches a list of basic-auth credentials
// in Kong associated with a specific consumer.
ListForConsumer(ctx context.Context, consumerUsernameOrID *string, opt *ListOpt) ([]*BasicAuth, *ListOpt, error)
}
// BasicAuthService handles basic-auth credentials in Kong.
type BasicAuthService service
// Create creates a basic-auth credential in Kong
// If an ID is specified, it will be used to
// create a basic-auth in Kong, otherwise an ID
// is auto-generated.
func (s *BasicAuthService) Create(ctx context.Context,
consumerUsernameOrID *string, basicAuth *BasicAuth,
) (*BasicAuth, error) {
cred, err := s.client.credentials.Create(ctx, "basic-auth",
consumerUsernameOrID, basicAuth)
if err != nil {
return nil, err
}
var createdBasicAuth BasicAuth
err = json.Unmarshal(cred, &createdBasicAuth)
if err != nil {
return nil, err
}
return &createdBasicAuth, nil
}
// Get fetches a basic-auth credential from Kong.
func (s *BasicAuthService) Get(ctx context.Context,
consumerUsernameOrID, usernameOrID *string,
) (*BasicAuth, error) {
cred, err := s.client.credentials.Get(ctx, "basic-auth",
consumerUsernameOrID, usernameOrID)
if err != nil {
return nil, err
}
var basicAuth BasicAuth
err = json.Unmarshal(cred, &basicAuth)
if err != nil {
return nil, err
}
return &basicAuth, nil
}
// Update updates a basic-auth credential in Kong
func (s *BasicAuthService) Update(ctx context.Context,
consumerUsernameOrID *string, basicAuth *BasicAuth,
) (*BasicAuth, error) {
cred, err := s.client.credentials.Update(ctx, "basic-auth",
consumerUsernameOrID, basicAuth)
if err != nil {
return nil, err
}
var updatedBasicAuth BasicAuth
err = json.Unmarshal(cred, &updatedBasicAuth)
if err != nil {
return nil, err
}
return &updatedBasicAuth, nil
}
// Delete deletes a basic-auth credential in Kong
func (s *BasicAuthService) Delete(ctx context.Context,
consumerUsernameOrID, usernameOrID *string,
) error {
return s.client.credentials.Delete(ctx, "basic-auth",
consumerUsernameOrID, usernameOrID)
}
// List fetches a list of basic-auth credentials in Kong.
// opt can be used to control pagination.
func (s *BasicAuthService) List(ctx context.Context,
opt *ListOpt,
) ([]*BasicAuth, *ListOpt, error) {
data, next, err := s.client.list(ctx, "/basic-auths", opt)
if err != nil {
return nil, nil, err
}
var basicAuths []*BasicAuth
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, nil, err
}
var basicAuth BasicAuth
err = json.Unmarshal(b, &basicAuth)
if err != nil {
return nil, nil, err
}
basicAuths = append(basicAuths, &basicAuth)
}
return basicAuths, next, nil
}
// ListAll fetches all basic-auth credentials in Kong.
// This method can take a while if there
// a lot of basic-auth credentials present.
func (s *BasicAuthService) ListAll(ctx context.Context) ([]*BasicAuth, error) {
var basicAuths, data []*BasicAuth
var err error
opt := &ListOpt{Size: pageSize}
for opt != nil {
data, opt, err = s.List(ctx, opt)
if err != nil {
return nil, err
}
basicAuths = append(basicAuths, data...)
}
return basicAuths, nil
}
// ListForConsumer fetches a list of basic-auth credentials
// in Kong associated with a specific consumer.
// opt can be used to control pagination.
func (s *BasicAuthService) ListForConsumer(ctx context.Context,
consumerUsernameOrID *string, opt *ListOpt,
) ([]*BasicAuth, *ListOpt, error) {
data, next, err := s.client.list(ctx,
"/consumers/"+*consumerUsernameOrID+"/basic-auth", opt)
if err != nil {
return nil, nil, err
}
var basicAuths []*BasicAuth
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, nil, err
}
var basicAuth BasicAuth
err = json.Unmarshal(b, &basicAuth)
if err != nil {
return nil, nil, err
}
basicAuths = append(basicAuths, &basicAuth)
}
return basicAuths, next, nil
}