Joe Topjian | f61691c | 2016-11-05 12:34:59 -0600 | [diff] [blame] | 1 | package projects |
| 2 | |
| 3 | import ( |
Joe Topjian | 1c236d3 | 2017-01-09 15:33:32 -0700 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
Joe Topjian | f61691c | 2016-11-05 12:34:59 -0600 | [diff] [blame] | 5 | "github.com/gophercloud/gophercloud/pagination" |
| 6 | ) |
| 7 | |
Joe Topjian | 1c236d3 | 2017-01-09 15:33:32 -0700 | [diff] [blame] | 8 | type projectResult struct { |
| 9 | gophercloud.Result |
| 10 | } |
| 11 | |
| 12 | // GetResult temporarily contains the response from the Get call. |
| 13 | type GetResult struct { |
| 14 | projectResult |
| 15 | } |
| 16 | |
Joe Topjian | 8ad602c | 2017-01-11 21:01:47 -0700 | [diff] [blame] | 17 | // CreateResult temporarily contains the reponse from the Create call. |
| 18 | type CreateResult struct { |
| 19 | projectResult |
| 20 | } |
| 21 | |
Joe Topjian | d131fb8 | 2017-01-11 21:41:44 -0700 | [diff] [blame] | 22 | // DeleteResult temporarily contains the response from the Delete call. |
| 23 | type DeleteResult struct { |
| 24 | gophercloud.ErrResult |
| 25 | } |
| 26 | |
Joe Topjian | 8baf47a | 2017-01-11 21:50:24 -0700 | [diff] [blame] | 27 | // UpdateResult temporarily contains the response from the Update call. |
| 28 | type UpdateResult struct { |
| 29 | projectResult |
| 30 | } |
| 31 | |
Joe Topjian | f61691c | 2016-11-05 12:34:59 -0600 | [diff] [blame] | 32 | // Project is a base unit of ownership. |
| 33 | type Project struct { |
| 34 | // IsDomain indicates whether the project is a domain. |
| 35 | IsDomain bool `json:"is_domain"` |
| 36 | |
| 37 | // Description is the description of the project. |
| 38 | Description string `json:"description"` |
| 39 | |
| 40 | // DomainID is the domain ID the project belongs to. |
| 41 | DomainID string `json:"domain_id"` |
| 42 | |
| 43 | // Enabled is whether or not the project is enabled. |
| 44 | Enabled bool `json:"enabled"` |
| 45 | |
| 46 | // ID is the unique ID of the project. |
| 47 | ID string `json:"id"` |
| 48 | |
| 49 | // Name is the name of the project. |
| 50 | Name string `json:"name"` |
| 51 | |
| 52 | // ParentID is the parent_id of the project. |
| 53 | ParentID string `json:"parent_id"` |
| 54 | } |
| 55 | |
| 56 | // ProjectPage is a single page of Project results. |
| 57 | type ProjectPage struct { |
| 58 | pagination.LinkedPageBase |
| 59 | } |
| 60 | |
| 61 | // IsEmpty determines whether or not a page of Projects contains any results. |
| 62 | func (r ProjectPage) IsEmpty() (bool, error) { |
| 63 | projects, err := ExtractProjects(r) |
| 64 | return len(projects) == 0, err |
| 65 | } |
| 66 | |
| 67 | // NextPageURL extracts the "next" link from the links section of the result. |
| 68 | func (r ProjectPage) NextPageURL() (string, error) { |
| 69 | var s struct { |
| 70 | Links struct { |
| 71 | Next string `json:"next"` |
| 72 | Previous string `json:"previous"` |
| 73 | } `json:"links"` |
| 74 | } |
| 75 | err := r.ExtractInto(&s) |
| 76 | if err != nil { |
| 77 | return "", err |
| 78 | } |
| 79 | return s.Links.Next, err |
| 80 | } |
| 81 | |
| 82 | // ExtractProjects returns a slice of Projects contained in a single page of results. |
| 83 | func ExtractProjects(r pagination.Page) ([]Project, error) { |
| 84 | var s struct { |
| 85 | Projects []Project `json:"projects"` |
| 86 | } |
| 87 | err := (r.(ProjectPage)).ExtractInto(&s) |
| 88 | return s.Projects, err |
| 89 | } |
Joe Topjian | 1c236d3 | 2017-01-09 15:33:32 -0700 | [diff] [blame] | 90 | |
| 91 | // Extract interprets any projectResults as a Project. |
| 92 | func (r projectResult) Extract() (*Project, error) { |
| 93 | var s struct { |
| 94 | Project *Project `json:"project"` |
| 95 | } |
| 96 | err := r.ExtractInto(&s) |
| 97 | return s.Project, err |
| 98 | } |