blob: c49338357d6a062dc73c1c9c809976dd3d496b5b [file] [log] [blame]
Jamie Hannaford2a130242014-10-28 11:19:46 +01001package users
Jamie Hannaford929bd002014-10-29 11:14:25 +01002
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannaford929bd002014-10-29 11:14:25 +01006)
7
8// User represents a user resource that exists on the API.
9type User struct {
10 // The UUID for this user.
11 ID string
12
13 // The human name for this user.
14 Name string
15
16 // The username for this user.
17 Username string
18
19 // Indicates whether the user is enabled (true) or disabled (false).
20 Enabled bool
21
22 // The email address for this user.
23 Email string
24
25 // The ID of the tenant to which this user belongs.
Jon Perritt3c166472016-02-25 03:07:41 -060026 TenantID string `json:"tenant_id"`
Jamie Hannaford929bd002014-10-29 11:14:25 +010027}
28
Jamie Hannaforde680e422014-10-29 14:55:57 +010029// Role assigns specific responsibilities to users, allowing them to accomplish
30// certain API operations whilst scoped to a service.
31type Role struct {
32 // UUID of the role
33 ID string
34
35 // Name of the role
36 Name string
37}
38
Jamie Hannaford929bd002014-10-29 11:14:25 +010039// UserPage is a single page of a User collection.
40type UserPage struct {
41 pagination.SinglePageBase
42}
43
Jamie Hannaforde680e422014-10-29 14:55:57 +010044// RolePage is a single page of a user Role collection.
45type RolePage struct {
46 pagination.SinglePageBase
47}
48
Jamie Hannaford929bd002014-10-29 11:14:25 +010049// IsEmpty determines whether or not a page of Tenants contains any results.
Jon Perritt31b66462016-02-25 22:25:30 -060050func (r UserPage) IsEmpty() (bool, error) {
51 users, err := ExtractUsers(r)
Jon Perritt3c166472016-02-25 03:07:41 -060052 return len(users) == 0, err
Jamie Hannaford929bd002014-10-29 11:14:25 +010053}
54
55// ExtractUsers returns a slice of Tenants contained in a single page of results.
Jon Perritt31b66462016-02-25 22:25:30 -060056func ExtractUsers(r pagination.Page) ([]User, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060057 var s struct {
58 Users []User `json:"users"`
Jamie Hannaford929bd002014-10-29 11:14:25 +010059 }
Jon Perritt31b66462016-02-25 22:25:30 -060060 err := (r.(UserPage)).ExtractInto(&s)
Jon Perritt3c166472016-02-25 03:07:41 -060061 return s.Users, err
Jamie Hannaford929bd002014-10-29 11:14:25 +010062}
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010063
Jamie Hannaforde680e422014-10-29 14:55:57 +010064// IsEmpty determines whether or not a page of Tenants contains any results.
Jon Perritt31b66462016-02-25 22:25:30 -060065func (r RolePage) IsEmpty() (bool, error) {
66 users, err := ExtractRoles(r)
Jon Perritt3c166472016-02-25 03:07:41 -060067 return len(users) == 0, err
Jamie Hannaforde680e422014-10-29 14:55:57 +010068}
69
70// ExtractRoles returns a slice of Roles contained in a single page of results.
Jon Perritt31b66462016-02-25 22:25:30 -060071func ExtractRoles(r pagination.Page) ([]Role, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060072 var s struct {
73 Roles []Role `json:"roles"`
Jamie Hannaforde680e422014-10-29 14:55:57 +010074 }
Jon Perritt31b66462016-02-25 22:25:30 -060075 err := (r.(RolePage)).ExtractInto(&s)
Jon Perritt3c166472016-02-25 03:07:41 -060076 return s.Roles, err
Jamie Hannaforde680e422014-10-29 14:55:57 +010077}
78
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010079type commonResult struct {
80 gophercloud.Result
81}
82
83// Extract interprets any commonResult as a User, if possible.
84func (r commonResult) Extract() (*User, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060085 var s struct {
86 User *User `json:"user"`
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010087 }
Jon Perritt3c166472016-02-25 03:07:41 -060088 err := r.ExtractInto(&s)
89 return s.User, err
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010090}
91
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +010092// CreateResult represents the result of a Create operation
Jamie Hannaford9c7bb8e2014-10-29 11:47:34 +010093type CreateResult struct {
94 commonResult
95}
Jamie Hannaford2ad98bd2014-10-29 13:26:47 +010096
97// GetResult represents the result of a Get operation
98type GetResult struct {
99 commonResult
100}
Jamie Hannaford7e04adf2014-10-29 13:47:58 +0100101
102// UpdateResult represents the result of an Update operation
103type UpdateResult struct {
104 commonResult
105}
Jamie Hannaford8b9a8002014-10-29 14:20:24 +0100106
107// DeleteResult represents the result of a Delete operation
108type DeleteResult struct {
109 commonResult
110}