blob: 4b230075b943af631383fbdf40943c9a13800f89 [file] [log] [blame]
Jamie Hannaford80c70672015-10-06 16:09:33 +02001// +build acceptance db rackspace
Jamie Hannaford05d200d2015-02-20 14:49:05 +01002
3package v1
4
5import (
Jon Perritt27249f42016-02-18 10:35:59 -06006 "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 Hannaford05d200d2015-02-20 14:49:05 +010011)
12
Jamie Hannaford99eced52015-03-02 15:24:22 +010013func (c *context) createUsers() {
Jamie Hannaford11108402015-02-23 10:31:41 +010014 c.users = []string{
15 tools.RandomString("user_", 5),
16 tools.RandomString("user_", 5),
17 tools.RandomString("user_", 5),
Jamie Hannaford05d200d2015-02-20 14:49:05 +010018 }
19
Jamie Hannaford11108402015-02-23 10:31:41 +010020 db1 := db.CreateOpts{Name: c.DBIDs[0]}
21 db2 := db.CreateOpts{Name: c.DBIDs[1]}
22 db3 := db.CreateOpts{Name: c.DBIDs[2]}
Jamie Hannaford05d200d2015-02-20 14:49:05 +010023
Jamie Hannaford11108402015-02-23 10:31:41 +010024 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 Hannaford05d200d2015-02-20 14:49:05 +010029 },
Jamie Hannaford11108402015-02-23 10:31:41 +010030 os.CreateOpts{
31 Name: c.users[1],
32 Password: tools.RandomString("db_", 5),
33 Databases: db.BatchCreateOpts{db1, db2},
Jamie Hannaford05d200d2015-02-20 14:49:05 +010034 },
Jamie Hannaford11108402015-02-23 10:31:41 +010035 os.CreateOpts{
36 Name: c.users[2],
37 Password: tools.RandomString("db_", 5),
38 Databases: db.BatchCreateOpts{db3},
Jamie Hannaford05d200d2015-02-20 14:49:05 +010039 },
40 }
41
Jamie Hannaford11108402015-02-23 10:31:41 +010042 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 Hannaford05d200d2015-02-20 14:49:05 +010045}
46
Jamie Hannaford99eced52015-03-02 15:24:22 +010047func (c *context) listUsers() {
Jamie Hannaford05d200d2015-02-20 14:49:05 +010048 c.Logf("Listing users on instance %s", c.instanceID)
49
Jamie Hannaford11108402015-02-23 10:31:41 +010050 err := os.List(c.client, c.instanceID).EachPage(func(page pagination.Page) (bool, error) {
51 uList, err := os.ExtractUsers(page)
Jamie Hannaford05d200d2015-02-20 14:49:05 +010052 c.AssertNoErr(err)
53
54 for _, u := range uList {
55 c.Logf("User: %#v", u)
56 }
57
58 return true, nil
59 })
60
Jamie Hannaford11108402015-02-23 10:31:41 +010061 c.AssertNoErr(err)
Jamie Hannaford05d200d2015-02-20 14:49:05 +010062}
63
Jamie Hannaford99eced52015-03-02 15:24:22 +010064func (c *context) deleteUsers() {
Jamie Hannaford05d200d2015-02-20 14:49:05 +010065 for _, id := range c.users {
Jamie Hannaford11108402015-02-23 10:31:41 +010066 err := users.Delete(c.client, c.instanceID, id).ExtractErr()
67 c.AssertNoErr(err)
68 c.Logf("Deleted user %s", id)
Jamie Hannaford05d200d2015-02-20 14:49:05 +010069 }
70}
71
Jamie Hannaford99eced52015-03-02 15:24:22 +010072func (c *context) changeUserPwd() {
Jamie Hannaford11108402015-02-23 10:31:41 +010073 opts := os.BatchCreateOpts{}
Jamie Hannaford05d200d2015-02-20 14:49:05 +010074
Jamie Hannaford11108402015-02-23 10:31:41 +010075 for _, name := range c.users[:1] {
76 opts = append(opts, os.CreateOpts{Name: name, Password: tools.RandomString("", 5)})
Jamie Hannaford05d200d2015-02-20 14:49:05 +010077 }
78
Jamie Hannaford11108402015-02-23 10:31:41 +010079 err := users.ChangePassword(c.client, c.instanceID, opts).ExtractErr()
Jamie Hannaford05d200d2015-02-20 14:49:05 +010080 c.Logf("Updated 2 users' passwords")
81 c.AssertNoErr(err)
82}
83
Jamie Hannaford99eced52015-03-02 15:24:22 +010084func (c *context) getUser() {
Jamie Hannaford11108402015-02-23 10:31:41 +010085 user, err := users.Get(c.client, c.instanceID, c.users[0]).Extract()
Jamie Hannaford05d200d2015-02-20 14:49:05 +010086 c.Logf("Getting user %s", user)
87 c.AssertNoErr(err)
88}
89
Jamie Hannaford99eced52015-03-02 15:24:22 +010090func (c *context) updateUser() {
91 opts := users.UpdateOpts{Name: tools.RandomString("new_name_", 5)}
Jamie Hannaford11108402015-02-23 10:31:41 +010092 err := users.Update(c.client, c.instanceID, c.users[0], opts).ExtractErr()
93 c.Logf("Updated user %s", c.users[0])
Jamie Hannaford05d200d2015-02-20 14:49:05 +010094 c.AssertNoErr(err)
Jamie Hannaford99eced52015-03-02 15:24:22 +010095 c.users[0] = opts.Name
Jamie Hannaford05d200d2015-02-20 14:49:05 +010096}
97
Jamie Hannaford99eced52015-03-02 15:24:22 +010098func (c *context) listUserAccess() {
Jamie Hannaford11108402015-02-23 10:31:41 +010099 err := users.ListAccess(c.client, c.instanceID, c.users[0]).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford99eced52015-03-02 15:24:22 +0100100 dbList, err := users.ExtractDBs(page)
Jamie Hannaford05d200d2015-02-20 14:49:05 +0100101 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 Hannaford11108402015-02-23 10:31:41 +0100110 c.AssertNoErr(err)
Jamie Hannaford05d200d2015-02-20 14:49:05 +0100111}
112
Jamie Hannaford99eced52015-03-02 15:24:22 +0100113func (c *context) grantUserAccess() {
Jamie Hannaford11108402015-02-23 10:31:41 +0100114 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 Hannaford05d200d2015-02-20 14:49:05 +0100118}
119
Jamie Hannaford99eced52015-03-02 15:24:22 +0100120func (c *context) revokeUserAccess() {
Jamie Hannaford11108402015-02-23 10:31:41 +0100121 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 Hannaford05d200d2015-02-20 14:49:05 +0100125}