Jamie Hannaford | 05d200d | 2015-02-20 14:49:05 +0100 | [diff] [blame^] | 1 | // +build acceptance db |
| 2 | |
| 3 | package v1 |
| 4 | |
| 5 | import ( |
| 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 | |
| 13 | func (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 | |
| 47 | func (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 | |
| 64 | func (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 | |
| 72 | func (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 | |
| 84 | func (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 | |
| 90 | func (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 | |
| 97 | func (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 | |
| 112 | func (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 | |
| 118 | func (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 | } |