Jamie Hannaford | 984e917 | 2015-02-13 14:34:03 +0100 | [diff] [blame^] | 1 | package users |
| 2 | |
| 3 | import ( |
| 4 | "github.com/racker/perigee" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | db "github.com/rackspace/gophercloud/openstack/db/v1/databases" |
| 7 | os "github.com/rackspace/gophercloud/openstack/db/v1/users" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | ) |
| 10 | |
| 11 | func ChangePassword(client *gophercloud.ServiceClient, instanceID string, opts os.BatchCreateOpts) UpdatePasswordsResult { |
| 12 | var res UpdatePasswordsResult |
| 13 | |
| 14 | reqBody, err := opts.ToUserCreateMap() |
| 15 | if err != nil { |
| 16 | res.Err = err |
| 17 | return res |
| 18 | } |
| 19 | |
| 20 | _, res.Err = perigee.Request("PUT", baseURL(client, instanceID), perigee.Options{ |
| 21 | MoreHeaders: client.AuthenticatedHeaders(), |
| 22 | ReqBody: &reqBody, |
| 23 | OkCodes: []int{202}, |
| 24 | }) |
| 25 | |
| 26 | return res |
| 27 | } |
| 28 | |
| 29 | func Update(client *gophercloud.ServiceClient, instanceID, userName string, opts os.CreateOpts) UpdateResult { |
| 30 | var res UpdateResult |
| 31 | |
| 32 | reqBody, err := opts.ToMap() |
| 33 | if err != nil { |
| 34 | res.Err = err |
| 35 | return res |
| 36 | } |
| 37 | reqBody = map[string]interface{}{"user": reqBody} |
| 38 | |
| 39 | _, res.Err = perigee.Request("PUT", userURL(client, instanceID, userName), perigee.Options{ |
| 40 | MoreHeaders: client.AuthenticatedHeaders(), |
| 41 | ReqBody: &reqBody, |
| 42 | OkCodes: []int{202}, |
| 43 | }) |
| 44 | |
| 45 | return res |
| 46 | } |
| 47 | |
| 48 | func Get(client *gophercloud.ServiceClient, instanceID, userName string) GetResult { |
| 49 | var res GetResult |
| 50 | |
| 51 | _, res.Err = perigee.Request("GET", userURL(client, instanceID, userName), perigee.Options{ |
| 52 | MoreHeaders: client.AuthenticatedHeaders(), |
| 53 | Results: &res.Body, |
| 54 | OkCodes: []int{200}, |
| 55 | }) |
| 56 | |
| 57 | return res |
| 58 | } |
| 59 | |
| 60 | func ListAccess(client *gophercloud.ServiceClient, instanceID, userName string) pagination.Pager { |
| 61 | pageFn := func(r pagination.PageResult) pagination.Page { |
| 62 | return AccessPage{pagination.LinkedPageBase{PageResult: r}} |
| 63 | } |
| 64 | |
| 65 | return pagination.NewPager(client, dbsURL(client, instanceID, userName), pageFn) |
| 66 | } |
| 67 | |
| 68 | func GrantAccess(client *gophercloud.ServiceClient, instanceID, userName string, opts db.BatchCreateOpts) GrantAccessResult { |
| 69 | var res GrantAccessResult |
| 70 | |
| 71 | reqBody, err := opts.ToDBCreateMap() |
| 72 | if err != nil { |
| 73 | res.Err = err |
| 74 | return res |
| 75 | } |
| 76 | |
| 77 | _, res.Err = perigee.Request("PUT", dbsURL(client, instanceID, userName), perigee.Options{ |
| 78 | MoreHeaders: client.AuthenticatedHeaders(), |
| 79 | ReqBody: &reqBody, |
| 80 | OkCodes: []int{202}, |
| 81 | }) |
| 82 | |
| 83 | return res |
| 84 | } |
| 85 | |
| 86 | func RevokeAccess(client *gophercloud.ServiceClient, instanceID, userName, dbName string) RevokeAccessResult { |
| 87 | var res RevokeAccessResult |
| 88 | |
| 89 | _, res.Err = perigee.Request("DELETE", dbURL(client, instanceID, userName, dbName), perigee.Options{ |
| 90 | MoreHeaders: client.AuthenticatedHeaders(), |
| 91 | OkCodes: []int{202}, |
| 92 | }) |
| 93 | |
| 94 | return res |
| 95 | } |