Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 1 | package utils |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
Ash Wilson | aef9dc5 | 2014-08-29 14:21:26 -0400 | [diff] [blame] | 5 | "strings" |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 6 | |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 8 | ) |
| 9 | |
| 10 | // Version is a supported API version, corresponding to a vN package within the appropriate service. |
| 11 | type Version struct { |
| 12 | ID string |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 13 | Suffix string |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 14 | Priority int |
| 15 | } |
| 16 | |
Ash Wilson | aef9dc5 | 2014-08-29 14:21:26 -0400 | [diff] [blame] | 17 | var goodStatus = map[string]bool{ |
| 18 | "current": true, |
| 19 | "supported": true, |
| 20 | "stable": true, |
| 21 | } |
| 22 | |
Alex Gaynor | a6d5f9f | 2014-10-27 10:52:32 -0700 | [diff] [blame] | 23 | // ChooseVersion queries the base endpoint of an API to choose the most recent non-experimental alternative from a service's |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 24 | // published versions. |
| 25 | // It returns the highest-Priority Version among the alternatives that are provided, as well as its corresponding endpoint. |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 26 | func ChooseVersion(client *gophercloud.ProviderClient, recognized []*Version) (*Version, string, error) { |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 27 | type linkResp struct { |
| 28 | Href string `json:"href"` |
| 29 | Rel string `json:"rel"` |
| 30 | } |
| 31 | |
| 32 | type valueResp struct { |
| 33 | ID string `json:"id"` |
| 34 | Status string `json:"status"` |
| 35 | Links []linkResp `json:"links"` |
| 36 | } |
| 37 | |
| 38 | type versionsResp struct { |
| 39 | Values []valueResp `json:"values"` |
| 40 | } |
| 41 | |
| 42 | type response struct { |
| 43 | Versions versionsResp `json:"versions"` |
| 44 | } |
| 45 | |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 46 | normalize := func(endpoint string) string { |
| 47 | if !strings.HasSuffix(endpoint, "/") { |
| 48 | return endpoint + "/" |
| 49 | } |
| 50 | return endpoint |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 51 | } |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 52 | identityEndpoint := normalize(client.IdentityEndpoint) |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 53 | |
| 54 | // If a full endpoint is specified, check version suffixes for a match first. |
| 55 | for _, v := range recognized { |
| 56 | if strings.HasSuffix(identityEndpoint, v.Suffix) { |
| 57 | return v, identityEndpoint, nil |
| 58 | } |
| 59 | } |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 60 | |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 61 | var resp response |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 62 | _, err := client.Request("GET", client.IdentityBase, gophercloud.RequestOpts{ |
Ash Wilson | 4bf41a3 | 2015-02-12 15:52:44 -0500 | [diff] [blame] | 63 | JSONResponse: &resp, |
| 64 | OkCodes: []int{200, 300}, |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 65 | }) |
| 66 | |
| 67 | if err != nil { |
| 68 | return nil, "", err |
| 69 | } |
| 70 | |
| 71 | byID := make(map[string]*Version) |
| 72 | for _, version := range recognized { |
| 73 | byID[version.ID] = version |
| 74 | } |
| 75 | |
| 76 | var highest *Version |
| 77 | var endpoint string |
| 78 | |
| 79 | for _, value := range resp.Versions.Values { |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 80 | href := "" |
| 81 | for _, link := range value.Links { |
| 82 | if link.Rel == "self" { |
Ash Wilson | e7da01c | 2014-09-09 12:31:06 -0400 | [diff] [blame] | 83 | href = normalize(link.Href) |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 84 | } |
| 85 | } |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 86 | |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 87 | if matching, ok := byID[value.ID]; ok { |
| 88 | // Prefer a version that exactly matches the provided endpoint. |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 89 | if href == identityEndpoint { |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 90 | if href == "" { |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 91 | return nil, "", fmt.Errorf("Endpoint missing in version %s response from %s", value.ID, client.IdentityBase) |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 92 | } |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 93 | return matching, href, nil |
| 94 | } |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 95 | |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 96 | // Otherwise, find the highest-priority version with a whitelisted status. |
| 97 | if goodStatus[strings.ToLower(value.Status)] { |
| 98 | if highest == nil || matching.Priority > highest.Priority { |
| 99 | highest = matching |
| 100 | endpoint = href |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 106 | if highest == nil { |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 107 | return nil, "", fmt.Errorf("No supported version available from endpoint %s", client.IdentityBase) |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame] | 108 | } |
| 109 | if endpoint == "" { |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 110 | return nil, "", fmt.Errorf("Endpoint missing in version %s response from %s", highest.ID, client.IdentityBase) |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return highest, endpoint, nil |
| 114 | } |