/
apiversion.go
234 lines (215 loc) · 6.63 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
package client
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"path"
"strconv"
"strings"
gooseerrors "gopkg.in/goose.v1/errors"
goosehttp "gopkg.in/goose.v1/http"
"gopkg.in/goose.v1/logging"
)
type apiVersion struct {
major int
minor int
}
type apiVersionInfo struct {
Version apiVersion `json:"id"`
Links []apiVersionLink `json:"links"`
Status string `json:"status"`
}
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 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
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) {
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 != "/" {
// The rackspace object-store endpoint triggers the version removal here,
// so keep the original parts for object-store and container endpoints.
// e.g. https://storage101.dfw1.clouddrive.com/v1/MossoCloudFS_1019383
origPathParts = strings.Split(strings.Trim(url.Path, "/"), "/")
pathParts = origPathParts
if _, err := parseVersion(pathParts[0]); err == nil {
pathParts = pathParts[1:]
}
url.Path = "/"
}
baseURL := 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"]) {
objectStoreLink := apiVersionLink{Href: baseURL, 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",
},
}
return &apiURLVersion{*url, strings.Join(origPathParts, "/"), objectStoreApiVersionInfo}, nil
}
// Make sure we haven't already received the version info.
c.apiVersionMu.Lock()
defer c.apiVersionMu.Unlock()
if apiInfo, ok := c.apiURLVersions[baseURL]; ok {
return apiInfo, nil
}
logger := logging.FromCompat(c.logger)
logger.Debugf("performing API version discovery for %q", baseURL)
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", baseURL, c.Token(), requestData); err != nil {
if gooseerrors.IsNotFound(err) {
// Some OpenStack clouds do not support the
// version endpoint, and will return a status
// code of 404. We check for 404 and return an
// empty set of versions.
logger.Warningf("API version discovery failed: %v", err)
c.apiURLVersions[baseURL] = apiURLVersionInfo
return apiURLVersionInfo, nil
}
return nil, err
}
versions, err := unmarshallVersion(raw.Versions)
if err != nil {
return nil, err
}
apiURLVersionInfo.versions = versions
logger.Debugf("discovered API versions: %+v", versions)
// Cache the result.
c.apiURLVersions[baseURL] = apiURLVersionInfo
return apiURLVersionInfo, nil
}