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 | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 12 | /* |
| 13 | ChangePassword changes the password for one or more users. For example, to |
| 14 | change 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 Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 23 | func 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 Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 32 | _, res.Err = client.Request("PUT", baseURL(client, instanceID), gophercloud.RequestOpts{ |
| 33 | JSONBody: &reqBody, |
| 34 | OkCodes: []int{202}, |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 35 | }) |
| 36 | |
| 37 | return res |
| 38 | } |
| 39 | |
Jamie Hannaford | 99eced5 | 2015-03-02 15:24:22 +0100 | [diff] [blame] | 40 | // UpdateOpts is the struct responsible for updating an existing user. |
| 41 | type 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. |
| 61 | func (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 Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 83 | // Update will modify the attributes of a specified user. Attributes that can |
| 84 | // be updated are: user name, password, and host. |
Jamie Hannaford | 99eced5 | 2015-03-02 15:24:22 +0100 | [diff] [blame] | 85 | func Update(client *gophercloud.ServiceClient, instanceID, userName string, opts UpdateOpts) UpdateResult { |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 86 | 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 Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 95 | _, res.Err = client.Request("PUT", userURL(client, instanceID, userName), gophercloud.RequestOpts{ |
| 96 | JSONBody: &reqBody, |
| 97 | OkCodes: []int{202}, |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 98 | }) |
| 99 | |
| 100 | return res |
| 101 | } |
| 102 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 103 | // Get will retrieve the details for a particular user. |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 104 | func Get(client *gophercloud.ServiceClient, instanceID, userName string) GetResult { |
| 105 | var res GetResult |
| 106 | |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 107 | _, res.Err = client.Request("GET", userURL(client, instanceID, userName), gophercloud.RequestOpts{ |
| 108 | JSONResponse: &res.Body, |
| 109 | OkCodes: []int{200}, |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 110 | }) |
| 111 | |
| 112 | return res |
| 113 | } |
| 114 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 115 | // ListAccess will list all of the databases a user has access to. |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 116 | func 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 Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 124 | /* |
| 125 | GrantAccess for the specified user to one or more databases on a specified |
| 126 | instance. 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 Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 136 | func 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 Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 145 | _, res.Err = client.Request("PUT", dbsURL(client, instanceID, userName), gophercloud.RequestOpts{ |
| 146 | JSONBody: &reqBody, |
| 147 | OkCodes: []int{202}, |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 148 | }) |
| 149 | |
| 150 | return res |
| 151 | } |
| 152 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 153 | /* |
| 154 | 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] | 155 | on a specified instance. For example: |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 156 | |
Jamie Hannaford | 1110840 | 2015-02-23 10:31:41 +0100 | [diff] [blame] | 157 | RevokeAccess(client, "instance_id", "user_name", "db_name") |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 158 | */ |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 159 | func RevokeAccess(client *gophercloud.ServiceClient, instanceID, userName, dbName string) RevokeAccessResult { |
| 160 | var res RevokeAccessResult |
| 161 | |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 162 | _, res.Err = client.Request("DELETE", dbURL(client, instanceID, userName, dbName), gophercloud.RequestOpts{ |
| 163 | OkCodes: []int{202}, |
Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 164 | }) |
| 165 | |
| 166 | return res |
| 167 | } |