blob: 9bf00ddaa423049b8e53332dc03759fd5bf0f4cf [file] [log] [blame]
Jamie Hannaford05d200d2015-02-20 14:49:05 +01001// +build acceptance db
2
3package v1
4
5import (
6 "github.com/rackspace/gophercloud/acceptance/tools"
7 "github.com/rackspace/gophercloud/openstack/identity/v2/users"
8 "github.com/rackspace/gophercloud/pagination"
9 db "github.com/rackspace/gophercloud/rackspace/db/v1/databases"
10 u "github.com/rackspace/gophercloud/rackspace/db/v1/users"
11)
12
13func (c context) createUsers() {
14 users := []string{
15 tools.RandomString("user_"),
16 tools.RandomString("user_"),
17 tools.RandomString("user_"),
18 }
19
20 db1 := db.CreateOpt{Name: c.DBIDs[0]}
21 db2 := db.CreateOpt{Name: c.DBIDs[1]}
22 db3 := db.CreateOpt{Name: c.DBIDs[2]}
23
24 opts := u.BatchCreateOpts{
25 u.CreateOpts{
26 Name: users[0],
27 Password: tools.RandomString(),
28 databases: db.BatchCreateOpts{db1, db2, db3},
29 },
30 u.CreateOpts{
31 Name: users[1],
32 Password: tools.RandomString(),
33 databases: db.BatchCreateOpts{db1, db2},
34 },
35 u.CreateOpts{
36 Name: users[2],
37 Password: tools.RandomString(),
38 databases: db.BatchCreateOpts{db3},
39 },
40 }
41
42 err := u.Create(c.client, c.instanceID, opts).ExtractErr()
43 c.Logf("Created three users on instance %s: %s, %s, %s", c.instanceID, users[0], users[1], users[2])
44 c.users = users
45}
46
47func (c context) listUsers() {
48 c.Logf("Listing users on instance %s", c.instanceID)
49
50 err := users.List(c.client, c.instanceID).EachPage(func(page pagination.Page) (bool, error) {
51 uList, err := u.ExtractUsers(page)
52 c.AssertNoErr(err)
53
54 for _, u := range uList {
55 c.Logf("User: %#v", u)
56 }
57
58 return true, nil
59 })
60
61 c.CheckNoErr(err)
62}
63
64func (c context) deleteUsers() {
65 for _, id := range c.users {
66 err := u.Delete(c.client, c.instanceID, id).ExtractErr()
67 c.CheckNoErr(err)
68 t.Logf("Deleted user %s", id)
69 }
70}
71
72func (c context) changeUserPwd() {
73 opts := u.BatchCreateOpts{}
74
75 for _, id := range c.users[:1] {
76 opts = append(opts, u.CreateOpts{Password: tools.PrefixString("", 5)})
77 }
78
79 err := u.UpdatePassword(c.client, c.instanceID, opts).ExtractErr()
80 c.Logf("Updated 2 users' passwords")
81 c.AssertNoErr(err)
82}
83
84func (c context) getUser() {
85 user, err := u.Get(c.client, c.instanceID, c.users[0]).Extract()
86 c.Logf("Getting user %s", user)
87 c.AssertNoErr(err)
88}
89
90func (c context) updateUser() {
91 opts := u.CreateOpts{Name: tools.PrefixString("new_name_", 5)}
92 user, err := u.Update(c.client, c.instanceID, c.users[0], opts).Extract()
93 c.Logf("Updated user %s", user)
94 c.AssertNoErr(err)
95}
96
97func (c context) listUserAccess() {
98 err := u.ListAccess(c.client, c.instanceId, c.users[0]).EachPage(func(page pagination.Page) (bool, error) {
99 dbList, err := db.ExtractDBs(page)
100 c.AssertNoErr(err)
101
102 for _, db := range dbList {
103 c.Logf("User %s has access to DB: %#v", db)
104 }
105
106 return true, nil
107 })
108
109 c.CheckNoErr(err)
110}
111
112func (c context) grantUserAccess() {
113 userID, dbID := c.users[0], c.DBIDS[0]
114 err := u.GrantUserAccess(c.client, c.instanceID, userID, dbID)
115 c.Logf("Granted access for user %s to DB %s", userID, dbID)
116}
117
118func (c context) revokeUserAccess() {
119 userID, dbID := c.users[0], c.DBIDS[0]
120 err := u.RevokeUserAccess(c.client, c.instanceID, userID, dbID)
121 c.Logf("Revoked access for user %s to DB %s", userID, dbID)
122}