Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 1 | package tenants |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 6 | ) |
| 7 | |
| 8 | // Tenant is a grouping of users in the identity service. |
| 9 | type Tenant struct { |
| 10 | // ID is a unique identifier for this tenant. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 11 | ID string `json:"id"` |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 12 | |
| 13 | // Name is a friendlier user-facing name for this tenant. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 14 | Name string `json:"name"` |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 15 | |
| 16 | // Description is a human-readable explanation of this Tenant's purpose. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 17 | Description string `json:"description"` |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 18 | |
| 19 | // Enabled indicates whether or not a tenant is active. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 20 | Enabled bool `json:"enabled"` |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | // TenantPage is a single page of Tenant results. |
| 24 | type TenantPage struct { |
| 25 | pagination.LinkedPageBase |
| 26 | } |
| 27 | |
| 28 | // IsEmpty determines whether or not a page of Tenants contains any results. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 29 | func (r TenantPage) IsEmpty() (bool, error) { |
| 30 | tenants, err := ExtractTenants(r) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 31 | return len(tenants) == 0, err |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // NextPageURL extracts the "next" link from the tenants_links section of the result. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 35 | func (r TenantPage) NextPageURL() (string, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 36 | var s struct { |
| 37 | Links []gophercloud.Link `json:"tenants_links"` |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 38 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 39 | err := r.ExtractInto(&s) |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 40 | if err != nil { |
| 41 | return "", err |
| 42 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 43 | return gophercloud.ExtractNextURL(s.Links) |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // ExtractTenants returns a slice of Tenants contained in a single page of results. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 47 | func ExtractTenants(r pagination.Page) ([]Tenant, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 48 | var s struct { |
| 49 | Tenants []Tenant `json:"tenants"` |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 50 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 51 | err := (r.(TenantPage)).ExtractInto(&s) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 52 | return s.Tenants, err |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 53 | } |