-
Notifications
You must be signed in to change notification settings - Fork 39
/
apiversion.go
280 lines (258 loc) · 8.51 KB
/
apiversion.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
package client
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"path"
"strconv"
"strings"
goosehttp "gopkg.in/goose.v3/http"
"gopkg.in/goose.v3/logging"
)
// ApiVersion represents choices.id from the openstack
// api version Multiple choices JSON response, broken
// into major and minor from the string.
type ApiVersion struct {
Major int
Minor int
}
// ApiVersionInfo represents choices from the openstack
// api version Multiple choices JSON response.
type ApiVersionInfo struct {
Version ApiVersion `json:"id"`
Links []ApiVersionLink `json:"links"`
Status string `json:"status"`
}
// ApiVersionLink represents choices.links from the openstack
// api version Multiple choices JSON response.
type ApiVersionLink struct {
Href string `json:"href"`
Rel string `json:"rel"`
}
type apiURLVersion struct {
rootURL url.URL
serviceURLSuffix string
versions []ApiVersionInfo
}
// getAPIVersionURL returns a full formed serviceURL based on the API version requested,
// the rootURL and the serviceURLSuffix. If there is no match to the requested API
// version an error is returned. If only the Major number is defined for the requested
// version, the first match found is returned.
func (c *authenticatingClient) getAPIVersionURL(apiURLVersionInfo *apiURLVersion, requested ApiVersion) (string, error) {
var match string
for _, v := range apiURLVersionInfo.versions {
if v.Version.Major != requested.Major {
continue
}
if requested.Minor != -1 && v.Version.Minor != requested.Minor {
continue
}
for _, link := range v.Links {
if link.Rel != "self" {
continue
}
hrefURL, err := url.Parse(link.Href)
if err != nil {
return "", err
}
match = hrefURL.Path
}
if requested.Minor != -1 {
break
}
}
if match == "" {
return "", fmt.Errorf("could not find matching URL")
}
versionURL := apiURLVersionInfo.rootURL
// https://bugs.launchpad.net/juju/+bug/1756135:
// some hrefURL.Path contain more than the version, with
// overlap on the apiURLVersionInfo.rootURL
if strings.HasPrefix(match, "/"+versionURL.Path) {
logger := logging.FromCompat(c.logger)
logger.Tracef("version href path %q overlaps with url path %q, using version href", match, versionURL.Path)
versionURL.Path = "/"
}
versionURL.Path = path.Join(versionURL.Path, match, apiURLVersionInfo.serviceURLSuffix)
return versionURL.String(), nil
}
func (v *ApiVersion) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
parsed, err := parseVersion(s)
if err != nil {
return err
}
*v = parsed
return nil
}
// parseVersion takes a version string into the Major and Minor ints for an ApiVersion
// structure. The string part of the data is returned by a request to List API versions
// send to an OpenStack service. It is in the format "v<Major>.<Minor>". If ApiVersion
// is empty, return {-1, -1}, to differentiate with "v0".
func parseVersion(s string) (ApiVersion, error) {
if s == "" {
return ApiVersion{-1, -1}, nil
}
s = strings.TrimPrefix(s, "v")
parts := strings.SplitN(s, ".", 2)
if len(parts) == 0 || len(parts) > 2 {
return ApiVersion{}, fmt.Errorf("invalid API version %q", s)
}
var minor int = -1
major, err := strconv.Atoi(parts[0])
if err != nil {
return ApiVersion{}, err
}
if len(parts) == 2 {
var err error
minor, err = strconv.Atoi(parts[1])
if err != nil {
return ApiVersion{}, err
}
}
return ApiVersion{major, minor}, nil
}
func unmarshallVersion(Versions json.RawMessage) ([]ApiVersionInfo, error) {
// Some services respond with {"versions":[...]}, and
// some respond with {"versions":{"values":[...]}}.
var object interface{}
var versions []ApiVersionInfo
if err := json.Unmarshal(Versions, &object); err != nil {
return versions, err
}
if _, ok := object.(map[string]interface{}); ok {
var valuesObject struct {
Values []ApiVersionInfo `json:"values"`
}
if err := json.Unmarshal(Versions, &valuesObject); err != nil {
return versions, err
}
versions = valuesObject.Values
} else {
if err := json.Unmarshal(Versions, &versions); err != nil {
return versions, err
}
}
return versions, nil
}
// getAPIVersions returns data on the API versions supported by the specified
// service endpoint. Some OpenStack clouds do not support the version endpoint,
// in which case this method will return an empty set of versions in the result
// structure.
func (c *authenticatingClient) getAPIVersions(serviceCatalogURL string) (*apiURLVersion, error) {
c.apiVersionMu.Lock()
defer c.apiVersionMu.Unlock()
logger := logging.FromCompat(c.logger)
// Make sure we haven't already received the version info.
// Cache done on serviceCatalogURL, https://<url.Host> is not
// guaranteed to be unique.
if apiInfo, ok := c.apiURLVersions[serviceCatalogURL]; ok {
return apiInfo, nil
}
url, err := url.Parse(serviceCatalogURL)
if err != nil {
return nil, err
}
// Identify the version in the URL, if there is one, and record
// everything proceeding it. We will need to append this to the
// API version-specific base URL.
var pathParts, origPathParts []string
if url.Path != "/" {
// If a version is included in the serviceCatalogURL, the
// part before the version will end up in url, the part after
// the version will end up in pathParts. origPathParts is a
// special case for "object-store"
// e.g. https://storage101.dfw1.clouddrive.com/v1/MossoCloudFS_1019383
// becomes: https://storage101.dfw1.clouddrive.com/ and MossoCloudFS_1019383
// https://x.x.x.x/image
// becomes: https://x.x.x.x/image/
// https://x.x.x.x/cloudformation/v1
// becomes: https://x.x.x.x/cloudformation/
// https://x.x.x.x/compute/v2/9032a0051293421eb20b64da69d46252
// becomes: https://x.x.x.x/compute/ and 9032a0051293421eb20b64da69d46252
// https://x.x.x.x/volumev1/v2
// becomes: https://x.x.x.x/volumev1/
// http://y.y.y.y:9292
// becomes: http://y.y.y.y:9292/
// http://y.y.y.y:8774/v2/010ab46135ba414882641f663ec917b6
// becomes: http://y.y.y.y:8774/ and 010ab46135ba414882641f663ec917b6
origPathParts = strings.Split(strings.Trim(url.Path, "/"), "/")
pathParts = origPathParts
found := false
for i, p := range pathParts {
if _, err := parseVersion(p); err == nil {
found = true
if i == 0 {
pathParts = pathParts[1:]
url.Path = "/"
} else {
url.Path = pathParts[0] + "/"
pathParts = pathParts[2:]
}
break
}
}
if !found {
url.Path = path.Join(pathParts...) + "/"
pathParts = []string{}
}
}
logger.Tracef("api version will be inserted between %q and %q", url.String(), path.Join(pathParts...)+"/")
getVersionURL := url.String()
// If this is an object-store serviceType, or an object-store container endpoint,
// there is no list version API call to make. Return a apiURLVersion which will
// satisfy a requested api version of "", "v1" or "v1.0"
if c.serviceURLs["object-store"] != "" && strings.Contains(serviceCatalogURL, c.serviceURLs["object-store"]) {
url.Path = "/"
objectStoreLink := ApiVersionLink{Href: url.String(), Rel: "self"}
objectStoreApiVersionInfo := []ApiVersionInfo{
{
Version: ApiVersion{Major: 1, Minor: 0},
Links: []ApiVersionLink{objectStoreLink},
Status: "stable",
},
{
Version: ApiVersion{Major: -1, Minor: -1},
Links: []ApiVersionLink{objectStoreLink},
Status: "stable",
},
}
apiURLVersionInfo := &apiURLVersion{*url, strings.Join(origPathParts, "/"), objectStoreApiVersionInfo}
c.apiURLVersions[serviceCatalogURL] = apiURLVersionInfo
return apiURLVersionInfo, nil
}
var raw struct {
Versions json.RawMessage `json:"versions"`
}
requestData := &goosehttp.RequestData{
RespValue: &raw,
ExpectedStatus: []int{
http.StatusOK,
http.StatusMultipleChoices,
},
}
apiURLVersionInfo := &apiURLVersion{
rootURL: *url,
serviceURLSuffix: strings.Join(pathParts, "/"),
}
if err := c.sendRequest("GET", getVersionURL, c.Token(), requestData); err != nil {
logger.Warningf("API version discovery failed: %v", err)
c.apiURLVersions[serviceCatalogURL] = apiURLVersionInfo
return apiURLVersionInfo, nil
}
versions, err := unmarshallVersion(raw.Versions)
if err != nil {
logger.Debugf("API version discovery unmarshallVersion failed: %v", err)
c.apiURLVersions[serviceCatalogURL] = apiURLVersionInfo
return apiURLVersionInfo, nil
}
apiURLVersionInfo.versions = versions
logger.Debugf("discovered API versions: %+v", versions)
// Cache the result.
c.apiURLVersions[serviceCatalogURL] = apiURLVersionInfo
return apiURLVersionInfo, nil
}