Identity v3 Projects List (#163)
* Identity v3 Projects List
* Rename ToListQuery to ToProjectListQuery
diff --git a/openstack/identity/v3/projects/requests.go b/openstack/identity/v3/projects/requests.go
new file mode 100644
index 0000000..213db9c
--- /dev/null
+++ b/openstack/identity/v3/projects/requests.go
@@ -0,0 +1,52 @@
+package projects
+
+import (
+ "github.com/gophercloud/gophercloud"
+ "github.com/gophercloud/gophercloud/pagination"
+)
+
+// ListOptsBuilder allows extensions to add additional parameters to
+// the List request
+type ListOptsBuilder interface {
+ ToProjectListQuery() (string, error)
+}
+
+// ListOpts allows you to query the List method.
+type ListOpts struct {
+ // DomainID filters the response by a domain ID.
+ DomainID string `q:"domain_id"`
+
+ // Enabled filters the response by enabled projects.
+ Enabled *bool `q:"enabled"`
+
+ // IsDomain filters the response by projects that are domains.
+ // Setting this to true is effectively listing domains.
+ IsDomain *bool `q:"is_domain"`
+
+ // Name filters the response by project name.
+ Name string `q:"name"`
+
+ // ParentID filters the response by projects of a given parent project.
+ ParentID string `q:"parent_id"`
+}
+
+// ToProjectListQuery formats a ListOpts into a query string.
+func (opts ListOpts) ToProjectListQuery() (string, error) {
+ q, err := gophercloud.BuildQueryString(opts)
+ return q.String(), err
+}
+
+// List enumerats the Projects to which the current token has access.
+func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
+ url := listURL(client)
+ if opts != nil {
+ query, err := opts.ToProjectListQuery()
+ if err != nil {
+ return pagination.Pager{Err: err}
+ }
+ url += query
+ }
+ return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
+ return ProjectPage{pagination.LinkedPageBase{PageResult: r}}
+ })
+}
diff --git a/openstack/identity/v3/projects/results.go b/openstack/identity/v3/projects/results.go
new file mode 100644
index 0000000..33c70e1
--- /dev/null
+++ b/openstack/identity/v3/projects/results.go
@@ -0,0 +1,64 @@
+package projects
+
+import (
+ "github.com/gophercloud/gophercloud/pagination"
+)
+
+// Project is a base unit of ownership.
+type Project struct {
+ // IsDomain indicates whether the project is a domain.
+ IsDomain bool `json:"is_domain"`
+
+ // Description is the description of the project.
+ Description string `json:"description"`
+
+ // DomainID is the domain ID the project belongs to.
+ DomainID string `json:"domain_id"`
+
+ // Enabled is whether or not the project is enabled.
+ Enabled bool `json:"enabled"`
+
+ // ID is the unique ID of the project.
+ ID string `json:"id"`
+
+ // Name is the name of the project.
+ Name string `json:"name"`
+
+ // ParentID is the parent_id of the project.
+ ParentID string `json:"parent_id"`
+}
+
+// ProjectPage is a single page of Project results.
+type ProjectPage struct {
+ pagination.LinkedPageBase
+}
+
+// IsEmpty determines whether or not a page of Projects contains any results.
+func (r ProjectPage) IsEmpty() (bool, error) {
+ projects, err := ExtractProjects(r)
+ return len(projects) == 0, err
+}
+
+// NextPageURL extracts the "next" link from the links section of the result.
+func (r ProjectPage) NextPageURL() (string, error) {
+ var s struct {
+ Links struct {
+ Next string `json:"next"`
+ Previous string `json:"previous"`
+ } `json:"links"`
+ }
+ err := r.ExtractInto(&s)
+ if err != nil {
+ return "", err
+ }
+ return s.Links.Next, err
+}
+
+// ExtractProjects returns a slice of Projects contained in a single page of results.
+func ExtractProjects(r pagination.Page) ([]Project, error) {
+ var s struct {
+ Projects []Project `json:"projects"`
+ }
+ err := (r.(ProjectPage)).ExtractInto(&s)
+ return s.Projects, err
+}
diff --git a/openstack/identity/v3/projects/testing/fixtures.go b/openstack/identity/v3/projects/testing/fixtures.go
new file mode 100644
index 0000000..c4d9c03
--- /dev/null
+++ b/openstack/identity/v3/projects/testing/fixtures.go
@@ -0,0 +1,80 @@
+package testing
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+
+ "github.com/gophercloud/gophercloud/openstack/identity/v3/projects"
+ th "github.com/gophercloud/gophercloud/testhelper"
+ "github.com/gophercloud/gophercloud/testhelper/client"
+)
+
+// ListOutput provides a single page of Project results.
+const ListOutput = `
+{
+ "projects": [
+ {
+ "is_domain": false,
+ "description": "The team that is red",
+ "domain_id": "default",
+ "enabled": true,
+ "id": "1234",
+ "name": "Red Team",
+ "parent_id": null
+ },
+ {
+ "is_domain": false,
+ "description": "The team that is blue",
+ "domain_id": "default",
+ "enabled": true,
+ "id": "9876",
+ "name": "Blue Team",
+ "parent_id": null
+ }
+ ],
+ "links": {
+ "next": null,
+ "previous": null
+ }
+}
+`
+
+// RedTeam is a Project fixture.
+var RedTeam = projects.Project{
+ IsDomain: false,
+ Description: "The team that is red",
+ DomainID: "default",
+ Enabled: true,
+ ID: "1234",
+ Name: "Red Team",
+ ParentID: "",
+}
+
+// BlueTeam is a Project fixture.
+var BlueTeam = projects.Project{
+ IsDomain: false,
+ Description: "The team that is blue",
+ DomainID: "default",
+ Enabled: true,
+ ID: "9876",
+ Name: "Blue Team",
+ ParentID: "",
+}
+
+// ExpectedProjectSlice is the slice of projects expected to be returned from ListOutput.
+var ExpectedProjectSlice = []projects.Project{RedTeam, BlueTeam}
+
+// HandleListProjectSuccessfully creates an HTTP handler at `/projects` on the
+// test handler mux that responds with a list of two tenants.
+func HandleListProjectsSuccessfully(t *testing.T) {
+ th.Mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) {
+ th.TestMethod(t, r, "GET")
+ th.TestHeader(t, r, "Accept", "application/json")
+ th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ fmt.Fprintf(w, ListOutput)
+ })
+}
diff --git a/openstack/identity/v3/projects/testing/requests_test.go b/openstack/identity/v3/projects/testing/requests_test.go
new file mode 100644
index 0000000..31730f9
--- /dev/null
+++ b/openstack/identity/v3/projects/testing/requests_test.go
@@ -0,0 +1,30 @@
+package testing
+
+import (
+ "testing"
+
+ "github.com/gophercloud/gophercloud/openstack/identity/v3/projects"
+ "github.com/gophercloud/gophercloud/pagination"
+ th "github.com/gophercloud/gophercloud/testhelper"
+ "github.com/gophercloud/gophercloud/testhelper/client"
+)
+
+func TestListProjects(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+ HandleListProjectsSuccessfully(t)
+
+ count := 0
+ err := projects.List(client.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
+ count++
+
+ actual, err := projects.ExtractProjects(page)
+ th.AssertNoErr(t, err)
+
+ th.CheckDeepEquals(t, ExpectedProjectSlice, actual)
+
+ return true, nil
+ })
+ th.AssertNoErr(t, err)
+ th.CheckEquals(t, count, 1)
+}
diff --git a/openstack/identity/v3/projects/urls.go b/openstack/identity/v3/projects/urls.go
new file mode 100644
index 0000000..e80a395
--- /dev/null
+++ b/openstack/identity/v3/projects/urls.go
@@ -0,0 +1,7 @@
+package projects
+
+import "github.com/gophercloud/gophercloud"
+
+func listURL(client *gophercloud.ServiceClient) string {
+ return client.ServiceURL("projects")
+}