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