blob: c1220c384bcb81547e1543c43907835d08043e7b [file] [log] [blame]
Jamie Hannaford8c072a32014-10-16 14:33:32 +02001package tenants
2
3import (
4 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud"
6 "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) {
40 type resp struct {
41 Links []gophercloud.Link `mapstructure:"tenants_links"`
42 }
43
44 var r resp
45 err := mapstructure.Decode(page.Body, &r)
46 if err != nil {
47 return "", err
48 }
49
50 return gophercloud.ExtractNextURL(r.Links)
51}
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
60 err := mapstructure.Decode(casted, &response)
61 return response.Tenants, err
62}