Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 1 | package utils |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame^] | 5 | "net/url" |
Ash Wilson | aef9dc5 | 2014-08-29 14:21:26 -0400 | [diff] [blame] | 6 | "strings" |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 7 | |
| 8 | "github.com/racker/perigee" |
| 9 | ) |
| 10 | |
| 11 | // Version is a supported API version, corresponding to a vN package within the appropriate service. |
| 12 | type Version struct { |
| 13 | ID string |
| 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 | |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 23 | // ChooseVersion queries the base endpoint of a API to choose the most recent non-experimental alternative from a service's |
| 24 | // published versions. |
| 25 | // It returns the highest-Priority Version among the alternatives that are provided, as well as its corresponding endpoint. |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame^] | 26 | func ChooseVersion(identityEndpoint string, 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 | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame^] | 46 | // Normalize the identity endpoint that's provided by trimming any path, query or fragment from the URL. |
| 47 | u, err := url.Parse(identityEndpoint) |
| 48 | if err != nil { |
| 49 | return nil, "", err |
| 50 | } |
| 51 | u.Path, u.RawQuery, u.Fragment = "", "", "" |
| 52 | normalized := u.String() |
| 53 | |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 54 | var resp response |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame^] | 55 | _, err = perigee.Request("GET", normalized, perigee.Options{ |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 56 | Results: &resp, |
Ash Wilson | 72f7dfb | 2014-09-02 14:06:00 -0400 | [diff] [blame] | 57 | OkCodes: []int{200, 300}, |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 58 | }) |
| 59 | |
| 60 | if err != nil { |
| 61 | return nil, "", err |
| 62 | } |
| 63 | |
| 64 | byID := make(map[string]*Version) |
| 65 | for _, version := range recognized { |
| 66 | byID[version.ID] = version |
| 67 | } |
| 68 | |
| 69 | var highest *Version |
| 70 | var endpoint string |
| 71 | |
| 72 | for _, value := range resp.Versions.Values { |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame^] | 73 | href := "" |
| 74 | for _, link := range value.Links { |
| 75 | if link.Rel == "self" { |
| 76 | href = link.Href |
| 77 | } |
| 78 | } |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 79 | |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame^] | 80 | if matching, ok := byID[value.ID]; ok { |
| 81 | // Prefer a version that exactly matches the provided endpoint. |
| 82 | if href == identityEndpoint { |
| 83 | if href == "" { |
| 84 | return nil, "", fmt.Errorf("Endpoint missing in version %s response from %s", value.ID, normalized) |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 85 | } |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame^] | 86 | return matching, href, nil |
| 87 | } |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 88 | |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame^] | 89 | // Otherwise, find the highest-priority version with a whitelisted status. |
| 90 | if goodStatus[strings.ToLower(value.Status)] { |
| 91 | if highest == nil || matching.Priority > highest.Priority { |
| 92 | highest = matching |
| 93 | endpoint = href |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
Ash Wilson | a0c4c84 | 2014-09-09 11:30:58 -0400 | [diff] [blame^] | 99 | if highest == nil { |
| 100 | return nil, "", fmt.Errorf("No supported version available from endpoint %s", normalized) |
| 101 | } |
| 102 | if endpoint == "" { |
| 103 | return nil, "", fmt.Errorf("Endpoint missing in version %s response from %s", highest.ID, normalized) |
Ash Wilson | c436020 | 2014-08-29 14:14:24 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return highest, endpoint, nil |
| 107 | } |