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
|
package kong
import (
"context"
"encoding/json"
"errors"
"fmt"
)
// TargetService handles Targets in Kong.
type TargetService service
// TODO foreign key can be read directly from the embedded key itself
// upstreamNameOrID need not be an explicit parameter.
// Create creates a Target in Kong under upstreamID.
// If an ID is specified, it will be used to
// create a target in Kong, otherwise an ID
// is auto-generated.
func (s *TargetService) Create(ctx context.Context,
upstreamNameOrID *string, target *Target) (*Target, error) {
if isEmptyString(upstreamNameOrID) {
return nil, errors.New("upstreamNameOrID can not be nil")
}
queryPath := "/upstreams/" + *upstreamNameOrID + "/targets"
method := "POST"
// if target.ID != nil {
// queryPath = queryPath + "/" + *target.ID
// method = "PUT"
// }
req, err := s.client.NewRequest(method, queryPath, nil, target)
if err != nil {
return nil, err
}
var createdTarget Target
_, err = s.client.Do(ctx, req, &createdTarget)
if err != nil {
return nil, err
}
return &createdTarget, nil
}
// Delete deletes a Target in Kong
func (s *TargetService) Delete(ctx context.Context,
upstreamNameOrID *string, targetOrID *string) error {
if isEmptyString(upstreamNameOrID) {
return errors.New("upstreamNameOrID cannot be nil for Get operation")
}
if isEmptyString(targetOrID) {
return errors.New("targetOrID cannot be nil for Delete operation")
}
endpoint := fmt.Sprintf("/upstreams/%v/targets/%v",
*upstreamNameOrID, *targetOrID)
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 Targets in Kong.
// opt can be used to control pagination.
func (s *TargetService) List(ctx context.Context,
upstreamNameOrID *string, opt *ListOpt) ([]*Target, *ListOpt, error) {
if isEmptyString(upstreamNameOrID) {
return nil, nil, errors.New(
"upstreamNameOrID cannot be nil for Get operation")
}
data, next, err := s.client.list(ctx,
"/upstreams/"+*upstreamNameOrID+"/targets", opt)
if err != nil {
return nil, nil, err
}
var targets []*Target
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, nil, err
}
var target Target
err = json.Unmarshal(b, &target)
if err != nil {
return nil, nil, err
}
targets = append(targets, &target)
}
return targets, next, nil
}
// ListAll fetches all Targets in Kong for an upstream.
func (s *TargetService) ListAll(ctx context.Context,
upstreamNameOrID *string) ([]*Target, error) {
var targets, data []*Target
var err error
opt := &ListOpt{Size: pageSize}
for opt != nil {
data, opt, err = s.List(ctx, upstreamNameOrID, opt)
if err != nil {
return nil, err
}
targets = append(targets, data...)
}
return targets, nil
}
// MarkHealthy marks target belonging to upstreamNameOrID as healthy in
// Kong's load balancer.
func (s *TargetService) MarkHealthy(ctx context.Context,
upstreamNameOrID *string, target *Target) error {
if target == nil {
return errors.New("cannot set health status for a nil target")
}
if isEmptyString(target.ID) && isEmptyString(target.Target) {
return errors.New("need at least one of target or ID to" +
" set health status")
}
if isEmptyString(upstreamNameOrID) {
return errors.New("upstreamNameOrID cannot be nil " +
"for updating health check")
}
tid := target.ID
if target.ID == nil {
tid = target.Target
}
endpoint := fmt.Sprintf("/upstreams/%v/targets/%v/healthy",
*upstreamNameOrID, *tid)
req, err := s.client.NewRequest("POST", endpoint, nil, nil)
if err != nil {
return err
}
_, err = s.client.Do(ctx, req, nil)
return err
}
// MarkUnhealthy marks target belonging to upstreamNameOrID as unhealthy in
// Kong's load balancer.
func (s *TargetService) MarkUnhealthy(ctx context.Context,
upstreamNameOrID *string, target *Target) error {
if target == nil {
return errors.New("cannot set health status for a nil target")
}
if isEmptyString(target.ID) && isEmptyString(target.Target) {
return errors.New("need at least one of target or ID to" +
" set health status")
}
if isEmptyString(upstreamNameOrID) {
return errors.New("upstreamNameOrID cannot be nil " +
"for updating health check")
}
tid := target.ID
if target.ID == nil {
tid = target.Target
}
endpoint := fmt.Sprintf("/upstreams/%v/targets/%v/unhealthy",
*upstreamNameOrID, *tid)
req, err := s.client.NewRequest("POST", endpoint, nil, nil)
if err != nil {
return err
}
_, err = s.client.Do(ctx, req, nil)
return err
}
|