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