Exercise the endpoint discovery code a little.
diff --git a/acceptance/openstack/client_test.go b/acceptance/openstack/client_test.go
index 578469d..053e2ac 100644
--- a/acceptance/openstack/client_test.go
+++ b/acceptance/openstack/client_test.go
@@ -3,6 +3,7 @@
package openstack
import (
+ "os"
"testing"
"github.com/rackspace/gophercloud/openstack"
@@ -17,7 +18,7 @@
}
// Trim out unused fields.
- ao.TenantID, ao.TenantName = "", ""
+ ao.TenantID, ao.TenantName, ao.Username = "", "", ""
client, err := openstack.AuthenticatedClient(ao)
if err != nil {
@@ -29,4 +30,12 @@
}
t.Logf("Client successfully acquired a token: %v", client.TokenID)
+
+ // Find the storage service in the service catalog.
+ storage, err := openstack.NewStorageV1(client, os.Getenv("OS_REGION_NAME"))
+ if err != nil {
+ t.Errorf("Unable to locate a storage service: %v", err)
+ } else {
+ t.Logf("Located a storage service at endpoint: [%s]", storage.Endpoint)
+ }
}
diff --git a/endpoint_search.go b/endpoint_search.go
index eea89e8..7611f19 100644
--- a/endpoint_search.go
+++ b/endpoint_search.go
@@ -3,6 +3,9 @@
import "errors"
var (
+ // ErrServiceNotFound is returned when no service matches the EndpointOpts.
+ ErrServiceNotFound = errors.New("No suitable service could be found in the service catalog.")
+
// ErrEndpointNotFound is returned when no available endpoints match the provided EndpointOpts.
ErrEndpointNotFound = errors.New("No suitable endpoint could be found in the service catalog.")
)
diff --git a/openstack/client.go b/openstack/client.go
index b058823..207ba13 100644
--- a/openstack/client.go
+++ b/openstack/client.go
@@ -184,19 +184,19 @@
}
// Discover the service we're interested in.
- computeResults, err := services3.List(v3Client, services3.ListOpts{ServiceType: opts.Type})
+ serviceResults, err := services3.List(v3Client, services3.ListOpts{ServiceType: opts.Type})
if err != nil {
return "", err
}
- serviceResults, err := gophercloud.AllPages(computeResults)
+ allServiceResults, err := gophercloud.AllPages(serviceResults)
if err != nil {
return "", err
}
- allServices := services3.AsServices(serviceResults)
+ allServices := services3.AsServices(allServiceResults)
if opts.Name != "" {
- filtered := make([]services3.Service, 1)
+ filtered := make([]services3.Service, 0, 1)
for _, service := range allServices {
if service.Name == opts.Name {
filtered = append(filtered, service)
@@ -206,7 +206,7 @@
}
if len(allServices) == 0 {
- return "", gophercloud.ErrEndpointNotFound
+ return "", gophercloud.ErrServiceNotFound
}
if len(allServices) > 1 {
return "", fmt.Errorf("Discovered %d matching services: %#v", len(allServices), allServices)
@@ -222,18 +222,16 @@
if err != nil {
return "", err
}
-
allEndpoints, err := gophercloud.AllPages(endpointResults)
if err != nil {
return "", err
}
-
endpoints := endpoints3.AsEndpoints(allEndpoints)
if opts.Name != "" {
- filtered := make([]endpoints3.Endpoint, 1)
+ filtered := make([]endpoints3.Endpoint, 0, 1)
for _, endpoint := range endpoints {
- if endpoint.Region == opts.Region {
+ if opts.Region == "" || endpoint.Region == opts.Region {
filtered = append(filtered, endpoint)
}
}
@@ -271,3 +269,12 @@
Endpoint: v3Endpoint,
}
}
+
+// NewStorageV1 creates a ServiceClient that may be used with the v1 object storage package.
+func NewStorageV1(client *gophercloud.ProviderClient, region string) (*gophercloud.ServiceClient, error) {
+ url, err := client.EndpointLocator(gophercloud.EndpointOpts{Type: "object-store", Name: "swift"})
+ if err != nil {
+ return nil, err
+ }
+ return &gophercloud.ServiceClient{Provider: client, Endpoint: url}, nil
+}