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

     1  package users
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // User represents a user resource that exists on the API.
     9  type User struct {
    10  	// ID is the UUID for this user.
    11  	ID string
    12  
    13  	// Name is the human name for this user.
    14  	Name string
    15  
    16  	// Username is the username for this user.
    17  	Username string
    18  
    19  	// Enabled indicates whether the user is enabled (true) or disabled (false).
    20  	Enabled bool
    21  
    22  	// Email is the email address for this user.
    23  	Email string
    24  
    25  	// TenantID is the ID of the tenant to which this user belongs.
    26  	TenantID string `json:"tenant_id"`
    27  }
    28  
    29  // Role assigns specific responsibilities to users, allowing them to accomplish
    30  // certain API operations whilst scoped to a service.
    31  type Role struct {
    32  	// ID is the UUID of the role.
    33  	ID string
    34  
    35  	// Name is the name of the role.
    36  	Name string
    37  }
    38  
    39  // UserPage is a single page of a User collection.
    40  type UserPage struct {
    41  	pagination.SinglePageBase
    42  }
    43  
    44  // RolePage is a single page of a user Role collection.
    45  type RolePage struct {
    46  	pagination.SinglePageBase
    47  }
    48  
    49  // IsEmpty determines whether or not a page of Users contains any results.
    50  func (r UserPage) IsEmpty() (bool, error) {
    51  	if r.StatusCode == 204 {
    52  		return true, nil
    53  	}
    54  
    55  	users, err := ExtractUsers(r)
    56  	return len(users) == 0, err
    57  }
    58  
    59  // ExtractUsers returns a slice of Users contained in a single page of results.
    60  func ExtractUsers(r pagination.Page) ([]User, error) {
    61  	var s struct {
    62  		Users []User `json:"users"`
    63  	}
    64  	err := (r.(UserPage)).ExtractInto(&s)
    65  	return s.Users, err
    66  }
    67  
    68  // IsEmpty determines whether or not a page of Roles contains any results.
    69  func (r RolePage) IsEmpty() (bool, error) {
    70  	if r.StatusCode == 204 {
    71  		return true, nil
    72  	}
    73  
    74  	users, err := ExtractRoles(r)
    75  	return len(users) == 0, err
    76  }
    77  
    78  // ExtractRoles returns a slice of Roles contained in a single page of results.
    79  func ExtractRoles(r pagination.Page) ([]Role, error) {
    80  	var s struct {
    81  		Roles []Role `json:"roles"`
    82  	}
    83  	err := (r.(RolePage)).ExtractInto(&s)
    84  	return s.Roles, err
    85  }
    86  
    87  type commonResult struct {
    88  	gophercloud.Result
    89  }
    90  
    91  // Extract interprets any commonResult as a User, if possible.
    92  func (r commonResult) Extract() (*User, error) {
    93  	var s struct {
    94  		User *User `json:"user"`
    95  	}
    96  	err := r.ExtractInto(&s)
    97  	return s.User, err
    98  }
    99  
   100  // CreateResult represents the result of a Create operation. Call its Extract
   101  // method to interpret the result as a User.
   102  type CreateResult struct {
   103  	commonResult
   104  }
   105  
   106  // GetResult represents the result of a Get operation. Call its Extract method
   107  // to interpret the result as a User.
   108  type GetResult struct {
   109  	commonResult
   110  }
   111  
   112  // UpdateResult represents the result of an Update operation. Call its Extract
   113  // method to interpret the result as a User.
   114  type UpdateResult struct {
   115  	commonResult
   116  }
   117  
   118  // DeleteResult represents the result of a Delete operation. Call its
   119  // ExtractErr method to determine if the request succeeded or failed.
   120  type DeleteResult struct {
   121  	commonResult
   122  }