ensure list use rackspace User structs
diff --git a/rackspace/db/v1/users/delegate.go b/rackspace/db/v1/users/delegate.go
index 0bb4c8c..8298c46 100644
--- a/rackspace/db/v1/users/delegate.go
+++ b/rackspace/db/v1/users/delegate.go
@@ -3,7 +3,6 @@
import (
"github.com/rackspace/gophercloud"
os "github.com/rackspace/gophercloud/openstack/db/v1/users"
- "github.com/rackspace/gophercloud/pagination"
)
// Create will create a new database user for the specified database instance.
@@ -11,11 +10,6 @@
return os.Create(client, instanceID, opts)
}
-// List will list all available users for a specified database instance.
-func List(client *gophercloud.ServiceClient, instanceID string) pagination.Pager {
- return os.List(client, instanceID)
-}
-
// Delete will permanently remove a user from a specified database instance.
func Delete(client *gophercloud.ServiceClient, instanceID, userName string) os.DeleteResult {
return os.Delete(client, instanceID, userName)
diff --git a/rackspace/db/v1/users/delegate_test.go b/rackspace/db/v1/users/delegate_test.go
index c818fe1..7a1b773 100644
--- a/rackspace/db/v1/users/delegate_test.go
+++ b/rackspace/db/v1/users/delegate_test.go
@@ -5,7 +5,6 @@
db "github.com/rackspace/gophercloud/openstack/db/v1/databases"
os "github.com/rackspace/gophercloud/openstack/db/v1/users"
- "github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
fake "github.com/rackspace/gophercloud/testhelper/client"
)
@@ -39,45 +38,6 @@
th.AssertNoErr(t, res.Err)
}
-func TestUserList(t *testing.T) {
- th.SetupHTTP()
- defer th.TeardownHTTP()
- os.HandleList(t)
-
- expectedUsers := []os.User{
- os.User{
- Databases: []db.Database{
- db.Database{Name: "databaseA"},
- },
- Name: "dbuser3",
- },
- os.User{
- Databases: []db.Database{
- db.Database{Name: "databaseB"},
- db.Database{Name: "databaseC"},
- },
- Name: "dbuser4",
- },
- }
-
- pages := 0
- err := List(fake.ServiceClient(), instanceID).EachPage(func(page pagination.Page) (bool, error) {
- pages++
-
- actual, err := os.ExtractUsers(page)
- if err != nil {
- return false, err
- }
-
- th.CheckDeepEquals(t, expectedUsers, actual)
-
- return true, nil
- })
-
- th.AssertNoErr(t, err)
- th.AssertEquals(t, 1, pages)
-}
-
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
diff --git a/rackspace/db/v1/users/fixtures.go b/rackspace/db/v1/users/fixtures.go
index 5388123..5314e85 100644
--- a/rackspace/db/v1/users/fixtures.go
+++ b/rackspace/db/v1/users/fixtures.go
@@ -43,6 +43,34 @@
}
`
+var listResp = `
+{
+"users": [
+ {
+ "name": "dbuser1",
+ "host": "localhost",
+ "databases": [
+ {
+ "name": "databaseA"
+ }
+ ]
+ },
+ {
+ "name": "dbuser2",
+ "host": "localhost",
+ "databases": [
+ {
+ "name": "databaseB"
+ },
+ {
+ "name": "databaseC"
+ }
+ ]
+ }
+]
+}
+`
+
var (
listUserAccessResp = singleDB
grantUserAccessReq = singleDB
diff --git a/rackspace/db/v1/users/requests.go b/rackspace/db/v1/users/requests.go
index 5667452..aab7e41 100644
--- a/rackspace/db/v1/users/requests.go
+++ b/rackspace/db/v1/users/requests.go
@@ -9,6 +9,15 @@
"github.com/rackspace/gophercloud/pagination"
)
+// List will list all available users for a specified database instance.
+func List(client *gophercloud.ServiceClient, instanceID string) pagination.Pager {
+ createPageFn := func(r pagination.PageResult) pagination.Page {
+ return UserPage{pagination.LinkedPageBase{PageResult: r}}
+ }
+
+ return pagination.NewPager(client, baseURL(client, instanceID), createPageFn)
+}
+
/*
ChangePassword changes the password for one or more users. For example, to
change the respective passwords for two users:
diff --git a/rackspace/db/v1/users/requests_test.go b/rackspace/db/v1/users/requests_test.go
index 655d229..2f2dca7 100644
--- a/rackspace/db/v1/users/requests_test.go
+++ b/rackspace/db/v1/users/requests_test.go
@@ -94,6 +94,48 @@
th.AssertEquals(t, 1, pages)
}
+func TestUserList(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ fixture.SetupHandler(t, "/instances/"+instanceID+"/users", "GET", "", listResp, 200)
+
+ expectedUsers := []User{
+ User{
+ Databases: []db.Database{
+ db.Database{Name: "databaseA"},
+ },
+ Name: "dbuser1",
+ Host: "localhost",
+ },
+ User{
+ Databases: []db.Database{
+ db.Database{Name: "databaseB"},
+ db.Database{Name: "databaseC"},
+ },
+ Name: "dbuser2",
+ Host: "localhost",
+ },
+ }
+
+ pages := 0
+ err := List(fake.ServiceClient(), instanceID).EachPage(func(page pagination.Page) (bool, error) {
+ pages++
+
+ actual, err := ExtractUsers(page)
+ if err != nil {
+ return false, err
+ }
+
+ th.CheckDeepEquals(t, expectedUsers, actual)
+
+ return true, nil
+ })
+
+ th.AssertNoErr(t, err)
+ th.AssertEquals(t, 1, pages)
+}
+
func TestGrantAccess(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
diff --git a/rackspace/db/v1/users/results.go b/rackspace/db/v1/users/results.go
index 3d4acb4..85b3a7a 100644
--- a/rackspace/db/v1/users/results.go
+++ b/rackspace/db/v1/users/results.go
@@ -95,6 +95,49 @@
return response.DBs, err
}
+// UserPage represents a single page of a paginated user collection.
+type UserPage struct {
+ pagination.LinkedPageBase
+}
+
+// IsEmpty checks to see whether the collection is empty.
+func (page UserPage) IsEmpty() (bool, error) {
+ users, err := ExtractUsers(page)
+ if err != nil {
+ return true, err
+ }
+ return len(users) == 0, nil
+}
+
+// NextPageURL will retrieve the next page URL.
+func (page UserPage) NextPageURL() (string, error) {
+ type resp struct {
+ Links []gophercloud.Link `mapstructure:"users_links"`
+ }
+
+ var r resp
+ err := mapstructure.Decode(page.Body, &r)
+ if err != nil {
+ return "", err
+ }
+
+ return gophercloud.ExtractNextURL(r.Links)
+}
+
+// ExtractUsers will convert a generic pagination struct into a more
+// relevant slice of User structs.
+func ExtractUsers(page pagination.Page) ([]User, error) {
+ casted := page.(UserPage).Body
+
+ var response struct {
+ Users []User `mapstructure:"users"`
+ }
+
+ err := mapstructure.Decode(casted, &response)
+
+ return response.Users, err
+}
+
// GrantAccessResult represents the result of granting access to a user.
type GrantAccessResult struct {
gophercloud.ErrResult