github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v2/tenants/results.go (about)

     1  package tenants
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/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 `json:"id"`
    12  
    13  	// Name is a friendlier user-facing name for this tenant.
    14  	Name string `json:"name"`
    15  
    16  	// Description is a human-readable explanation of this Tenant's purpose.
    17  	Description string `json:"description"`
    18  
    19  	// Enabled indicates whether or not a tenant is active.
    20  	Enabled bool `json:"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 (r TenantPage) IsEmpty() (bool, error) {
    30  	if r.StatusCode == 204 {
    31  		return true, nil
    32  	}
    33  
    34  	tenants, err := ExtractTenants(r)
    35  	return len(tenants) == 0, err
    36  }
    37  
    38  // NextPageURL extracts the "next" link from the tenants_links section of the result.
    39  func (r TenantPage) NextPageURL() (string, error) {
    40  	var s struct {
    41  		Links []gophercloud.Link `json:"tenants_links"`
    42  	}
    43  	err := r.ExtractInto(&s)
    44  	if err != nil {
    45  		return "", err
    46  	}
    47  	return gophercloud.ExtractNextURL(s.Links)
    48  }
    49  
    50  // ExtractTenants returns a slice of Tenants contained in a single page of
    51  // results.
    52  func ExtractTenants(r pagination.Page) ([]Tenant, error) {
    53  	var s struct {
    54  		Tenants []Tenant `json:"tenants"`
    55  	}
    56  	err := (r.(TenantPage)).ExtractInto(&s)
    57  	return s.Tenants, err
    58  }
    59  
    60  type tenantResult struct {
    61  	gophercloud.Result
    62  }
    63  
    64  // Extract interprets any tenantResults as a Tenant.
    65  func (r tenantResult) Extract() (*Tenant, error) {
    66  	var s struct {
    67  		Tenant *Tenant `json:"tenant"`
    68  	}
    69  	err := r.ExtractInto(&s)
    70  	return s.Tenant, err
    71  }
    72  
    73  // GetResult is the response from a Get request. Call its Extract method to
    74  // interpret it as a Tenant.
    75  type GetResult struct {
    76  	tenantResult
    77  }
    78  
    79  // CreateResult is the response from a Create request. Call its Extract method
    80  // to interpret it as a Tenant.
    81  type CreateResult struct {
    82  	tenantResult
    83  }
    84  
    85  // DeleteResult is the response from a Get request. Call its ExtractErr method
    86  // to determine if the call succeeded or failed.
    87  type DeleteResult struct {
    88  	gophercloud.ErrResult
    89  }
    90  
    91  // UpdateResult is the response from a Update request. Call its Extract method
    92  // to interpret it as a Tenant.
    93  type UpdateResult struct {
    94  	tenantResult
    95  }