-
Notifications
You must be signed in to change notification settings - Fork 17
/
pivnet.go
349 lines (295 loc) · 8.6 KB
/
pivnet.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package pivnet
import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"github.com/pivotal-cf/go-pivnet/v7/download"
"github.com/pivotal-cf/go-pivnet/v7/logger"
)
const (
DefaultHost = "https://network.tanzu.vmware.com"
apiVersion = "/api/v2"
concurrentDownloads = 10
)
type Client struct {
baseURL string
token AccessTokenService
userAgent string
logger logger.Logger
usingUAAToken bool
HTTP *http.Client
downloader download.Client
Auth *AuthService
EULA *EULAsService
ProductFiles *ProductFilesService
ArtifactReferences *ArtifactReferencesService
FederationToken *FederationTokenService
FileGroups *FileGroupsService
Releases *ReleasesService
Products *ProductsService
UserGroups *UserGroupsService
SubscriptionGroups *SubscriptionGroupsService
ReleaseTypes *ReleaseTypesService
ReleaseDependencies *ReleaseDependenciesService
DependencySpecifiers *DependencySpecifiersService
ReleaseUpgradePaths *ReleaseUpgradePathsService
UpgradePathSpecifiers *UpgradePathSpecifiersService
PivnetVersions *PivnetVersionsService
}
type AccessTokenOrLegacyToken struct {
host string
refreshToken string
skipSSLValidation bool
userAgent string
}
type QueryParameter struct {
Key string
Value string
}
func (o AccessTokenOrLegacyToken) AccessToken() (string, error) {
const legacyAPITokenLength = 20
if len(o.refreshToken) > legacyAPITokenLength {
baseURL := fmt.Sprintf("%s%s", o.host, apiVersion)
tokenFetcher := NewTokenFetcher(baseURL, o.refreshToken, o.skipSSLValidation, o.userAgent)
accessToken, err := tokenFetcher.GetToken()
if err != nil {
log.Panicf("Exiting with error: %s", err)
return "", err
}
return accessToken, nil
} else {
return o.refreshToken, nil
}
}
func AuthorizationHeader(accessToken string) (string, error) {
const legacyAPITokenLength = 20
if len(accessToken) > legacyAPITokenLength {
return fmt.Sprintf("Bearer %s", accessToken), nil
} else {
return fmt.Sprintf("Token %s", accessToken), nil
}
}
type ClientConfig struct {
Host string
UserAgent string
SkipSSLValidation bool
}
//go:generate counterfeiter . AccessTokenService
type AccessTokenService interface {
AccessToken() (string, error)
}
func NewAccessTokenOrLegacyToken(token string, host string, skipSSLValidation bool, userAgentOptional ...string) AccessTokenOrLegacyToken {
var userAgent = ""
if len(userAgentOptional) > 0 {
userAgent = userAgentOptional[0]
}
return AccessTokenOrLegacyToken{
refreshToken: token,
host: host,
skipSSLValidation: skipSSLValidation,
userAgent: userAgent,
}
}
func NewClient(
token AccessTokenService,
config ClientConfig,
logger logger.Logger,
) Client {
baseURL := fmt.Sprintf("%s%s", config.Host, apiVersion)
httpClient := &http.Client{
Timeout: 10 * time.Minute,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: config.SkipSSLValidation,
},
Proxy: http.ProxyFromEnvironment,
},
}
downloadClient := &http.Client{
Timeout: 0,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: config.SkipSSLValidation,
},
Proxy: http.ProxyFromEnvironment,
},
}
ranger := download.NewRanger(concurrentDownloads)
downloader := download.Client{
HTTPClient: downloadClient,
Ranger: ranger,
Logger: logger,
Timeout: 30 * time.Second,
}
client := Client{
baseURL: baseURL,
token: token,
userAgent: config.UserAgent,
logger: logger,
downloader: downloader,
HTTP: httpClient,
}
client.Auth = &AuthService{client: client}
client.EULA = &EULAsService{client: client}
client.ProductFiles = &ProductFilesService{client: client}
client.ArtifactReferences = &ArtifactReferencesService{client: client}
client.FederationToken = &FederationTokenService{client: client}
client.FileGroups = &FileGroupsService{client: client}
client.Releases = &ReleasesService{client: client, l: logger}
client.Products = &ProductsService{client: client, l: logger}
client.UserGroups = &UserGroupsService{client: client}
client.SubscriptionGroups = &SubscriptionGroupsService{client: client}
client.ReleaseTypes = &ReleaseTypesService{client: client}
client.ReleaseDependencies = &ReleaseDependenciesService{client: client}
client.DependencySpecifiers = &DependencySpecifiersService{client: client}
client.ReleaseUpgradePaths = &ReleaseUpgradePathsService{client: client}
client.UpgradePathSpecifiers = &UpgradePathSpecifiersService{client: client}
client.PivnetVersions = &PivnetVersionsService{client: client}
return client
}
func (c Client) CreateRequest(
requestType string,
endpoint string,
body io.Reader,
) (*http.Request, error) {
u, err := url.Parse(c.baseURL)
if err != nil {
return nil, err
}
endpoint = c.stripHostPrefix(endpoint)
u.Path = u.Path + endpoint
req, err := http.NewRequest(requestType, u.String(), body)
if err != nil {
return nil, err
}
if !isVersionsEndpoint(endpoint) {
accessToken, err := c.token.AccessToken()
if err != nil {
return nil, err
}
authorizationHeader, err := AuthorizationHeader(accessToken)
if err != nil {
return nil, fmt.Errorf("could not create authorization header: %s", err)
}
req.Header.Add("Authorization", authorizationHeader)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", c.userAgent)
return req, nil
}
func (c Client) MakeRequest(
requestType string,
endpoint string,
expectedStatusCode int,
body io.Reader,
) (*http.Response, error) {
req, err := c.CreateRequest(requestType, endpoint, body)
if err != nil {
return nil, err
}
reqBytes, err := httputil.DumpRequestOut(req, true)
if err != nil {
return nil, err
}
c.logger.Debug("Making request", logger.Data{"request": string(reqBytes)})
resp, err := c.HTTP.Do(req)
if err != nil {
return nil, err
}
c.logger.Debug("Response status code", logger.Data{"status code": resp.StatusCode})
c.logger.Debug("Response headers", logger.Data{"headers": resp.Header})
if expectedStatusCode > 0 && resp.StatusCode != expectedStatusCode {
return nil, c.handleUnexpectedResponse(resp)
}
return resp, nil
}
func (c Client) MakeRequestWithParams(
requestType string,
endpoint string,
expectedStatusCode int,
params []QueryParameter,
body io.Reader,
) (*http.Response, error) {
req, err := c.CreateRequest(requestType, endpoint, body)
if err != nil {
return nil, err
}
q := req.URL.Query()
for _, param := range params {
q.Add(param.Key, param.Value)
}
req.URL.RawQuery = q.Encode()
reqBytes, err := httputil.DumpRequestOut(req, true)
if err != nil {
return nil, err
}
c.logger.Debug("Making request", logger.Data{"request": string(reqBytes)})
resp, err := c.HTTP.Do(req)
if err != nil {
return nil, err
}
c.logger.Debug("Response status code", logger.Data{"status code": resp.StatusCode})
c.logger.Debug("Response headers", logger.Data{"headers": resp.Header})
if expectedStatusCode > 0 && resp.StatusCode != expectedStatusCode {
return nil, c.handleUnexpectedResponse(resp)
}
return resp, nil
}
func (c Client) stripHostPrefix(downloadLink string) string {
if strings.HasPrefix(downloadLink, apiVersion) {
return downloadLink
}
sp := strings.Split(downloadLink, apiVersion)
return sp[len(sp)-1]
}
func (c Client) handleUnexpectedResponse(resp *http.Response) error {
var pErr pivnetErr
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode == http.StatusTooManyRequests {
return newErrTooManyRequests()
}
// We have to handle 500 differently because it has a different structure
if resp.StatusCode == http.StatusInternalServerError {
var internalServerError pivnetInternalServerErr
err = json.Unmarshal(b, &internalServerError)
if err != nil {
return err
}
pErr = pivnetErr{
Message: internalServerError.Error,
}
} else {
err = json.Unmarshal(b, &pErr)
if err != nil {
return fmt.Errorf("could not parse json [%q] \n%s", b, err)
}
}
switch resp.StatusCode {
case http.StatusUnauthorized:
return newErrUnauthorized(pErr.Message)
case http.StatusNotFound:
return newErrNotFound(pErr.Message)
case http.StatusUnavailableForLegalReasons:
return newErrUnavailableForLegalReasons(pErr.Message)
default:
return ErrPivnetOther{
ResponseCode: resp.StatusCode,
Message: pErr.Message,
Errors: pErr.Errors,
}
}
}
func isVersionsEndpoint(endpoint string) bool {
return endpoint == "/versions"
}