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
|
package kong
import (
"context"
"encoding/json"
"errors"
"fmt"
)
// UpstreamService handles Upstreams in Kong.
type UpstreamService service
// Create creates a Upstream in Kong.
// If an ID is specified, it will be used to
// create a upstream in Kong, otherwise an ID
// is auto-generated.
func (s *UpstreamService) Create(ctx context.Context,
upstream *Upstream) (*Upstream, error) {
queryPath := "/upstreams"
method := "POST"
if upstream.ID != nil {
queryPath = queryPath + "/" + *upstream.ID
method = "PUT"
}
req, err := s.client.NewRequest(method, queryPath, nil, upstream)
if err != nil {
return nil, err
}
var createdUpstream Upstream
_, err = s.client.Do(ctx, req, &createdUpstream)
if err != nil {
return nil, err
}
return &createdUpstream, nil
}
// Get fetches a Upstream in Kong.
func (s *UpstreamService) Get(ctx context.Context,
upstreamNameOrID *string) (*Upstream, error) {
if isEmptyString(upstreamNameOrID) {
return nil, errors.New("upstreamNameOrID cannot" +
" be nil for Get operation")
}
endpoint := fmt.Sprintf("/upstreams/%v", *upstreamNameOrID)
req, err := s.client.NewRequest("GET", endpoint, nil, nil)
if err != nil {
return nil, err
}
var upstream Upstream
_, err = s.client.Do(ctx, req, &upstream)
if err != nil {
return nil, err
}
return &upstream, nil
}
// Update updates a Upstream in Kong
func (s *UpstreamService) Update(ctx context.Context,
upstream *Upstream) (*Upstream, error) {
if isEmptyString(upstream.ID) {
return nil, errors.New("ID cannot be nil for Update operation")
}
endpoint := fmt.Sprintf("/upstreams/%v", *upstream.ID)
req, err := s.client.NewRequest("PATCH", endpoint, nil, upstream)
if err != nil {
return nil, err
}
var updatedUpstream Upstream
_, err = s.client.Do(ctx, req, &updatedUpstream)
if err != nil {
return nil, err
}
return &updatedUpstream, nil
}
// Delete deletes a Upstream in Kong
func (s *UpstreamService) Delete(ctx context.Context,
upstreamNameOrID *string) error {
if isEmptyString(upstreamNameOrID) {
return errors.New("upstreamNameOrID cannot be nil for Delete operation")
}
endpoint := fmt.Sprintf("/upstreams/%v", *upstreamNameOrID)
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 Upstreams in Kong.
// opt can be used to control pagination.
func (s *UpstreamService) List(ctx context.Context,
opt *ListOpt) ([]*Upstream, *ListOpt, error) {
data, next, err := s.client.list(ctx, "/upstreams", opt)
if err != nil {
return nil, nil, err
}
var upstreams []*Upstream
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, nil, err
}
var upstream Upstream
err = json.Unmarshal(b, &upstream)
if err != nil {
return nil, nil, err
}
upstreams = append(upstreams, &upstream)
}
return upstreams, next, nil
}
// ListAll fetches all Upstreams in Kong.
// This method can take a while if there
// a lot of Upstreams present.
func (s *UpstreamService) ListAll(ctx context.Context) ([]*Upstream, error) {
var upstreams, data []*Upstream
var err error
opt := &ListOpt{Size: pageSize}
for opt != nil {
data, opt, err = s.List(ctx, opt)
if err != nil {
return nil, err
}
upstreams = append(upstreams, data...)
}
return upstreams, nil
}
|