blob: 18303e9fccfd18ba2c7facde28a74a502dc9a4e4 [file] [log] [blame]
Jamie Hannaford984e9172015-02-13 14:34:03 +01001package users
2
3import (
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
11func 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
29func 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
48func 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
60func 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
68func 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
86func 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}