Joe Topjian | f61691c | 2016-11-05 12:34:59 -0600 | [diff] [blame] | 1 | package projects |
| 2 | |
| 3 | import ( |
| 4 | "github.com/gophercloud/gophercloud/pagination" |
| 5 | ) |
| 6 | |
| 7 | // Project is a base unit of ownership. |
| 8 | type Project struct { |
| 9 | // IsDomain indicates whether the project is a domain. |
| 10 | IsDomain bool `json:"is_domain"` |
| 11 | |
| 12 | // Description is the description of the project. |
| 13 | Description string `json:"description"` |
| 14 | |
| 15 | // DomainID is the domain ID the project belongs to. |
| 16 | DomainID string `json:"domain_id"` |
| 17 | |
| 18 | // Enabled is whether or not the project is enabled. |
| 19 | Enabled bool `json:"enabled"` |
| 20 | |
| 21 | // ID is the unique ID of the project. |
| 22 | ID string `json:"id"` |
| 23 | |
| 24 | // Name is the name of the project. |
| 25 | Name string `json:"name"` |
| 26 | |
| 27 | // ParentID is the parent_id of the project. |
| 28 | ParentID string `json:"parent_id"` |
| 29 | } |
| 30 | |
| 31 | // ProjectPage is a single page of Project results. |
| 32 | type ProjectPage struct { |
| 33 | pagination.LinkedPageBase |
| 34 | } |
| 35 | |
| 36 | // IsEmpty determines whether or not a page of Projects contains any results. |
| 37 | func (r ProjectPage) IsEmpty() (bool, error) { |
| 38 | projects, err := ExtractProjects(r) |
| 39 | return len(projects) == 0, err |
| 40 | } |
| 41 | |
| 42 | // NextPageURL extracts the "next" link from the links section of the result. |
| 43 | func (r ProjectPage) NextPageURL() (string, error) { |
| 44 | var s struct { |
| 45 | Links struct { |
| 46 | Next string `json:"next"` |
| 47 | Previous string `json:"previous"` |
| 48 | } `json:"links"` |
| 49 | } |
| 50 | err := r.ExtractInto(&s) |
| 51 | if err != nil { |
| 52 | return "", err |
| 53 | } |
| 54 | return s.Links.Next, err |
| 55 | } |
| 56 | |
| 57 | // ExtractProjects returns a slice of Projects contained in a single page of results. |
| 58 | func ExtractProjects(r pagination.Page) ([]Project, error) { |
| 59 | var s struct { |
| 60 | Projects []Project `json:"projects"` |
| 61 | } |
| 62 | err := (r.(ProjectPage)).ExtractInto(&s) |
| 63 | return s.Projects, err |
| 64 | } |