Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 1b2f8cb

Browse files
author
Jamie Hannaford
committed
ensure list use rackspace User structs
1 parent 7274916 commit 1b2f8cb

File tree

6 files changed

+122
-46
lines changed

6 files changed

+122
-46
lines changed

rackspace/db/v1/users/delegate.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,13 @@ package users
33
import (
44
"github.com/rackspace/gophercloud"
55
os "github.com/rackspace/gophercloud/openstack/db/v1/users"
6-
"github.com/rackspace/gophercloud/pagination"
76
)
87

98
// Create will create a new database user for the specified database instance.
109
func Create(client *gophercloud.ServiceClient, instanceID string, opts os.CreateOptsBuilder) os.CreateResult {
1110
return os.Create(client, instanceID, opts)
1211
}
1312

14-
// List will list all available users for a specified database instance.
15-
func List(client *gophercloud.ServiceClient, instanceID string) pagination.Pager {
16-
return os.List(client, instanceID)
17-
}
18-
1913
// Delete will permanently remove a user from a specified database instance.
2014
func Delete(client *gophercloud.ServiceClient, instanceID, userName string) os.DeleteResult {
2115
return os.Delete(client, instanceID, userName)

rackspace/db/v1/users/delegate_test.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
db "github.com/rackspace/gophercloud/openstack/db/v1/databases"
77
os "github.com/rackspace/gophercloud/openstack/db/v1/users"
8-
"github.com/rackspace/gophercloud/pagination"
98
th "github.com/rackspace/gophercloud/testhelper"
109
fake "github.com/rackspace/gophercloud/testhelper/client"
1110
)
@@ -39,45 +38,6 @@ func TestCreate(t *testing.T) {
3938
th.AssertNoErr(t, res.Err)
4039
}
4140

42-
func TestUserList(t *testing.T) {
43-
th.SetupHTTP()
44-
defer th.TeardownHTTP()
45-
os.HandleList(t)
46-
47-
expectedUsers := []os.User{
48-
os.User{
49-
Databases: []db.Database{
50-
db.Database{Name: "databaseA"},
51-
},
52-
Name: "dbuser3",
53-
},
54-
os.User{
55-
Databases: []db.Database{
56-
db.Database{Name: "databaseB"},
57-
db.Database{Name: "databaseC"},
58-
},
59-
Name: "dbuser4",
60-
},
61-
}
62-
63-
pages := 0
64-
err := List(fake.ServiceClient(), instanceID).EachPage(func(page pagination.Page) (bool, error) {
65-
pages++
66-
67-
actual, err := os.ExtractUsers(page)
68-
if err != nil {
69-
return false, err
70-
}
71-
72-
th.CheckDeepEquals(t, expectedUsers, actual)
73-
74-
return true, nil
75-
})
76-
77-
th.AssertNoErr(t, err)
78-
th.AssertEquals(t, 1, pages)
79-
}
80-
8141
func TestDelete(t *testing.T) {
8242
th.SetupHTTP()
8343
defer th.TeardownHTTP()

rackspace/db/v1/users/fixtures.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,34 @@ var getResp = `
4343
}
4444
`
4545

46+
var listResp = `
47+
{
48+
"users": [
49+
{
50+
"name": "dbuser1",
51+
"host": "localhost",
52+
"databases": [
53+
{
54+
"name": "databaseA"
55+
}
56+
]
57+
},
58+
{
59+
"name": "dbuser2",
60+
"host": "localhost",
61+
"databases": [
62+
{
63+
"name": "databaseB"
64+
},
65+
{
66+
"name": "databaseC"
67+
}
68+
]
69+
}
70+
]
71+
}
72+
`
73+
4674
var (
4775
listUserAccessResp = singleDB
4876
grantUserAccessReq = singleDB

rackspace/db/v1/users/requests.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ import (
99
"github.com/rackspace/gophercloud/pagination"
1010
)
1111

12+
// List will list all available users for a specified database instance.
13+
func List(client *gophercloud.ServiceClient, instanceID string) pagination.Pager {
14+
createPageFn := func(r pagination.PageResult) pagination.Page {
15+
return UserPage{pagination.LinkedPageBase{PageResult: r}}
16+
}
17+
18+
return pagination.NewPager(client, baseURL(client, instanceID), createPageFn)
19+
}
20+
1221
/*
1322
ChangePassword changes the password for one or more users. For example, to
1423
change the respective passwords for two users:

rackspace/db/v1/users/requests_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,48 @@ func TestUserAccessList(t *testing.T) {
9494
th.AssertEquals(t, 1, pages)
9595
}
9696

97+
func TestUserList(t *testing.T) {
98+
th.SetupHTTP()
99+
defer th.TeardownHTTP()
100+
101+
fixture.SetupHandler(t, "/instances/"+instanceID+"/users", "GET", "", listResp, 200)
102+
103+
expectedUsers := []User{
104+
User{
105+
Databases: []db.Database{
106+
db.Database{Name: "databaseA"},
107+
},
108+
Name: "dbuser1",
109+
Host: "localhost",
110+
},
111+
User{
112+
Databases: []db.Database{
113+
db.Database{Name: "databaseB"},
114+
db.Database{Name: "databaseC"},
115+
},
116+
Name: "dbuser2",
117+
Host: "localhost",
118+
},
119+
}
120+
121+
pages := 0
122+
err := List(fake.ServiceClient(), instanceID).EachPage(func(page pagination.Page) (bool, error) {
123+
pages++
124+
125+
actual, err := ExtractUsers(page)
126+
if err != nil {
127+
return false, err
128+
}
129+
130+
th.CheckDeepEquals(t, expectedUsers, actual)
131+
132+
return true, nil
133+
})
134+
135+
th.AssertNoErr(t, err)
136+
th.AssertEquals(t, 1, pages)
137+
}
138+
97139
func TestGrantAccess(t *testing.T) {
98140
th.SetupHTTP()
99141
defer th.TeardownHTTP()

rackspace/db/v1/users/results.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,49 @@ func ExtractDBs(page pagination.Page) ([]db.Database, error) {
9595
return response.DBs, err
9696
}
9797

98+
// UserPage represents a single page of a paginated user collection.
99+
type UserPage struct {
100+
pagination.LinkedPageBase
101+
}
102+
103+
// IsEmpty checks to see whether the collection is empty.
104+
func (page UserPage) IsEmpty() (bool, error) {
105+
users, err := ExtractUsers(page)
106+
if err != nil {
107+
return true, err
108+
}
109+
return len(users) == 0, nil
110+
}
111+
112+
// NextPageURL will retrieve the next page URL.
113+
func (page UserPage) NextPageURL() (string, error) {
114+
type resp struct {
115+
Links []gophercloud.Link `mapstructure:"users_links"`
116+
}
117+
118+
var r resp
119+
err := mapstructure.Decode(page.Body, &r)
120+
if err != nil {
121+
return "", err
122+
}
123+
124+
return gophercloud.ExtractNextURL(r.Links)
125+
}
126+
127+
// ExtractUsers will convert a generic pagination struct into a more
128+
// relevant slice of User structs.
129+
func ExtractUsers(page pagination.Page) ([]User, error) {
130+
casted := page.(UserPage).Body
131+
132+
var response struct {
133+
Users []User `mapstructure:"users"`
134+
}
135+
136+
err := mapstructure.Decode(casted, &response)
137+
138+
return response.Users, err
139+
}
140+
98141
// GrantAccessResult represents the result of granting access to a user.
99142
type GrantAccessResult struct {
100143
gophercloud.ErrResult

0 commit comments

Comments
 (0)