| Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 1 | package users | 
|  | 2 |  | 
|  | 3 | import ( | 
| Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud" | 
|  | 5 | db "github.com/rackspace/gophercloud/openstack/db/v1/databases" | 
|  | 6 | os "github.com/rackspace/gophercloud/openstack/db/v1/users" | 
|  | 7 | "github.com/rackspace/gophercloud/pagination" | 
|  | 8 | ) | 
|  | 9 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame^] | 10 | /* | 
|  | 11 | ChangePassword changes the password for one or more users. For example, to | 
|  | 12 | change the respective passwords for two users: | 
|  | 13 |  | 
|  | 14 | opts := os.BatchCreateOpts{ | 
|  | 15 | os.CreateOpts{Name: "db_user_1", Password: "new_password_1"}, | 
|  | 16 | os.CreateOpts{Name: "db_user_2", Password: "new_password_2"}, | 
|  | 17 | } | 
|  | 18 |  | 
|  | 19 | ChangePassword(client, "instance_id", opts) | 
|  | 20 | */ | 
| Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 21 | func ChangePassword(client *gophercloud.ServiceClient, instanceID string, opts os.BatchCreateOpts) UpdatePasswordsResult { | 
|  | 22 | var res UpdatePasswordsResult | 
|  | 23 |  | 
|  | 24 | reqBody, err := opts.ToUserCreateMap() | 
|  | 25 | if err != nil { | 
|  | 26 | res.Err = err | 
|  | 27 | return res | 
|  | 28 | } | 
|  | 29 |  | 
| Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 30 | _, res.Err = client.Request("PUT", baseURL(client, instanceID), gophercloud.RequestOpts{ | 
|  | 31 | JSONBody: &reqBody, | 
|  | 32 | OkCodes:  []int{202}, | 
| Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 33 | }) | 
|  | 34 |  | 
|  | 35 | return res | 
|  | 36 | } | 
|  | 37 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame^] | 38 | // Update will modify the attributes of a specified user. Attributes that can | 
|  | 39 | // be updated are: user name, password, and host. | 
| Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 40 | func Update(client *gophercloud.ServiceClient, instanceID, userName string, opts os.CreateOpts) UpdateResult { | 
|  | 41 | var res UpdateResult | 
|  | 42 |  | 
|  | 43 | reqBody, err := opts.ToMap() | 
|  | 44 | if err != nil { | 
|  | 45 | res.Err = err | 
|  | 46 | return res | 
|  | 47 | } | 
|  | 48 | reqBody = map[string]interface{}{"user": reqBody} | 
|  | 49 |  | 
| Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 50 | _, res.Err = client.Request("PUT", userURL(client, instanceID, userName), gophercloud.RequestOpts{ | 
|  | 51 | JSONBody: &reqBody, | 
|  | 52 | OkCodes:  []int{202}, | 
| Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 53 | }) | 
|  | 54 |  | 
|  | 55 | return res | 
|  | 56 | } | 
|  | 57 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame^] | 58 | // Get will retrieve the details for a particular user. | 
| Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 59 | func Get(client *gophercloud.ServiceClient, instanceID, userName string) GetResult { | 
|  | 60 | var res GetResult | 
|  | 61 |  | 
| Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 62 | _, res.Err = client.Request("GET", userURL(client, instanceID, userName), gophercloud.RequestOpts{ | 
|  | 63 | JSONResponse: &res.Body, | 
|  | 64 | OkCodes:      []int{200}, | 
| Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 65 | }) | 
|  | 66 |  | 
|  | 67 | return res | 
|  | 68 | } | 
|  | 69 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame^] | 70 | // ListAccess will list all of the databases a user has access to. | 
| Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 71 | func ListAccess(client *gophercloud.ServiceClient, instanceID, userName string) pagination.Pager { | 
|  | 72 | pageFn := func(r pagination.PageResult) pagination.Page { | 
|  | 73 | return AccessPage{pagination.LinkedPageBase{PageResult: r}} | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | return pagination.NewPager(client, dbsURL(client, instanceID, userName), pageFn) | 
|  | 77 | } | 
|  | 78 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame^] | 79 | /* | 
|  | 80 | GrantAccess for the specified user to one or more databases on a specified | 
|  | 81 | instance. For example, to add a user to multiple databases: | 
|  | 82 |  | 
|  | 83 | opts := db.BatchCreateOpts{ | 
|  | 84 | db.CreateOpts{Name: "database_1"}, | 
|  | 85 | db.CreateOpts{Name: "database_3"}, | 
|  | 86 | db.CreateOpts{Name: "database_19"}, | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | GrantAccess(client, "instance_id", "user_name", opts) | 
|  | 90 | */ | 
| Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 91 | func GrantAccess(client *gophercloud.ServiceClient, instanceID, userName string, opts db.BatchCreateOpts) GrantAccessResult { | 
|  | 92 | var res GrantAccessResult | 
|  | 93 |  | 
|  | 94 | reqBody, err := opts.ToDBCreateMap() | 
|  | 95 | if err != nil { | 
|  | 96 | res.Err = err | 
|  | 97 | return res | 
|  | 98 | } | 
|  | 99 |  | 
| Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 100 | _, res.Err = client.Request("PUT", dbsURL(client, instanceID, userName), gophercloud.RequestOpts{ | 
|  | 101 | JSONBody: &reqBody, | 
|  | 102 | OkCodes:  []int{202}, | 
| Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 103 | }) | 
|  | 104 |  | 
|  | 105 | return res | 
|  | 106 | } | 
|  | 107 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame^] | 108 | /* | 
|  | 109 | RevokeAccess will revoke access for the specified user to one or more databases | 
|  | 110 | on a specified instance. For example, to remove a user's access to multiple | 
|  | 111 | databases: | 
|  | 112 |  | 
|  | 113 | opts := db.BatchCreateOpts{ | 
|  | 114 | db.CreateOpts{Name: "database_1"}, | 
|  | 115 | db.CreateOpts{Name: "database_3"}, | 
|  | 116 | db.CreateOpts{Name: "database_19"}, | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | RevokeAccess(client, "instance_id", "user_name", opts) | 
|  | 120 | */ | 
| Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 121 | func RevokeAccess(client *gophercloud.ServiceClient, instanceID, userName, dbName string) RevokeAccessResult { | 
|  | 122 | var res RevokeAccessResult | 
|  | 123 |  | 
| Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 124 | _, res.Err = client.Request("DELETE", dbURL(client, instanceID, userName, dbName), gophercloud.RequestOpts{ | 
|  | 125 | OkCodes: []int{202}, | 
| Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame] | 126 | }) | 
|  | 127 |  | 
|  | 128 | return res | 
|  | 129 | } |