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
|
package kong
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httputil"
"net/url"
"os"
"github.com/pkg/errors"
"github.com/kong/go-kong/kong/custom"
)
const defaultBaseURL = "http://localhost:8001"
var pageSize = 1000
type service struct {
client *Client
}
var (
defaultCtx = context.Background()
)
// Client talks to the Admin API or control plane of a
// Kong cluster
type Client struct {
client *http.Client
baseURL string
common service
Consumers *ConsumerService
Services *Svcservice
Routes *RouteService
CACertificates *CACertificateService
Certificates *CertificateService
Plugins *PluginService
SNIs *SNIService
Upstreams *UpstreamService
Targets *TargetService
Workspaces *WorkspaceService
Admins *AdminService
RBACUsers *RBACUserService
RBACRoles *RBACRoleService
RBACEndpointPermissions *RBACEndpointPermissionService
RBACEntityPermissions *RBACEntityPermissionService
credentials *credentialService
KeyAuths *KeyAuthService
BasicAuths *BasicAuthService
HMACAuths *HMACAuthService
JWTAuths *JWTAuthService
MTLSAuths *MTLSAuthService
ACLs *ACLService
Oauth2Credentials *Oauth2Service
logger io.Writer
debug bool
CustomEntities *CustomEntityService
custom.Registry
}
// Status respresents current status of a Kong node.
type Status struct {
Database struct {
Reachable bool `json:"reachable"`
} `json:"database"`
Server struct {
ConnectionsAccepted int `json:"connections_accepted"`
ConnectionsActive int `json:"connections_active"`
ConnectionsHandled int `json:"connections_handled"`
ConnectionsReading int `json:"connections_reading"`
ConnectionsWaiting int `json:"connections_waiting"`
ConnectionsWriting int `json:"connections_writing"`
TotalRequests int `json:"total_requests"`
} `json:"server"`
}
// NewClient returns a Client which talks to Admin API of Kong
func NewClient(baseURL *string, client *http.Client) (*Client, error) {
if client == nil {
client = http.DefaultClient
}
kong := new(Client)
kong.client = client
var rootURL string
if baseURL != nil {
rootURL = *baseURL
} else {
rootURL = defaultBaseURL
}
url, err := url.ParseRequestURI(rootURL)
if err != nil {
return nil, errors.Wrap(err, "parsing URL")
}
kong.baseURL = url.String()
kong.common.client = kong
kong.Consumers = (*ConsumerService)(&kong.common)
kong.Services = (*Svcservice)(&kong.common)
kong.Routes = (*RouteService)(&kong.common)
kong.Plugins = (*PluginService)(&kong.common)
kong.Certificates = (*CertificateService)(&kong.common)
kong.CACertificates = (*CACertificateService)(&kong.common)
kong.SNIs = (*SNIService)(&kong.common)
kong.Upstreams = (*UpstreamService)(&kong.common)
kong.Targets = (*TargetService)(&kong.common)
kong.Workspaces = (*WorkspaceService)(&kong.common)
kong.Admins = (*AdminService)(&kong.common)
kong.RBACUsers = (*RBACUserService)(&kong.common)
kong.RBACRoles = (*RBACRoleService)(&kong.common)
kong.RBACEndpointPermissions = (*RBACEndpointPermissionService)(&kong.common)
kong.RBACEntityPermissions = (*RBACEntityPermissionService)(&kong.common)
kong.credentials = (*credentialService)(&kong.common)
kong.KeyAuths = (*KeyAuthService)(&kong.common)
kong.BasicAuths = (*BasicAuthService)(&kong.common)
kong.HMACAuths = (*HMACAuthService)(&kong.common)
kong.JWTAuths = (*JWTAuthService)(&kong.common)
kong.MTLSAuths = (*MTLSAuthService)(&kong.common)
kong.ACLs = (*ACLService)(&kong.common)
kong.Oauth2Credentials = (*Oauth2Service)(&kong.common)
kong.CustomEntities = (*CustomEntityService)(&kong.common)
kong.Registry = custom.NewDefaultRegistry()
for i := 0; i < len(defaultCustomEntities); i++ {
err := kong.Register(defaultCustomEntities[i].Type(),
&defaultCustomEntities[i])
if err != nil {
return nil, err
}
}
kong.logger = os.Stderr
return kong, nil
}
// Do executes a HTTP request and returns a response
func (c *Client) Do(ctx context.Context, req *http.Request,
v interface{}) (*Response, error) {
var err error
if req == nil {
return nil, errors.New("request cannot be nil")
}
if ctx == nil {
ctx = defaultCtx
}
req = req.WithContext(ctx)
// log the request
err = c.logRequest(req)
if err != nil {
return nil, err
}
//Make the request
resp, err := c.client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "making HTTP request")
}
// log the response
err = c.logResponse(resp)
if err != nil {
return nil, err
}
response := newResponse(resp)
///check for API errors
if err = hasError(resp); err != nil {
return response, err
}
// Call Close on exit
defer func() {
e := resp.Body.Close()
if e != nil {
err = e
}
}()
// response
if v != nil {
if writer, ok := v.(io.Writer); ok {
_, err = io.Copy(writer, resp.Body)
if err != nil {
return nil, err
}
} else {
err = json.NewDecoder(resp.Body).Decode(v)
if err != nil {
return nil, err
}
}
}
return response, err
}
// SetDebugMode enables or disables logging of
// the request to the logger set by SetLogger().
// By default, debug logging is disabled.
func (c *Client) SetDebugMode(enableDebug bool) {
c.debug = enableDebug
}
func (c *Client) logRequest(r *http.Request) error {
if !c.debug {
return nil
}
dump, err := httputil.DumpRequestOut(r, true)
if err != nil {
return err
}
_, err = c.logger.Write(append(dump, '\n'))
return err
}
func (c *Client) logResponse(r *http.Response) error {
if !c.debug {
return nil
}
dump, err := httputil.DumpResponse(r, true)
if err != nil {
return err
}
_, err = c.logger.Write(append(dump, '\n'))
return err
}
// SetLogger sets the debug logger, defaults to os.StdErr
func (c *Client) SetLogger(w io.Writer) {
if w == nil {
return
}
c.logger = w
}
// Status returns the status of a Kong node
func (c *Client) Status(ctx context.Context) (*Status, error) {
req, err := c.NewRequest("GET", "/status", nil, nil)
if err != nil {
return nil, err
}
var s Status
_, err = c.Do(ctx, req, &s)
if err != nil {
return nil, err
}
return &s, nil
}
// Root returns the response of GET request on root of
// Admin API (GET /).
func (c *Client) Root(ctx context.Context) (map[string]interface{}, error) {
req, err := c.NewRequest("GET", "/", nil, nil)
if err != nil {
return nil, err
}
var root map[string]interface{}
_, err = c.Do(ctx, req, &root)
if err != nil {
return nil, err
}
return root, nil
}
|