Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 1 | package users |
| 2 | |
| 3 | import ( |
Jamie Hannaford | 99eced5 | 2015-03-02 15:24:22 +0100 | [diff] [blame] | 4 | "errors" |
| 5 | |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 6 | "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 Hannaford | 1b2f8cb | 2015-10-16 12:25:07 +0200 | [diff] [blame] | 12 | // List will list all available users for a specified database instance. |
| 13 | func 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 Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 21 | /* |
| 22 | ChangePassword changes the password for one or more users. For example, to |
| 23 | change 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 Hannaford | 2e695a3 | 2015-11-16 16:36:10 +0100 | [diff] [blame] | 32 | func ChangePassword(client *gophercloud.ServiceClient, instanceID string, opts os.CreateOptsBuilder) UpdatePasswordsResult { |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 33 | var res UpdatePasswordsResult |
| 34 | |
| 35 | reqBody, err := opts.ToUserCreateMap() |
| 36 | if err != nil { |
| 37 | res.Err = err |
| 38 | return res |
| 39 | } |
| 40 | |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 41 | _, res.Err = client.Request("PUT", baseURL(client, instanceID), gophercloud.RequestOpts{ |
| 42 | JSONBody: &reqBody, |
| 43 | OkCodes: []int{202}, |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 44 | }) |
| 45 | |
| 46 | return res |
| 47 | } |
| 48 | |
Jamie Hannaford | 99eced5 | 2015-03-02 15:24:22 +0100 | [diff] [blame] | 49 | // UpdateOpts is the struct responsible for updating an existing user. |
| 50 | type 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. |
| 70 | func (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 Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 92 | // Update will modify the attributes of a specified user. Attributes that can |
| 93 | // be updated are: user name, password, and host. |
Jamie Hannaford | 99eced5 | 2015-03-02 15:24:22 +0100 | [diff] [blame] | 94 | func Update(client *gophercloud.ServiceClient, instanceID, userName string, opts UpdateOpts) UpdateResult { |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 95 | 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 Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 104 | _, res.Err = client.Request("PUT", userURL(client, instanceID, userName), gophercloud.RequestOpts{ |
| 105 | JSONBody: &reqBody, |
| 106 | OkCodes: []int{202}, |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 107 | }) |
| 108 | |
| 109 | return res |
| 110 | } |
| 111 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 112 | // Get will retrieve the details for a particular user. |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 113 | func Get(client *gophercloud.ServiceClient, instanceID, userName string) GetResult { |
| 114 | var res GetResult |
| 115 | |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 116 | _, res.Err = client.Request("GET", userURL(client, instanceID, userName), gophercloud.RequestOpts{ |
| 117 | JSONResponse: &res.Body, |
| 118 | OkCodes: []int{200}, |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 119 | }) |
| 120 | |
| 121 | return res |
| 122 | } |
| 123 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 124 | // ListAccess will list all of the databases a user has access to. |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 125 | func 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 Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 133 | /* |
| 134 | GrantAccess for the specified user to one or more databases on a specified |
| 135 | instance. 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 Hannaford | 2e695a3 | 2015-11-16 16:36:10 +0100 | [diff] [blame] | 145 | func GrantAccess(client *gophercloud.ServiceClient, instanceID, userName string, opts db.CreateOptsBuilder) GrantAccessResult { |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 146 | var res GrantAccessResult |
| 147 | |
| 148 | reqBody, err := opts.ToDBCreateMap() |
| 149 | if err != nil { |
| 150 | res.Err = err |
| 151 | return res |
| 152 | } |
| 153 | |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 154 | _, res.Err = client.Request("PUT", dbsURL(client, instanceID, userName), gophercloud.RequestOpts{ |
| 155 | JSONBody: &reqBody, |
| 156 | OkCodes: []int{202}, |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 157 | }) |
| 158 | |
| 159 | return res |
| 160 | } |
| 161 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 162 | /* |
| 163 | RevokeAccess will revoke access for the specified user to one or more databases |
Jamie Hannaford | 1110840 | 2015-02-23 10:31:41 +0100 | [diff] [blame] | 164 | on a specified instance. For example: |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 165 | |
Jamie Hannaford | 1110840 | 2015-02-23 10:31:41 +0100 | [diff] [blame] | 166 | RevokeAccess(client, "instance_id", "user_name", "db_name") |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 167 | */ |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 168 | func RevokeAccess(client *gophercloud.ServiceClient, instanceID, userName, dbName string) RevokeAccessResult { |
| 169 | var res RevokeAccessResult |
| 170 | |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 171 | _, res.Err = client.Request("DELETE", dbURL(client, instanceID, userName, dbName), gophercloud.RequestOpts{ |
| 172 | OkCodes: []int{202}, |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 173 | }) |
| 174 | |
| 175 | return res |
| 176 | } |