This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
apis.go
223 lines (205 loc) · 6.59 KB
/
apis.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
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
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////
package client
import (
"context"
"fmt"
"github.com/go-openapi/runtime"
"github.com/go-openapi/strfmt"
swaggerclient "github.com/vmware/dispatch/pkg/api-manager/gen/client"
"github.com/vmware/dispatch/pkg/api-manager/gen/client/endpoint"
"github.com/vmware/dispatch/pkg/api/v1"
)
// APIsClient defines the api client interface
type APIsClient interface {
// APIs
CreateAPI(ctx context.Context, organizationID string, api *v1.API) (*v1.API, error)
DeleteAPI(ctx context.Context, organizationID string, apiName string) (*v1.API, error)
UpdateAPI(ctx context.Context, organizationID string, api *v1.API) (*v1.API, error)
GetAPI(ctx context.Context, organizationID string, apiName string) (*v1.API, error)
ListAPIs(ctx context.Context, organizationID string) ([]v1.API, error)
}
// NewAPIsClient is used to create a new APIs client
func NewAPIsClient(host string, auth runtime.ClientAuthInfoWriter, organizationID string) *DefaultAPIsClient {
transport := DefaultHTTPClient(host, swaggerclient.DefaultBasePath)
return &DefaultAPIsClient{
baseClient: baseClient{
organizationID: organizationID,
},
client: swaggerclient.New(transport, strfmt.Default),
auth: auth,
}
}
// DefaultAPIsClient defines the default APIs client
type DefaultAPIsClient struct {
baseClient
client *swaggerclient.APIManager
auth runtime.ClientAuthInfoWriter
}
// CreateAPI creates new api
func (c *DefaultAPIsClient) CreateAPI(ctx context.Context, organizationID string, api *v1.API) (*v1.API, error) {
params := endpoint.AddAPIParams{
Context: ctx,
Body: api,
XDispatchOrg: c.getOrgID(organizationID),
}
response, err := c.client.Endpoint.AddAPI(¶ms, c.auth)
if err != nil {
return nil, createAPISwaggerError(err)
}
return response.Payload, nil
}
func createAPISwaggerError(err error) error {
if err == nil {
return nil
}
switch v := err.(type) {
case *endpoint.AddAPIBadRequest:
return NewErrorBadRequest(v.Payload)
case *endpoint.AddAPIUnauthorized:
return NewErrorUnauthorized(v.Payload)
case *endpoint.AddAPIForbidden:
return NewErrorForbidden(v.Payload)
case *endpoint.AddAPIConflict:
return NewErrorAlreadyExists(v.Payload)
case *endpoint.AddAPIDefault:
return NewErrorServerUnknownError(v.Payload)
default:
// shouldn't happen, but we need to be prepared:
return fmt.Errorf("unexpected error received from server: %s", err)
}
}
// DeleteAPI deletes an api
func (c *DefaultAPIsClient) DeleteAPI(ctx context.Context, organizationID string, apiName string) (*v1.API, error) {
params := endpoint.DeleteAPIParams{
Context: ctx,
API: apiName,
XDispatchOrg: c.getOrgID(organizationID),
}
response, err := c.client.Endpoint.DeleteAPI(¶ms, c.auth)
if err != nil {
return nil, deleteAPISwaggerError(err)
}
return response.Payload, nil
}
func deleteAPISwaggerError(err error) error {
if err == nil {
return nil
}
switch v := err.(type) {
case *endpoint.DeleteAPIBadRequest:
return NewErrorBadRequest(v.Payload)
case *endpoint.DeleteAPIUnauthorized:
return NewErrorUnauthorized(v.Payload)
case *endpoint.DeleteAPIForbidden:
return NewErrorForbidden(v.Payload)
case *endpoint.DeleteAPINotFound:
return NewErrorNotFound(v.Payload)
case *endpoint.DeleteAPIDefault:
return NewErrorServerUnknownError(v.Payload)
default:
// shouldn't happen, but we need to be prepared:
return fmt.Errorf("unexpected error received from server: %s", err)
}
}
// UpdateAPI updates an api
func (c *DefaultAPIsClient) UpdateAPI(ctx context.Context, organizationID string, api *v1.API) (*v1.API, error) {
params := endpoint.UpdateAPIParams{
Context: ctx,
Body: api,
API: *api.Name,
XDispatchOrg: c.getOrgID(organizationID),
}
response, err := c.client.Endpoint.UpdateAPI(¶ms, c.auth)
if err != nil {
return nil, updateAPISwaggerError(err)
}
return response.Payload, nil
}
func updateAPISwaggerError(err error) error {
if err == nil {
return nil
}
switch v := err.(type) {
case *endpoint.UpdateAPIBadRequest:
return NewErrorBadRequest(v.Payload)
case *endpoint.UpdateAPIUnauthorized:
return NewErrorUnauthorized(v.Payload)
case *endpoint.UpdateAPIForbidden:
return NewErrorForbidden(v.Payload)
case *endpoint.UpdateAPINotFound:
return NewErrorNotFound(v.Payload)
case *endpoint.UpdateAPIDefault:
return NewErrorServerUnknownError(v.Payload)
default:
// shouldn't happen, but we need to be prepared:
return fmt.Errorf("unexpected error received from server: %s", err)
}
}
// GetAPI retrieves an api
func (c *DefaultAPIsClient) GetAPI(ctx context.Context, organizationID string, apiName string) (*v1.API, error) {
params := endpoint.GetAPIParams{
Context: ctx,
API: apiName,
XDispatchOrg: c.getOrgID(organizationID),
}
response, err := c.client.Endpoint.GetAPI(¶ms, c.auth)
if err != nil {
return nil, getAPISwaggerError(err)
}
return response.Payload, nil
}
func getAPISwaggerError(err error) error {
if err == nil {
return nil
}
switch v := err.(type) {
case *endpoint.GetAPIBadRequest:
return NewErrorBadRequest(v.Payload)
case *endpoint.GetAPIUnauthorized:
return NewErrorUnauthorized(v.Payload)
case *endpoint.GetAPIForbidden:
return NewErrorForbidden(v.Payload)
case *endpoint.GetAPINotFound:
return NewErrorNotFound(v.Payload)
case *endpoint.GetAPIDefault:
return NewErrorServerUnknownError(v.Payload)
default:
// shouldn't happen, but we need to be prepared:
return fmt.Errorf("unexpected error received from server: %s", err)
}
}
// ListAPIs returns a list of APIs
func (c *DefaultAPIsClient) ListAPIs(ctx context.Context, organizationID string) ([]v1.API, error) {
params := endpoint.GetApisParams{
Context: ctx,
XDispatchOrg: c.getOrgID(organizationID),
}
response, err := c.client.Endpoint.GetApis(¶ms, c.auth)
if err != nil {
return nil, listAPIsSwaggerError(err)
}
apis := []v1.API{}
for _, api := range response.Payload {
apis = append(apis, *api)
}
return apis, nil
}
func listAPIsSwaggerError(err error) error {
if err == nil {
return nil
}
switch v := err.(type) {
case *endpoint.GetApisUnauthorized:
return NewErrorUnauthorized(v.Payload)
case *endpoint.GetApisForbidden:
return NewErrorForbidden(v.Payload)
case *endpoint.GetApisDefault:
return NewErrorServerUnknownError(v.Payload)
default:
// shouldn't happen, but we need to be prepared:
return fmt.Errorf("unexpected error received from server: %s", err)
}
}