Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 1 | package tenants |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
| 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. |
| 11 | ID string `mapstructure:"id"` |
| 12 | |
| 13 | // Name is a friendlier user-facing name for this tenant. |
| 14 | Name string `mapstructure:"name"` |
| 15 | |
| 16 | // Description is a human-readable explanation of this Tenant's purpose. |
| 17 | Description string `mapstructure:"description"` |
| 18 | |
| 19 | // Enabled indicates whether or not a tenant is active. |
| 20 | Enabled bool `mapstructure:"enabled"` |
| 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. |
| 29 | func (page TenantPage) IsEmpty() (bool, error) { |
| 30 | tenants, err := ExtractTenants(page) |
| 31 | if err != nil { |
| 32 | return false, err |
| 33 | } |
| 34 | return len(tenants) == 0, nil |
| 35 | } |
| 36 | |
| 37 | // NextPageURL extracts the "next" link from the tenants_links section of the result. |
| 38 | func (page TenantPage) NextPageURL() (string, error) { |
| 39 | type link struct { |
| 40 | Href string `mapstructure:"href"` |
| 41 | Rel string `mapstructure:"rel"` |
| 42 | } |
| 43 | type resp struct { |
| 44 | Links []link `mapstructure:"tenants_links"` |
| 45 | } |
| 46 | |
| 47 | var r resp |
| 48 | err := mapstructure.Decode(page.Body, &r) |
| 49 | if err != nil { |
| 50 | return "", err |
| 51 | } |
| 52 | |
| 53 | var url string |
| 54 | for _, l := range r.Links { |
| 55 | if l.Rel == "next" { |
| 56 | url = l.Href |
| 57 | } |
| 58 | } |
| 59 | if url == "" { |
| 60 | return "", nil |
| 61 | } |
| 62 | |
| 63 | return url, nil |
| 64 | } |
| 65 | |
| 66 | // ExtractTenants returns a slice of Tenants contained in a single page of results. |
| 67 | func ExtractTenants(page pagination.Page) ([]Tenant, error) { |
| 68 | casted := page.(TenantPage).Body |
| 69 | var response struct { |
| 70 | Tenants []Tenant `mapstructure:"tenants"` |
| 71 | } |
| 72 | |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 73 | err := mapstructure.Decode(casted, &response) |
| 74 | return response.Tenants, err |
| 75 | } |