This repository has been archived by the owner on May 26, 2021. It is now read-only.
forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.go
161 lines (130 loc) · 4.37 KB
/
requests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package users
import (
"errors"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
func List(client *gophercloud.ServiceClient) pagination.Pager {
createPage := func(r pagination.PageResult) pagination.Page {
return UserPage{pagination.SinglePageBase(r)}
}
return pagination.NewPager(client, rootURL(client), createPage)
}
// EnabledState represents whether the user is enabled or not.
type EnabledState *bool
// Useful variables to use when creating or updating users.
var (
iTrue = true
iFalse = false
Enabled EnabledState = &iTrue
Disabled EnabledState = &iFalse
)
// CommonOpts are the parameters that are shared between CreateOpts and
// UpdateOpts
type CommonOpts struct {
// Either a name or username is required. When provided, the value must be
// unique or a 409 conflict error will be returned. If you provide a name but
// omit a username, the latter will be set to the former; and vice versa.
Name, Username string
// The ID of the tenant to which you want to assign this user.
TenantID string
// Indicates whether this user is enabled or not.
Enabled EnabledState
// The email address of this user.
Email string
}
// CreateOpts represents the options needed when creating new users.
type CreateOpts CommonOpts
// CreateOptsBuilder describes struct types that can be accepted by the Create call.
type CreateOptsBuilder interface {
ToUserCreateMap() (map[string]interface{}, error)
}
// ToUserCreateMap assembles a request body based on the contents of a CreateOpts.
func (opts CreateOpts) ToUserCreateMap() (map[string]interface{}, error) {
m := make(map[string]interface{})
if opts.Name == "" && opts.Username == "" {
return m, errors.New("Either a Name or Username must be provided")
}
if opts.Name != "" {
m["name"] = opts.Name
}
if opts.Username != "" {
m["username"] = opts.Username
}
if opts.Enabled != nil {
m["enabled"] = &opts.Enabled
}
if opts.Email != "" {
m["email"] = opts.Email
}
if opts.TenantID != "" {
m["tenant_id"] = opts.TenantID
}
return map[string]interface{}{"user": m}, nil
}
// Create is the operation responsible for creating new users.
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
var res CreateResult
reqBody, err := opts.ToUserCreateMap()
if err != nil {
res.Err = err
return res
}
_, res.Err = client.Post(rootURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{
OkCodes: []int{200, 201},
})
return res
}
// Get requests details on a single user, either by ID.
func Get(client *gophercloud.ServiceClient, id string) GetResult {
var result GetResult
_, result.Err = client.Get(ResourceURL(client, id), &result.Body, nil)
return result
}
// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
type UpdateOptsBuilder interface {
ToUserUpdateMap() map[string]interface{}
}
// UpdateOpts specifies the base attributes that may be updated on an existing server.
type UpdateOpts CommonOpts
// ToUserUpdateMap formats an UpdateOpts structure into a request body.
func (opts UpdateOpts) ToUserUpdateMap() map[string]interface{} {
m := make(map[string]interface{})
if opts.Name != "" {
m["name"] = opts.Name
}
if opts.Username != "" {
m["username"] = opts.Username
}
if opts.Enabled != nil {
m["enabled"] = &opts.Enabled
}
if opts.Email != "" {
m["email"] = opts.Email
}
if opts.TenantID != "" {
m["tenant_id"] = opts.TenantID
}
return map[string]interface{}{"user": m}
}
// Update is the operation responsible for updating exist users by their UUID.
func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
var result UpdateResult
reqBody := opts.ToUserUpdateMap()
_, result.Err = client.Put(ResourceURL(client, id), reqBody, &result.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
return result
}
// Delete is the operation responsible for permanently deleting an API user.
func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
var result DeleteResult
_, result.Err = client.Delete(ResourceURL(client, id), nil)
return result
}
func ListRoles(client *gophercloud.ServiceClient, tenantID, userID string) pagination.Pager {
createPage := func(r pagination.PageResult) pagination.Page {
return RolePage{pagination.SinglePageBase(r)}
}
return pagination.NewPager(client, listRolesURL(client, tenantID, userID), createPage)
}