blob: 74e47ab23b2b40f1723de48381e0f8d164721553 [file] [log] [blame]
Jamie Hannaford984e9172015-02-13 14:34:03 +01001package users
2
3import (
Jamie Hannaford99eced52015-03-02 15:24:22 +01004 "errors"
5
Jamie Hannaford984e9172015-02-13 14:34:03 +01006 "github.com/rackspace/gophercloud"
7 db "github.com/rackspace/gophercloud/openstack/db/v1/databases"
8 os "github.com/rackspace/gophercloud/openstack/db/v1/users"
9 "github.com/rackspace/gophercloud/pagination"
10)
11
Jamie Hannaford1b2f8cb2015-10-16 12:25:07 +020012// List will list all available users for a specified database instance.
13func List(client *gophercloud.ServiceClient, instanceID string) pagination.Pager {
14 createPageFn := func(r pagination.PageResult) pagination.Page {
15 return UserPage{pagination.LinkedPageBase{PageResult: r}}
16 }
17
18 return pagination.NewPager(client, baseURL(client, instanceID), createPageFn)
19}
20
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010021/*
22ChangePassword changes the password for one or more users. For example, to
23change the respective passwords for two users:
24
25 opts := os.BatchCreateOpts{
26 os.CreateOpts{Name: "db_user_1", Password: "new_password_1"},
27 os.CreateOpts{Name: "db_user_2", Password: "new_password_2"},
28 }
29
30 ChangePassword(client, "instance_id", opts)
31*/
Jamie Hannaford2e695a32015-11-16 16:36:10 +010032func ChangePassword(client *gophercloud.ServiceClient, instanceID string, opts os.CreateOptsBuilder) UpdatePasswordsResult {
Jamie Hannaford984e9172015-02-13 14:34:03 +010033 var res UpdatePasswordsResult
34
35 reqBody, err := opts.ToUserCreateMap()
36 if err != nil {
37 res.Err = err
38 return res
39 }
40
Jamie Hannaforda50d1352015-02-18 11:38:38 +010041 _, res.Err = client.Request("PUT", baseURL(client, instanceID), gophercloud.RequestOpts{
42 JSONBody: &reqBody,
43 OkCodes: []int{202},
Jamie Hannaford984e9172015-02-13 14:34:03 +010044 })
45
46 return res
47}
48
Jamie Hannaford99eced52015-03-02 15:24:22 +010049// UpdateOpts is the struct responsible for updating an existing user.
50type UpdateOpts struct {
51 // [OPTIONAL] Specifies a name for the user. Valid names can be composed
52 // of the following characters: letters (either case); numbers; these
53 // characters '@', '?', '#', ' ' but NEVER beginning a name string; '_' is
54 // permitted anywhere. Prohibited characters that are forbidden include:
55 // single quotes, double quotes, back quotes, semicolons, commas, backslashes,
56 // and forward slashes. Spaces at the front or end of a user name are also
57 // not permitted.
58 Name string
59
60 // [OPTIONAL] Specifies a password for the user.
61 Password string
62
63 // [OPTIONAL] Specifies the host from which a user is allowed to connect to
64 // the database. Possible values are a string containing an IPv4 address or
65 // "%" to allow connecting from any host. Optional; the default is "%".
66 Host string
67}
68
69// ToMap is a convenience function for creating sub-maps for individual users.
70func (opts UpdateOpts) ToMap() (map[string]interface{}, error) {
71 if opts.Name == "root" {
72 return nil, errors.New("root is a reserved user name and cannot be used")
73 }
74
75 user := map[string]interface{}{}
76
77 if opts.Name != "" {
78 user["name"] = opts.Name
79 }
80
81 if opts.Password != "" {
82 user["password"] = opts.Password
83 }
84
85 if opts.Host != "" {
86 user["host"] = opts.Host
87 }
88
89 return user, nil
90}
91
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010092// Update will modify the attributes of a specified user. Attributes that can
93// be updated are: user name, password, and host.
Jamie Hannaford99eced52015-03-02 15:24:22 +010094func Update(client *gophercloud.ServiceClient, instanceID, userName string, opts UpdateOpts) UpdateResult {
Jamie Hannaford984e9172015-02-13 14:34:03 +010095 var res UpdateResult
96
97 reqBody, err := opts.ToMap()
98 if err != nil {
99 res.Err = err
100 return res
101 }
102 reqBody = map[string]interface{}{"user": reqBody}
103
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100104 _, res.Err = client.Request("PUT", userURL(client, instanceID, userName), gophercloud.RequestOpts{
105 JSONBody: &reqBody,
106 OkCodes: []int{202},
Jamie Hannaford984e9172015-02-13 14:34:03 +0100107 })
108
109 return res
110}
111
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100112// Get will retrieve the details for a particular user.
Jamie Hannaford984e9172015-02-13 14:34:03 +0100113func Get(client *gophercloud.ServiceClient, instanceID, userName string) GetResult {
114 var res GetResult
115
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100116 _, res.Err = client.Request("GET", userURL(client, instanceID, userName), gophercloud.RequestOpts{
117 JSONResponse: &res.Body,
118 OkCodes: []int{200},
Jamie Hannaford984e9172015-02-13 14:34:03 +0100119 })
120
121 return res
122}
123
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100124// ListAccess will list all of the databases a user has access to.
Jamie Hannaford984e9172015-02-13 14:34:03 +0100125func ListAccess(client *gophercloud.ServiceClient, instanceID, userName string) pagination.Pager {
126 pageFn := func(r pagination.PageResult) pagination.Page {
127 return AccessPage{pagination.LinkedPageBase{PageResult: r}}
128 }
129
130 return pagination.NewPager(client, dbsURL(client, instanceID, userName), pageFn)
131}
132
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100133/*
134GrantAccess for the specified user to one or more databases on a specified
135instance. For example, to add a user to multiple databases:
136
137 opts := db.BatchCreateOpts{
138 db.CreateOpts{Name: "database_1"},
139 db.CreateOpts{Name: "database_3"},
140 db.CreateOpts{Name: "database_19"},
141 }
142
143 GrantAccess(client, "instance_id", "user_name", opts)
144*/
Jamie Hannaford2e695a32015-11-16 16:36:10 +0100145func GrantAccess(client *gophercloud.ServiceClient, instanceID, userName string, opts db.CreateOptsBuilder) GrantAccessResult {
Jamie Hannaford984e9172015-02-13 14:34:03 +0100146 var res GrantAccessResult
147
148 reqBody, err := opts.ToDBCreateMap()
149 if err != nil {
150 res.Err = err
151 return res
152 }
153
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100154 _, res.Err = client.Request("PUT", dbsURL(client, instanceID, userName), gophercloud.RequestOpts{
155 JSONBody: &reqBody,
156 OkCodes: []int{202},
Jamie Hannaford984e9172015-02-13 14:34:03 +0100157 })
158
159 return res
160}
161
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100162/*
163RevokeAccess will revoke access for the specified user to one or more databases
Jamie Hannaford11108402015-02-23 10:31:41 +0100164on a specified instance. For example:
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100165
Jamie Hannaford11108402015-02-23 10:31:41 +0100166 RevokeAccess(client, "instance_id", "user_name", "db_name")
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100167*/
Jamie Hannaford984e9172015-02-13 14:34:03 +0100168func RevokeAccess(client *gophercloud.ServiceClient, instanceID, userName, dbName string) RevokeAccessResult {
169 var res RevokeAccessResult
170
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100171 _, res.Err = client.Request("DELETE", dbURL(client, instanceID, userName, dbName), gophercloud.RequestOpts{
172 OkCodes: []int{202},
Jamie Hannaford984e9172015-02-13 14:34:03 +0100173 })
174
175 return res
176}