Joe Topjian | f61691c | 2016-11-05 12:34:59 -0600 | [diff] [blame] | 1 | package projects |
| 2 | |
| 3 | import ( |
| 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
| 6 | ) |
| 7 | |
| 8 | // ListOptsBuilder allows extensions to add additional parameters to |
| 9 | // the List request |
| 10 | type ListOptsBuilder interface { |
| 11 | ToProjectListQuery() (string, error) |
| 12 | } |
| 13 | |
| 14 | // ListOpts allows you to query the List method. |
| 15 | type ListOpts struct { |
| 16 | // DomainID filters the response by a domain ID. |
| 17 | DomainID string `q:"domain_id"` |
| 18 | |
| 19 | // Enabled filters the response by enabled projects. |
| 20 | Enabled *bool `q:"enabled"` |
| 21 | |
| 22 | // IsDomain filters the response by projects that are domains. |
| 23 | // Setting this to true is effectively listing domains. |
| 24 | IsDomain *bool `q:"is_domain"` |
| 25 | |
| 26 | // Name filters the response by project name. |
| 27 | Name string `q:"name"` |
| 28 | |
| 29 | // ParentID filters the response by projects of a given parent project. |
| 30 | ParentID string `q:"parent_id"` |
| 31 | } |
| 32 | |
| 33 | // ToProjectListQuery formats a ListOpts into a query string. |
| 34 | func (opts ListOpts) ToProjectListQuery() (string, error) { |
| 35 | q, err := gophercloud.BuildQueryString(opts) |
| 36 | return q.String(), err |
| 37 | } |
| 38 | |
| 39 | // List enumerats the Projects to which the current token has access. |
| 40 | func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 41 | url := listURL(client) |
| 42 | if opts != nil { |
| 43 | query, err := opts.ToProjectListQuery() |
| 44 | if err != nil { |
| 45 | return pagination.Pager{Err: err} |
| 46 | } |
| 47 | url += query |
| 48 | } |
| 49 | return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
| 50 | return ProjectPage{pagination.LinkedPageBase{PageResult: r}} |
| 51 | }) |
| 52 | } |
Joe Topjian | 1c236d3 | 2017-01-09 15:33:32 -0700 | [diff] [blame^] | 53 | |
| 54 | // GetOptsBuilder allows extensions to add additional parameters to |
| 55 | // the Get request. |
| 56 | type GetOptsBuilder interface { |
| 57 | ToProjectGetQuery() (string, error) |
| 58 | } |
| 59 | |
| 60 | // GetOpts allows you to modify the details included in the Get request. |
| 61 | type GetOpts struct{} |
| 62 | |
| 63 | // ToProjectGetQuery formats a GetOpts into a query string. |
| 64 | func (opts GetOpts) ToProjectGetQuery() (string, error) { |
| 65 | q, err := gophercloud.BuildQueryString(opts) |
| 66 | return q.String(), err |
| 67 | } |
| 68 | |
| 69 | // Get retrieves details on a single project, by ID. |
| 70 | func Get(client *gophercloud.ServiceClient, id string, opts GetOptsBuilder) (r GetResult) { |
| 71 | url := getURL(client, id) |
| 72 | if opts != nil { |
| 73 | query, err := opts.ToProjectGetQuery() |
| 74 | if err != nil { |
| 75 | r.Err = err |
| 76 | return |
| 77 | } |
| 78 | url += query |
| 79 | } |
| 80 | |
| 81 | _, r.Err = client.Get(url, &r.Body, &gophercloud.RequestOpts{ |
| 82 | OkCodes: []int{200}, |
| 83 | }) |
| 84 | return |
| 85 | } |