blob: 56674520bba379f5641b1cf9b87df8eb94e5fbba [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 Hannafordb0d267b2015-02-19 11:59:53 +010012/*
13ChangePassword changes the password for one or more users. For example, to
14change the respective passwords for two users:
15
16 opts := os.BatchCreateOpts{
17 os.CreateOpts{Name: "db_user_1", Password: "new_password_1"},
18 os.CreateOpts{Name: "db_user_2", Password: "new_password_2"},
19 }
20
21 ChangePassword(client, "instance_id", opts)
22*/
Jamie Hannaford984e9172015-02-13 14:34:03 +010023func ChangePassword(client *gophercloud.ServiceClient, instanceID string, opts os.BatchCreateOpts) UpdatePasswordsResult {
24 var res UpdatePasswordsResult
25
26 reqBody, err := opts.ToUserCreateMap()
27 if err != nil {
28 res.Err = err
29 return res
30 }
31
Jamie Hannaforda50d1352015-02-18 11:38:38 +010032 _, res.Err = client.Request("PUT", baseURL(client, instanceID), gophercloud.RequestOpts{
33 JSONBody: &reqBody,
34 OkCodes: []int{202},
Jamie Hannaford984e9172015-02-13 14:34:03 +010035 })
36
37 return res
38}
39
Jamie Hannaford99eced52015-03-02 15:24:22 +010040// UpdateOpts is the struct responsible for updating an existing user.
41type UpdateOpts struct {
42 // [OPTIONAL] Specifies a name for the user. Valid names can be composed
43 // of the following characters: letters (either case); numbers; these
44 // characters '@', '?', '#', ' ' but NEVER beginning a name string; '_' is
45 // permitted anywhere. Prohibited characters that are forbidden include:
46 // single quotes, double quotes, back quotes, semicolons, commas, backslashes,
47 // and forward slashes. Spaces at the front or end of a user name are also
48 // not permitted.
49 Name string
50
51 // [OPTIONAL] Specifies a password for the user.
52 Password string
53
54 // [OPTIONAL] Specifies the host from which a user is allowed to connect to
55 // the database. Possible values are a string containing an IPv4 address or
56 // "%" to allow connecting from any host. Optional; the default is "%".
57 Host string
58}
59
60// ToMap is a convenience function for creating sub-maps for individual users.
61func (opts UpdateOpts) ToMap() (map[string]interface{}, error) {
62 if opts.Name == "root" {
63 return nil, errors.New("root is a reserved user name and cannot be used")
64 }
65
66 user := map[string]interface{}{}
67
68 if opts.Name != "" {
69 user["name"] = opts.Name
70 }
71
72 if opts.Password != "" {
73 user["password"] = opts.Password
74 }
75
76 if opts.Host != "" {
77 user["host"] = opts.Host
78 }
79
80 return user, nil
81}
82
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010083// Update will modify the attributes of a specified user. Attributes that can
84// be updated are: user name, password, and host.
Jamie Hannaford99eced52015-03-02 15:24:22 +010085func Update(client *gophercloud.ServiceClient, instanceID, userName string, opts UpdateOpts) UpdateResult {
Jamie Hannaford984e9172015-02-13 14:34:03 +010086 var res UpdateResult
87
88 reqBody, err := opts.ToMap()
89 if err != nil {
90 res.Err = err
91 return res
92 }
93 reqBody = map[string]interface{}{"user": reqBody}
94
Jamie Hannaforda50d1352015-02-18 11:38:38 +010095 _, res.Err = client.Request("PUT", userURL(client, instanceID, userName), gophercloud.RequestOpts{
96 JSONBody: &reqBody,
97 OkCodes: []int{202},
Jamie Hannaford984e9172015-02-13 14:34:03 +010098 })
99
100 return res
101}
102
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100103// Get will retrieve the details for a particular user.
Jamie Hannaford984e9172015-02-13 14:34:03 +0100104func Get(client *gophercloud.ServiceClient, instanceID, userName string) GetResult {
105 var res GetResult
106
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100107 _, res.Err = client.Request("GET", userURL(client, instanceID, userName), gophercloud.RequestOpts{
108 JSONResponse: &res.Body,
109 OkCodes: []int{200},
Jamie Hannaford984e9172015-02-13 14:34:03 +0100110 })
111
112 return res
113}
114
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100115// ListAccess will list all of the databases a user has access to.
Jamie Hannaford984e9172015-02-13 14:34:03 +0100116func ListAccess(client *gophercloud.ServiceClient, instanceID, userName string) pagination.Pager {
117 pageFn := func(r pagination.PageResult) pagination.Page {
118 return AccessPage{pagination.LinkedPageBase{PageResult: r}}
119 }
120
121 return pagination.NewPager(client, dbsURL(client, instanceID, userName), pageFn)
122}
123
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100124/*
125GrantAccess for the specified user to one or more databases on a specified
126instance. For example, to add a user to multiple databases:
127
128 opts := db.BatchCreateOpts{
129 db.CreateOpts{Name: "database_1"},
130 db.CreateOpts{Name: "database_3"},
131 db.CreateOpts{Name: "database_19"},
132 }
133
134 GrantAccess(client, "instance_id", "user_name", opts)
135*/
Jamie Hannaford984e9172015-02-13 14:34:03 +0100136func GrantAccess(client *gophercloud.ServiceClient, instanceID, userName string, opts db.BatchCreateOpts) GrantAccessResult {
137 var res GrantAccessResult
138
139 reqBody, err := opts.ToDBCreateMap()
140 if err != nil {
141 res.Err = err
142 return res
143 }
144
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100145 _, res.Err = client.Request("PUT", dbsURL(client, instanceID, userName), gophercloud.RequestOpts{
146 JSONBody: &reqBody,
147 OkCodes: []int{202},
Jamie Hannaford984e9172015-02-13 14:34:03 +0100148 })
149
150 return res
151}
152
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100153/*
154RevokeAccess will revoke access for the specified user to one or more databases
Jamie Hannaford11108402015-02-23 10:31:41 +0100155on a specified instance. For example:
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100156
Jamie Hannaford11108402015-02-23 10:31:41 +0100157 RevokeAccess(client, "instance_id", "user_name", "db_name")
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100158*/
Jamie Hannaford984e9172015-02-13 14:34:03 +0100159func RevokeAccess(client *gophercloud.ServiceClient, instanceID, userName, dbName string) RevokeAccessResult {
160 var res RevokeAccessResult
161
Jamie Hannaforda50d1352015-02-18 11:38:38 +0100162 _, res.Err = client.Request("DELETE", dbURL(client, instanceID, userName, dbName), gophercloud.RequestOpts{
163 OkCodes: []int{202},
Jamie Hannaford984e9172015-02-13 14:34:03 +0100164 })
165
166 return res
167}