Vendor all dependencies w/ Godep

* Remove `make updatedeps` from Travis build. We'll follow up with more
   specific plans around dependency updating in subsequent PRs.
 * Update all `make` targets to set `GO15VENDOREXPERIMENT=1` and to
   filter out `/vendor/` from `./...` where appropriate.
 * Temporarily remove `vet` from the `make test` target until we can
   figure out how to get it to not vet `vendor/`. (Initial
   experimentation failed to yield the proper incantation.)

Everything is pinned to current master, with the exception of:

 * Azure/azure-sdk-for-go which is pinned before the breaking change today
 * aws/aws-sdk-go which is pinned to the most recent tag

The documentation still needs to be updated, which we can do in a follow
up PR. The goal here is to unblock release.
This commit is contained in:
Paul Hinze
2016-01-29 13:53:56 -06:00
parent 4c3e134088
commit 6fe2703665
4144 changed files with 974543 additions and 22 deletions

View File

@@ -0,0 +1,114 @@
package utils
import (
"fmt"
"strings"
"github.com/rackspace/gophercloud"
)
// Version is a supported API version, corresponding to a vN package within the appropriate service.
type Version struct {
ID string
Suffix string
Priority int
}
var goodStatus = map[string]bool{
"current": true,
"supported": true,
"stable": true,
}
// ChooseVersion queries the base endpoint of an API to choose the most recent non-experimental alternative from a service's
// published versions.
// It returns the highest-Priority Version among the alternatives that are provided, as well as its corresponding endpoint.
func ChooseVersion(client *gophercloud.ProviderClient, recognized []*Version) (*Version, string, error) {
type linkResp struct {
Href string `json:"href"`
Rel string `json:"rel"`
}
type valueResp struct {
ID string `json:"id"`
Status string `json:"status"`
Links []linkResp `json:"links"`
}
type versionsResp struct {
Values []valueResp `json:"values"`
}
type response struct {
Versions versionsResp `json:"versions"`
}
normalize := func(endpoint string) string {
if !strings.HasSuffix(endpoint, "/") {
return endpoint + "/"
}
return endpoint
}
identityEndpoint := normalize(client.IdentityEndpoint)
// If a full endpoint is specified, check version suffixes for a match first.
for _, v := range recognized {
if strings.HasSuffix(identityEndpoint, v.Suffix) {
return v, identityEndpoint, nil
}
}
var resp response
_, err := client.Request("GET", client.IdentityBase, gophercloud.RequestOpts{
JSONResponse: &resp,
OkCodes: []int{200, 300},
})
if err != nil {
return nil, "", err
}
byID := make(map[string]*Version)
for _, version := range recognized {
byID[version.ID] = version
}
var highest *Version
var endpoint string
for _, value := range resp.Versions.Values {
href := ""
for _, link := range value.Links {
if link.Rel == "self" {
href = normalize(link.Href)
}
}
if matching, ok := byID[value.ID]; ok {
// Prefer a version that exactly matches the provided endpoint.
if href == identityEndpoint {
if href == "" {
return nil, "", fmt.Errorf("Endpoint missing in version %s response from %s", value.ID, client.IdentityBase)
}
return matching, href, nil
}
// Otherwise, find the highest-priority version with a whitelisted status.
if goodStatus[strings.ToLower(value.Status)] {
if highest == nil || matching.Priority > highest.Priority {
highest = matching
endpoint = href
}
}
}
}
if highest == nil {
return nil, "", fmt.Errorf("No supported version available from endpoint %s", client.IdentityBase)
}
if endpoint == "" {
return nil, "", fmt.Errorf("Endpoint missing in version %s response from %s", highest.ID, client.IdentityBase)
}
return highest, endpoint, nil
}

View File

@@ -0,0 +1,118 @@
package utils
import (
"fmt"
"net/http"
"testing"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/testhelper"
)
func setupVersionHandler() {
testhelper.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `
{
"versions": {
"values": [
{
"status": "stable",
"id": "v3.0",
"links": [
{ "href": "%s/v3.0", "rel": "self" }
]
},
{
"status": "stable",
"id": "v2.0",
"links": [
{ "href": "%s/v2.0", "rel": "self" }
]
}
]
}
}
`, testhelper.Server.URL, testhelper.Server.URL)
})
}
func TestChooseVersion(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
setupVersionHandler()
v2 := &Version{ID: "v2.0", Priority: 2, Suffix: "blarg"}
v3 := &Version{ID: "v3.0", Priority: 3, Suffix: "hargl"}
c := &gophercloud.ProviderClient{
IdentityBase: testhelper.Endpoint(),
IdentityEndpoint: "",
}
v, endpoint, err := ChooseVersion(c, []*Version{v2, v3})
if err != nil {
t.Fatalf("Unexpected error from ChooseVersion: %v", err)
}
if v != v3 {
t.Errorf("Expected %#v to win, but %#v did instead", v3, v)
}
expected := testhelper.Endpoint() + "v3.0/"
if endpoint != expected {
t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint)
}
}
func TestChooseVersionOpinionatedLink(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
setupVersionHandler()
v2 := &Version{ID: "v2.0", Priority: 2, Suffix: "nope"}
v3 := &Version{ID: "v3.0", Priority: 3, Suffix: "northis"}
c := &gophercloud.ProviderClient{
IdentityBase: testhelper.Endpoint(),
IdentityEndpoint: testhelper.Endpoint() + "v2.0/",
}
v, endpoint, err := ChooseVersion(c, []*Version{v2, v3})
if err != nil {
t.Fatalf("Unexpected error from ChooseVersion: %v", err)
}
if v != v2 {
t.Errorf("Expected %#v to win, but %#v did instead", v2, v)
}
expected := testhelper.Endpoint() + "v2.0/"
if endpoint != expected {
t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint)
}
}
func TestChooseVersionFromSuffix(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
v2 := &Version{ID: "v2.0", Priority: 2, Suffix: "/v2.0/"}
v3 := &Version{ID: "v3.0", Priority: 3, Suffix: "/v3.0/"}
c := &gophercloud.ProviderClient{
IdentityBase: testhelper.Endpoint(),
IdentityEndpoint: testhelper.Endpoint() + "v2.0/",
}
v, endpoint, err := ChooseVersion(c, []*Version{v2, v3})
if err != nil {
t.Fatalf("Unexpected error from ChooseVersion: %v", err)
}
if v != v2 {
t.Errorf("Expected %#v to win, but %#v did instead", v2, v)
}
expected := testhelper.Endpoint() + "v2.0/"
if endpoint != expected {
t.Errorf("Expected endpoint [%s], but was [%s] instead", expected, endpoint)
}
}