blob: c1220c384bcb81547e1543c43907835d08043e7b [file] [log] [blame]
Ash Wilson1f110512014-10-02 15:43:47 -04001package tenants
2
3import (
4 "github.com/mitchellh/mapstructure"
Jamie Hannaforda253adf2014-10-08 17:14:24 +02005 "github.com/rackspace/gophercloud"
Ash Wilson1f110512014-10-02 15:43:47 -04006 "github.com/rackspace/gophercloud/pagination"
7)
8
9// Tenant is a grouping of users in the identity service.
10type 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.
25type TenantPage struct {
26 pagination.LinkedPageBase
27}
28
29// IsEmpty determines whether or not a page of Tenants contains any results.
30func (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.
39func (page TenantPage) NextPageURL() (string, error) {
Ash Wilson1f110512014-10-02 15:43:47 -040040 type resp struct {
Jamie Hannaforda253adf2014-10-08 17:14:24 +020041 Links []gophercloud.Link `mapstructure:"tenants_links"`
Ash Wilson1f110512014-10-02 15:43:47 -040042 }
43
44 var r resp
45 err := mapstructure.Decode(page.Body, &r)
46 if err != nil {
47 return "", err
48 }
49
Jamie Hannaforda253adf2014-10-08 17:14:24 +020050 return gophercloud.ExtractNextURL(r.Links)
Ash Wilson1f110512014-10-02 15:43:47 -040051}
52
53// ExtractTenants returns a slice of Tenants contained in a single page of results.
54func ExtractTenants(page pagination.Page) ([]Tenant, error) {
55 casted := page.(TenantPage).Body
56 var response struct {
57 Tenants []Tenant `mapstructure:"tenants"`
58 }
59
Ash Wilson1f110512014-10-02 15:43:47 -040060 err := mapstructure.Decode(casted, &response)
61 return response.Tenants, err
62}