Skip to content

Commit 44c3297

Browse files
committed
1 parent e34a44d commit 44c3297

5 files changed

Lines changed: 128 additions & 0 deletions

File tree

acceptance/openstack/identity/v3/projects_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ import (
1111
th "github.com/gophercloud/gophercloud/testhelper"
1212
)
1313

14+
func TestProjectsListAvailable(t *testing.T) {
15+
clients.RequireNonAdmin(t)
16+
17+
client, err := clients.NewIdentityV3Client()
18+
th.AssertNoErr(t, err)
19+
20+
allPages, err := projects.ListAvailable(client).AllPages()
21+
th.AssertNoErr(t, err)
22+
23+
allProjects, err := projects.ExtractProjects(allPages)
24+
th.AssertNoErr(t, err)
25+
26+
for _, project := range allProjects {
27+
tools.PrintResource(t, project)
28+
}
29+
}
30+
1431
func TestProjectsList(t *testing.T) {
1532
clients.RequireAdmin(t)
1633

openstack/identity/v3/projects/requests.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pa
8585
})
8686
}
8787

88+
// ListAvailable enumerates the Projects which are available to a specific user.
89+
func ListAvailable(client *gophercloud.ServiceClient) pagination.Pager {
90+
url := listAvailableURL(client)
91+
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
92+
return ProjectPage{pagination.LinkedPageBase{PageResult: r}}
93+
})
94+
}
95+
8896
// Get retrieves details on a single project, by ID.
8997
func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
9098
resp, err := client.Get(getURL(client, id), &r.Body, nil)

openstack/identity/v3/projects/testing/fixtures.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,41 @@ import (
1010
"github.com/gophercloud/gophercloud/testhelper/client"
1111
)
1212

13+
// ListAvailableOutput provides a single page of available Project results.
14+
const ListAvailableOutput = `
15+
{
16+
"projects": [
17+
{
18+
"description": "my first project",
19+
"domain_id": "11111",
20+
"enabled": true,
21+
"id": "abcde",
22+
"links": {
23+
"self": "http://localhost:5000/identity/v3/projects/abcde"
24+
},
25+
"name": "project 1",
26+
"parent_id": "11111"
27+
},
28+
{
29+
"description": "my second project",
30+
"domain_id": "22222",
31+
"enabled": true,
32+
"id": "bcdef",
33+
"links": {
34+
"self": "http://localhost:5000/identity/v3/projects/bcdef"
35+
},
36+
"name": "project 2",
37+
"parent_id": "22222"
38+
}
39+
],
40+
"links": {
41+
"next": null,
42+
"previous": null,
43+
"self": "http://localhost:5000/identity/v3/users/foobar/projects"
44+
}
45+
}
46+
`
47+
1348
// ListOutput provides a single page of Project results.
1449
const ListOutput = `
1550
{
@@ -103,6 +138,32 @@ const UpdateOutput = `
103138
}
104139
`
105140

141+
// FirstProject is a Project fixture.
142+
var FirstProject = projects.Project{
143+
Description: "my first project",
144+
DomainID: "11111",
145+
Enabled: true,
146+
ID: "abcde",
147+
Name: "project 1",
148+
ParentID: "11111",
149+
Extra: map[string]interface{}{
150+
"links": map[string]interface{}{"self": "http://localhost:5000/identity/v3/projects/abcde"},
151+
},
152+
}
153+
154+
// SecondProject is a Project fixture.
155+
var SecondProject = projects.Project{
156+
Description: "my second project",
157+
DomainID: "22222",
158+
Enabled: true,
159+
ID: "bcdef",
160+
Name: "project 2",
161+
ParentID: "22222",
162+
Extra: map[string]interface{}{
163+
"links": map[string]interface{}{"self": "http://localhost:5000/identity/v3/projects/bcdef"},
164+
},
165+
}
166+
106167
// RedTeam is a Project fixture.
107168
var RedTeam = projects.Project{
108169
IsDomain: false,
@@ -144,9 +205,27 @@ var UpdatedRedTeam = projects.Project{
144205
Extra: map[string]interface{}{"test": "new"},
145206
}
146207

208+
// ExpectedAvailableProjectsSlice is the slice of projects expected to be returned
209+
// from ListAvailableOutput.
210+
var ExpectedAvailableProjectsSlice = []projects.Project{FirstProject, SecondProject}
211+
147212
// ExpectedProjectSlice is the slice of projects expected to be returned from ListOutput.
148213
var ExpectedProjectSlice = []projects.Project{RedTeam, BlueTeam}
149214

215+
// HandleListAvailableProjectsSuccessfully creates an HTTP handler at `/auth/projects`
216+
// on the test handler mux that responds with a list of two tenants.
217+
func HandleListAvailableProjectsSuccessfully(t *testing.T) {
218+
th.Mux.HandleFunc("/auth/projects", func(w http.ResponseWriter, r *http.Request) {
219+
th.TestMethod(t, r, "GET")
220+
th.TestHeader(t, r, "Accept", "application/json")
221+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
222+
223+
w.Header().Set("Content-Type", "application/json")
224+
w.WriteHeader(http.StatusOK)
225+
fmt.Fprintf(w, ListAvailableOutput)
226+
})
227+
}
228+
150229
// HandleListProjectsSuccessfully creates an HTTP handler at `/projects` on the
151230
// test handler mux that responds with a list of two tenants.
152231
func HandleListProjectsSuccessfully(t *testing.T) {

openstack/identity/v3/projects/testing/requests_test.go

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

12+
func TestListAvailableProjects(t *testing.T) {
13+
th.SetupHTTP()
14+
defer th.TeardownHTTP()
15+
HandleListAvailableProjectsSuccessfully(t)
16+
17+
count := 0
18+
err := projects.ListAvailable(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
19+
count++
20+
21+
actual, err := projects.ExtractProjects(page)
22+
th.AssertNoErr(t, err)
23+
24+
th.CheckDeepEquals(t, ExpectedAvailableProjectsSlice, actual)
25+
26+
return true, nil
27+
})
28+
th.AssertNoErr(t, err)
29+
th.CheckEquals(t, count, 1)
30+
}
31+
1232
func TestListProjects(t *testing.T) {
1333
th.SetupHTTP()
1434
defer th.TeardownHTTP()

openstack/identity/v3/projects/urls.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package projects
22

33
import "github.com/gophercloud/gophercloud"
44

5+
func listAvailableURL(client *gophercloud.ServiceClient) string {
6+
return client.ServiceURL("auth", "projects")
7+
}
8+
59
func listURL(client *gophercloud.ServiceClient) string {
610
return client.ServiceURL("projects")
711
}

0 commit comments

Comments
 (0)