1package users
2
3import (
4	"github.com/gophercloud/gophercloud"
5	"github.com/gophercloud/gophercloud/pagination"
6)
7
8// User represents a user resource that exists on the API.
9type 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.
31type 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.
40type UserPage struct {
41	pagination.SinglePageBase
42}
43
44// RolePage is a single page of a user Role collection.
45type RolePage struct {
46	pagination.SinglePageBase
47}
48
49// IsEmpty determines whether or not a page of Users contains any results.
50func (r UserPage) IsEmpty() (bool, error) {
51	users, err := ExtractUsers(r)
52	return len(users) == 0, err
53}
54
55// ExtractUsers returns a slice of Users contained in a single page of results.
56func ExtractUsers(r pagination.Page) ([]User, error) {
57	var s struct {
58		Users []User `json:"users"`
59	}
60	err := (r.(UserPage)).ExtractInto(&s)
61	return s.Users, err
62}
63
64// IsEmpty determines whether or not a page of Roles contains any results.
65func (r RolePage) IsEmpty() (bool, error) {
66	users, err := ExtractRoles(r)
67	return len(users) == 0, err
68}
69
70// ExtractRoles returns a slice of Roles contained in a single page of results.
71func ExtractRoles(r pagination.Page) ([]Role, error) {
72	var s struct {
73		Roles []Role `json:"roles"`
74	}
75	err := (r.(RolePage)).ExtractInto(&s)
76	return s.Roles, err
77}
78
79type commonResult struct {
80	gophercloud.Result
81}
82
83// Extract interprets any commonResult as a User, if possible.
84func (r commonResult) Extract() (*User, error) {
85	var s struct {
86		User *User `json:"user"`
87	}
88	err := r.ExtractInto(&s)
89	return s.User, err
90}
91
92// CreateResult represents the result of a Create operation. Call its Extract
93// method to interpret the result as a User.
94type CreateResult struct {
95	commonResult
96}
97
98// GetResult represents the result of a Get operation. Call its Extract method
99// to interpret the result as a User.
100type GetResult struct {
101	commonResult
102}
103
104// UpdateResult represents the result of an Update operation. Call its Extract
105// method to interpret the result as a User.
106type UpdateResult struct {
107	commonResult
108}
109
110// DeleteResult represents the result of a Delete operation. Call its
111// ExtractErr method to determine if the request succeeded or failed.
112type DeleteResult struct {
113	commonResult
114}
115