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
|
package kong
import (
"context"
"encoding/json"
"errors"
"fmt"
)
// WorkspaceService handles Workspaces in Kong.
type WorkspaceService service
// Create creates a Workspace in Kong.
func (s *WorkspaceService) Create(ctx context.Context,
workspace *Workspace) (*Workspace, error) {
if workspace == nil {
return nil, errors.New("cannot create a nil workspace")
}
endpoint := "/workspaces"
method := "POST"
if workspace.ID != nil {
endpoint = endpoint + "/" + *workspace.ID
method = "PUT"
}
req, err := s.client.NewRequest(method, endpoint, nil, workspace)
if err != nil {
return nil, err
}
var createdWorkspace Workspace
_, err = s.client.Do(ctx, req, &createdWorkspace)
if err != nil {
return nil, err
}
return &createdWorkspace, nil
}
// Get fetches a Workspace in Kong.
func (s *WorkspaceService) Get(ctx context.Context,
nameOrID *string) (*Workspace, error) {
if isEmptyString(nameOrID) {
return nil, errors.New("nameOrID cannot be nil for Get operation")
}
endpoint := fmt.Sprintf("/workspaces/%v", *nameOrID)
req, err := s.client.NewRequest("GET", endpoint, nil, nil)
if err != nil {
return nil, err
}
var Workspace Workspace
_, err = s.client.Do(ctx, req, &Workspace)
if err != nil {
return nil, err
}
return &Workspace, nil
}
// Update updates a Workspace in Kong. Only updates to the
// `comment` field are supported. To rename a workspace use Create.
func (s *WorkspaceService) Update(ctx context.Context,
workspace *Workspace) (*Workspace, error) {
if workspace == nil {
return nil, errors.New("cannot update a nil Workspace")
}
if isEmptyString(workspace.ID) {
return nil, errors.New("ID cannot be nil for Update operation")
}
endpoint := fmt.Sprintf("/workspaces/%v", *workspace.ID)
req, err := s.client.NewRequest("PATCH", endpoint, nil, workspace)
if err != nil {
return nil, err
}
var updatedWorkspace Workspace
_, err = s.client.Do(ctx, req, &updatedWorkspace)
if err != nil {
return nil, err
}
return &updatedWorkspace, nil
}
// Delete deletes a Workspace in Kong
func (s *WorkspaceService) Delete(ctx context.Context,
WorkspaceOrID *string) error {
if isEmptyString(WorkspaceOrID) {
return errors.New("WorkspaceOrID cannot be nil for Delete operation")
}
endpoint := fmt.Sprintf("/workspaces/%v", *WorkspaceOrID)
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 all Workspaces in Kong.
func (s *WorkspaceService) List(ctx context.Context,
opt *ListOpt) ([]*Workspace, *ListOpt, error) {
data, next, err := s.client.list(ctx, "/workspaces/", opt)
if err != nil {
return nil, nil, err
}
var workspaces []*Workspace
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, nil, err
}
var workspace Workspace
err = json.Unmarshal(b, &workspace)
if err != nil {
return nil, nil, err
}
workspaces = append(workspaces, &workspace)
}
return workspaces, next, nil
}
// ListAll fetches all workspaces in Kong.
func (s *WorkspaceService) ListAll(ctx context.Context) ([]*Workspace, error) {
var workspaces, data []*Workspace
var err error
opt := &ListOpt{Size: pageSize}
for opt != nil {
data, opt, err = s.List(ctx, opt)
if err != nil {
return nil, err
}
workspaces = append(workspaces, data...)
}
return workspaces, nil
}
// AddEntities adds entity ids given as a a comma delimited string
// to a given workspace in Kong. The response is a representation
// of the entity that was added to the workspace.
func (s *WorkspaceService) AddEntities(ctx context.Context,
workspaceNameOrID *string, entityIds *string) (*[]map[string]interface{}, error) {
if entityIds == nil {
return nil, errors.New("entityIds cannot be nil")
}
endpoint := fmt.Sprintf("/workspaces/%v/entities", *workspaceNameOrID)
var entities struct {
Entities *string `json:"entities,omitempty"`
}
entities.Entities = entityIds
req, err := s.client.NewRequest("POST", endpoint, nil, entities)
if err != nil {
return nil, err
}
var createdWorkspaceEntities []map[string]interface{}
_, err = s.client.Do(ctx, req, &createdWorkspaceEntities)
if err != nil {
return nil, err
}
return &createdWorkspaceEntities, nil
}
// DeleteEntities deletes entity ids given as a a comma delimited string
// to a given workspace in Kong.
func (s *WorkspaceService) DeleteEntities(ctx context.Context,
workspaceNameOrID *string, entityIds *string) error {
if entityIds == nil {
return errors.New("entityIds cannot be nil")
}
endpoint := fmt.Sprintf("/workspaces/%v/entities", *workspaceNameOrID)
var entities struct {
Entities *string `json:"entities,omitempty"`
}
entities.Entities = entityIds
req, err := s.client.NewRequest("DELETE", endpoint, nil, entities)
if err != nil {
return err
}
_, err = s.client.Do(ctx, req, nil)
if err != nil {
return err
}
return nil
}
// ListEntities fetches a list of all workspace entities in Kong.
func (s *WorkspaceService) ListEntities(ctx context.Context,
workspaceNameOrID *string) ([]*WorkspaceEntity, error) {
endpoint := fmt.Sprintf("/workspaces/%v/entities", *workspaceNameOrID)
data, _, err := s.client.list(ctx, endpoint, nil)
if err != nil {
return nil, err
}
var workspaceEntities []*WorkspaceEntity
for _, object := range data {
b, err := object.MarshalJSON()
if err != nil {
return nil, err
}
var workspaceEntity WorkspaceEntity
err = json.Unmarshal(b, &workspaceEntity)
if err != nil {
return nil, err
}
workspaceEntities = append(workspaceEntities, &workspaceEntity)
}
return workspaceEntities, nil
}
|